Skip to content

Commit

Permalink
Merge pull request #2032 from petrlicman/error-in-WellDatums#1209
Browse files Browse the repository at this point in the history
Error in well datums#1209
  • Loading branch information
petrlicman authored Sep 11, 2023
2 parents 4c27196 + 746d0f9 commit 08054d3
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Src/Witsml/Data/WitsmlWell.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

Expand Down Expand Up @@ -56,7 +57,7 @@ public class WitsmlWell
public WitsmlLengthMeasure WaterDepth { get; set; }

[XmlElement("wellLocation")]
public WitsmlLocation WellLocation { get; set; }
public List<WitsmlLocation> WellLocation { get; set; }

[XmlElement("commonData")]
public WitsmlCommonData CommonData { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Src/Witsml/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class ListExtensions
{
public static List<T> AsSingletonList<T>(this T item)
{
return new List<T>(new T[] { item });
return new List<T> { item };
}
}
}
5 changes: 1 addition & 4 deletions Src/WitsmlExplorer.Api/Models/Well.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ public class Well
public string DateTimeCreation { get; init; }
public string DateTimeLastChange { get; init; }
public string ItemState { get; init; }
public IEnumerable<Wellbore> Wellbores { get; set; }
public IList<Wellbore> Wellbores { get; set; }
public string Country { get; init; }
public string StatusWell { get; init; }
public string PurposeWell { get; init; }
public WellDatum WellDatum { get; init; }
public string WaterDepth { get; init; }
public WellLocation WellLocation { get; init; }
}
}
14 changes: 7 additions & 7 deletions Src/WitsmlExplorer.Api/Models/WellDatum.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

namespace WitsmlExplorer.Api.Models
{
Expand All @@ -8,14 +9,8 @@ public record WellDatum
public string Code { get; private init; }
public string Elevation { get; private init; }

public static WellDatum FromWitsmlWellDatum(List<Witsml.Data.WellDatum> witsmlWellDatumList)
public static WellDatum FromWitsmlWellDatum(Witsml.Data.WellDatum witsmlWellDatum)
{
if (witsmlWellDatumList == null || witsmlWellDatumList.Count == 0)
{
return null;
}

Witsml.Data.WellDatum witsmlWellDatum = witsmlWellDatumList[0];
return witsmlWellDatum == null
? null
: new WellDatum
Expand All @@ -25,5 +20,10 @@ public static WellDatum FromWitsmlWellDatum(List<Witsml.Data.WellDatum> witsmlWe
Elevation = witsmlWellDatum.Elevation?.Value,
};
}

public static List<WellDatum> FromWitsmlWellDatum(IEnumerable<Witsml.Data.WellDatum> witsmlWellDatums)
{
return witsmlWellDatums?.Select(FromWitsmlWellDatum).ToList() ?? new List<WellDatum>();
}
}
}
8 changes: 8 additions & 0 deletions Src/WitsmlExplorer.Api/Models/WellLocation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Linq;

using Witsml.Data;

using WitsmlExplorer.Api.Models.Measure;
Expand Down Expand Up @@ -27,5 +30,10 @@ public static WellLocation FromWitsmlLocation(WitsmlLocation witsmlLocation)
LocalY = WellMeasure.FromWitsmlMeasure(witsmlLocation.LocalY)
};
}

