Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
DefaultDataService.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.engine.e1.services;
2 
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.*;
12 
13 import java.util.*;
14 
25 public class DefaultDataService implements DataService {
26 
27  private DatabaseService databaseService;
28 
29  // For all user operations - get all test results without matching
30  private final SessionMatchingSetup SESSION_MATCHING_SETUP = new SessionMatchingSetup(false, Collections.<SessionMatchingSetup.MatchBy>emptySet());
31 
32  public DefaultDataService(NodeContext context) {
33  databaseService = context.getService(DatabaseService.class);
34  }
35 
36  public DefaultDataService(DatabaseService databaseService) {
37  this.databaseService = databaseService;
38  }
39 
40  @Override
41  public SessionEntity getSession(String sessionId) {
42  Set<SessionEntity> sessions = getSessions(Arrays.asList(sessionId));
43  if (sessions.isEmpty()) {
44  return null;
45  }
46  return sessions.iterator().next();
47  }
48 
49  @Override
50  public DecisionPerSessionDto getSessionDecisions(String sessionId) {
51  return databaseService.getDecisionPerSession(sessionId);
52  }
53 
54  @Override
55  public Set<SessionEntity> getSessions(Collection<String> sessionIds) {
56 
57  List<SessionDataDto> sessionDataDtoList = databaseService.getSessionInfoService().getBySessionIds(0, sessionIds.size(), new HashSet<String>(sessionIds));
58 
59  if (sessionDataDtoList.isEmpty()) {
60  return Collections.emptySet();
61  }
62 
63  Set<SessionEntity> entities = new TreeSet<>(new SessionEntity.IdComparator());
64  for (SessionDataDto sessionDataDto : sessionDataDtoList) {
65  SessionEntity sessionEntity = new SessionEntity();
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());
71 
72  entities.add(sessionEntity);
73  }
74 
75  return entities;
76  }
77 
78  @Override
79  public Set<TestEntity> getTests(SessionEntity session) {
80  return getTests(session.getId());
81  }
82 
83  @Override
84  public Set<TestEntity> getTests(String sessionId) {
85  Map<String, Set<TestEntity>> map = getTests(Arrays.asList(sessionId));
86 
87  Set<TestEntity> result = map.get(sessionId);
88  if (result != null) {
89  return result;
90  }
91 
92  return Collections.emptySet();
93  }
94 
95  @Override
96  public Map<String, Set<TestEntity>> getTests(Collection<String> sessionIds) {
97  return getTestsWithName(sessionIds, null, SESSION_MATCHING_SETUP);
98  }
99 
100  @Override
101  public TestEntity getTestByName(SessionEntity session, String testName) {
102  return getTestByName(session.getId(), testName);
103  }
104 
105  @Override
106  public TestEntity getTestByName(String sessionId, String testName) {
107  Map<String, TestEntity> map = getTestsByName(Collections.singletonList(sessionId), testName);
108 
109  TestEntity result = map.get(sessionId);
110  if (result != null) {
111  return result;
112  }
113 
114  return null;
115  }
116 
117  @Override
118  public Map<String, TestEntity> getTestsByName(Collection<String> sessionIds, String testName) {
119  Map<String, Set<TestEntity>> tests = getTestsWithName(sessionIds, testName, SESSION_MATCHING_SETUP);
120 
121  Map<String, TestEntity> result = new HashMap<String, TestEntity>(tests.size());
122 
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());
127  }
128  }
129 
130  return result;
131  }
132 
133  // if testName=null => no filtering for test name => all tests for session(s) will be returned
134  // @return map <sessionId, set<test>>
135  public Map<String, Set<TestEntity>> getTestsWithName(Collection<String> sessionIds, String testName, SessionMatchingSetup sessionMatchingSetup) {
136  if (sessionIds.isEmpty()) {
137  return Collections.emptyMap();
138  }
139 
140  // basic
141  List<TaskDataDto> taskDataDtoList = databaseService.getTaskDataForSessions(new HashSet<String>(sessionIds), sessionMatchingSetup);
142  // info
143  Map<TaskDataDto, Map<String, TestInfoDto>> testInfoMap = databaseService.getTestInfoByTaskDataDto(taskDataDtoList);
144  // decision
145  Set<Long> ids = new HashSet<Long>();
146  for (TaskDataDto taskDataDto : taskDataDtoList) {
147  ids.addAll(taskDataDto.getIds());
148  }
149  Map<Long, TaskDecisionDto> idToDecisionPerTest = new HashMap<Long, TaskDecisionDto>();
150  for (TaskDecisionDto taskDecisionDto : databaseService.getDecisionsPerTask(ids)) {
151  idToDecisionPerTest.put(taskDecisionDto.getId(), taskDecisionDto);
152  }
153 
154  Map<String, Set<TestEntity>> result = new HashMap<String, Set<TestEntity>>();
155 
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();
162 
163  TestEntity testEntity = new TestEntity();
164  testEntity.setId(testId);
165  testEntity.setDescription(taskDataDto.getDescription());
166  testEntity.setName(taskDataDto.getTaskName());
167  testEntity.setTaskDataDto(taskDataDto);
168 
169  if (testInfoMap.containsKey(taskDataDto)) {
170  TestInfoDto testInfoDto = testInfoMap.get(taskDataDto).entrySet().iterator().next().getValue();
171  testEntity.setLoad(testInfoDto.getClock());
172  testEntity.setClockValue(testInfoDto.getClockValue());
173  testEntity.setTerminationStrategy(testInfoDto.getTermination());
174  testEntity.setStartDate(testInfoDto.getStartTime());
175  testEntity.setTestGroupIndex(testInfoDto.getNumber());
176  testEntity.setTestExecutionStatus(testInfoDto.getStatus());
177  }
178 
179  if (idToDecisionPerTest.containsKey(testId)) {
180  testEntity.setDecision(idToDecisionPerTest.get(testId).getDecision());
181  }
182 
183  if (result.containsKey(sessionId)) {
184  result.get(sessionId).add(testEntity);
185  } else {
186  Set<TestEntity> testEntitySet = new HashSet<TestEntity>();
187  testEntitySet.add(testEntity);
188  result.put(sessionId, testEntitySet);
189  }
190  }
191  }
192  }
193 
194  // add empty results when not found
195  for (String sessionId : sessionIds) {
196  if (!result.containsKey(sessionId)) {
197  result.put(sessionId, Collections.<TestEntity>emptySet());
198  }
199  }
200 
201  return result;
202  }
203 
204  @Override
205  public Set<MetricEntity> getMetrics(Long testId) {
206  Map<Long, Set<MetricEntity>> map = getMetricsByTestIds(Arrays.asList(testId));
207 
208  Set<MetricEntity> result = map.get(testId);
209  if (result != null) {
210  return result;
211  }
212 
213  return Collections.emptySet();
214  }
215 
216  @Override
217  public Set<MetricEntity> getMetrics(TestEntity test) {
218  Map<TestEntity, Set<MetricEntity>> map = getMetricsByTests(Arrays.asList(test));
219 
220  Set<MetricEntity> result = map.get(test);
221  if (result != null) {
222  return result;
223  }
224 
225  return Collections.emptySet();
226  }
227 
228  @Override
229  public Map<TestEntity, Set<MetricEntity>> getMetricsByTests(Collection<TestEntity> tests) {
230  Map<Long, TestEntity> map = new HashMap<Long, TestEntity>(tests.size());
231  Set<Long> ids = new HashSet<Long>(tests.size());
232 
233  for (TestEntity test : tests) {
234  map.put(test.getId(), test);
235  ids.add(test.getId());
236  }
237 
238  Map<Long, Set<MetricEntity>> metrics = getMetricsByTestIds(ids);
239 
240  Map<TestEntity, Set<MetricEntity>> result = new HashMap<TestEntity, Set<MetricEntity>>();
241 
242  for (Long key : map.keySet()) {
243  result.put(map.get(key), metrics.get(key));
244  }
245 
246  return result;
247  }
248 
249  @Override
250  public Map<Long, Set<MetricEntity>> getMetricsByTestIds(Collection<Long> testIds) {
251  if (testIds.isEmpty()) {
252  return Collections.emptyMap();
253  }
254 
255  // Get
256  List<String> sessionIds = databaseService.getSessionIdsByTaskIds(new HashSet<Long>(testIds));
257 
258  // Get all test results without matching
259  SessionMatchingSetup sessionMatchingSetup = new SessionMatchingSetup(false, Collections.<SessionMatchingSetup.MatchBy>emptySet());
260  RootNode rootNode = databaseService.getControlTreeForSessions(new HashSet<String>(sessionIds), sessionMatchingSetup);
261 
262  // Filter
263  List<TestNode> summaryNodeTests = new ArrayList<TestNode>();
264  List<TestDetailsNode> detailsNodeTests = new ArrayList<TestDetailsNode>();
265 
266  for (TestNode testNode : rootNode.getSummaryNode().getTests()) {
267  Long testId = testNode.getTaskDataDto().getId();
268  if (testIds.contains(testId)) {
269  summaryNodeTests.add(testNode);
270  }
271  }
272  for (TestDetailsNode testDetailsNode : rootNode.getDetailsNode().getTests()) {
273  Long testId = testDetailsNode.getTaskDataDto().getId();
274  if (testIds.contains(testId)) {
275  detailsNodeTests.add(testDetailsNode);
276  }
277  }
278 
279  // Join
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>();
283 
284  for (TestNode testNode : summaryNodeTests) {
285  Long testId = testNode.getTaskDataDto().getId();
286 
287  if (!metrics.containsKey(testId)) {
288  metrics.put(testId, new HashSet<MetricNameDto>());
289  }
290 
291  for (MetricNode metricNode : testNode.getMetrics()) {
292  for (MetricNameDto metricNameDto : metricNode.getMetricNameDtoList()) {
293  metrics.get(testId).add(metricNameDto);
294  metricsWithSummary.add(metricNameDto.getMetricName());
295  }
296  }
297  }
298 
299  for (TestDetailsNode testDetailsNode : detailsNodeTests) {
300  Long testId = testDetailsNode.getTaskDataDto().getId();
301 
302  if (!metrics.containsKey(testId)) {
303  metrics.put(testId, new HashSet<MetricNameDto>());
304  }
305 
306  for (MetricNode metricNode : testDetailsNode.getMetrics()) {
307  for (MetricNameDto metricNameDto : metricNode.getMetricNameDtoList()) {
308  metrics.get(testId).add(metricNameDto);
309  metricsWithPlots.add(metricNameDto.getMetricName());
310  }
311  }
312  }
313 
314  // Convert
315  Map<Long, Set<MetricEntity>> result = new HashMap<Long, Set<MetricEntity>>();
316  for (Long key : metrics.keySet()) {
317  result.put(key, new HashSet<MetricEntity>());
318 
319  for (MetricNameDto metricNameDto : metrics.get(key)) {
320  MetricEntity metricEntity = new MetricEntity();
321  metricEntity.setMetricNameDto(metricNameDto);
322  if (metricsWithSummary.contains(metricNameDto.getMetricName())) {
323  metricEntity.setSummaryAvailable(true);
324  }
325  if (metricsWithPlots.contains(metricNameDto.getMetricName())) {
326  metricEntity.setPlotAvailable(true);
327  }
328  result.get(key).add(metricEntity);
329  }
330  }
331 
332  return result;
333  }
334 
335  @Override
337  Map<MetricEntity, MetricSummaryValueEntity> map = getMetricSummary(Arrays.asList(metric));
338 
339  return map.get(metric);
340  }
341 
342  @Override
343  public Map<MetricEntity, MetricSummaryValueEntity> getMetricSummary(Collection<MetricEntity> metrics) {
344 
345  Set<MetricNameDto> metricNameDtoSet = new HashSet<MetricNameDto>();
346  Map<MetricNameDto, MetricEntity> matchMap = new HashMap<MetricNameDto, MetricEntity>();
347 
348  for (MetricEntity metric : metrics) {
349  if (metric.isSummaryAvailable()) {
350  metricNameDtoSet.add(metric.getMetricNameDto());
351  matchMap.put(metric.getMetricNameDto(), metric);
352  }
353  }
354 
355  Collection<SummarySingleDto> metricDtoList = databaseService.getSummaryByMetricNameDto(metricNameDtoSet, true).values();
356 
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);
364  }
365 
366  return result;
367  }
368 
369  @Override
370  public List<MetricPlotPointEntity> getMetricPlotData(MetricEntity metric) {
371  Map<MetricEntity, List<MetricPlotPointEntity>> map = getMetricPlotData(Collections.singletonList(metric));
372 
373  return map.get(metric);
374  }
375 
376  @Override
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>();
380 
381  for (MetricEntity metric : metrics) {
382  if (metric.isPlotAvailable()) {
383  metricNameDtoSet.add(metric.getMetricNameDto());
384  matchMap.put(metric.getMetricNameDto(), metric);
385  }
386  }
387 
388  Map<MetricNameDto, List<PlotSingleDto>> resultMap = databaseService.getPlotDataByMetricNameDto(metricNameDtoSet);
389 
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()) {
395  MetricPlotPointEntity metricPlotPointEntity = new MetricPlotPointEntity();
396  metricPlotPointEntity.setTime(pointDto.getX());
397  metricPlotPointEntity.setValue(pointDto.getY());
398  values.add(metricPlotPointEntity);
399  }
400  result.put(metricEntity, values);
401  }
402 
403  return result;
404  }
405 
406  public DatabaseService getDatabaseService() {
407  return databaseService;
408  }
409 
410  @Override
411  public boolean isAvailable() {
412  return true;
413  }
414 }