Skip to content

Commit

Permalink
FIX-2203 Make delete empty mnemonics available on logs
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbruvik committed Jan 15, 2024
1 parent acaaa5c commit b182c56
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 95 deletions.
58 changes: 51 additions & 7 deletions Src/WitsmlExplorer.Api/Jobs/DeleteEmptyMnemonicsJob.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Extensions.Primitives;
using Microsoft.IdentityModel.Tokens;

using WitsmlExplorer.Api.Jobs.Common;
using WitsmlExplorer.Api.Models;

namespace WitsmlExplorer.Api.Jobs
{
public record DeleteEmptyMnemonicsJob : Job
{

public ICollection<WellReference> Wells { get; init; } = new List<WellReference>();
public ICollection<WellboreReference> Wellbores { get; init; } = new List<WellboreReference>();
public ICollection<ObjectReference> Logs { get; init; } = new List<ObjectReference>();
public double NullDepthValue { get; init; }
public DateTime NullTimeValue { get; init; }

public override string Description()
{
return "DeleteEmptyMnemonicsJob"
+ $" - WellUids: {GetWellUid()};"
+ $" WellboreUids: {string.Join(", ", Wellbores.Select(w => w.WellboreUid))}";
+ $" WellboreUids: {GetWellboreUid()};"
+ $" LogUids: {GetObjectUid()};";
}

public override string GetObjectName()
{
return null;
var logNames = Logs.Select(l => l.Name).Distinct();
return string.Join(", ", logNames);
}

public override string GetWellboreName()
{
return Wellbores.IsNullOrEmpty() ? null : string.Join(", ", Wellbores.Select(w => w.WellboreName));
var wellboreNames = new List<string>();

if (!Logs.IsNullOrEmpty())
{
wellboreNames.AddRange(Logs.Select(w => w.WellboreName).Distinct());
}

if (!Wellbores.IsNullOrEmpty())
{
wellboreNames.AddRange(Wellbores.Select(w => w.WellboreName).Distinct());
}

return string.Join(", ", wellboreNames.Distinct());
}

public override string GetWellName()
{
var wellNames = new List<string>();

if (!Logs.IsNullOrEmpty())
{
wellNames.AddRange(Logs.Select(w => w.WellName).Distinct());
}

if (!Wellbores.IsNullOrEmpty())
{
wellNames.AddRange(Wellbores.Select(w => w.WellName).Distinct());
Expand All @@ -57,6 +73,11 @@ private string GetWellUid()
{
var wellUids = new List<string>();

if (!Logs.IsNullOrEmpty())
{
wellUids.AddRange(Logs.Select(w => w.WellUid).Distinct());
}

if (!Wellbores.IsNullOrEmpty())
{
wellUids.AddRange(Wellbores.Select(w => w.WellUid).Distinct());
Expand All @@ -69,5 +90,28 @@ private string GetWellUid()

return string.Join(", ", wellUids.Distinct());
}

private string GetWellboreUid()
{
var wellboreUids = new List<string>();

if (!Logs.IsNullOrEmpty())
{
wellboreUids.AddRange(Logs.Select(w => w.WellboreUid).Distinct());
}

if (!Wellbores.IsNullOrEmpty())
{
wellboreUids.AddRange(Wellbores.Select(w => w.WellboreUid).Distinct());
}

return string.Join(", ", wellboreUids.Distinct());
}

private string GetObjectUid()
{
var logUids = Logs.Select(l => l.Uid).Distinct();
return string.Join(", ", logUids);
}
}
}
139 changes: 83 additions & 56 deletions Src/WitsmlExplorer.Api/Workers/Delete/DeleteEmptyMnemonicsWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;

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

using Witsml;
Expand All @@ -17,7 +16,6 @@
using WitsmlExplorer.Api.Jobs.Common;
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Models.Reports;
using WitsmlExplorer.Api.Query;
using WitsmlExplorer.Api.Services;

namespace WitsmlExplorer.Api.Workers.Delete
Expand All @@ -26,19 +24,16 @@ public class DeleteEmptyMnemonicsWorker : BaseWorker<DeleteEmptyMnemonicsJob>, I
{
public JobType JobType => JobType.DeleteEmptyMnemonics;

private readonly IWellboreService _wellboreService;
private readonly ILogObjectService _logObjectService;
private readonly IMnemonicService _mnemonicService;

public DeleteEmptyMnemonicsWorker(
ILogger<DeleteEmptyMnemonicsJob> logger,
IWitsmlClientProvider witsmlClientProvider,
IWellboreService wellboreService,
ILogObjectService logObjectService,
IMnemonicService mnemonicService)
: base(witsmlClientProvider, logger)
{
_wellboreService = wellboreService;
_logObjectService = logObjectService;
_mnemonicService = mnemonicService;
}
Expand All @@ -47,55 +42,52 @@ public DeleteEmptyMnemonicsWorker(
{
IWitsmlClient client = GetTargetWitsmlClientOrThrow();

var wellboreRefsToCheck = job.Wellbores.ToList();
List<LogObject> logsToCheck = new List<LogObject>();

wellboreRefsToCheck.AddRange(await ExtractWellboreRefs(job.Wells));
logsToCheck.AddRange(await ExtractLogs(job.Logs));
logsToCheck.AddRange(await ExtractLogs(job.Wellbores));
logsToCheck.AddRange(await ExtractLogs(job.Wells));

var reportItems = new List<DeleteEmptyMnemonicsReportItem>();
var logCurvesCheckedCount = 0;

foreach (var wellboreRef in wellboreRefsToCheck)
if (!logsToCheck.IsNullOrEmpty())
{
var logsToCheck = await ExtractLogs(wellboreRef);

if (!logsToCheck.IsNullOrEmpty())
foreach (var logToCheck in logsToCheck)
{
foreach (var logToCheck in logsToCheck)
{
var logCurves = await GetLogCurveInfos(logToCheck);
var logCurves = await GetLogCurveInfos(logToCheck);

logCurvesCheckedCount += logCurves.Count;

var mnemonicsToDelete = FindNullMnemonics(job.NullDepthValue, job.NullTimeValue, logToCheck, logCurves);

logCurvesCheckedCount += logCurves.Count;
foreach (var mnemonicToDelete in mnemonicsToDelete)
{
var result = await DeleteMnemonic(logToCheck.WellUid, logToCheck.WellboreUid, logToCheck.Uid, mnemonicToDelete);

var mnemonicsToDelete = FindNullMnemonics(job.NullDepthValue, job.NullTimeValue, logToCheck, logCurves);
var reportItem = new DeleteEmptyMnemonicsReportItem
{
WellName = logToCheck.WellName,
WellUid = logToCheck.WellUid,
WellboreName = logToCheck.WellboreName,
WellboreUid = logToCheck.WellboreUid,
LogName = logToCheck.Name,
LogUid = logToCheck.Uid,
LogIndexType = logToCheck.IndexType,
Mnemonic = mnemonicToDelete.Mnemonic
};

if (result.IsSuccessful)
{
reportItems.Add(reportItem);

foreach (var mnemonicToDelete in mnemonicsToDelete)
Logger.LogInformation("Successfully deleted empty mnemonic. WellUid: {WellUid}, WellboreUid: {WellboreUid}, Uid: {LogUid}, Mnemonic: {Mnemonic}"
, logToCheck.WellUid, logToCheck.WellboreUid, logToCheck.Uid, mnemonicToDelete.Mnemonic);
}
else
{
var result = await DeleteMnemonic(wellboreRef.WellUid, wellboreRef.WellboreUid, logToCheck.Uid, mnemonicToDelete);

var reportItem = new DeleteEmptyMnemonicsReportItem
{
WellName = wellboreRef.WellName,
WellUid = wellboreRef.WellUid,
WellboreName = wellboreRef.WellboreName,
WellboreUid = wellboreRef.WellboreUid,
LogName = logToCheck.Name,
LogUid = logToCheck.Uid,
LogIndexType = logToCheck.IndexType,
Mnemonic = mnemonicToDelete.Mnemonic
};

if (result.IsSuccessful)
{
reportItems.Add(reportItem);

Logger.LogInformation("Successfully deleted empty mnemonic. WellUid: {WellUid}, WellboreUid: {WellboreUid}, Uid: {LogUid}, Mnemonic: {Mnemonic}"
, wellboreRef.WellUid, wellboreRef.WellboreUid, logToCheck.Uid, mnemonicToDelete.Mnemonic);
}
else
{
Logger.LogWarning("Failed to delete empty mnemonic. WellUid: {WellUid}, WellboreUid: {WellboreUid}, Uid: {LogUid}, Mnemonic: {Mnemonic}"
, wellboreRef.WellUid, wellboreRef.WellboreUid, logToCheck.Uid, mnemonicToDelete.Mnemonic);
}
Logger.LogWarning("Failed to delete empty mnemonic. WellUid: {WellUid}, WellboreUid: {WellboreUid}, Uid: {LogUid}, Mnemonic: {Mnemonic}"
, logToCheck.WellUid, logToCheck.WellboreUid, logToCheck.Uid, mnemonicToDelete.Mnemonic);
}
}
}
Expand All @@ -111,9 +103,13 @@ public DeleteEmptyMnemonicsWorker(

Logger.LogInformation("{JobType} - Job successful. {Message}", GetType().Name, reportItems.IsNullOrEmpty() ? "No empty mnemonics deleted" : "Empty mnemonics deleted.");

return (
new WorkerResult(client.GetServerHostname(), true, $"Empty mnemonics deleted"),
null);
var workerResult = new WorkerResult(client.GetServerHostname(), true, $"Empty mnemonics deleted", jobId: job.JobInfo.Id);

RefreshObjects refreshAction = job.Logs.Any()
? new RefreshObjects(GetTargetWitsmlClientOrThrow().GetServerHostname(), job.Logs.First().WellUid, job.Logs.First().WellboreUid, EntityType.Log)
: null;

return (workerResult, refreshAction);
}

private string CreateReportSummary(DeleteEmptyMnemonicsJob job, int mnemonicsCheckedCount, int mnemonicsDeletedCount)
Expand Down Expand Up @@ -185,29 +181,60 @@ private async Task<ICollection<LogCurveInfo>> GetLogCurveInfos(LogObject logToCh
return (await _logObjectService.GetLogCurveInfo(logToCheck.WellUid, logToCheck.WellboreUid, logToCheck.Uid)).ToList();
}

private async Task<ICollection<LogObject>> ExtractLogs(WellboreReference wellboreRef)
private async Task<ICollection<LogObject>> ExtractLogs(ICollection<ObjectReference> logRefs)
{
return (await _logObjectService.GetLogs(wellboreRef.WellUid, wellboreRef.WellboreUid)).ToList();
var logs = new List<LogObject>();

if (!logRefs.IsNullOrEmpty())
{
foreach (var logObjectRef in logRefs)
{
var log = await _logObjectService.GetLog(logObjectRef.WellUid, logObjectRef.WellboreUid, logObjectRef.Uid);
if (log != null)
{
logs.Add(log);
}
}
}

return logs;
}
private async Task<ICollection<LogObject>> ExtractLogs(ICollection<WellboreReference> wellboreRefs)
{
var logs = new List<LogObject>();

if (!wellboreRefs.IsNullOrEmpty())
{
foreach (var wellboreRef in wellboreRefs)
{
var wellboreLogs = await _logObjectService.GetLogs(wellboreRef.WellUid, wellboreRef.WellboreUid);
if (!wellboreLogs.IsNullOrEmpty())
{
logs.AddRange(wellboreLogs);
}
}
}

return logs;
}

private async Task<ICollection<WellboreReference>> ExtractWellboreRefs(ICollection<WellReference> wellRefs)
private async Task<ICollection<LogObject>> ExtractLogs(ICollection<WellReference> wellRefs)
{
var wellboreRefs = new List<WellboreReference>();
var logs = new List<LogObject>();

if (!wellRefs.IsNullOrEmpty())
{
foreach (var wellRef in wellRefs)
foreach (var wellboreRef in wellRefs)
{
var wellbores = await _wellboreService.GetWellbores(wellRef.WellUid);

if (!wellbores.IsNullOrEmpty())
var wellLogs = await _logObjectService.GetLogs(wellboreRef.WellUid, string.Empty);
if (!wellLogs.IsNullOrEmpty())
{
wellboreRefs.AddRange(wellbores.Select(wb => new WellboreReference { WellboreUid = wb.Uid, WellboreName = wb.Name, WellUid = wb.WellUid, WellName = wb.WellName }));
logs.AddRange(wellLogs);
}
}
}

return wellboreRefs;
return logs;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { useClipboardComponentReferencesOfType } from "components/ContextMenus/U
import AnalyzeGapModal, {
AnalyzeGapModalProps
} from "components/Modals/AnalyzeGapModal";
import DeleteEmptyMnemonicsModal, {
DeleteEmptyMnemonicsModalProps
} from "components/Modals/DeleteEmptyMnemonicsModal";
import LogComparisonModal, {
LogComparisonModalProps
} from "components/Modals/LogComparisonModal";
Expand All @@ -34,6 +37,7 @@ import TrimLogObjectModal, {
} from "components/Modals/TrimLogObject/TrimLogObjectModal";
import NavigationContext from "contexts/navigationContext";
import OperationContext from "contexts/operationContext";
import { DisplayModalAction } from "contexts/operationStateReducer";
import OperationType from "contexts/operationType";
import { useOpenInQueryView } from "hooks/useOpenInQueryView";
import { ComponentType } from "models/componentType";
Expand Down Expand Up @@ -239,6 +243,17 @@ const LogObjectContextMenu = (
}
};

const onClickDeleteEmptyMnemonics = async () => {
const deleteEmptyMnemonicsModalProps: DeleteEmptyMnemonicsModalProps = {
logs: checkedObjects
};
const action: DisplayModalAction = {
type: OperationType.DisplayModal,
payload: <DeleteEmptyMnemonicsModal {...deleteEmptyMnemonicsModalProps} />
};
dispatchOperation(action);
};

const extraMenuItems = (): React.ReactElement[] => {
return [
<MenuItem
Expand Down Expand Up @@ -364,6 +379,16 @@ const LogObjectContextMenu = (
"log data",
[]
)}`}</Typography>
</MenuItem>,
<MenuItem
key={"deleteEmptyMnemonics"}
onClick={onClickDeleteEmptyMnemonics}
>
<StyledIcon
name="deleteToTrash"
color={colors.interactive.primaryResting}
/>
<Typography color={"primary"}>Delete empty mnemonics</Typography>
</MenuItem>
]}
</NestedMenuItem>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ const WellContextMenu = (props: WellContextMenuProps): React.ReactElement => {

const onClickDeleteEmptyMnemonics = async () => {
const deleteEmptyMnemonicsModalProps: DeleteEmptyMnemonicsModalProps = {
wells: [well],
dispatchOperation: dispatchOperation
wells: [well]
};
const action: DisplayModalAction = {
type: OperationType.DisplayModal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ const WellboreContextMenu = (

const onClickDeleteEmptyMnemonics = async () => {
const deleteEmptyMnemonicsModalProps: DeleteEmptyMnemonicsModalProps = {
wellbores: [wellbore],
dispatchOperation: dispatchOperation
wellbores: [wellbore]
};
const action: DisplayModalAction = {
type: OperationType.DisplayModal,
Expand Down
Loading

0 comments on commit b182c56

Please sign in to comment.