1 package com.griddynamics.jagger.engine.e1.services;
3 import com.griddynamics.jagger.coordinator.NodeContext;
4 import com.griddynamics.jagger.dbapi.DatabaseService;
5 import com.griddynamics.jagger.dbapi.dto.*;
6 import com.griddynamics.jagger.dbapi.model.MetricNode;
7 import com.griddynamics.jagger.dbapi.model.RootNode;
8 import com.griddynamics.jagger.dbapi.model.TestDetailsNode;
9 import com.griddynamics.jagger.dbapi.model.TestNode;
10 import com.griddynamics.jagger.dbapi.util.SessionMatchingSetup;
11 import com.griddynamics.jagger.engine.e1.services.data.service.*;
27 private DatabaseService databaseService;
30 private final SessionMatchingSetup SESSION_MATCHING_SETUP =
new SessionMatchingSetup(
false, Collections.<SessionMatchingSetup.MatchBy>emptySet());
33 databaseService = context.getService(DatabaseService.class);
37 this.databaseService = databaseService;
42 Set<SessionEntity> sessions =
getSessions(Arrays.asList(sessionId));
43 if (sessions.isEmpty()) {
46 return sessions.iterator().next();
51 return databaseService.getDecisionPerSession(sessionId);
55 public Set<SessionEntity>
getSessions(Collection<String> sessionIds) {
57 List<SessionDataDto> sessionDataDtoList = databaseService.getSessionInfoService().getBySessionIds(0, sessionIds.size(),
new HashSet<String>(sessionIds));
59 if (sessionDataDtoList.isEmpty()) {
60 return Collections.emptySet();
63 Set<SessionEntity> entities =
new TreeSet<>(
new SessionEntity.IdComparator());
64 for (SessionDataDto sessionDataDto : sessionDataDtoList) {
66 sessionEntity.
setId(sessionDataDto.getSessionId());
67 sessionEntity.
setStartDate(sessionDataDto.getStartDate());
68 sessionEntity.
setEndDate(sessionDataDto.getEndDate());
69 sessionEntity.
setKernels(sessionDataDto.getActiveKernelsCount());
70 sessionEntity.
setComment(sessionDataDto.getComment());
72 entities.add(sessionEntity);
84 public Set<TestEntity>
getTests(String sessionId) {
85 Map<String, Set<TestEntity>> map =
getTests(Arrays.asList(sessionId));
87 Set<TestEntity> result = map.get(sessionId);
92 return Collections.emptySet();
96 public Map<String, Set<TestEntity>>
getTests(Collection<String> sessionIds) {
107 Map<String, TestEntity> map =
getTestsByName(Collections.singletonList(sessionId), testName);
110 if (result != null) {
118 public Map<String, TestEntity>
getTestsByName(Collection<String> sessionIds, String testName) {
119 Map<String, Set<TestEntity>> tests =
getTestsWithName(sessionIds, testName, SESSION_MATCHING_SETUP);
121 Map<String, TestEntity> result =
new HashMap<String, TestEntity>(tests.size());
123 for (Map.Entry<String, Set<TestEntity>> entry : tests.entrySet()) {
124 Set<TestEntity> testEntities = entry.getValue();
125 if (!testEntities.isEmpty()) {
126 result.put(entry.getKey(), testEntities.iterator().next());
135 public Map<String, Set<TestEntity>>
getTestsWithName(Collection<String> sessionIds, String testName, SessionMatchingSetup sessionMatchingSetup) {
136 if (sessionIds.isEmpty()) {
137 return Collections.emptyMap();
141 List<TaskDataDto> taskDataDtoList = databaseService.getTaskDataForSessions(
new HashSet<String>(sessionIds), sessionMatchingSetup);
143 Map<TaskDataDto, Map<String, TestInfoDto>> testInfoMap = databaseService.getTestInfoByTaskDataDto(taskDataDtoList);
145 Set<Long> ids =
new HashSet<Long>();
146 for (TaskDataDto taskDataDto : taskDataDtoList) {
147 ids.addAll(taskDataDto.getIds());
149 Map<Long, TaskDecisionDto> idToDecisionPerTest =
new HashMap<Long, TaskDecisionDto>();
150 for (TaskDecisionDto taskDecisionDto : databaseService.getDecisionsPerTask(ids)) {
151 idToDecisionPerTest.put(taskDecisionDto.getId(), taskDecisionDto);
154 Map<String, Set<TestEntity>> result =
new HashMap<String, Set<TestEntity>>();
156 for (TaskDataDto taskDataDto : taskDataDtoList) {
157 for (Map.Entry<Long, String> entry : taskDataDto.getIdToSessionId().entrySet()) {
158 if (((testName != null) && (testName.equals(taskDataDto.getTaskName()))) ||
159 (testName == null)) {
160 Long testId = entry.getKey();
161 String sessionId = entry.getValue();
164 testEntity.
setId(testId);
166 testEntity.
setName(taskDataDto.getTaskName());
169 if (testInfoMap.containsKey(taskDataDto)) {
170 TestInfoDto testInfoDto = testInfoMap.get(taskDataDto).entrySet().iterator().next().getValue();
171 testEntity.
setLoad(testInfoDto.getClock());
179 if (idToDecisionPerTest.containsKey(testId)) {
180 testEntity.
setDecision(idToDecisionPerTest.get(testId).getDecision());
183 if (result.containsKey(sessionId)) {
184 result.get(sessionId).add(testEntity);
186 Set<TestEntity> testEntitySet =
new HashSet<TestEntity>();
187 testEntitySet.add(testEntity);
188 result.put(sessionId, testEntitySet);
195 for (String sessionId : sessionIds) {
196 if (!result.containsKey(sessionId)) {
197 result.put(sessionId, Collections.<
TestEntity>emptySet());
208 Set<MetricEntity> result = map.get(testId);
209 if (result != null) {
213 return Collections.emptySet();
220 Set<MetricEntity> result = map.get(test);
221 if (result != null) {
225 return Collections.emptySet();
230 Map<Long, TestEntity> map =
new HashMap<Long, TestEntity>(tests.size());
231 Set<Long> ids =
new HashSet<Long>(tests.size());
234 map.put(test.getId(), test);
235 ids.add(test.getId());
240 Map<TestEntity, Set<MetricEntity>> result =
new HashMap<TestEntity, Set<MetricEntity>>();
242 for (Long key : map.keySet()) {
243 result.put(map.get(key), metrics.get(key));
251 if (testIds.isEmpty()) {
252 return Collections.emptyMap();
256 List<String> sessionIds = databaseService.getSessionIdsByTaskIds(
new HashSet<Long>(testIds));
259 SessionMatchingSetup sessionMatchingSetup =
new SessionMatchingSetup(
false, Collections.<SessionMatchingSetup.MatchBy>emptySet());
260 RootNode rootNode = databaseService.getControlTreeForSessions(
new HashSet<String>(sessionIds), sessionMatchingSetup);
263 List<TestNode> summaryNodeTests =
new ArrayList<TestNode>();
264 List<TestDetailsNode> detailsNodeTests =
new ArrayList<TestDetailsNode>();
266 for (TestNode testNode : rootNode.getSummaryNode().getTests()) {
267 Long testId = testNode.getTaskDataDto().getId();
268 if (testIds.contains(testId)) {
269 summaryNodeTests.add(testNode);
272 for (TestDetailsNode testDetailsNode : rootNode.getDetailsNode().getTests()) {
273 Long testId = testDetailsNode.getTaskDataDto().getId();
274 if (testIds.contains(testId)) {
275 detailsNodeTests.add(testDetailsNode);
280 Map<Long, Set<MetricNameDto>> metrics =
new HashMap<Long, Set<MetricNameDto>>();
281 Set<String> metricsWithSummary =
new HashSet<String>();
282 Set<String> metricsWithPlots =
new HashSet<String>();
284 for (TestNode testNode : summaryNodeTests) {
285 Long testId = testNode.getTaskDataDto().getId();
287 if (!metrics.containsKey(testId)) {
288 metrics.put(testId,
new HashSet<MetricNameDto>());
291 for (MetricNode metricNode : testNode.getMetrics()) {
292 for (MetricNameDto metricNameDto : metricNode.getMetricNameDtoList()) {
293 metrics.get(testId).add(metricNameDto);
294 metricsWithSummary.add(metricNameDto.getMetricName());
299 for (TestDetailsNode testDetailsNode : detailsNodeTests) {
300 Long testId = testDetailsNode.getTaskDataDto().getId();
302 if (!metrics.containsKey(testId)) {
303 metrics.put(testId,
new HashSet<MetricNameDto>());
306 for (MetricNode metricNode : testDetailsNode.getMetrics()) {
307 for (MetricNameDto metricNameDto : metricNode.getMetricNameDtoList()) {
308 metrics.get(testId).add(metricNameDto);
309 metricsWithPlots.add(metricNameDto.getMetricName());
315 Map<Long, Set<MetricEntity>> result =
new HashMap<Long, Set<MetricEntity>>();
316 for (Long key : metrics.keySet()) {
317 result.put(key,
new HashSet<MetricEntity>());
319 for (MetricNameDto metricNameDto : metrics.get(key)) {
322 if (metricsWithSummary.contains(metricNameDto.getMetricName())) {
325 if (metricsWithPlots.contains(metricNameDto.getMetricName())) {
328 result.get(key).add(metricEntity);
337 Map<MetricEntity, MetricSummaryValueEntity> map =
getMetricSummary(Arrays.asList(metric));
339 return map.get(metric);
343 public Map<MetricEntity, MetricSummaryValueEntity>
getMetricSummary(Collection<MetricEntity> metrics) {
345 Set<MetricNameDto> metricNameDtoSet =
new HashSet<MetricNameDto>();
346 Map<MetricNameDto, MetricEntity> matchMap =
new HashMap<MetricNameDto, MetricEntity>();
349 if (metric.isSummaryAvailable()) {
350 metricNameDtoSet.add(metric.getMetricNameDto());
351 matchMap.put(metric.getMetricNameDto(), metric);
355 Collection<SummarySingleDto> metricDtoList = databaseService.getSummaryByMetricNameDto(metricNameDtoSet,
true).values();
357 Map<MetricEntity, MetricSummaryValueEntity> result =
new HashMap<MetricEntity, MetricSummaryValueEntity>();
358 for (SummarySingleDto metricDto : metricDtoList) {
359 MetricEntity metricEntity = matchMap.get(metricDto.getMetricName());
361 value.
setValue(Double.parseDouble(metricDto.getValues().iterator().next().getValue()));
362 value.
setDecision(metricDto.getValues().iterator().next().getDecision());
363 result.put(metricEntity, value);
371 Map<MetricEntity, List<MetricPlotPointEntity>> map =
getMetricPlotData(Collections.singletonList(metric));
373 return map.get(metric);
377 public Map<MetricEntity, List<MetricPlotPointEntity>>
getMetricPlotData(Collection<MetricEntity> metrics) {
378 Set<MetricNameDto> metricNameDtoSet =
new HashSet<MetricNameDto>();
379 Map<MetricNameDto, MetricEntity> matchMap =
new HashMap<MetricNameDto, MetricEntity>();
382 if (metric.isPlotAvailable()) {
383 metricNameDtoSet.add(metric.getMetricNameDto());
384 matchMap.put(metric.getMetricNameDto(), metric);
388 Map<MetricNameDto, List<PlotSingleDto>> resultMap = databaseService.getPlotDataByMetricNameDto(metricNameDtoSet);
390 Map<MetricEntity, List<MetricPlotPointEntity>> result =
new HashMap<MetricEntity, List<MetricPlotPointEntity>>();
391 for (Map.Entry<MetricNameDto, List<PlotSingleDto>> entry : resultMap.entrySet()) {
392 MetricEntity metricEntity = matchMap.get(entry.getKey());
393 List<MetricPlotPointEntity> values =
new ArrayList<MetricPlotPointEntity>();
394 for (PointDto pointDto : entry.getValue().iterator().next().getPlotData()) {
396 metricPlotPointEntity.
setTime(pointDto.getX());
397 metricPlotPointEntity.
setValue(pointDto.getY());
398 values.add(metricPlotPointEntity);
400 result.put(metricEntity, values);
407 return databaseService;