1 package de.dlr.shepard.neo4Core.services;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Collections;
6 import java.util.List;
7
8 import de.dlr.shepard.influxDB.FillOption;
9 import de.dlr.shepard.influxDB.SingleValuedUnaryFunction;
10 import de.dlr.shepard.influxDB.Timeseries;
11 import de.dlr.shepard.influxDB.TimeseriesPayload;
12 import de.dlr.shepard.influxDB.TimeseriesService;
13 import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
14 import de.dlr.shepard.neo4Core.dao.TimeseriesContainerDAO;
15 import de.dlr.shepard.neo4Core.dao.UserDAO;
16 import de.dlr.shepard.neo4Core.entities.Permissions;
17 import de.dlr.shepard.neo4Core.entities.TimeseriesContainer;
18 import de.dlr.shepard.neo4Core.io.TimeseriesContainerIO;
19 import de.dlr.shepard.util.DateHelper;
20 import de.dlr.shepard.util.PermissionType;
21 import de.dlr.shepard.util.QueryParamHelper;
22 import lombok.extern.slf4j.Slf4j;
23
24 @Slf4j
25 public class TimeseriesContainerService implements IContainerService<TimeseriesContainer, TimeseriesContainerIO> {
26
27 private TimeseriesContainerDAO timeseriesContainerDAO = new TimeseriesContainerDAO();
28 private TimeseriesService timeseriesService = new TimeseriesService();
29 private PermissionsDAO permissionsDAO = new PermissionsDAO();
30 private UserDAO userDAO = new UserDAO();
31 private DateHelper dateHelper = new DateHelper();
32
33
34
35
36
37
38
39
40 @Override
41 public TimeseriesContainer createContainer(TimeseriesContainerIO timeseriesContainer, String username) {
42 var user = userDAO.find(username);
43
44 var toCreate = new TimeseriesContainer();
45 toCreate.setCreatedAt(dateHelper.getDate());
46 toCreate.setCreatedBy(user);
47 toCreate.setDatabase(timeseriesService.createDatabase());
48 toCreate.setName(timeseriesContainer.getName());
49
50 var created = timeseriesContainerDAO.createOrUpdate(toCreate);
51 permissionsDAO.createOrUpdate(new Permissions(created, user, PermissionType.Private));
52 return created;
53 }
54
55
56
57
58
59
60
61 @Override
62 public TimeseriesContainer getContainer(long timeSeriesContainerId) {
63 TimeseriesContainer timeseriesContainer = timeseriesContainerDAO.find(timeSeriesContainerId);
64 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
65 log.error("Timeseries Container with id {} is null or deleted", timeSeriesContainerId);
66 return null;
67 }
68 return timeseriesContainer;
69 }
70
71
72
73
74
75
76
77
78 @Override
79 public List<TimeseriesContainer> getAllContainers(QueryParamHelper params, String username) {
80 var containers = timeseriesContainerDAO.findAllTimeseriesContainers(params, username);
81 return containers;
82 }
83
84
85
86
87
88
89
90
91
92
93 @Override
94 public boolean deleteContainer(long timeSeriesContainerId, String username) {
95 var user = userDAO.find(username);
96 TimeseriesContainer timeseriesContainer = timeseriesContainerDAO.find(timeSeriesContainerId);
97 if (timeseriesContainer == null) {
98 return false;
99 }
100
101 timeseriesContainer.setDeleted(true);
102 timeseriesContainer.setUpdatedAt(dateHelper.getDate());
103 timeseriesContainer.setUpdatedBy(user);
104 timeseriesContainerDAO.createOrUpdate(timeseriesContainer);
105 timeseriesService.deleteDatabase(timeseriesContainer.getDatabase());
106 return true;
107 }
108
109
110
111
112
113
114
115
116 public Timeseries createTimeseries(long timeseriesContainerId, TimeseriesPayload payload) {
117 var timeseriesContainer = timeseriesContainerDAO.find(timeseriesContainerId);
118 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
119 log.error("Timeseries Container with id {} is null or deleted", timeseriesContainerId);
120 return null;
121 }
122 var result = timeseriesService.createTimeseries(timeseriesContainer.getDatabase(), payload);
123 if (!result.isBlank()) {
124 log.error("Failed to create timeseries with error: {}", result);
125 return null;
126 }
127 return payload.getTimeseries();
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public TimeseriesPayload getTimeseriesPayload(long timeseriesContainerId, Timeseries timeseries, long start,
143 long end, SingleValuedUnaryFunction function, Long groupBy, FillOption fillOption) {
144 var timeseriesContainer = timeseriesContainerDAO.findLight(timeseriesContainerId);
145 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
146 log.error("Timeseries Container with id {} is null or deleted", timeseriesContainerId);
147 return null;
148 }
149 var result = timeseriesService.getTimeseriesPayload(start, end, timeseriesContainer.getDatabase(), timeseries,
150 function, groupBy, fillOption);
151 return result;
152 }
153
154
155
156
157
158
159
160 public List<Timeseries> getTimeseriesAvailable(long timeseriesContainerId) {
161 var timeseriesContainer = timeseriesContainerDAO.findLight(timeseriesContainerId);
162 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
163 log.error("Timeseries Container with id {} is null or deleted", timeseriesContainerId);
164 return Collections.emptyList();
165 }
166 return timeseriesService.getTimeseriesAvailable(timeseriesContainer.getDatabase());
167 }
168
169 public InputStream exportTimeseriesPayload(long timeseriesContainerId, Timeseries timeseries, long start, long end,
170 SingleValuedUnaryFunction function, Long groupBy, FillOption fillOption) throws IOException {
171 var timeseriesContainer = timeseriesContainerDAO.findLight(timeseriesContainerId);
172 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
173 log.error("Timeseries Container with id {} is null or deleted", timeseriesContainerId);
174 return null;
175 }
176 var result = timeseriesService.exportTimeseriesPayload(start, end, timeseriesContainer.getDatabase(),
177 List.of(timeseries), function, groupBy, fillOption, Collections.emptySet(), Collections.emptySet(),
178 Collections.emptySet());
179 return result;
180 }
181
182 public boolean importTimeseries(long timeseriesContainerId, InputStream stream) throws IOException {
183 var timeseriesContainer = timeseriesContainerDAO.findLight(timeseriesContainerId);
184 if (timeseriesContainer == null || timeseriesContainer.isDeleted()) {
185 log.error("Timeseries Container with id {} is null or deleted", timeseriesContainerId);
186 return false;
187 }
188 var result = timeseriesService.importTimeseries(timeseriesContainer.getDatabase(), stream);
189 if (!result.isBlank()) {
190 log.error("Failed to import timeseries with error: {}", result);
191 return false;
192 }
193 return true;
194 }
195
196 }