1 package de.dlr.shepard.context.references.timeseriesreference.services;
2
3 import de.dlr.shepard.context.collection.services.DataObjectService;
4 import de.dlr.shepard.context.references.timeseriesreference.io.MetricsIO;
5 import de.dlr.shepard.context.references.timeseriesreference.model.TimeseriesReference;
6 import de.dlr.shepard.data.timeseries.model.Timeseries;
7 import de.dlr.shepard.data.timeseries.model.TimeseriesDataPointsQueryParams;
8 import de.dlr.shepard.data.timeseries.model.TimeseriesEntity;
9 import de.dlr.shepard.data.timeseries.model.enums.AggregateFunction;
10 import de.dlr.shepard.data.timeseries.model.enums.DataPointValueType;
11 import de.dlr.shepard.data.timeseries.repositories.TimeseriesDataPointRepository;
12 import de.dlr.shepard.data.timeseries.repositories.TimeseriesRepository;
13 import de.dlr.shepard.data.timeseries.services.TimeseriesService;
14 import io.quarkus.logging.Log;
15 import jakarta.enterprise.context.RequestScoped;
16 import jakarta.inject.Inject;
17 import jakarta.ws.rs.NotFoundException;
18 import java.util.List;
19 import java.util.UUID;
20 import java.util.stream.Collectors;
21
22 @RequestScoped
23 public class TimeseriesReferenceMetricsService {
24
25 @Inject
26 TimeseriesRepository timeseriesRepository;
27
28 @Inject
29 TimeseriesService timeseriesService;
30
31 @Inject
32 TimeseriesDataPointRepository timeseriesDataPointRepository;
33
34 @Inject
35 TimeseriesReferenceService timeseriesReferenceService;
36
37 @Inject
38 DataObjectService dataObjectService;
39
40 public List<MetricsIO> getTimeseriesReferenceMetrics(
41 long collectionId,
42 long dataObjectId,
43 long timeseriesReferenceId,
44 UUID versionUID,
45 Timeseries timeseries
46 ) {
47 return getTimeseriesReferenceMetrics(
48 collectionId,
49 dataObjectId,
50 timeseriesReferenceId,
51 versionUID,
52 timeseries,
53 List.of(
54 AggregateFunction.COUNT,
55 AggregateFunction.MAX,
56 AggregateFunction.MIN,
57 AggregateFunction.STDDEV,
58 AggregateFunction.MEAN,
59 AggregateFunction.MEDIAN,
60 AggregateFunction.FIRST,
61 AggregateFunction.LAST
62 )
63 );
64 }
65
66 public List<MetricsIO> getTimeseriesReferenceMetrics(
67 long collectionId,
68 long dataObjectId,
69 long timeseriesReferenceId,
70 UUID versionUID,
71 Timeseries timeseries,
72 List<AggregateFunction> metrics
73 ) {
74 dataObjectService.getDataObject(collectionId, dataObjectId, versionUID);
75
76 TimeseriesReference timeseriesReference = timeseriesReferenceService.getReference(
77 collectionId,
78 dataObjectId,
79 timeseriesReferenceId,
80 versionUID
81 );
82
83 if (
84 timeseriesReference.getTimeseriesContainer() == null || timeseriesReference.getTimeseriesContainer().isDeleted()
85 ) {
86 String errorMsg = String.format(
87 "Referenced TimeseriesContainer is not set or deleted in TimeseriesReference with id %s",
88 timeseriesReferenceId
89 );
90 Log.error(errorMsg);
91 throw new NotFoundException(errorMsg);
92 }
93
94 TimeseriesEntity timeseriesEntity;
95
96 try {
97 timeseriesEntity = timeseriesService.getTimeseries(
98 timeseriesReference.getTimeseriesContainer().getId(),
99 timeseries
100 );
101 } catch (NotFoundException e) {
102 String errorMsg = String.format(
103 "Timeseries (%s, %s, %s, %s, %s) in the referenced TimeseriesContainer under TimeseriesReference with id %s",
104 timeseries.getMeasurement(),
105 timeseries.getDevice(),
106 timeseries.getLocation(),
107 timeseries.getSymbolicName(),
108 timeseries.getField(),
109 timeseriesReferenceId
110 );
111 Log.error(errorMsg);
112 throw new NotFoundException(errorMsg);
113 }
114
115 return metrics
116 .stream()
117 .map(metric -> {
118 DataPointValueType valueType = timeseriesEntity.getValueType();
119 TimeseriesDataPointsQueryParams queryParams = new TimeseriesDataPointsQueryParams(
120 timeseriesReference.getStart(),
121 timeseriesReference.getEnd(),
122 null,
123 null,
124 metric
125 );
126
127 if (
128 (valueType == DataPointValueType.String || valueType == DataPointValueType.Boolean) &&
129 (metric != AggregateFunction.COUNT && metric != AggregateFunction.FIRST && metric != AggregateFunction.LAST)
130 ) return new MetricsIO(metric, "N/A");
131 var dataPoints =
132 this.timeseriesDataPointRepository.queryAggregationFunction(timeseriesEntity.getId(), valueType, queryParams);
133 return new MetricsIO(metric, dataPoints.get(0).getValue());
134 })
135 .collect(Collectors.toList());
136 }
137 }