public static List<WellLocation> FromWitsmlLocation(IEnumerable<WitsmlLocation> witsmlLocations)
{
return witsmlLocations?.Select(FromWitsmlLocation).ToList() ?? new List<WellLocation>();
}
}
}
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Query/WellQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static WitsmlWells GetWitsmlWell(string wellUid = "")
PurposeWell = "",
WellDatum = new List<WellDatum>(),
WaterDepth = Measure.ToFetch<WitsmlLengthMeasure>(),
WellLocation = new WitsmlLocation(),
WellLocation = new List<WitsmlLocation>(),
CommonData = new WitsmlCommonData
{
DTimCreation = "",
Expand Down
34 changes: 16 additions & 18 deletions Src/WitsmlExplorer.Api/Services/WellService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -11,13 +13,11 @@
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Query;

using WellDatum = WitsmlExplorer.Api.Models.WellDatum;

namespace WitsmlExplorer.Api.Services
{
public interface IWellService
{
Task<IEnumerable<Well>> GetWells();
Task<IList<Well>> GetWells();
Task<Well> GetWell(string wellUid);
}

Expand All @@ -31,24 +31,25 @@ public WellService(IWitsmlClientProvider witsmlClientProvider, IWellboreService
_wellboreService = wellboreService;
}

public async Task<IEnumerable<Well>> GetWells()
public async Task<IList<Well>> GetWells()
{
Task<IEnumerable<Wellbore>> getWellbores = _wellboreService.GetWellbores();
Task<IEnumerable<Well>> getWells = GetWellsInformation();
Task<IList<Wellbore>> getWellbores = _wellboreService.GetWellbores();
Task<IList<Well>> getWells = GetWellsInformation();
await Task.WhenAll(getWellbores, getWells);

List<Well> wells = getWells.Result.ToList();
List<Well> wells = getWells.Result.OrderBy(well => well.Name).ToList();
foreach (Well well in wells)
{
well.Wellbores = getWellbores.Result.Where(wb => wb.WellUid == well.Uid);
well.Wellbores = getWellbores.Result.Where(wb => wb.WellUid == well.Uid).ToList();
}

return wells.OrderBy(well => well.Name);
return wells;
}

private async Task<IEnumerable<Well>> GetWellsInformation(string wellUid = null)
private async Task<IList<Well>> GetWellsInformation(string wellUid = null)
{
DateTime start = DateTime.Now;
var stopwatch = new Stopwatch();
stopwatch.Start();
WitsmlWells witsmlWells = string.IsNullOrEmpty(wellUid) ? WellQueries.GetAllWitsmlWells() : WellQueries.GetWitsmlWellByUid(wellUid);
WitsmlWells result = await _witsmlClient.GetFromStoreAsync(witsmlWells, new OptionsIn(ReturnElements.Requested));
List<Well> wells = result.Wells
Expand All @@ -65,21 +66,18 @@ private async Task<IEnumerable<Well>> GetWellsInformation(string wellUid = null)
ItemState = well.CommonData.ItemState,
StatusWell = well.StatusWell,
PurposeWell = well.PurposeWell,
WellDatum = WellDatum.FromWitsmlWellDatum(well.WellDatum),
WaterDepth = well.WaterDepth?.Value,
WellLocation = WellLocation.FromWitsmlLocation(well.WellLocation),
Country = well.Country
}
).ToList();
double elapsed = DateTime.Now.Subtract(start).Milliseconds / 1000.0;
Log.Debug("Fetched {Count} wells in {Elapsed} seconds", wells.Count, elapsed);
stopwatch.Stop();
Log.Debug("Fetched {Count} wells in {Elapsed} ms", wells.Count, stopwatch.ElapsedMilliseconds);
return wells;
}

public async Task<Well> GetWell(string wellUid)
{
Task<IEnumerable<Wellbore>> getWellbores = _wellboreService.GetWellbores(wellUid);
Task<IEnumerable<Well>> getWell = GetWellsInformation(wellUid);
Task<IList<Wellbore>> getWellbores = _wellboreService.GetWellbores(wellUid);
Task<IList<Well>> getWell = GetWellsInformation(wellUid);
await Task.WhenAll(getWellbores, getWell);

if (!getWell.Result.Any())
Expand Down
12 changes: 7 additions & 5 deletions Src/WitsmlExplorer.Api/Services/WellboreService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -17,7 +18,7 @@ namespace WitsmlExplorer.Api.Services
public interface IWellboreService
{
Task<Wellbore> GetWellbore(string wellUid, string wellboreUid);
Task<IEnumerable<Wellbore>> GetWellbores(string wellUid = "");
Task<IList<Wellbore>> GetWellbores(string wellUid = "");
}

// ReSharper disable once UnusedMember.Global
Expand Down Expand Up @@ -65,9 +66,10 @@ public async Task<Wellbore> GetWellbore(string wellUid, string wellboreUid)
};
}

