org.joda.time.Interval Java Examples

The following examples show how to use org.joda.time.Interval. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OfflineWorkflowCreationParameters.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
public static OfflineWorkflowCreationParameters load() {
    ModuleConfig config = PlatformConfig.defaultConfig().getModuleConfig("offline-default-creation-parameters");
    Set<Country> countries = config.getEnumSetProperty("countries", Country.class);
    DateTime baseCaseDate = config.getDateTimeProperty("baseCaseDate");
    Interval histoInterval = config.getIntervalProperty("histoInterval");
    boolean generationSampled = config.getBooleanProperty("generationSampled", false);
    boolean boundariesSampled = config.getBooleanProperty("boundariesSampled", false);
    boolean initTopo = config.getBooleanProperty("initTopo", DEFAULT_INIT_TOPO);
    double correlationThreshold = config.getDoubleProperty("correlationThreshold", DEFAULT_CORRELATION_THRESHOLD);
    double probabilityThreshold = config.getDoubleProperty("probabilityThreshold", DEFAULT_PROBABILITY_THRESHOLD);
    boolean loadFlowTransformerVoltageControlOn = config.getBooleanProperty("loadFlowTransformerVoltageControlOn", DEFAULT_LOAD_FLOW_TRANSFORMER_VOLTAGE_CONTROL_ON);
    boolean simplifiedWorkflow = config.getBooleanProperty("simplifiedWorkflow", DEFAULT_SIMPLIFIED_WORKFLOW);
    boolean mergeOptimized = config.getBooleanProperty("mergeOptimized", DEFAULT_MERGE_OPTIMIZED);
    Set<Country> attributesCountryFilter = config.getEnumSetProperty("attributesCountryFilter", Country.class, DEFAULT_ATTRIBUTES_COUNTRY_FILTER);
    int attributesMinBaseVoltageFilter = config.getIntProperty("attributesMinBaseVoltageFilter", DEFAULT_ATTRIBUTES_MIN_BASE_VOLTAGE_FILTER);
    return new OfflineWorkflowCreationParameters(countries, baseCaseDate, histoInterval, generationSampled, boundariesSampled,
                                                 initTopo, correlationThreshold, probabilityThreshold, loadFlowTransformerVoltageControlOn,
                                                 simplifiedWorkflow, mergeOptimized, attributesCountryFilter, attributesMinBaseVoltageFilter);
}
 
Example #2
Source File: MeanVarianceRuleDetectorTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Test
public void testWeekOverWeekLookbackChange() {
  MeanVarianceRuleDetector detector = new MeanVarianceRuleDetector();
  MeanVarianceRuleDetectorSpec spec = new MeanVarianceRuleDetectorSpec();
  spec.setMonitoringGranularity("7_DAYS");
  spec.setLookback(52);
  spec.setSensitivity(5);
  detector.init(spec, new DefaultInputDataFetcher(this.provider, -1));
  DetectionResult detectionResult = detector.runDetection(new Interval(1530576000000L, 1562630400000L), "thirdeye:metric:1");
  List<MergedAnomalyResultDTO> anomalies = detectionResult.getAnomalies();
  Assert.assertEquals(anomalies.size(), 6);
  Assert.assertEquals(anomalies.get(0).getStartTime(), 1530576000000L);
  Assert.assertEquals(anomalies.get(0).getEndTime(), 1531180800000L);
  Assert.assertEquals(anomalies.get(1).getStartTime(), 1540252800000L);
  Assert.assertEquals(anomalies.get(1).getEndTime(), 1541462400000L);
  Assert.assertEquals(anomalies.get(2).getStartTime(), 1545696000000L);
  Assert.assertEquals(anomalies.get(2).getEndTime(), 1546300800000L);
  Assert.assertEquals(anomalies.get(3).getStartTime(), 1546905600000L);
  Assert.assertEquals(anomalies.get(3).getEndTime(), 1547510400000L);
  Assert.assertEquals(anomalies.get(4).getStartTime(), 1553558400000L);
  Assert.assertEquals(anomalies.get(4).getEndTime(), 1554768000000L);
  Assert.assertEquals(anomalies.get(5).getStartTime(), 1555372800000L);
  Assert.assertEquals(anomalies.get(5).getEndTime(), 1557187200000L);
}
 
Example #3
Source File: OfflineSegmentIntervalCheckerCommand.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * Checks segments of table for invalid intervals and prints them out
 * @param offlineTableName
 */
