Skip to content

Commit

Permalink
[pinpoint-apm#9393] Return full range of timestamps and fill empty va…
Browse files Browse the repository at this point in the history
…lues to zero
  • Loading branch information
ga-ram committed Nov 9, 2022
1 parent f97430a commit 7497ee5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public UriStatView getCollectedUriStat(@RequestParam("applicationName") String a
uriStats = uriStatService.getCollectedUriStatAgent(builder.build());
}

return new UriStatView(uri, uriStats);
return new UriStatView(uri, timeWindow, uriStats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.navercorp.pinpoint.metric.common.model.StringPrecondition;
import com.navercorp.pinpoint.metric.common.model.UriStat;
import com.navercorp.pinpoint.metric.web.util.TimeWindow;
import com.navercorp.pinpoint.metric.web.view.TimeSeriesValueView;
import com.navercorp.pinpoint.metric.web.view.TimeseriesValueGroupView;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -27,6 +28,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Arrays;
import java.util.stream.Collectors;

public class UriStatGroup implements TimeseriesValueGroupView {
Expand All @@ -41,9 +43,9 @@ public UriStatGroup() {
this.values = Collections.emptyList();
}

public UriStatGroup(String uri, List<UriStat> uriStats) {
public UriStatGroup(String uri, int dataSize, TimeWindow timeWindow, List<UriStat> uriStats) {
this.uri = uri;
this.values = UriStatValue.createValueList(uriStats);
this.values = UriStatValue.createValueList(dataSize, timeWindow, uriStats);
}

@Override
Expand All @@ -61,22 +63,31 @@ public static class UriStatValue implements TimeSeriesValueView {
private final String fieldName;
private final List<Integer> values;

public static List<TimeSeriesValueView> createValueList(List<UriStat> uriStats) {
public static List<TimeSeriesValueView> createValueList(int dataSize, TimeWindow timeWindow, List<UriStat> uriStats) {
Objects.requireNonNull(uriStats);

List<TimeSeriesValueView> values = new ArrayList<>();

final int bucketSize = uriStats.get(0).getTotalHistogram().length;
for (int i = 0 ; i < bucketSize; i++) {
final int histogramIndex = i;
values.add(new UriStatValue(FIELD_PREFIX + histogramIndex,
uriStats.stream().map(e -> e.getTotalHistogram()[histogramIndex]).collect(Collectors.toList())));

for (int histogramIndex = 0 ; histogramIndex < bucketSize; histogramIndex++) {
int[] filledData = new int[dataSize];

for (UriStat uriStat : uriStats) {
int filledIndex = timeWindow.getWindowIndex(uriStat.getTimestamp());
if (filledData[filledIndex] != 0) {
throw new RuntimeException("Uri stat timestamp mismatch.");
}
filledData[filledIndex] = uriStat.getTotalHistogram()[histogramIndex];
}
values.add(new UriStatValue(FIELD_PREFIX + histogramIndex, filledData));
}
return values;
}

public UriStatValue(String fieldName, List<Integer> values) {
public UriStatValue(String fieldName, int[] uriStats) {
this.fieldName = StringPrecondition.requireHasLength(fieldName, "fieldName");
this.values = Objects.requireNonNull(values);
this.values = Arrays.stream(uriStats).boxed().collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.navercorp.pinpoint.metric.common.model.UriStat;
import com.navercorp.pinpoint.metric.web.model.UriStatGroup;
import com.navercorp.pinpoint.metric.web.util.Range;
import com.navercorp.pinpoint.metric.web.util.TimeWindow;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,16 +32,27 @@ public class UriStatView implements TimeSeriesView {

private final List<TimeseriesValueGroupView> uriStats = new ArrayList<>();

public UriStatView(String uri, List<UriStat> uriStats) {
public UriStatView(String uri, TimeWindow timeWindow, List<UriStat> uriStats) {
Objects.requireNonNull(timeWindow, "timeWindow");
Objects.requireNonNull(uriStats, "uriStats");
this.timestampList = uriStats.stream().map(UriStat::getTimestamp).collect(Collectors.toList());
this.timestampList = createTimeStampList(timeWindow);
if (uriStats.isEmpty()) {
this.uriStats.add(UriStatGroup.EMPTY_URI_STAT_GROUP);
} else {
this.uriStats.add(new UriStatGroup(uri, uriStats));
this.uriStats.add(new UriStatGroup(uri, timestampList.size(), timeWindow, uriStats));
}
}

private List<Long> createTimeStampList(TimeWindow timeWindow) {
List<Long> timestampList = new ArrayList<>((int) timeWindow.getWindowRangeCount());

for (Long timestamp : timeWindow) {
timestampList.add(timestamp);
}

return timestampList;
}

@Override
public String getTitle() {
return "uriStat";
Expand Down

0 comments on commit 7497ee5

Please sign in to comment.