-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
130 lines (98 loc) · 4.92 KB
/
Program.cs
File metadata and controls
130 lines (98 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Rdmp.Core.CommandLine.Options;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Startup;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Text.Json;
using DqeToImagingJson;
using ReusableLibraryCode.Checks;
using CommandLine;
using CommandLine.Text;
public class Program
{
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
LinkedRepositoryProvider repo;
string? databasesYaml = o.DatabasesYaml;
if (string.IsNullOrWhiteSpace(databasesYaml))
{
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
databasesYaml = Path.Combine(assemblyFolder, "Databases.yaml");
}
if (File.Exists(databasesYaml))
{
var cstrs = ConnectionStringsYamlFile.LoadFrom(new FileInfo(databasesYaml));
repo = new LinkedRepositoryProvider(cstrs.CatalogueConnectionString, cstrs.DataExportConnectionString);
}
else
{
throw new FileNotFoundException($"Could not find file {databasesYaml}");
}
var startup = new Startup(new EnvironmentInfo(), repo);
ICheckNotifier checkNotifier = o.LogStartup ? new ThrowImmediatelyCheckNotifier() { WriteToConsole = true } : new ToMemoryCheckNotifier();
try
{
startup.DoStartup(checkNotifier);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
// Figure out modalities in the RDMP metadata database by name
var imagingCatalogues = repo.CatalogueRepository.GetAllObjects<Catalogue>()
.Where(c => GetCatalogueModalityIfAny(c, o) != null);
var modalityGroups = imagingCatalogues.GroupBy(c => GetCatalogueModalityIfAny(c, o));
List<ModalityInfo> modalityInfos = new();
// for each modality
foreach (var modality in modalityGroups)
{
// generate statistics
var modalityToJson = new ModalityToJson(modality.Key, modality.ToArray(), repo, o);
modalityInfos.Add(modalityToJson.GetModalityInfo());
}
// output the final json
Console.WriteLine(JsonSerializer.Serialize(modalityInfos, new JsonSerializerOptions { WriteIndented = true }));
});
}
static string? GetCatalogueModalityIfAny(Catalogue c, Options o)
{
// find Catalogues called CT_ImageTable etc
Regex modalityExtractor = new Regex(o.ModalityPattern);
var match = modalityExtractor.Match(c.Name);
if (match.Success)
{
if (match.Groups.Count < 2)
throw new Exception("ModalityPattern must contain a regex capture group");
var modality = match.Groups[1].Value;
if (string.IsNullOrEmpty(modality))
throw new Exception($"ModalityPattern returned no capture group for Catalogue named '{c.Name}'");
return modality;
}
return null;
}
}
class Options
{
const string DefaultOnlyPattern = "Table$";
const string DefaultModalityPattern = "^([A-Z][A-Z])_";
[Option("logstartup", Required = false, HelpText = "Output the RDMP startup messages (for debugging connection issues)")]
public bool LogStartup { get; set; }
[Option("only", Default = DefaultOnlyPattern, HelpText = $"Regular expression for matching Catalogue names to process. Defaults to '{DefaultOnlyPattern}'")]
public string OnlyPattern { get; set; } = DefaultOnlyPattern;
[Option("modalitypattern", Default = DefaultModalityPattern, HelpText = $"Regular expression for extracting Modality from a Catalogue name. Must have a capture group. Defaults to '{DefaultModalityPattern}'")]
public string ModalityPattern { get; set; } = DefaultModalityPattern;
[Value(0, Required = false, HelpText = "Path to a yaml file that stores the connection strings to the RDMP platform databases. Defaults to 'Databases.yaml'",Default = "Databases.yaml")]
public string? DatabasesYaml { get; set; }
[Usage]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Normal usage", new Options { DatabasesYaml = null, ModalityPattern = null, OnlyPattern = null});
yield return new Example("Run only on Catalogues with 'edris' in the name (case insensitive)", new Options { DatabasesYaml = null, ModalityPattern = null, OnlyPattern = "edris" });
}
}
}