private List<String> checkOfflineTablesSegmentIntervals(String offlineTableName) {
  TableConfig tableConfig = ZKMetadataProvider.getOfflineTableConfig(_propertyStore, offlineTableName);
  List<OfflineSegmentZKMetadata> offlineSegmentZKMetadataList =
      ZKMetadataProvider.getOfflineSegmentZKMetadataListForTable(_propertyStore, offlineTableName);

  // collect segments with invalid time intervals
  List<String> segmentsWithInvalidIntervals = new ArrayList<>();
  if (SegmentIntervalUtils.eligibleForSegmentIntervalCheck(tableConfig.getValidationConfig())) {
    for (OfflineSegmentZKMetadata offlineSegmentZKMetadata : offlineSegmentZKMetadataList) {
      Interval timeInterval = offlineSegmentZKMetadata.getTimeInterval();
      if (timeInterval == null || !TimeUtils.isValidTimeInterval(timeInterval)) {
        segmentsWithInvalidIntervals.add(offlineSegmentZKMetadata.getSegmentName());
      }
    }
  }
  return segmentsWithInvalidIntervals;
}
 
Example #4
Source File: HoltWintersDetector.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Override
public TimeSeries computePredictedTimeSeries(MetricSlice slice) {
  MetricEntity metricEntity = MetricEntity.fromSlice(slice, 0);
  Interval window = new Interval(slice.getStart(), slice.getEnd());
  DateTime trainStart = getTrainingStartTime(window.getStart());

  DatasetConfigDTO datasetConfig = this.dataFetcher.fetchData(new InputDataSpec()
      .withMetricIdsForDataset(Collections.singleton(metricEntity.getId()))).getDatasetForMetricId()
      .get(metricEntity.getId());

  DataFrame inputDf = fetchData(metricEntity, trainStart.getMillis(), window.getEndMillis(), datasetConfig);
  DataFrame resultDF = computePredictionInterval(inputDf, window.getStartMillis(), datasetConfig.getTimezone());
  resultDF = resultDF.joinLeft(inputDf.renameSeries(COL_VALUE, COL_CURR), COL_TIME);

  // Exclude the end because baseline calculation should not contain the end
  if (resultDF.size() > 1) {
    resultDF = resultDF.head(resultDF.size() - 1);
  }

  return TimeSeries.fromDataFrame(resultDF);
}
 
Example #5
Source File: LessonInstanceSpaceOccupation.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void edit(LessonInstance lessonInstance) {
    check(this, SpacePredicates.checkPermissionsToManageLessonInstanceSpaceOccupationsWithTeacherCheck);

    if (getLessonInstancesSet().contains(lessonInstance)) {
        removeLessonInstances(lessonInstance);
    }

    Space space = getSpace();
    //final ExecutionCourse executionCourse = lessonInstance.getLesson().getExecutionCourse();
    if (/*!space.isOccupiedByExecutionCourse(executionCourse, lessonInstance.getBeginDateTime(),
            lessonInstance.getEndDateTime())
            &&*//*!space.isFree(lessonInstance.getDay(), lessonInstance.getDay(), lessonInstance.getStartTime(),
                lessonInstance.getEndTime(), lessonInstance.getDayOfweek(), null, null, null)*/
    !space.isFree(Lists.newArrayList(new Interval[] { new Interval(lessonInstance.getBeginDateTime(), lessonInstance
            .getEndDateTime()) }))) {

        throw new DomainException("error.LessonInstanceSpaceOccupation.room.is.not.free", space.getName(), lessonInstance
                .getDay().toString("dd-MM-yy"));
    }

    addLessonInstances(lessonInstance);
}
 
Example #6
Source File: TopologyContext.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
public static TopologyContext create(Network network, TopologyMiner topologyMiner, HistoDbClient histoDbClient, ComputationManager computationManager,
                                     Interval histoInterval, double correlationThreshold, double probabilityThreshold) throws IOException {
    // create topo cache dir
    Path topoCacheDir = createTopoCacheDir(network, histoInterval, correlationThreshold, probabilityThreshold);

    // query topology history
    TopologyContext topologyContext = topologyMiner.loadContext(topoCacheDir,
            histoInterval,
            correlationThreshold,
            probabilityThreshold);
    if (topologyContext == null) {
        LOGGER.info("No cached topology context found");
        topologyContext = topologyMiner.initContext(network,
                histoInterval,
                correlationThreshold,
                probabilityThreshold,
                histoDbClient,
                computationManager);
        topologyContext.save(topoCacheDir);
    }

    return topologyContext;
}
 
