use of org.eclipse.tracecompass.tmf.core.request.TmfEventRequest in project tracecompass by tracecompass.
the class EventAnnotationProvider method fetchAnnotations.
@Override
public TmfModelResponse<@NonNull AnnotationModel> fetchAnnotations(Map<@NonNull String, @NonNull Object> fetchParameters, @Nullable IProgressMonitor monitor) {
List<Long> timeRequested = DataProviderParameterUtils.extractTimeRequested(fetchParameters);
List<@NonNull Long> entries = DataProviderParameterUtils.extractSelectedItems(fetchParameters);
@Nullable Set<@NonNull String> categories = DataProviderParameterUtils.extractSelectedCategories(fetchParameters);
if (timeRequested == null || entries == null) {
return NO_DATA;
}
TmfModelResponse<@NonNull TmfTreeModel<M>> tree = Objects.requireNonNull(fTreeResolver.apply(fetchParameters, monitor));
TmfTreeModel<M> model = tree.getModel();
if (model == null) {
return NO_DATA;
}
Function<M, Integer> keyMapper = entry -> (Integer) Iterables.get(entry.getMetadata().get(fMetadataKey), 0);
Predicate<M> predicate = entry -> {
Collection<@NonNull Object> collection = entry.getMetadata().get(fMetadataKey);
return !collection.isEmpty() && !Objects.equals(Iterables.get(collection, 0), -1);
};
Map<Integer, TimeGraphEntryModel> rowMap = new LinkedHashMap<>();
List<@NonNull M> entries2 = model.getEntries();
entries2.stream().filter(predicate).filter(fAdditionalPredicate).forEach(element -> rowMap.put(keyMapper.apply(element), element));
Map<String, Collection<Annotation>> markers = new LinkedHashMap<>();
TmfTimeRange tr = new TmfTimeRange(TmfTimestamp.fromNanos(timeRequested.get(0)), TmfTimestamp.fromNanos(timeRequested.get(timeRequested.size() - 1)));
EventAnnotationProvider<@NonNull M> lock = this;
synchronized (lock) {
for (ITmfTrace source : fMarkerTraces) {
TmfEventRequest old = fRunningRequests.remove(source);
if (old != null && old.isRunning()) {
old.cancel();
}
}
for (ITmfTrace source : fMarkerTraces) {
if (categories != null && !categories.contains(source.getName())) {
// marker category is filtered out
continue;
}
TmfEventRequest req = new TmfEventRequest(ITmfEvent.class, tr, 0, Integer.MAX_VALUE, ExecutionType.FOREGROUND) {
private int timesliceIndex = 0;
private Set<Object> values = new HashSet<>();
private long next() {
if (timeRequested.size() > timesliceIndex + 1) {
return timeRequested.get(timesliceIndex + 1);
}
return Long.MAX_VALUE;
}
@Override
public void handleData(ITmfEvent event) {
super.handleData(event);
while (event.getTimestamp().toNanos() > next()) {
timesliceIndex++;
values.clear();
if (timesliceIndex >= timeRequested.size() - 1) {
done();
return;
}
}
Object value = TmfTraceUtils.resolveEventAspectOfClassForEvent(event.getTrace(), fAspect, event);
if (value != null && !values.contains(value)) {
values.add(value);
synchronized (markers) {
Collection<Annotation> markerList = markers.computeIfAbsent(String.valueOf(event.getTrace().getName()), string -> new ArrayList<>());
TimeGraphEntryModel entryModel = rowMap.get(value);
if (entryModel != null) {
String name = event.getName();
Map<String, Object> style = new HashMap<>();
style.put(StyleProperties.SYMBOL_TYPE, SymbolType.INVERTED_TRIANGLE);
style.put(StyleProperties.COLOR, getMarkerColor(name));
style.put(StyleProperties.HEIGHT, 0.3);
style.put(StyleProperties.VERTICAL_ALIGN, StyleProperties.VerticalAlign.TOP);
markerList.add(new Annotation(event.getTimestamp().toNanos(), 0L, entryModel.getId(), AnnotationType.CHART, name, new OutputElementStyle(name, style)));
}
}
}
}
private String getMarkerColor(String name) {
// $NON-NLS-1$
return Objects.requireNonNull(fMarkerColorCache.computeIfAbsent(name, label -> Objects.requireNonNull(String.format("#%6x", label.hashCode() & 0xffffff))));
}
};
fRunningRequests.put(source, req);
source.sendRequest(req);
}
try {
for (ITmfTrace source : fMarkerTraces) {
TmfEventRequest req = null;
synchronized (lock) {
req = fRunningRequests.get(source);
}
if (req != null) {
req.waitForCompletion();
fRunningRequests.remove(source);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// $NON-NLS-1$
return new TmfModelResponse<>(new AnnotationModel(markers), Status.COMPLETED, "");
}
Aggregations