KPIService.java

  1. /*
  2.  * Copyright 2019 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */

  16. package org.genesys.server.service;

  17. import java.time.LocalDate;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.SortedMap;

  22. import javax.validation.Valid;

  23. import org.genesys.server.model.kpi.Dimension;
  24. import org.genesys.server.model.kpi.DimensionKey;
  25. import org.genesys.server.model.kpi.Execution;
  26. import org.genesys.server.model.kpi.ExecutionRun;
  27. import org.genesys.server.model.kpi.KPIParameter;
  28. import org.genesys.server.model.kpi.Observation;
  29. import org.springframework.data.domain.Page;
  30. import org.springframework.data.domain.Pageable;

  31. import com.querydsl.core.Tuple;

  32. public interface KPIService {

  33.     /**
  34.      * Details
  35.      */
  36.     static class ExecutionDetails {
  37.         public Execution execution;
  38.         public ExecutionRun lastRun;
  39.         public Page<ExecutionRun> runs;

  40.         public static ExecutionDetails from(Execution execution, ExecutionRun lastRun, Page<ExecutionRun> runs) {
  41.             ExecutionDetails ed = new ExecutionDetails();
  42.             ed.execution=execution;
  43.             ed.lastRun = lastRun;
  44.             ed.runs = runs;
  45.             return ed;
  46.         }
  47.     }

  48.     static class GroupedRunObservations {
  49.         public String dimensionValue;
  50.         public Map<LocalDate, Double> runCountResult;
  51.    
  52.         public GroupedRunObservations() {
  53.         }
  54.    
  55.         public GroupedRunObservations(String dimensionValue, Map<LocalDate, Double> runCountResult) {
  56.             this.dimensionValue = dimensionValue;
  57.             this.runCountResult = runCountResult;
  58.         }
  59.     }

  60.     public static class ExecutionRunsRequest {
  61.         public List<LocalDate> dates;
  62.         public Map<String, Set<String>> keys;
  63.     }

  64.     KPIParameter save(@Valid KPIParameter parameter);

  65.     KPIParameter getParameter(String name);

  66.     KPIParameter getParameter(long id);

  67.     KPIParameter delete(KPIParameter parameter);

  68.     <T extends Dimension<?>> T save(@Valid T dimension);

  69.     Dimension<?> getDimension(long id);

  70.     Dimension<?> getDimension(String name);

  71.     <T extends Dimension<?>> T delete(T dimension);

  72.     <T> Set<T> getValues(Dimension<T> loadedJpa);

  73.     Execution save(@Valid Execution execution);

  74.     Execution loadExecution(String executionName);

  75.     Execution getExecution(long id);
  76.     Execution getExecution(String executionName);

  77.     Execution delete(Execution execution);

  78.     List<Observation> execute(Execution execution);
  79.     ExecutionRun executeAndSave(Execution execution);

  80.     Page<KPIParameter> listParameters(Pageable page);

  81.     Page<Dimension<?>> listDimensions(Pageable page);

  82.     Page<Execution> listExecutions(Pageable page);
  83.     List<Execution> listExecutions();

  84.     Page<Observation> listObservations(ExecutionRun executionRun, Map<String, String> dimensionFilters, Pageable page);

  85.     ExecutionRun findLastExecutionRun(Execution execution);

  86.     ExecutionRun findExecutionRunByDate(Execution execution, LocalDate date);

  87.     void deleteObservations(ExecutionRun executionRun);

  88.     Page<ExecutionRun> listExecutionRuns(Execution execution, Pageable pageable);

  89.     ExecutionRun getExecutionRun(long runId);

  90.     /**
  91.      * List selected observations of an execution with the executionRun#timestamp.
  92.      *
  93.      * @param execution the execution
  94.      * @param from the from
  95.      * @param to the to
  96.      * @param keys the keys
  97.      * @param page the page
  98.      * @return the page
  99.      */
  100.     Page<Tuple> listObservations(Execution execution, LocalDate from, LocalDate to, Map<String, Set<String>> keys, Pageable page);

  101.     List<Observation> filterObservations(Execution execution, LocalDate date, Map<String, Set<String>> keys);

  102.     /**
  103.      * Calculate differences in observations starting at 'to' date, comparing observations
  104.      * with the earlier runs until since 'from' date.
  105.      *
  106.      * @param execution the execution
  107.      * @param from the date of the earliest run
  108.      * @param to the date of the latest run
  109.      * @return the sorted map keyed by run date, containing run differences
  110.      */
  111.     SortedMap<LocalDate, List<Observation>> calculateRunDiff(Execution execution, LocalDate from, LocalDate to, Map<String, Set<String>> keys);

  112.     /**
  113.      * Remove runs that have the same observations and don't add much value.
  114.      *  
  115.      * @param execution the execution
  116.      * @return
  117.      */
  118.     long purgeExecutionRuns(Execution execution);

  119.     /**
  120.      * Find observations by dates and filter.
  121.      *
  122.      * @param execution the execution
  123.      * @param runsRequest the execution runs request
  124.      * @return the map of observations for dates
  125.      */
  126.     SortedMap<LocalDate, List<Observation>> findExecutionRuns(Execution execution, ExecutionRunsRequest runsRequest);

  127.     /**
  128.      * Get observations grouped by dimension.
  129.      *
  130.      * @param execution the execution
  131.      * @param dimensionName the dimension for grouping
  132.      * @param toDate the date for searching
  133.      * @param maxRuns the max execution runs number
  134.      * @return the list of GroupedRunObservations
  135.      */
  136.     List<GroupedRunObservations> getObservationsGroupedByDimension(Execution execution, String dimensionName, LocalDate toDate, Integer maxRuns);

  137.     /**
  138.      * Get the dimension keys generated by {@code execution} in its runs.
  139.      *
  140.      * @param execution the execution
  141.      * @param dimensionNames names of dimensions
  142.      * @return List of dimension keys
  143.      */
  144.     List<DimensionKey> getDimensionKeys(Execution execution, Set<String> dimensionNames);

  145.     /**
  146.      * Delete KPI run
  147.      * @param run run to delete
  148.      * @return the deleted run
  149.      */
  150.     ExecutionRun delete(ExecutionRun run);

  151. }