-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathApplicationImpl.scala
More file actions
898 lines (855 loc) · 30.7 KB
/
ApplicationImpl.scala
File metadata and controls
898 lines (855 loc) · 30.7 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
package controllers
import java.io.{ByteArrayOutputStream, File}
import controllers.ApplicationData.maybeCache
import csw.services.icd.IcdToPdf
import csw.services.icd.codegen.{JavaCodeGenerator, PythonCodeGenerator, ScalaCodeGenerator, TypescriptCodeGenerator}
import csw.services.icd.db.IcdVersionManager.{SubsystemAndVersion, VersionDiff}
import csw.services.icd.db.{
AlarmsReport,
ArchivedItemsReport,
CachedIcdDbQuery,
CachedIcdVersionManager,
ComponentInfoHelper,
IcdComponentInfo,
IcdDb,
IcdDbPrinter,
IcdDbQuery,
IcdVersionManager,
MissingItemsReport,
getFileContents
}
import csw.services.icd.fits.{IcdFits, IcdFitsPrinter}
import csw.services.icd.github.IcdGitManager
import csw.services.icd.html.NumberedHeadings
import csw.services.icd.viz.IcdVizManager
import diffson.playJson.DiffsonProtocol
import icd.web.shared.IcdModels.IcdModel
import icd.web.shared.{
ApiVersionInfo,
ComponentInfo,
DiffInfo,
FitsDictionary,
FitsTags,
HtmlHeadings,
IcdName,
IcdVersion,
IcdVersionInfo,
IcdVizOptions,
PdfOptions,
PublishApiInfo,
PublishIcdInfo,
SubsystemInfo,
SubsystemWithVersion,
UnpublishApiInfo,
UnpublishIcdInfo,
VersionInfo
}
import play.api.libs.json.Json
import scala.util.Try
case class ApplicationImpl(db: IcdDb) {
private val log = play.Logger.of("ApplicationImpl")
val icdGitManager = IcdGitManager(db.versionManager)
// Cache of API and ICD versions published on GitHub (cached for better performance)
var (allApiVersions, allIcdVersions) =
try {
icdGitManager.ingestLatest(db)
}
catch {
case ex: Exception =>
// XXX TODO FIXME: How to handle this unlikely error at startup? Exit? Retry?
log.error("Failed to fetch API and ICD versions from tmt-icd repo on GitHub", ex)
(Nil, Nil)
}
// Update the database and cache after a new API or ICD was published (or in case one was published)
def updateAfterPublish(): Unit = {
val pair = icdGitManager.ingestLatest(db)
allApiVersions = pair._1
allIcdVersions = pair._2
}
// Note: Using try/catch below to prevent an exception from killing the application actor,
// which is not automatically restarted
def getSubsystemNames: List[String] = {
try {
val subsystemsInDb = db.query.getSubsystemNames
val publishedSubsystemNames = allApiVersions.map(_.subsystem)
(publishedSubsystemNames ++ subsystemsInDb).distinct.sorted
}
catch {
case ex: Exception =>
log.error("Failed to get subsystem names", ex)
Nil
}
}
/**
* Gets information about a named subsystem
*/
def getSubsystemInfo(subsystem: String, maybeVersion: Option[String], maybeComponent: Option[String]): Option[SubsystemInfo] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
try {
if (sv.maybeComponent.isDefined) {
db.versionManager
.getComponentModel(sv, None)
.map(m => SubsystemInfo(sv, m.title, m.description))
}
else {
db.versionManager
.getSubsystemModel(sv, None)
.map(m => SubsystemInfo(sv, m.title, m.description))
}
}
catch {
case ex: Exception =>
log.error(s"Failed to get subsystem info for $sv", ex)
None
}
}
/**
* Gets a list of components belonging to the given version of the given subsystem
*/
def getComponents(subsystem: String, maybeVersion: Option[String]): List[String] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, None)
try {
db.versionManager.getComponentNames(sv)
}
catch {
case ex: Exception =>
log.error(s"Could not get list of components for $sv", ex)
Nil
}
}
/**
* Query the database for information about the subsystem's components
*
* @param subsystem the subsystem
* @param maybeVersion the subsystem's version (default: current)
* @param maybeComponent component name (default all in subsystem)
* @param searchAll if true, search all components for API dependencies
* @param clientApiOpt if true, include subscribed events, sent commands
*/
def getComponentInfo(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
searchAll: Option[Boolean],
clientApiOpt: Option[Boolean]
): List[ComponentInfo] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val clientApi = clientApiOpt.getOrElse(false)
val searchAllSubsystems = clientApi && searchAll.getOrElse(false)
val subsystems = if (searchAllSubsystems) None else Some(List(sv.subsystem))
try {
val fitsKeyMap = IcdFits(db).getFitsKeyMap()
val query = new CachedIcdDbQuery(db, subsystems, None, fitsKeyMap)
val versionManager = new CachedIcdVersionManager(query)
new ComponentInfoHelper(
versionManager,
displayWarnings = searchAllSubsystems,
clientApi = clientApi
).getComponentInfoList(sv, None, fitsKeyMap)
}
catch {
case ex: Exception =>
log.error(s"Could not get component info for $sv", ex)
Nil
}
}
/**
* Query the database for information about the given components in an ICD
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component name (default: all in subsystem)
* @param target the target subsystem
* @param maybeTargetVersion the target subsystem's version
* @param maybeTargetComponent optional target component name (default: all in target subsystem)
*/
def getIcdComponentInfo(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
target: String,
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String]
): List[ComponentInfo] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val targetSv = SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent)
try {
val fitsKeyMap = IcdFits(db).getFitsKeyMap()
val query = new CachedIcdDbQuery(db, Some(List(sv.subsystem, targetSv.subsystem)), None, fitsKeyMap)
val versionManager = new CachedIcdVersionManager(query)
IcdComponentInfo.getComponentInfoList(versionManager, sv, targetSv, None, fitsKeyMap)
}
catch {
case ex: Exception =>
log.error(s"Failed to get ICD component info for ICD between $sv and $targetSv", ex)
Nil
}
}
// Returns the selected subsystem, target subsystem and optional ICD version
// If the ICD version is specified, we can determine the subsystem and target versions, otherwise
// if only the subsystem or target versions were given, use those (default to latest versions)
private def getSelectedSubsystems(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
target: String,
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String],
maybeIcdVersion: Option[String]
): (SubsystemWithVersion, SubsystemWithVersion, Option[IcdVersion]) = {
val v = maybeIcdVersion.getOrElse("*")
val versions = db.versionManager.getIcdVersions(subsystem, target)
val iv = versions.find(_.icdVersion.icdVersion == v).map(_.icdVersion)
if (iv.isDefined) {
val i = iv.get
(
SubsystemWithVersion(i.subsystem, Some(i.subsystemVersion), maybeComponent),
SubsystemWithVersion(i.target, Some(i.targetVersion), maybeTargetComponent),
iv
)
}
else {
(
SubsystemWithVersion(subsystem, maybeVersion, maybeComponent),
SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent),
iv
)
}
}
/**
* Returns the PDF for the given ICD
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component name (default: all in subsystem)
* @param target the target subsystem
* @param maybeTargetVersion optional target subsystem's version (default: current)
* @param maybeTargetComponent optional target component name (default: all in target subsystem)
* @param maybeIcdVersion optional ICD version (default: current)
* @param pdfOptions options for PDF generation
*/
def getIcdAsPdf(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
target: String,
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String],
maybeIcdVersion: Option[String],
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
try {
val (sv, targetSv, iv) = getSelectedSubsystems(
subsystem,
maybeVersion,
maybeComponent,
target,
maybeTargetVersion,
maybeTargetComponent,
maybeIcdVersion
)
val icdPrinter = IcdDbPrinter(db, searchAllSubsystems = false, clientApi = true, maybeCache, Some(pdfOptions))
val result = icdPrinter.saveIcdAsPdf(sv, targetSv, iv, pdfOptions)
result
}
catch {
case ex: Exception =>
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val targetSv = SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent)
val v = maybeIcdVersion.getOrElse("*")
log.error(s"Failed to generate PDF for ICD (version: $v) between $sv and $targetSv", ex)
None
}
}
/**
* Returns the PDF for the given subsystem API
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param searchAll if true, search all components for API dependencies
* @param clientApiOpt if true, include subscribed events and sent commands in the API
* @param pdfOptions options for PDF generation
*/
def getApiAsPdf(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
searchAll: Option[Boolean],
clientApiOpt: Option[Boolean],
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val clientApi = clientApiOpt.getOrElse(false)
val searchAllSubsystems = clientApi && searchAll.getOrElse(false)
val icdPrinter = IcdDbPrinter(db, searchAllSubsystems, clientApi, maybeCache, Some(pdfOptions))
try {
icdPrinter.saveApiAsPdf(sv, pdfOptions)
}
catch {
case ex: Exception =>
log.error(s"Failed to generate PDF for API for $sv", ex)
None
}
}
/**
* Returns the PDF for the FITS keywords
*
* @param tag "All" for all keywords, otherwise restrict output to given tag, as defined in DMS-Model-Files/FITS-Dictionary
* @param pdfOptions options for PDF generation
*/
def getFitsDictionaryAsPdf(
tag: String,
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val maybeTag = if (tag == "All") None else Some(tag)
try {
val fitsDictionary =
IcdFits(db).getFitsDictionary(maybeSubsystem = None, maybeTag = maybeTag, maybePdfOptions = Some(pdfOptions))
IcdFitsPrinter(fitsDictionary).saveAsPdf(maybeTag, pdfOptions)
}
catch {
case ex: Exception =>
log.error(s"Failed to get FITS dictionary for tag $tag", ex)
None
}
}
/**
* Returns the archived items report (PDF) for the given subsystem API
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param pdfOptions options for PDF generation
*/
def getArchivedItemsReport(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
try {
val html = ArchivedItemsReport(db, Some(sv), Some(pdfOptions), new HtmlHeadings).makeReport(pdfOptions)
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error(s"Failed to get archived items report for $sv", ex)
None
}
}
/**
* Returns the archived items report (HTML) for the given subsystem API
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
*/
def getArchivedItemsReportHtml(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String]
): Option[String] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
try {
Some(
ArchivedItemsReport(db, Some(sv), None, new HtmlHeadings)
.makeReportMarkup(s"Archived Items for ${sv.subsystem}")
.render
)
}
catch {
case ex: Exception =>
log.error(s"Failed to get archived items report for $sv", ex)
None
}
}
/**
* Returns the archived items report (PDF) for all current subsystems
* @param pdfOptions options for PDF generation
*/
def getArchivedItemsReportFull(
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
try {
val html = ArchivedItemsReport(db, None, Some(pdfOptions), new HtmlHeadings).makeReport(pdfOptions)
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error("Failed to get full archived items report", ex)
None
}
}
/**
* Returns the alarms report (PDF) for the given subsystem API
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param pdfOptions options for PDF generation
*/
def getAlarmsReport(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
try {
val html = AlarmsReport(db, Some(sv), Some(pdfOptions), new HtmlHeadings).makeReport(pdfOptions)
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error(s"Failed to get alarms report for $sv", ex)
None
}
}
/**
* Returns the alarms report (PDF) for all current subsystems
* @param pdfOptions options for PDF generation
*/
def getAlarmsReportFull(
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
try {
val html = AlarmsReport(db, None, Some(pdfOptions), new HtmlHeadings).makeReport(pdfOptions)
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error("Failed to get full alarms report", ex)
None
}
}
/**
* Returns a missing items report (PDF) for the given subsystem API
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param maybeTarget optional target subsystem
* @param maybeTargetVersion optional target subsystem's version (default: current)
* @param maybeTargetComponent optional target component name (default: all in target subsystem)
* @param pdfOptions options for PDF generation
*/
def getMissingItemsReport(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
maybeTarget: Option[String],
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String],
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val maybeTargetSv = maybeTarget.map(target => SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent))
try {
val html = MissingItemsReport(db, List(Some(sv), maybeTargetSv).flatten, pdfOptions).makeReport()
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error(s"Failed to get missing items report for $sv", ex)
None
}
}
/**
* Returns a missing items list (HTML) for the given subsystem APIs
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param maybeTarget optional target subsystem
* @param maybeTargetVersion optional target subsystem's version (default: current)
* @param maybeTargetComponent optional target component name (default: all in target subsystem)
* @param pdfOptions options for PDF generation
*/
def getMissingItemsReportHtml(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
maybeTarget: Option[String],
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String],
pdfOptions: PdfOptions
): Option[String] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val maybeTargetSv = maybeTarget.map(target => SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent))
try {
Some(
MissingItemsReport(db, List(Some(sv), maybeTargetSv).flatten, pdfOptions)
.makeReportMarkup(new HtmlHeadings)
.render
)
}
catch {
case ex: Exception =>
log.error(s"Failed to get missing items report for $sv", ex)
None
}
}
/**
* Returns the missing items report (PDF) for all current subsystems
* @param pdfOptions options for PDF generation
*/
def getMissingItemsReportFull(
pdfOptions: PdfOptions
): Option[Array[Byte]] = {
val out = new ByteArrayOutputStream()
try {
val html = MissingItemsReport(db, List(), pdfOptions).makeReport()
IcdToPdf.saveAsPdf(out, html, showLogo = false, pdfOptions)
Some(out.toByteArray)
}
catch {
case ex: Exception =>
log.error("Failed to get full missing items report", ex)
None
}
}
/**
* Returns a generated graph of component relationships for the selected components
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component name (default: all in subsystem)
* @param maybeTarget optional target subsystem
* @param maybeTargetVersion optional target subsystem's version (default: current)
* @param maybeTargetComponent optional target component name (default: all in target subsystem)
* @param maybeIcdVersion optional ICD version (default: current)
* @param options options for graph generation
*/
def makeGraph(
subsystem: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
maybeTarget: Option[String],
maybeTargetVersion: Option[String],
maybeTargetComponent: Option[String],
maybeIcdVersion: Option[String],
options: IcdVizOptions
): Option[Array[Byte]] = {
try {
val selectedSv = maybeTarget match {
case Some(target) =>
val (sv, targetSv, _) = getSelectedSubsystems(
subsystem,
maybeVersion,
maybeComponent,
target,
maybeTargetVersion,
maybeTargetComponent,
maybeIcdVersion
)
List(sv, targetSv)
case None =>
List(SubsystemWithVersion(subsystem, maybeVersion, maybeComponent))
}
val subsystems = selectedSv.filter(_.maybeComponent.isEmpty)
val components = selectedSv.filter(_.maybeComponent.nonEmpty)
val newOptions =
options.copy(subsystems = subsystems, components = components, showPlot = false, imageFile = None, dotFile = None)
val out = new ByteArrayOutputStream()
if (IcdVizManager.showRelationships(db, newOptions, Some(out))) Some(out.toByteArray) else None
}
catch {
case ex: Exception =>
val sv = SubsystemWithVersion(subsystem, maybeVersion, maybeComponent)
val targetSv = maybeTarget.map(target => SubsystemWithVersion(target, maybeTargetVersion, maybeTargetComponent))
val v = maybeIcdVersion.getOrElse("*")
log.error(s"Failed to make graph for subsystem1 = $sv, subsystem2 = $targetSv, icd version - $v", ex)
None
}
}
/**
* Returns a detailed list of the versions of the given subsystem
*/
def getVersions(subsystem: String): List[VersionInfo] = {
allApiVersions
.filter(_.subsystem == subsystem)
.flatMap(_.apis)
.map(a => VersionInfo(Some(a.version), a.user, a.comment, a.date))
}
/**
* Returns a list of version names for the given subsystem
*/
def getVersionNames(subsystem: String): List[String] = {
// get published versions (with uploaded and master)
val versions = allApiVersions
.filter(_.subsystem == subsystem)
.flatMap(_.apis)
.map(_.version)
// If there were no published versions, apiVersions will be empty, so use the locally published versions
if (versions.nonEmpty)
versions
else
db.versionManager.getVersions(subsystem).flatMap(_.maybeVersion.toList)
}
/**
* Gets a list of ICD names as pairs of (subsystem, targetSubsystem)
*/
def getIcdNames: List[IcdName] = {
val list = allIcdVersions.map(i => IcdName(i.subsystems.head, i.subsystems.tail.head))
list.sortWith((a, b) => a.subsystem.compareTo(b.subsystem) < 0)
}
/**
* Gets a list of versions for the ICD from subsystem to target subsystem
*/
def getIcdVersions(subsystem: String, target: String): List[IcdVersionInfo] = {
// convert list to use shared IcdVersion class
allIcdVersions.find(i => i.subsystems.head == subsystem && i.subsystems.tail.head == target).toList.flatMap(_.icds).map {
icd =>
val icdVersion = IcdVersion(icd.icdVersion, subsystem, icd.versions.head, target, icd.versions.tail.head)
IcdVersionInfo(icdVersion, icd.user, icd.comment, icd.date)
}
}
// Packages the diff information for return to browser
private def getDiffInfo(diff: VersionDiff): DiffInfo = {
import DiffsonProtocol.*
val jsValue = Json.toJson(diff.patch)
val s = Json.prettyPrint(jsValue)
DiffInfo(diff.path, s)
}
/**
* Gets the difference between two subsystem versions
*/
def getDiff(subsystem: String, versionsStr: String): List[DiffInfo] = {
try {
val versions = versionsStr.split(',')
val v1 = versions.head
val v2 = versions.tail.head
val v1Opt = if (v1.nonEmpty) Some(v1) else None
val v2Opt = if (v2.nonEmpty) Some(v2) else None
// convert list to use shared IcdVersion class
db.versionManager.diff(subsystem, v1Opt, v2Opt).map(getDiffInfo)
}
catch {
case ex: Exception =>
log.error(s"Failed to get diff for $subsystem versions $versionsStr", ex)
Nil
}
}
/**
* Publish the selected API (add an entry for the current commit of the master branch on GitHub)
*/
def publishApi(publishApiInfo: PublishApiInfo): Try[ApiVersionInfo] = {
Try {
val apiVersionInfo = icdGitManager.publish(
publishApiInfo.subsystem,
publishApiInfo.majorVersion,
publishApiInfo.user,
publishApiInfo.password,
publishApiInfo.comment
)
updateAfterPublish()
apiVersionInfo
}
}
/**
* Publish an ICD (add an entry to the icds file on the master branch of https://github.com/tmt-icd/ICD-Model-Files)
*/
def publishIcd(publishIcdInfo: PublishIcdInfo): Try[IcdVersionInfo] = {
Try {
val sv = SubsystemAndVersion(publishIcdInfo.subsystem, Some(publishIcdInfo.subsystemVersion))
val tv = SubsystemAndVersion(publishIcdInfo.target, Some(publishIcdInfo.targetVersion))
val subsystems = List(sv, tv)
val icdVersionInfo = icdGitManager.publish(
subsystems,
publishIcdInfo.majorVersion,
publishIcdInfo.user,
publishIcdInfo.password,
publishIcdInfo.comment
)
updateAfterPublish()
icdGitManager.importIcdFiles(db, subsystems, (s: String) => println(s), allIcdVersions)
icdVersionInfo
}
}
/**
* Unublish the selected API (removes an entry from the file in the master branch on GitHub)
*/
def unpublishApi(unpublishApiInfo: UnpublishApiInfo): Try[Option[ApiVersionInfo]] = {
Try {
val result = icdGitManager.unpublish(
SubsystemAndVersion(unpublishApiInfo.subsystem, Some(unpublishApiInfo.subsystemVersion)),
unpublishApiInfo.user,
unpublishApiInfo.password,
unpublishApiInfo.comment
)
result.foreach(_ => updateAfterPublish())
result
}
}
/**
* Unpublish an ICD (remove an entry in the icds file on the master branch of https://github.com/tmt-icd/ICD-Model-Files)
*/
def unpublishIcd(unpublishIcdInfo: UnpublishIcdInfo): Try[Option[IcdVersionInfo]] = {
Try {
val maybeIcdEntry = icdGitManager.unpublish(
unpublishIcdInfo.icdVersion,
unpublishIcdInfo.subsystem,
unpublishIcdInfo.target,
unpublishIcdInfo.user,
unpublishIcdInfo.password,
unpublishIcdInfo.comment
)
maybeIcdEntry.map { e =>
updateAfterPublish()
val icdVersion =
IcdVersion(e.icdVersion, unpublishIcdInfo.subsystem, e.versions.head, unpublishIcdInfo.target, e.versions.tail.head)
IcdVersionInfo(icdVersion, e.user, e.comment, e.date)
}
}
}
/**
* Updates the cache of published APIs and ICDs (in case new ones were published)
*/
def updatePublished(): Unit = {
try {
updateAfterPublish()
}
catch {
case ex: Exception =>
log.error("Failed to update cache of published APIs and ICDs", ex)
}
}
/**
* Gets optional information about the ICD between two subsystems
* (from the <subsystem>-icd-model.conf files)
*
* @param subsystem the source subsystem
* @param maybeVersion the source subsystem's version
* @param target the target subsystem
* @param maybeTargetVersion the target subsystem's version
*/
def getIcdModels(
subsystem: String,
maybeVersion: Option[String],
target: String,
maybeTargetVersion: Option[String]
): List[IcdModel] = {
val sv = SubsystemWithVersion(subsystem, maybeVersion, None)
val targetSv = SubsystemWithVersion(target, maybeTargetVersion, None)
val query = new IcdDbQuery(db, Some(List(sv.subsystem, targetSv.subsystem)))
val versionManager = new IcdVersionManager(query)
try {
versionManager.getIcdModels(sv, targetSv, None)
}
catch {
case ex: Exception =>
log.error(s"Failed to get ICD models for ICD between $sv and $targetSv", ex)
Nil
}
}
/**
* Returns the generated source code for the given subsystem/component API in the given language
*
* @param subsystem the source subsystem
* @param lang the language to generate (scala, java, typescript, python)
* @param className the top level class name to generate
* @param maybeVersion the source subsystem's version (default: current)
* @param maybeComponent optional component (default: all in subsystem)
* @param maybePackageName optional package name for generated scala/java code
*/
def generate(
subsystem: String,
lang: String,
className: String,
maybeVersion: Option[String],
maybeComponent: Option[String],
maybePackageName: Option[String]
): Option[String] = {
val suffix = lang
.toLowerCase()
.replace("typescript", "ts")
.replace("python", "py")
val sourceFile = new File(s"$className.$suffix")
val tempFile = Some(File.createTempFile(className, s".$suffix"))
val versionStr = maybeVersion.map(v => s":$v").getOrElse("")
val subsysVers = s"$subsystem$versionStr"
def readAndDeleteFile(): Option[String] = {
try {
val result = getFileContents(tempFile.get)
tempFile.get.delete()
Some(result)
}
catch {
case ex: Exception =>
ex.printStackTrace()
None
}
}
try {
suffix match {
case "scala" =>
new ScalaCodeGenerator(db).generate(
subsysVers,
maybeComponent,
sourceFile,
tempFile,
maybePackageName
)
readAndDeleteFile()
case "java" =>
new JavaCodeGenerator(db).generate(
subsysVers,
maybeComponent,
sourceFile,
tempFile,
maybePackageName
)
readAndDeleteFile()
case "ts" =>
new TypescriptCodeGenerator(db).generate(
subsysVers,
maybeComponent,
sourceFile,
tempFile,
maybePackageName
)
readAndDeleteFile()
case "py" =>
new PythonCodeGenerator(db).generate(
subsysVers,
maybeComponent,
sourceFile,
tempFile,
maybePackageName
)
readAndDeleteFile()
case _ =>
println(s"Unsupported language fo code generation: $lang")
None
}
}
catch {
case ex: Exception =>
log.error(s"Failed to generate $sourceFile for $subsysVers", ex)
None
}
}
def getFitsDictionary(maybeSubsystem: Option[String], maybeComponent: Option[String]): FitsDictionary = {
try {
IcdFits(db).getFitsDictionary(maybeSubsystem, maybeComponent)
}
catch {
case ex: Exception =>
val maybeSv = maybeSubsystem.map(subsystem => SubsystemWithVersion(subsystem, None, maybeComponent))
log.error(s"Failed to get FITS dictionary for $maybeSv", ex)
FitsDictionary(Nil, FitsTags(Map.empty))
}
}
}