Example #7
Source File: RealJeff.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param rainfallMap the sorted map of rainfall values in time. <b>This has to be a sorted map.</b>
 */
public RealJeff( HashMap<DateTime, double[]> rainfallMap ) {
    this.rainfallMap = rainfallMap;

    Set<Entry<DateTime, double[]>> entrySet = rainfallMap.entrySet();
    for( Entry<DateTime, double[]> entry : entrySet ) {
        if (first == null) {
            first = entry.getKey();
        } else if (second == null) {
            second = entry.getKey();
            break;
        }
    }

    Interval interval = new Interval(first, second);
    rainTimestep = interval.toDuration().getStandardSeconds();
}
 
Example #8
Source File: AbsoluteChangeRuleDetectorTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Test
public void testWeekOverWeekDifference() {
  AbsoluteChangeRuleDetector detector = new AbsoluteChangeRuleDetector();
  AbsoluteChangeRuleDetectorSpec spec = new AbsoluteChangeRuleDetectorSpec();
  double absoluteChange = 400;
  spec.setAbsoluteChange(absoluteChange);
  spec.setPattern("up");
  detector.init(spec, new DefaultInputDataFetcher(this.provider, -1));
  DetectionResult result = detector.runDetection(new Interval(1814400000L, 2419200000L), "thirdeye:metric:1");
  List<MergedAnomalyResultDTO> anomalies = result.getAnomalies();

  Assert.assertEquals(anomalies.size(), 1);
  Assert.assertEquals(anomalies.get(0).getStartTime(), 2372400000L);
  Assert.assertEquals(anomalies.get(0).getEndTime(), 2376000000L);
  TimeSeries ts = result.getTimeseries();
  checkAbsoluteUpperBounds(ts, absoluteChange);
  Assert.assertEquals(ts.getPredictedLowerBound(), DoubleSeries.zeros(ts.size()));
}
 
Example #9
Source File: ImhotepClient.java    From imhotep with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a list of time intervals within the requested [start, end) range that are not covered by available shards.
 * Intervals in the list are sorted chronologically.
 */
public List<Interval> getTimeIntervalsMissingShards() {
    // expects the returned shards to be sorted by start time
    final List<ShardIdWithVersion> chosenShards = getChosenShards();

    final List<Interval> timeIntervalsMissingShards = Lists.newArrayList();
    DateTime processedUpTo = start;
    for(ShardIdWithVersion shard : chosenShards) {
        if(processedUpTo.isBefore(shard.getStart())) {
            timeIntervalsMissingShards.add(new Interval(processedUpTo, shard.getStart()));
        }
        processedUpTo = shard.getEnd();
    }

    if(processedUpTo.isBefore(end)) {
        timeIntervalsMissingShards.add(new Interval(processedUpTo, end));
    }
    return timeIntervalsMissingShards;
}
 
Example #10
Source File: DruidTable.java    From Quicksql with MIT License 6 votes vote down vote up
/**
 * Creates a Druid table.
 *
 * @param schema Druid schema that contains this table
 * @param dataSource Druid data source name
 * @param protoRowType Field names and types
 * @param metricFieldNames Names of fields that are metrics
 * @param intervals Default interval if query does not constrain the time, or null
 * @param timestampFieldName Name of the column that contains the time
 */
public DruidTable(DruidSchema schema, String dataSource,
    RelProtoDataType protoRowType, Set<String> metricFieldNames,
    String timestampFieldName, List<Interval> intervals,
    Map<String, List<ComplexMetric>> complexMetrics, Map<String, SqlTypeName> allFields) {
    this.timestampFieldName = Preconditions.checkNotNull(timestampFieldName);
    this.schema = Preconditions.checkNotNull(schema);
    this.dataSource = Preconditions.checkNotNull(dataSource);
    this.protoRowType = protoRowType;
    this.metricFieldNames = ImmutableSet.copyOf(metricFieldNames);
    this.intervals = intervals != null ? ImmutableList.copyOf(intervals)
        : ImmutableList.of(DEFAULT_INTERVAL);
    this.complexMetrics = complexMetrics == null ? ImmutableMap.<String, List<ComplexMetric>>of()
        : ImmutableMap.copyOf(complexMetrics);
    this.allFields = allFields == null ? ImmutableMap.<String, SqlTypeName>of()
        : ImmutableMap.copyOf(allFields);
}
 