public async Task<IEnumerable<Wellbore>> GetWellbores(string wellUid = "")
public async Task<IList<Wellbore>> GetWellbores(string wellUid = "")
{
DateTime start = DateTime.Now;
var stopwatch = new Stopwatch();
stopwatch.Start();
WitsmlWellbores query = WellboreQueries.GetWitsmlWellboreByWell(wellUid);

WitsmlWellbores result = await _witsmlClient.GetFromStoreAsync(query, new OptionsIn(ReturnElements.Requested));
Expand All @@ -86,8 +88,8 @@ public async Task<IEnumerable<Wellbore>> GetWellbores(string wellUid = "")
DateTimeCreation = witsmlWellbore.CommonData.DTimCreation
})
.OrderBy(wellbore => wellbore.Name).ToList();
double elapsed = DateTime.Now.Subtract(start).Milliseconds / 1000.0;
Log.Debug("Fetched {Count} wellbores in {Elapsed} seconds", wellbores.Count, elapsed);
stopwatch.Stop();
Log.Debug("Fetched {Count} wellbores in {Elapsed} ms", wellbores.Count, stopwatch.ElapsedMilliseconds);
return wellbores;
}
}
Expand Down
9 changes: 5 additions & 4 deletions Tests/WitsmlExplorer.Api.Tests/Models/WellDatumTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

using WitsmlExplorer.Api.Models;

Expand Down Expand Up @@ -26,7 +27,7 @@ public void FromWitsmlDatum_CopiesCorrectly_WhenListAndDatumExists(string name,

List<WitsmlDatum> sourceWitsmlDatumList = new() { sourceWitsmlDatum };

WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList);
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList).FirstOrDefault();

Assert.Equal(newWellDatum.Name, sourceWitsmlDatum.Name);
Assert.Equal(newWellDatum.Code, sourceWitsmlDatum.Code);
Expand All @@ -37,15 +38,15 @@ public void FromWitsmlDatum_CopiesCorrectly_WhenListAndDatumExists(string name,
public void FromWitsmlDatum_ReturnsNullIfListIsEmpty()
{
List<WitsmlDatum> sourceWitsmlDatumList = new();
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList);
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList).FirstOrDefault();
Assert.Null(newWellDatum);
}

[Fact]
public void FromWitsmlDatum_ReturnsNullIfListIsNull()
{
List<WitsmlDatum> sourceWitsmlDatumList = null;
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList);
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList).FirstOrDefault();
Assert.Null(newWellDatum);
}

Expand All @@ -54,7 +55,7 @@ public void FromWitsmlDatum_ReturnsNullIfWellDatumIsNull()
{
WitsmlDatum sourceWitsmlDatum = null;
List<WitsmlDatum> sourceWitsmlDatumList = new() { sourceWitsmlDatum };
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList);
WellDatum newWellDatum = WellDatum.FromWitsmlWellDatum(sourceWitsmlDatumList).FirstOrDefault();
Assert.Null(newWellDatum);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DeleteEmptyMnemonicsWorkerTest()
_wellboreService = new();
_wellboreService
.Setup(ws => ws.GetWellbores(It.IsAny<string>()))
.Returns(Task.Run(() => new List<Wellbore>().AsEnumerable()));
.Returns(Task.Run(() => new List<Wellbore>() as IList<Wellbore>));

_logObjectService = new();

Expand Down

0 comments on commit 08054d3

Please sign in to comment.