Skip to content

Commit

Permalink
FIX-2312 Fix download log data bug + add progress (#2313)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbruvik authored Mar 18, 2024
1 parent 2f94cdd commit cce193a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
1 change: 0 additions & 1 deletion Src/WitsmlExplorer.Api/Jobs/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public abstract record Job
public abstract string GetWellboreName();
public abstract string GetObjectName();
public virtual bool IsCancelable { get; } = false;

public IProgress<double> ProgressReporter { get; set; }

public JobInfo JobInfo
Expand Down
17 changes: 12 additions & 5 deletions Src/WitsmlExplorer.Api/Services/LogObjectService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand All @@ -16,6 +17,7 @@
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Models.Measure;
using WitsmlExplorer.Api.Query;
using WitsmlExplorer.Api.Workers;

using Index = Witsml.Data.Curves.Index;

Expand All @@ -27,7 +29,7 @@ public interface ILogObjectService
Task<LogObject> GetLog(string wellUid, string wellboreUid, string logUid);
Task<LogObject> GetLog(string wellUid, string wellboreUid, string logUid, OptionsIn queryOptions);
Task<ICollection<LogCurveInfo>> GetLogCurveInfo(string wellUid, string wellboreUid, string logUid);
Task<LogData> ReadLogData(string wellUid, string wellboreUid, string logUid, List<string> mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken);
Task<LogData> ReadLogData(string wellUid, string wellboreUid, string logUid, List<string> mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken, IProgress<double> progressReporter = null);
}

// ReSharper disable once UnusedMember.Global
Expand Down Expand Up @@ -151,7 +153,7 @@ public async Task<ICollection<LogCurveInfo>> GetLogCurveInfo(string wellUid, str
}).ToList();
}