Example #11
Source File: DruidQuery.java    From Quicksql with MIT License 6 votes vote down vote up
protected static void writeObject(JsonGenerator generator, Object o)
    throws IOException {
    if (o instanceof String) {
        String s = (String) o;
        generator.writeString(s);
    } else if (o instanceof Interval) {
        generator.writeString(o.toString());
    } else if (o instanceof Integer) {
        Integer i = (Integer) o;
        generator.writeNumber(i);
    } else if (o instanceof List) {
        writeArray(generator, (List<?>) o);
    } else if (o instanceof Json) {
        ((Json) o).write(generator);
    } else {
        throw new AssertionError("not a json object: " + o);
    }
}
 
Example #12
Source File: Lesson.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public SortedSet<Interval> getAllLessonIntervalsWithoutInstanceDates() {
    SortedSet<Interval> dates = new TreeSet<Interval>(new Comparator<Interval>() {

        @Override
        public int compare(Interval o1, Interval o2) {
            return o1.getStart().compareTo(o2.getStart());
        }

    });
    if (!wasFinished()) {
        YearMonthDay startDateToSearch = getLessonStartDay();
        YearMonthDay endDateToSearch = getLessonEndDay();
        final HourMinuteSecond b = getBeginHourMinuteSecond();
        final HourMinuteSecond e = getEndHourMinuteSecond();
        for (final YearMonthDay yearMonthDay : getAllValidLessonDatesWithoutInstancesDates(startDateToSearch, endDateToSearch)) {
            dates.add(new Interval(toDateTime(yearMonthDay, b), toDateTime(yearMonthDay, e)));
        }
    }
    return dates;
}
 
Example #13
Source File: PerformanceEvaluateHelper.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
/**
 * This helper initialize the performance evaluator with requested performance evaluation method.
 * @param performanceEvaluationMethod
 * The enum of performance evaluation method; if null or not found, then ANOMALY_PERCENTAGE
 * @param functionId
 * the original function id. It is for providing user labeled anomaly information for supervised performance
 * evaluation, such as precision, recall and f1 score.
 * @param clonedFunctionId
 * the cloned function id. It is the function id to be evaluated. If functionId == cloneFunctionId, we evaluate the
 * performance of the original function.
 * @param windowInterval
 * the time interval to be evaluated.
 * @param mergedAnomalyResultDAO
 * @return
 * A proper initiated performance evaluator.
 */
public static PerformanceEvaluate getPerformanceEvaluator(PerformanceEvaluationMethod performanceEvaluationMethod,
    long functionId, long clonedFunctionId, Interval windowInterval,
    MergedAnomalyResultManager mergedAnomalyResultDAO) {
  PerformanceEvaluate performanceEvaluator = null;
  List<MergedAnomalyResultDTO> knownAnomalies = mergedAnomalyResultDAO.findOverlappingByFunctionId(functionId,
      windowInterval.getStartMillis(), windowInterval.getEndMillis());
  List<MergedAnomalyResultDTO> detectedMergedAnomalies = mergedAnomalyResultDAO.findOverlappingByFunctionId(
      clonedFunctionId, windowInterval.getStartMillis(), windowInterval.getEndMillis());
  switch (performanceEvaluationMethod){
    case F1_SCORE:
      performanceEvaluator = new F1ScoreByTimePerformanceEvaluation(knownAnomalies, detectedMergedAnomalies);
      break;
    case RECALL:
      performanceEvaluator = new RecallByTimePreformanceEvaluation(knownAnomalies, detectedMergedAnomalies);
      break;
    case PRECISION:
      performanceEvaluator = new PrecisionByTimePerformanceEvaluation(knownAnomalies, detectedMergedAnomalies);
      break;
    case ANOMALY_PERCENTAGE:
    default:
      performanceEvaluator = new AnomalyPercentagePerformanceEvaluation(windowInterval, detectedMergedAnomalies);
  }
  return performanceEvaluator;
}
 
Example #14
Source File: SpaceOccupationEventBean.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public SpaceOccupationEventBean(Space space, Interval interval, String description, OccupationType type) {
    super();
    this.space = space;
    this.interval = interval;
    this.description = description;
    this.type = type;
}
 
