-
Notifications
You must be signed in to change notification settings - Fork 16
[Datasets] Task/rdmp 33 dataset update post load #2191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JFriel
wants to merge
3
commits into
task/RDMP-33-dataset-integration-interface
Choose a base branch
from
task/RDMP-33-dataset-update-post-load
base: task/RDMP-33-dataset-integration-interface
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
Rdmp.Core/DataLoad/Engine/Mutilators/PostLoad/UpdateCatalogueMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using FAnsi.Discovery; | ||
| using MongoDB.Driver; | ||
| using Rdmp.Core.Curation.Data; | ||
| using Rdmp.Core.Curation.Data.DataLoad; | ||
| using Rdmp.Core.DataLoad.Engine.Job; | ||
| using Rdmp.Core.DataLoad.Triggers; | ||
| using Rdmp.Core.MapsDirectlyToDatabaseTable; | ||
| using Rdmp.Core.QueryBuilding; | ||
| using Rdmp.Core.ReusableLibraryCode.Checks; | ||
| using Rdmp.Core.ReusableLibraryCode.Progress; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Data; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Rdmp.Core.DataLoad.Engine.Mutilators.PostLoad | ||
| { | ||
| class UpdateCatalogueMetadata : IMutilateDataTables | ||
| { | ||
|
|
||
| [DemandsInitialization("Update the time period associated with the dataset", DefaultValue = true)] | ||
| public bool UpdateTimePeriods { get; set; } | ||
|
|
||
| [DemandsInitialization("Update the description of the dataset", DefaultValue = true)] | ||
| public bool UpdateDescription { get; set; } | ||
|
|
||
| [DemandsInitialization("The Column within the data to use for temporal updates")] | ||
| public string DateColumn { get; set; } | ||
|
|
||
|
|
||
| [DemandsInitialization("Catalogues in Data Load to not update")] | ||
| public List<Catalogue> CataloguestoIgnore { get; set; } | ||
|
|
||
| [DemandsInitialization(@"The Text added to the description to denote the update. | ||
| Some variables are available: | ||
| %d - Todays date | ||
| %c - The number of new records | ||
| %s - The earliest date in the new records | ||
| %l - The latest date in the new records | ||
| ", DefaultValue = "Update %d: Added %c Records with dates between %s and %l.")] | ||
| public string DescriptionUpdateText { get; set; } | ||
|
|
||
| public void Check(ICheckNotifier notifier) | ||
| { | ||
| } | ||
|
|
||
| public void Initialize(DiscoveredDatabase dbInfo, LoadStage loadStage) | ||
| { | ||
| } | ||
|
|
||
| public void LoadCompletedSoDispose(ExitCodeType exitCode, IDataLoadEventListener postLoadEventsListener) | ||
| { | ||
| } | ||
|
|
||
| public ExitCodeType Mutilate(IDataLoadJob job) | ||
| { | ||
| if (!UpdateTimePeriods && !UpdateDescription) return ExitCodeType.OperationNotRequired; | ||
| var catalogues = job.LoadMetadata.GetAllCatalogues().Where(c => !CataloguestoIgnore.Contains(c)); | ||
| foreach(var catalogue in catalogues) | ||
| { | ||
| var column = catalogue.CatalogueItems.Where(ci => ci.ColumnInfo.GetRuntimeName() == DateColumn).First(); | ||
| var dataLoadID = job.JobID; | ||
| var qb = new QueryBuilder(null, null); | ||
| qb.AddColumn(new ColumnInfoToIColumn(new MemoryRepository(), column.ColumnInfo)); | ||
| qb.AddCustomLine($"{SpecialFieldNames.DataLoadRunID} = {dataLoadID}", FAnsi.Discovery.QuerySyntax.QueryComponent.WHERE); | ||
| var sql = qb.SQL; | ||
|
|
||
| var dt = new DataTable(); | ||
| dt.BeginLoadData(); | ||
| var server = catalogue.GetDistinctLiveDatabaseServer(ReusableLibraryCode.DataAccess.DataAccessContext.DataLoad, false); | ||
| var con = server.GetConnection(); | ||
| con.Open(); | ||
| using (var cmd = server.GetCommand(sql, con)) | ||
| { | ||
| using var da = server.GetDataAdapter(cmd); | ||
| da.Fill(dt); | ||
| } | ||
| dt.EndLoadData(); | ||
| if (dt.Rows.Count == 0) return ExitCodeType.OperationNotRequired; | ||
| var latestDate = dt.AsEnumerable().Max(row => DateTime.Parse(row[0].ToString())); | ||
| var earliestDate = dt.AsEnumerable().Min(row => DateTime.Parse(row[0].ToString())); | ||
| var newRecordsCount = dt.Rows.Count; | ||
| if (UpdateTimePeriods) | ||
| { | ||
| catalogue.StartDate = earliestDate; | ||
| catalogue.EndDate = latestDate; | ||
| } | ||
| if (UpdateDescription) | ||
| { | ||
| var description = catalogue.Description; | ||
| description = description + "\n\r" + DescriptionUpdateText | ||
| .Replace("%d", DateTime.Now.ToString("dd/MM/yyyy")) | ||
| .Replace("%c", $"{newRecordsCount}") | ||
| .Replace("%s", $"{earliestDate.Day}/{earliestDate.Month}/{earliestDate.Year}") | ||
| .Replace("%l", $"{latestDate.Day}/{latestDate.Month}/{latestDate.Year}"); | ||
| } | ||
| catalogue.SaveToDatabase(); | ||
| dt.Dispose(); | ||
| } | ||
|
|
||
| return ExitCodeType.Success; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Missing Dispose call on local IDisposable Warning