diff --git a/Src/WitsmlExplorer.Api/Jobs/Job.cs b/Src/WitsmlExplorer.Api/Jobs/Job.cs index 9af4d1510..57b379801 100644 --- a/Src/WitsmlExplorer.Api/Jobs/Job.cs +++ b/Src/WitsmlExplorer.Api/Jobs/Job.cs @@ -11,7 +11,6 @@ public abstract record Job public abstract string GetWellboreName(); public abstract string GetObjectName(); public virtual bool IsCancelable { get; } = false; - public IProgress ProgressReporter { get; set; } public JobInfo JobInfo diff --git a/Src/WitsmlExplorer.Api/Services/LogObjectService.cs b/Src/WitsmlExplorer.Api/Services/LogObjectService.cs index 2283ec87b..3a7c80866 100644 --- a/Src/WitsmlExplorer.Api/Services/LogObjectService.cs +++ b/Src/WitsmlExplorer.Api/Services/LogObjectService.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -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; @@ -27,7 +29,7 @@ public interface ILogObjectService Task GetLog(string wellUid, string wellboreUid, string logUid); Task GetLog(string wellUid, string wellboreUid, string logUid, OptionsIn queryOptions); Task> GetLogCurveInfo(string wellUid, string wellboreUid, string logUid); - Task ReadLogData(string wellUid, string wellboreUid, string logUid, List mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken); + Task ReadLogData(string wellUid, string wellboreUid, string logUid, List mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken, IProgress progressReporter = null); } // ReSharper disable once UnusedMember.Global @@ -151,7 +153,7 @@ public async Task> GetLogCurveInfo(string wellUid, str }).ToList(); } - public async Task ReadLogData(string wellUid, string wellboreUid, string logUid, List mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken = null) + public async Task ReadLogData(string wellUid, string wellboreUid, string logUid, List mnemonics, bool startIndexIsInclusive, string start, string end, bool loadAllData, CancellationToken? cancellationToken = null, IProgress progressReporter = null) { WitsmlLog log = await GetLogHeader(wellUid, wellboreUid, logUid); @@ -169,7 +171,7 @@ public async Task 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()) @@ -211,7 +213,7 @@ private async Task LoadData(List mnemonics, WitsmlLog log, In return witsmlLog; } - private async Task LoadDataRecursive(List mnemonics, WitsmlLog log, Index startIndex, Index endIndex, CancellationToken cancellationToken, string wellUid = null, string wellboreUid = null, string logUid = null) + private async Task LoadDataRecursive(List mnemonics, WitsmlLog log, Index startIndex, Index endIndex, CancellationToken cancellationToken, string wellUid = null, string wellboreUid = null, string logUid = null, IProgress progressReporter = null) { await using LogDataReader logDataReader = new(_witsmlClient, log, new List(mnemonics), null, startIndex, endIndex); WitsmlLogData logData = await logDataReader.GetNextBatch(); @@ -222,8 +224,13 @@ private async Task LoadDataRecursive(List 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(); diff --git a/Src/WitsmlExplorer.Api/Workers/Copy/CopyLogDataWorker.cs b/Src/WitsmlExplorer.Api/Workers/Copy/CopyLogDataWorker.cs index 81c430e23..3c80aebbc 100644 --- a/Src/WitsmlExplorer.Api/Workers/Copy/CopyLogDataWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/Copy/CopyLogDataWorker.cs @@ -192,21 +192,7 @@ private async Task 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; } diff --git a/Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs b/Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs index 5de7ec12f..7386e0c6c 100644 --- a/Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs @@ -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; @@ -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 progressReporter = new Progress(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); } diff --git a/Src/WitsmlExplorer.Api/Workers/Tools/LogWorkerTools.cs b/Src/WitsmlExplorer.Api/Workers/Tools/LogWorkerTools.cs index 93a753598..75f787e1b 100644 --- a/Src/WitsmlExplorer.Api/Workers/Tools/LogWorkerTools.cs +++ b/Src/WitsmlExplorer.Api/Workers/Tools/LogWorkerTools.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -59,5 +60,23 @@ public static async Task 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; + } + } } }