Example #15
Source File: ThresholdRuleDetector.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
public DetectionResult runDetection(Interval window, String metricUrn) {
  MetricEntity me = MetricEntity.fromURN(metricUrn);
  Long endTime = window.getEndMillis();
  MetricSlice slice = MetricSlice.from(me.getId(), window.getStartMillis(), endTime, me.getFilters(), timeGranularity);

  InputData data = this.dataFetcher.fetchData(
      new InputDataSpec().withTimeseriesSlices(Collections.singletonList(slice))
          .withMetricIdsForDataset(Collections.singletonList(me.getId())));
  DataFrame df = data.getTimeseries().get(slice).renameSeries(COL_VALUE, COL_CURRENT);

  // defaults
  df.addSeries(COL_TOO_HIGH, BooleanSeries.fillValues(df.size(), false));
  df.addSeries(COL_TOO_LOW, BooleanSeries.fillValues(df.size(), false));

  // max
  if (!Double.isNaN(this.max)) {
    df.addSeries(COL_TOO_HIGH, df.getDoubles(COL_CURRENT).gt(this.max));
  }

  // min
  if (!Double.isNaN(this.min)) {
    df.addSeries(COL_TOO_LOW, df.getDoubles(COL_CURRENT).lt(this.min));
  }
  df.mapInPlace(BooleanSeries.HAS_TRUE, COL_ANOMALY, COL_TOO_HIGH, COL_TOO_LOW);
  DatasetConfigDTO datasetConfig = data.getDatasetForMetricId().get(me.getId());
  List<MergedAnomalyResultDTO> anomalies = DetectionUtils.makeAnomalies(slice, df, COL_ANOMALY, endTime,
      DetectionUtils.getMonitoringGranularityPeriod(monitoringGranularity, datasetConfig), datasetConfig);
  DataFrame baselineWithBoundaries = constructBaselineAndBoundaries(df);

  return DetectionResult.from(anomalies, TimeSeries.fromDataFrame(baselineWithBoundaries));
}
 
Example #16
Source File: AcademicChronology.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int getDayOfAcademicSemester(long instant) {
    AcademicCalendarEntry entryByInstant =
            academicCalendar.getEntryByInstant(instant, AcademicSemesterCE.class, AcademicYearCE.class);
    if (entryByInstant != null) {
        DateTime instantDateTime = new DateTime(instant);
        Interval interval = new Interval(entryByInstant.getBegin(), instantDateTime);
        int days = interval.toPeriod().getDays();
        if (days > 0) {
            return days;
        }
    }
    return 0;
}
 
Example #17
Source File: ExecutionCourse.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public WeeklyWorkLoadView getWeeklyWorkLoadView() {
    final Attends attends = findAttendsWithEnrolment();
    if (attends != null) {
        final Interval interval = attends.getWeeklyWorkLoadInterval();
        final WeeklyWorkLoadView weeklyWorkLoadView = new WeeklyWorkLoadView(interval);
        for (final Attends attend : getAttendsSet()) {
            weeklyWorkLoadView.add(attend);
        }
        return weeklyWorkLoadView;
    } else {
        return null;
    }
}
 
Example #18
Source File: InfoLessonInstanceAggregation.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public SortedSet<Integer> getWeeks() {
    final ExecutionCourse executionCourse = shift.getExecutionCourse();
    final YearMonthDay firstPossibleLessonDay = executionCourse.getMaxLessonsPeriod().getLeft();
    final YearMonthDay lastPossibleLessonDay = executionCourse.getMaxLessonsPeriod().getRight();
    return getWeeks(new Interval(firstPossibleLessonDay.toDateTimeAtMidnight(), lastPossibleLessonDay.toDateTimeAtMidnight()
            .plusDays(1)));
}
 
Example #19
Source File: FmtInterval.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * @throws SuperCsvCellProcessorException
 *             if value is null or not a Interval
 */
public Object execute(final Object value, final CsvContext context) {
	validateInputNotNull(value, context);
	if (!(value instanceof Interval)) {
		throw new SuperCsvCellProcessorException(Interval.class, value,
				context, this);
	}
	final Interval interval = (Interval) value;
	final String result = interval.toString();
	return next.execute(result, context);
}
 
