1 package de.dlr.shepard.neo4Core.services;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Set;
8
9 import de.dlr.shepard.exceptions.InvalidAuthException;
10 import de.dlr.shepard.exceptions.InvalidBodyException;
11 import de.dlr.shepard.influxDB.FillOption;
12 import de.dlr.shepard.influxDB.InfluxUtil;
13 import de.dlr.shepard.influxDB.SingleValuedUnaryFunction;
14 import de.dlr.shepard.influxDB.TimeseriesPayload;
15 import de.dlr.shepard.influxDB.TimeseriesService;
16 import de.dlr.shepard.neo4Core.dao.DataObjectDAO;
17 import de.dlr.shepard.neo4Core.dao.TimeseriesContainerDAO;
18 import de.dlr.shepard.neo4Core.dao.TimeseriesDAO;
19 import de.dlr.shepard.neo4Core.dao.TimeseriesReferenceDAO;
20 import de.dlr.shepard.neo4Core.dao.UserDAO;
21 import de.dlr.shepard.neo4Core.entities.TimeseriesReference;
22 import de.dlr.shepard.neo4Core.io.TimeseriesReferenceIO;
23 import de.dlr.shepard.security.PermissionsUtil;
24 import de.dlr.shepard.util.AccessType;
25 import de.dlr.shepard.util.DateHelper;
26 import lombok.extern.slf4j.Slf4j;
27
28 @Slf4j
29 public class TimeseriesReferenceService implements IReferenceService<TimeseriesReference, TimeseriesReferenceIO> {
30 private TimeseriesReferenceDAO timeseriesReferenceDAO = new TimeseriesReferenceDAO();
31 private TimeseriesService timeseriesService = new TimeseriesService();
32 private DataObjectDAO dataObjectDAO = new DataObjectDAO();
33 private TimeseriesContainerDAO timeseriesContainerDAO = new TimeseriesContainerDAO();
34 private TimeseriesDAO timeseriesDAO = new TimeseriesDAO();
35 private UserDAO userDAO = new UserDAO();
36 private DateHelper dateHelper = new DateHelper();
37 private PermissionsUtil permissionsUtil = new PermissionsUtil();
38
39 @Override
40 public List<TimeseriesReference> getAllReferences(long dataObjectId) {
41 var references = timeseriesReferenceDAO.findByDataObject(dataObjectId);
42 return references;
43 }
44
45 @Override
46 public TimeseriesReference getReference(long id) {
47 var reference = timeseriesReferenceDAO.find(id);
48 if (reference == null || reference.isDeleted()) {
49 log.error("Timeseries Reference with id {} is null or deleted", id);
50 return null;
51 }
52 return reference;
53 }
54
55 @Override
56 public TimeseriesReference createReference(long dataObjectId, TimeseriesReferenceIO timeseriesReference,
57 String username) {
58 var user = userDAO.find(username);
59 var dataObject = dataObjectDAO.findLight(dataObjectId);
60 var container = timeseriesContainerDAO.findLight(timeseriesReference.getTimeseriesContainerId());
61
62 if (container == null || container.isDeleted()) {
63 throw new InvalidBodyException(String.format("The timeseries container with id %d could not be found.",
64 timeseriesReference.getTimeseriesContainerId()));
65 }
66
67
68 var errors = Arrays.stream(timeseriesReference.getTimeseries()).map(InfluxUtil::sanitize)
69 .filter(e -> !e.isBlank()).toList();
70 if (!errors.isEmpty())
71 throw new InvalidBodyException(
72 "The timeseries list contains illegal characters: " + String.join(", ", errors));
73
74 var toCreate = new TimeseriesReference();
75 toCreate.setCreatedAt(dateHelper.getDate());
76 toCreate.setCreatedBy(user);
77 toCreate.setDataObject(dataObject);
78 toCreate.setName(timeseriesReference.getName());
79 toCreate.setStart(timeseriesReference.getStart());
80 toCreate.setEnd(timeseriesReference.getEnd());
81 toCreate.setTimeseriesContainer(container);
82
83 for (var ts : timeseriesReference.getTimeseries()) {
84 var found = timeseriesDAO.find(ts.getMeasurement(), ts.getDevice(), ts.getLocation(), ts.getSymbolicName(),
85 ts.getField());
86 if (found != null) {
87 toCreate.addTimeseries(found);
88 } else {
89 toCreate.addTimeseries(ts);
90 }
91 }
92
93 var created = timeseriesReferenceDAO.createOrUpdate(toCreate);
94 return created;
95 }
96
97 @Override
98 public boolean deleteReference(long timeseriesId, String username) {
99 var user = userDAO.find(username);
100
101 var old = timeseriesReferenceDAO.find(timeseriesId);
102 old.setDeleted(true);
103 old.setUpdatedAt(dateHelper.getDate());
104 old.setUpdatedBy(user);
105
106 timeseriesReferenceDAO.createOrUpdate(old);
107 return true;
108 }
109
110 public List<TimeseriesPayload> getTimeseriesPayload(long timeseriesId, SingleValuedUnaryFunction function,
111 Long groupBy, FillOption fillOption, Set<String> devicesFilterSet, Set<String> locationsFilterSet,
112 Set<String> symbolicNameFilterSet, String username) {
113 var ref = timeseriesReferenceDAO.find(timeseriesId);
114 var containerId = ref.getTimeseriesContainer().getId();
115 var database = ref.getTimeseriesContainer().getDatabase();
116
117 if (!permissionsUtil.isAllowed(containerId, AccessType.Read, username))
118 throw new InvalidAuthException("You are not authorized to access this timeseries");
119
120 return timeseriesService.getTimeseriesPayloadList(ref.getStart(), ref.getEnd(), database, ref.getTimeseries(),
121 function, groupBy, fillOption, devicesFilterSet, locationsFilterSet, symbolicNameFilterSet);
122 }
123
124 public InputStream exportTimeseriesPayload(long timeseriesId, SingleValuedUnaryFunction function, Long groupBy,
125 FillOption fillOption, Set<String> devicesFilterSet, Set<String> locationsFilterSet,
126 Set<String> symbolicNameFilterSet, String username) throws IOException {
127 var ref = timeseriesReferenceDAO.find(timeseriesId);
128 var containerId = ref.getTimeseriesContainer().getId();
129 var database = ref.getTimeseriesContainer().getDatabase();
130
131 if (!permissionsUtil.isAllowed(containerId, AccessType.Read, username))
132 throw new InvalidAuthException("You are not authorized to access this timeseries");
133
134 return timeseriesService.exportTimeseriesPayload(ref.getStart(), ref.getEnd(), database, ref.getTimeseries(),
135 function, groupBy, fillOption, devicesFilterSet, locationsFilterSet, symbolicNameFilterSet);
136 }
137
138 }