@Test public void testTimeSeriesStats() throws Throwable { long startTime = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()); int numBins = 4; long interval = 1000; double value = 100; TimeSeriesStats timeSeriesStats = new TimeSeriesStats(numBins, interval, EnumSet.allOf(AggregationType.class)); for (int i = 0; i < numBins; i++) { startTime += TimeUnit.MILLISECONDS.toMicros(interval); value += 1; timeSeriesStats.add(startTime, value, 1); } assertTrue(timeSeriesStats.bins.size() == numBins); for (int i = 0; i < numBins / 2; i++) { startTime += TimeUnit.MILLISECONDS.toMicros(interval); value += 1; timeSeriesStats.add(startTime, value, 1); } assertTrue(timeSeriesStats.bins.size() == numBins); long timeMicros = startTime - TimeUnit.MILLISECONDS.toMicros(interval * (numBins - 1)); long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicros); timeMillis -= (timeMillis % interval); assertTrue(timeSeriesStats.bins.firstKey() == timeMillis); double origValue = value; double accumulatedValue = value; double newValue = value; double count = 1; for (int i = 0; i < numBins / 2; i++) { newValue++; count++; timeSeriesStats.add(startTime, newValue, 2); accumulatedValue += newValue; } ExtendedTimeBin lastBin = (ExtendedTimeBin) timeSeriesStats.bins .get(timeSeriesStats.bins.lastKey()); assertTrue(lastBin.avg == accumulatedValue / count); assertTrue(lastBin.var == (1.0 * numBins) / 2.0); assertTrue(lastBin.sum.equals((2 * count) - 1)); assertTrue(lastBin.count == count); assertTrue(lastBin.max.equals(newValue)); assertTrue(lastBin.min.equals(origValue)); assertTrue(lastBin.latest.equals(newValue)); ServiceStats allStats = new ServiceStats(); ServiceStat tsst = new ServiceStat(); tsst.name = "timeSeriesStat"; tsst.timeSeriesStats = timeSeriesStats; allStats.entries.put(tsst.name, tsst); validateJsonStatSerialization(allStats, "\"min\": 103.0", "\"max\": 108.0"); timeSeriesStats = new TimeSeriesStats(numBins, interval, EnumSet.of(AggregationType.AVG)); timeSeriesStats.add(startTime, value, value); TimeBin lBin = timeSeriesStats.bins.get(timeSeriesStats.bins.lastKey()); assertTrue(lBin.avg != 0.0); assertTrue(lBin.count != 0); allStats = new ServiceStats(); tsst = new ServiceStat(); tsst.name = "timeSeriesStat"; tsst.timeSeriesStats = timeSeriesStats; allStats.entries.put(tsst.name, tsst); validateJsonStatSerialization(allStats, "\"min\": 106.0", "\"max\": 106.0"); timeSeriesStats = new TimeSeriesStats(numBins, interval, EnumSet.of(AggregationType.MIN, AggregationType.MAX)); timeSeriesStats.add(startTime, value, value); lastBin = (ExtendedTimeBin) timeSeriesStats.bins.get(timeSeriesStats.bins.lastKey()); assertTrue(lastBin.avg == 0.0); assertTrue(lastBin.count == 0); assertTrue(lastBin.sum == null); assertTrue(lastBin.max != null); assertTrue(lastBin.min != null); timeSeriesStats = new TimeSeriesStats(numBins, interval, EnumSet.of(AggregationType.LATEST)); timeSeriesStats.add(startTime, value, value); lastBin = (ExtendedTimeBin) timeSeriesStats.bins.get(timeSeriesStats.bins.lastKey()); assertTrue(lastBin.avg == 0.0); assertTrue(lastBin.count == 0); assertTrue(lastBin.sum == null); assertTrue(lastBin.max == null); assertTrue(lastBin.min == null); assertTrue(lastBin.latest.equals(value)); String name = UUID.randomUUID().toString(); ExampleServiceState s = new ExampleServiceState(); s.name = name; Consumer<Operation> bodySetter = (o) -> { o.setBody(s); }; URI factoryURI = UriUtils.buildFactoryUri(this.host, ExampleService.class); Map<URI, ExampleServiceState> states = this.host.doFactoryChildServiceStart(null, 1, ExampleServiceState.class, bodySetter, factoryURI); ExampleServiceState exampleServiceState = states.values().iterator().next(); ServiceStats.ServiceStat stat = new ServiceStat(); stat.name = "key1"; stat.latestValue = 100; stat.timeSeriesStats = new TimeSeriesStats(numBins, 1, EnumSet.allOf(AggregationType.class)); this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); for (int i = 0; i < numBins; i++) { Thread.sleep(1); this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)).setBody(stat)); } allStats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri( this.host, exampleServiceState.documentSelfLink)); validateJsonStatSerialization(allStats, "\"min\": 100.0", "\"max\": 100.0"); ServiceStat retStatEntry = allStats.entries.get(stat.name); assertTrue(retStatEntry.accumulatedValue == 100 * (numBins + 1)); assertTrue(retStatEntry.latestValue == 100); assertTrue(retStatEntry.version == numBins + 1); assertTrue(retStatEntry.timeSeriesStats.bins.size() == numBins); String statName = UUID.randomUUID().toString(); ExampleServiceState exampleState = new ExampleServiceState(); exampleState.name = statName; Consumer<Operation> setter = (o) -> { o.setBody(exampleState); }; Map<URI, ExampleServiceState> stateMap = this.host.doFactoryChildServiceStart(null, 1, ExampleServiceState.class, setter, UriUtils.buildFactoryUri(this.host, ExampleService.class)); ExampleServiceState returnExampleState = stateMap.values().iterator().next(); ServiceStats.ServiceStat sourceStat1 = new ServiceStat(); sourceStat1.name = "sourceKey1"; sourceStat1.latestValue = 100; Long sourceTimeMicrosUtc1 = 946713600000000L; sourceStat1.sourceTimeMicrosUtc = sourceTimeMicrosUtc1; ServiceStats.ServiceStat sourceStat2 = new ServiceStat(); sourceStat2.name = "sourceKey2"; sourceStat2.latestValue = 100; Long sourceTimeMicrosUtc2 = 946800000000000L; sourceStat2.sourceTimeMicrosUtc = sourceTimeMicrosUtc2; sourceStat1.timeSeriesStats = new TimeSeriesStats(numBins, 1, EnumSet.allOf(AggregationType.class)); sourceStat2.timeSeriesStats = new TimeSeriesStats(numBins, 1, EnumSet.allOf(AggregationType.class)); this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, returnExampleState.documentSelfLink)).setBody(sourceStat1)); this.host.sendAndWaitExpectSuccess(Operation.createPost(UriUtils.buildStatsUri( this.host, returnExampleState.documentSelfLink)).setBody(sourceStat2)); allStats = getStats(returnExampleState); retStatEntry = allStats.entries.get(sourceStat1.name); assertTrue(retStatEntry.accumulatedValue == 100); assertTrue(retStatEntry.latestValue == 100); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.timeSeriesStats.bins.size() == 1); assertTrue(retStatEntry.timeSeriesStats.bins.firstKey() .equals(TimeUnit.MICROSECONDS.toMillis(sourceTimeMicrosUtc1))); retStatEntry = allStats.entries.get(sourceStat2.name); assertTrue(retStatEntry.accumulatedValue == 100); assertTrue(retStatEntry.latestValue == 100); assertTrue(retStatEntry.version == 1); assertTrue(retStatEntry.timeSeriesStats.bins.size() == 1); assertTrue(retStatEntry.timeSeriesStats.bins.firstKey() .equals(TimeUnit.MICROSECONDS.toMillis(sourceTimeMicrosUtc2))); }
@Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); UtilityService setParent(Service parent); @Override void authorizeRequest(Operation op); @Override void handleRequest(Operation op); @Override void handleCreate(Operation post); @Override void handleStart(Operation startPost); @Override void handleStop(Operation op); @Override void handleRequest(Operation op, OperationProcessingStage opProcessingStage); boolean hasSubscribers(); boolean hasStats(); void notifySubscribers(Operation op); void redirectGetToHtmlUiResource(Operation op, String htmlResourcePath); @Override void handleConfigurationRequest(Operation op); void handlePatchConfiguration(Operation op, ServiceConfigUpdateRequest updateBody); @Override void setStat(ServiceStat stat, double newValue); @Override void adjustStat(ServiceStat stat, double delta); @Override ServiceStat getStat(String name); @Override ServiceStats getStats(); ServiceStat getStat(String name, boolean create); @Override ServiceHost getHost(); @Override String getSelfLink(); @Override URI getUri(); @Override OperationProcessingChain getOperationProcessingChain(); @Override ProcessingStage getProcessingStage(); @Override EnumSet<ServiceOption> getOptions(); @Override boolean hasOption(ServiceOption cap); @Override void toggleOption(ServiceOption cap, boolean enable); @Override void adjustStat(String name, double delta); @Override void setStat(String name, double newValue); @Override void handleMaintenance(Operation post); @Override void setHost(ServiceHost serviceHost); @Override void setSelfLink(String path); @Override void setOperationProcessingChain(OperationProcessingChain opProcessingChain); @Override void setProcessingStage(ProcessingStage initialized); @Override ServiceDocument setInitialState(Object state, Long initialVersion); @Override Service getUtilityService(String uriPath, boolean allocate); @Override boolean queueRequest(Operation op); @Override void sendRequest(Operation op); @Override ServiceDocument getDocumentTemplate(); @Override void setPeerNodeSelectorPath(String uriPath); @Override String getPeerNodeSelectorPath(); @Override void setDocumentIndexPath(String uriPath); @Override String getDocumentIndexPath(); @Override void setState(Operation op, ServiceDocument newState); @SuppressWarnings("unchecked") @Override T getState(Operation op); @Override void setMaintenanceIntervalMicros(long micros); @Override void setCacheClearDelayMicros(long micros); @Override long getMaintenanceIntervalMicros(); @Override long getCacheClearDelayMicros(); @Override Operation dequeueRequest(); @Override Class<? extends ServiceDocument> getStateType(); @Override final void setAuthorizationContext(Operation op, AuthorizationContext ctx); @Override final AuthorizationContext getSystemAuthorizationContext(); }
UtilityService implements Service { @Override public ServiceStats getStats() { if (this.stats == null) { return null; } synchronized (this.stats) { return Utils.clone(this.stats); } } UtilityService(); UtilityService setParent(Service parent); @Override void authorizeRequest(Operation op); @Override void handleRequest(Operation op); @Override void handleCreate(Operation post); @Override void handleStart(Operation startPost); @Override void handleStop(Operation op); @Override void handleRequest(Operation op, OperationProcessingStage opProcessingStage); boolean hasSubscribers(); boolean hasStats(); void notifySubscribers(Operation op); void redirectGetToHtmlUiResource(Operation op, String htmlResourcePath); @Override void handleConfigurationRequest(Operation op); void handlePatchConfiguration(Operation op, ServiceConfigUpdateRequest updateBody); @Override void setStat(ServiceStat stat, double newValue); @Override void adjustStat(ServiceStat stat, double delta); @Override ServiceStat getStat(String name); @Override ServiceStats getStats(); ServiceStat getStat(String name, boolean create); @Override ServiceHost getHost(); @Override String getSelfLink(); @Override URI getUri(); @Override OperationProcessingChain getOperationProcessingChain(); @Override ProcessingStage getProcessingStage(); @Override EnumSet<ServiceOption> getOptions(); @Override boolean hasOption(ServiceOption cap); @Override void toggleOption(ServiceOption cap, boolean enable); @Override void adjustStat(String name, double delta); @Override void setStat(String name, double newValue); @Override void handleMaintenance(Operation post); @Override void setHost(ServiceHost serviceHost); @Override void setSelfLink(String path); @Override void setOperationProcessingChain(OperationProcessingChain opProcessingChain); @Override void setProcessingStage(ProcessingStage initialized); @Override ServiceDocument setInitialState(Object state, Long initialVersion); @Override Service getUtilityService(String uriPath, boolean allocate); @Override boolean queueRequest(Operation op); @Override void sendRequest(Operation op); @Override ServiceDocument getDocumentTemplate(); @Override void setPeerNodeSelectorPath(String uriPath); @Override String getPeerNodeSelectorPath(); @Override void setDocumentIndexPath(String uriPath); @Override String getDocumentIndexPath(); @Override void setState(Operation op, ServiceDocument newState); @SuppressWarnings("unchecked") @Override T getState(Operation op); @Override void setMaintenanceIntervalMicros(long micros); @Override void setCacheClearDelayMicros(long micros); @Override long getMaintenanceIntervalMicros(); @Override long getCacheClearDelayMicros(); @Override Operation dequeueRequest(); @Override Class<? extends ServiceDocument> getStateType(); @Override final void setAuthorizationContext(Operation op, AuthorizationContext ctx); @Override final AuthorizationContext getSystemAuthorizationContext();  }