Example #20
Source File: PercentageChangeRuleDetectorTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonthlyDetectionPercentage() throws DetectorException {
  AnomalyDetector percentageRule = new PercentageChangeRuleDetector();
  PercentageChangeRuleDetectorSpec spec = new PercentageChangeRuleDetectorSpec();
  spec.setOffset("mo1m");
  spec.setTimezone("UTC");
  spec.setPercentageChange(0.4);
  spec.setMonitoringGranularity("1_MONTHS");
  percentageRule.init(spec, new DefaultInputDataFetcher(this.provider, -1));
  List<MergedAnomalyResultDTO> anomalies = percentageRule.runDetection(new Interval(1546214400000L, 1551312000000L), "thirdeye:metric:1").getAnomalies();
  Assert.assertEquals(anomalies.size(), 1);
  Assert.assertEquals(anomalies.get(0).getStartTime(), 1548892800000L);
  Assert.assertEquals(anomalies.get(0).getEndTime(), 1551312000000L);
}
 
Example #21
Source File: TestBackwardAnoamlyFunctionUtils.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSplitSetsOfTimeSeries() {
  List<String> metricNames = new ArrayList<>();
  metricNames.add(TEST_METRIC);
  MetricSchema metricSchema =
      new MetricSchema(metricNames, Collections.nCopies(metricNames.size(), MetricType.DOUBLE));
  MetricTimeSeries metricTimeSeries = new MetricTimeSeries(metricSchema);
  metricTimeSeries.set(0, TEST_METRIC, 0);
  metricTimeSeries.set(TimeUnit.DAYS.toMillis(28), TEST_METRIC, 1);
  metricTimeSeries.set(TimeUnit.DAYS.toMillis(56), TEST_METRIC, 2);
  metricTimeSeries.set(TimeUnit.DAYS.toMillis(84), TEST_METRIC, 3);
  metricTimeSeries.set(TimeUnit.DAYS.toMillis(112), TEST_METRIC, 4);
  metricTimeSeries.set(TimeUnit.DAYS.toMillis(140), TEST_METRIC, 5);

  List<Interval> intervalList = new ArrayList<>();
  intervalList.add(new Interval(TimeUnit.DAYS.toMillis(140), TimeUnit.DAYS.toMillis(168)));
  intervalList.add(new Interval(0, TimeUnit.DAYS.toMillis(140)));

  List<TimeSeries> timeSeriesList =
      BackwardAnomalyFunctionUtils.splitSetsOfTimeSeries(metricTimeSeries, TEST_METRIC, intervalList);
  assert(timeSeriesList.size() == 2);
  assert(timeSeriesList.get(0).size() == 1);
  assert(timeSeriesList.get(1).size() == 5);

  // assert current time series
  assert(timeSeriesList.get(0).get(TimeUnit.DAYS.toMillis(140)).equals(5.0));

  for (int i = 0; i < 5; i++) {
    assert (timeSeriesList.get(1).get(TimeUnit.DAYS.toMillis(28 * i)).equals(Double.valueOf(i)));
  }
}
 
Example #22
Source File: OfflineSegmentIntervalChecker.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the number of missing segments based on the given existing segment intervals and the expected frequency
 * of the intervals.
 * <p>We count the interval as missing if there are at least two intervals between the start of the previous interval
 * and current interval. For long intervals (span over multiple intervals), count its start time as the start time of
 * the last interval it covers.
 *
 * @param segmentIntervals List of existing segment intervals
 * @param frequency Expected interval frequency
 * @return Number of missing segments
 */
@VisibleForTesting
static int computeNumMissingSegments(List<Interval> segmentIntervals, Duration frequency) {
  int numSegments = segmentIntervals.size();

  // If there are less than two segments, none can be missing
  if (numSegments < 2) {
    return 0;
  }

  // Sort the intervals by ascending starting time
  segmentIntervals.sort(Comparator.comparingLong(BaseInterval::getStartMillis));

  int numMissingSegments = 0;
  long frequencyMs = frequency.getMillis();
  long lastStartTimeMs = -1L;
  for (Interval segmentInterval : segmentIntervals) {
    long startTimeMs = segmentInterval.getStartMillis();
    if (lastStartTimeMs != -1L && startTimeMs - lastStartTimeMs > frequencyMs) {
      // If there are at least two intervals between the start of the previous interval and current interval, then
      // count the interval(s) as missing
      numMissingSegments += (startTimeMs - lastStartTimeMs - frequencyMs) / frequencyMs;
    }
    // Handle long intervals
    long endTimeMs = segmentInterval.getEndMillis();
    while (startTimeMs + frequencyMs <= endTimeMs) {
      startTimeMs += frequencyMs;
    }
    lastStartTimeMs = Math.max(lastStartTimeMs, startTimeMs);
  }
  return numMissingSegments;
}
 