public async Task<LogData> ReadLogData(string wellUid, string wellboreUid, string logUid, List<string> mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken = null)
public async Task<LogData> ReadLogData(string wellUid, string wellboreUid, string logUid, List<string> mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken = null, IProgress<double> progressReporter = null)
{
WitsmlLog log = await GetLogHeader(wellUid, wellboreUid, logUid);

Expand All @@ -169,7 +171,7 @@ public async Task<LogData> ReadLogData(string wellUid, string wellboreUid, strin
mnemonics.Insert(0, indexMnemonic);
}

WitsmlLog witsmlLog = loadAllData ? await LoadDataRecursive(mnemonics, log, startIndex, endIndex, cancellationToken.Value, wellUid, wellboreUid, logUid)
WitsmlLog witsmlLog = loadAllData ? await LoadDataRecursive(mnemonics, log, startIndex, endIndex, cancellationToken.Value, wellUid, wellboreUid, logUid, progressReporter)
: await LoadData(mnemonics, log, startIndex, endIndex, wellUid, wellboreUid, logUid);

if (witsmlLog?.LogData == null || witsmlLog.LogData.Data.IsNullOrEmpty())
Expand Down Expand Up @@ -211,7 +213,7 @@ private async Task<WitsmlLog> LoadData(List<string> mnemonics, WitsmlLog log, In
return witsmlLog;
}

private async Task<WitsmlLog> LoadDataRecursive(List<string> mnemonics, WitsmlLog log, Index startIndex, Index endIndex, CancellationToken cancellationToken, string wellUid = null, string wellboreUid = null, string logUid = null)
private async Task<WitsmlLog> LoadDataRecursive(List<string> mnemonics, WitsmlLog log, Index startIndex, Index endIndex, CancellationToken cancellationToken, string wellUid = null, string wellboreUid = null, string logUid = null, IProgress<double> progressReporter = null)
{
await using LogDataReader logDataReader = new(_witsmlClient, log, new List<string>(mnemonics), null, startIndex, endIndex);
WitsmlLogData logData = await logDataReader.GetNextBatch();
Expand All @@ -222,8 +224,13 @@ private async Task<WitsmlLog> LoadDataRecursive(List<string> mnemonics, WitsmlLo
{
cancellationToken.ThrowIfCancellationRequested();
}
allLogData.Data.AddRange(logData.Data);
if (progressReporter != null)
{
double progress = LogWorkerTools.CalculateProgressBasedOnIndex(log, logData);
progressReporter.Report(progress);
}
logData = await logDataReader.GetNextBatch();
if (logData != null) allLogData.Data.AddRange(logData.Data);
}

var witsmlLog = new WitsmlLog();
Expand Down
16 changes: 1 addition & 15 deletions Src/WitsmlExplorer.Api/Workers/Copy/CopyLogDataWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,7 @@ private async Task<CopyResult> CopyLogData(WitsmlLog sourceLog, WitsmlLog target

private void UpdateJobProgress(CopyLogDataJob job, WitsmlLog sourceLog, WitsmlLogData copiedData)
{
string index = copiedData.Data.LastOrDefault()?.Data.Split(CommonConstants.DataSeparator).FirstOrDefault();
if (index == null) return;
double progress = 0;
if (sourceLog.IndexType == WitsmlLog.WITSML_INDEX_TYPE_MD)
{
string startIndex = sourceLog.StartIndex.Value;
string endIndex = sourceLog.EndIndex.Value;
progress = (StringHelpers.ToDouble(index) - StringHelpers.ToDouble(startIndex)) / (StringHelpers.ToDouble(endIndex) - StringHelpers.ToDouble(startIndex));
}
else if (sourceLog.IndexType == WitsmlLog.WITSML_INDEX_TYPE_DATE_TIME)
{
string startIndex = sourceLog.StartDateTimeIndex;
string endIndex = sourceLog.EndDateTimeIndex;
progress = (DateTime.Parse(index) - DateTime.Parse(startIndex)).TotalMilliseconds / (DateTime.Parse(endIndex) - DateTime.Parse(startIndex)).TotalMilliseconds;
}
double progress = LogWorkerTools.CalculateProgressBasedOnIndex(sourceLog, copiedData);
job.ProgressReporter?.Report(progress);
if (job.JobInfo != null) job.JobInfo.Progress = progress;
}
Expand Down
15 changes: 7 additions & 8 deletions Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;

using Witsml.Data;
using Witsml.Extensions;
using Witsml.ServiceReference;

using WitsmlExplorer.Api.Jobs;
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Models.Reports;
using WitsmlExplorer.Api.Query;
using WitsmlExplorer.Api.Services;

namespace WitsmlExplorer.Api.Workers;
Expand Down Expand Up @@ -43,7 +37,12 @@ public DownloadAllLogDataWorker(
public override async Task<(WorkerResult, RefreshAction)> Execute(DownloadAllLogDataJob job, CancellationToken? cancellationToken = null)
{
Logger.LogInformation("Downloading of all data started. {jobDescription}", job.Description());
var logData = await _logObjectService.ReadLogData(job.LogReference.WellUid, job.LogReference.WellboreUid, job.LogReference.Uid, job.Mnemonics.ToList(), job.StartIndexIsInclusive, job.LogReference.StartIndex, job.LogReference.EndIndex, true, cancellationToken);
IProgress<double> progressReporter = new Progress<double>(progress =>
{
job.ProgressReporter?.Report(progress);
if (job.JobInfo != null) job.JobInfo.Progress = progress;
});
var logData = await _logObjectService.ReadLogData(job.LogReference.WellUid, job.LogReference.WellboreUid, job.LogReference.Uid, job.Mnemonics.ToList(), job.StartIndexIsInclusive, job.LogReference.StartIndex, job.LogReference.EndIndex, true, cancellationToken, progressReporter);
return DownloadAllLogDataResult(job, logData.Data, job.LogReference.Uid);
}

Expand Down
19 changes: 19 additions & 0 deletions Src/WitsmlExplorer.Api/Workers/Tools/LogWorkerTools.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -59,5 +60,23 @@ public static async Task<WitsmlLogData> GetLogDataForCurve(IWitsmlClient witsmlC
Data = data
};
}

public static double CalculateProgressBasedOnIndex(WitsmlLog log, WitsmlLogData currentData)
{
string index = currentData.Data.LastOrDefault()?.Data.Split(CommonConstants.DataSeparator).FirstOrDefault();
if (index == null) return 0;
if (log.IndexType == WitsmlLog.WITSML_INDEX_TYPE_MD)
{
string startIndex = log.StartIndex.Value;
string endIndex = log.EndIndex.Value;
return (StringHelpers.ToDouble(index) - StringHelpers.ToDouble(startIndex)) / (StringHelpers.ToDouble(endIndex) - StringHelpers.ToDouble(startIndex));
}
else
{
string startIndex = log.StartDateTimeIndex;
string endIndex = log.EndDateTimeIndex;
return (DateTime.Parse(index) - DateTime.Parse(startIndex)).TotalMilliseconds / (DateTime.Parse(endIndex) - DateTime.Parse(startIndex)).TotalMilliseconds;
}
}
}
}

0 comments on commit cce193a

Please sign in to comment.