Example #23
Source File: WCATool.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public HistoDbStats queryStats(Set<HistoDbAttributeId> attrIds, Interval interval, HistoDbHorizon horizon, boolean async) throws IOException, InterruptedException {
    lock.lock();
    try {
        return super.queryStats(attrIds, interval, horizon, async);
    } finally {
        lock.unlock();
    }
}
 
Example #24
Source File: WCAHistoLimitsTest.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testWrite() throws IOException, InterruptedException {
    Interval histoInterval = Interval.parse("2013-01-01T00:00:00+01:00/2013-01-31T23:59:00+01:00");

    Network network = NetworkTest1Factory.create();

    HistoDbClient histoDbClient = Mockito.mock(HistoDbClient.class);
    HistoDbStats histoDbStats = new HistoDbStats();
    histoDbStats.setValue(HistoDbStatsType.MIN, new HistoDbNetworkAttributeId(network.getLoads().iterator().next().getId(), HistoDbAttr.P), 0f);
    histoDbStats.setValue(HistoDbStatsType.MAX, new HistoDbNetworkAttributeId(network.getLoads().iterator().next().getId(), HistoDbAttr.P), 20f);
    histoDbStats.setValue(HistoDbStatsType.MIN, new HistoDbNetworkAttributeId(network.getGenerators().iterator().next().getId(), HistoDbAttr.P), 200f);
    histoDbStats.setValue(HistoDbStatsType.MAX, new HistoDbNetworkAttributeId(network.getGenerators().iterator().next().getId(), HistoDbAttr.P), 900f);
    Mockito.when(histoDbClient.queryStats(Matchers.anySet(), Matchers.eq(histoInterval), Matchers.eq(HistoDbHorizon.SN), Matchers.eq(true)))
           .thenReturn(histoDbStats);

    MemDataSource dataSource = new MemDataSource();

    StringToIntMapper<AmplSubset> mapper = new StringToIntMapper<>(AmplSubset.class);
    AmplUtil.fillMapper(mapper, network);

    WCAHistoLimits histoLimits = new WCAHistoLimits(histoInterval);
    histoLimits.load(network, histoDbClient);
    histoLimits.write(dataSource, mapper);

    String fileContent = String.join(System.lineSeparator(),
                                     "#loads historical data " + histoInterval,
                                     "#\"num\" \"min p (MW)\" \"max p (MW)\" \"id\"",
                                     "1 0.00000 20.0000 \""+ network.getLoads().iterator().next().getId() + "\"");
    assertEquals(fileContent, new String(dataSource.getData(WCAConstants.HISTO_LOADS_FILE_SUFFIX, WCAConstants.TXT_EXT), StandardCharsets.UTF_8).trim());

    fileContent = String.join(System.lineSeparator(),
                              "#generators historical data " + histoInterval,
                              "#\"num\" \"min p (MW)\" \"max p (MW)\" \"id\"",
                              "1 200.000 900.000 \""+ network.getGenerators().iterator().next().getId() + "\"");
    assertEquals(fileContent, new String(dataSource.getData(WCAConstants.HISTO_GENERATORS_FILE_SUFFIX, WCAConstants.TXT_EXT), StandardCharsets.UTF_8).trim());
}
 
Example #25
Source File: OnlineDbMVStore.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public List<OnlineWorkflowDetails> listWorkflows(Interval basecaseInterval) {
    LOGGER.info("Getting list of stored workflows run on basecases within the interval {}", basecaseInterval);
    String dateFormatPattern = "yyyyMMdd_HHmm";
    DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormatPattern);
    List<OnlineWorkflowDetails> workflowIds = new ArrayList<OnlineWorkflowDetails>();
    File[] files = config.getOnlineDbDir().toFile().listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().startsWith(STORED_WORKFLOW_PREFIX);
        }
    });
    for (File file : files) {
        if (file.isFile()) {
            String workflowId = file.getName().substring(STORED_WORKFLOW_PREFIX.length());
            if (workflowId.length() > dateFormatPattern.length() && workflowId.substring(dateFormatPattern.length(), dateFormatPattern.length() + 1).equals("_")) {
                String basecaseName = workflowId.substring(0, dateFormatPattern.length() - 1);
                DateTime basecaseDate = DateTime.parse(basecaseName, formatter);
                if (basecaseInterval.contains(basecaseDate.getMillis())) {
                    OnlineWorkflowDetails workflowDetails = new OnlineWorkflowDetails(workflowId);
                    workflowDetails.setWorkflowDate(getWorkflowDate(workflowId));
                    workflowIds.add(workflowDetails);
                }
            }
        }
    }
    Collections.sort(workflowIds, new Comparator<OnlineWorkflowDetails>() {
        @Override
        public int compare(OnlineWorkflowDetails wfDetails1, OnlineWorkflowDetails wfDetails2) {
            return wfDetails1.getWorkflowDate().compareTo(wfDetails2.getWorkflowDate());
        }
    });
    LOGGER.info("Found {} workflow(s)", workflowIds.size());
    return workflowIds;
}
 
Example #26
Source File: OccupationPeriod.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<Interval> getIntervals() {
    List<Interval> intervals = new LinkedList<Interval>();

    OccupationPeriod period = this;

    while (period != null) {
        intervals.add(period.getPeriodInterval());
        period = period.getNextPeriod();
    }

    return intervals;
}
 
Example #27
Source File: OnlineWorkflowParameters.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
public OnlineWorkflowParameters(DateTime baseCaseDate, int states, Interval histoInterval, String offlineWorkflowId, TimeHorizon timeHorizon,
                                String feAnalysisId, double rulesPurityThreshold, boolean storeStates, boolean analyseBasecase, boolean validation,
                                Set<SecurityIndexType> securityIndexes, CaseType caseType, Set<Country> countries, boolean mergeOptimized,
                                float limitReduction, boolean handleViolationsInN, float constraintMargin) {
    this(baseCaseDate,
            states,
            histoInterval,
            offlineWorkflowId,
            timeHorizon,
            feAnalysisId,
            rulesPurityThreshold,
            storeStates,
            analyseBasecase,
            validation,
            securityIndexes,
            caseType,
            countries,
            mergeOptimized,
            limitReduction,
            handleViolationsInN,
            constraintMargin,
            null);
    Objects.requireNonNull(this.baseCaseDate);
    Objects.requireNonNull(this.countries);
    Objects.requireNonNull(this.caseType);
    Objects.requireNonNull(this.histoInterval);
}
 
Example #28
Source File: HistoDbPrintAttributesTool.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void run(CommandLine line, ToolRunningContext context) throws Exception {
    OfflineConfig config = OfflineConfig.load();
    try (HistoDbClient histoDbClient = config.getHistoDbClientFactoryClass().newInstance().create()) {
        boolean statistics = line.hasOption("statistics");
        Set<HistoDbAttributeId> attrs = new LinkedHashSet<>();
        if (!statistics && line.hasOption("add-datetime")) {
            attrs.add(HistoDbMetaAttributeId.datetime);
        }
        for (String str : line.getOptionValue("attributes").split(",")) {
            attrs.add(HistoDbAttributeIdParser.parse(str));
        }
        Interval interval = Interval.parse(line.getOptionValue("interval"));
        boolean format = line.hasOption("format");
        HistoDbHorizon horizon = HistoDbHorizon.SN;
        if (line.hasOption("horizon")) {
            horizon = HistoDbHorizon.valueOf(line.getOptionValue("horizon"));
        }
        boolean async = false;
        boolean zipped = false;
        InputStream is = histoDbClient.queryCsv(statistics ? HistoQueryType.stats : HistoQueryType.data, attrs, interval, horizon, zipped, async);
        if (format) {
            format(is, zipped, context.getOutputStream());
        } else {
            try (Reader reader = createReader(is, zipped)) {
                CharStreams.copy(reader, context.getOutputStream());
            }
        }
    }
}
 
Example #29
Source File: TestReadableIntervalConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetIntoInterval_Object1() throws Exception {
    Interval i = new Interval(0L, 123L, CopticChronology.getInstance());
    MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
    ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
    assertEquals(0L, m.getStartMillis());
    assertEquals(123L, m.getEndMillis());
    assertEquals(CopticChronology.getInstance(), m.getChronology());
}
 
Example #30
Source File: CalendarUtils_Test.java    From estatio with Apache License 2.0 5 votes vote down vote up
@Test
public void intervalsInRangeTest() {
    List<Interval> intervals = CalendarUtils.intervalsInRange(new LocalDate(2012, 2, 1), new LocalDate(2012, 8, 1), "RRULE:FREQ=MONTHLY;INTERVAL=3");
    assertThat(intervals.size(), is(3));
    assertThat(intervals.get(0).getStart().toLocalDate(), is(new LocalDate(2012, 1, 1)));
    assertThat(intervals.get(2).getEnd().toLocalDate(), is(new LocalDate(2012, 10, 1)));
}