public static boolean isRegistered(int operatorType, int metricId){ CoreOperatorTypeMetricsMap coreOperatorTypeMetricsMap = getCoreOperatorTypeMetricsMap(); return getMetricById(coreOperatorTypeMetricsMap, operatorType, metricId).isPresent(); }
/** * @param coreOperatorTypeMetricsMap * @param operatorType * @return Array of Metric names if operator type is present in the map else empty list */ public static String[] getMetricNames(CoreOperatorTypeMetricsMap coreOperatorTypeMetricsMap, int operatorType) { Optional<MetricsDef> metricsDef = getMetricsDef(coreOperatorTypeMetricsMap, operatorType); return metricsDef.map((metricDef) -> metricDef.getMetricDefList().stream().map(UserBitShared.MetricDef::getName).toArray(String[]::new)).orElse(ArrayUtils.EMPTY_STRING_ARRAY); }
final String[] metricNames = OperatorMetricRegistry.getMetricNames(operatorType.getNumber()); final Integer[] metricIds = OperatorMetricRegistry.getMetricIds(coreOperatorTypeMetricsMap, operatorType.getNumber()); int i = 1; for (final int metricId : metricIds) { Optional<MetricDef> metric = OperatorMetricRegistry.getMetricById(coreOperatorTypeMetricsMap, operatorType.getNumber(), metricId); assert metric.isPresent(); // Since metric id was retrieved from map, doing a reverse lookup shouldn't fail. metricIdToMetricTableColumnIndex.put(metricId, i - 1);
public OperatorWrapper(int major, List<ImmutablePair<OperatorProfile, Integer>> ops, CoreOperatorTypeMetricsMap coreOperatorTypeMetricsMap) { Preconditions.checkArgument(ops.size() > 0); this.major = major; firstProfile = ops.get(0).getLeft(); operatorType = CoreOperatorType.valueOf(firstProfile.getOperatorType()); operatorName = operatorType == null ? "UNKNOWN_OPERATOR" : operatorType.toString(); this.coreOperatorTypeMetricsMap = Optional.ofNullable(coreOperatorTypeMetricsMap).orElse(OperatorMetricRegistry.getCoreOperatorTypeMetricsMap()); this.ops = ops; size = ops.size(); }
@Override public String toString(){ String[] names = OperatorMetricRegistry.getMetricNames(operatorType); StringBuilder sb = new StringBuilder();
@Test public void testGetMetricsTableHandlesNotRegisteredMetrics() { OperatorProfile op = OperatorProfile .newBuilder().addMetric( UserBitShared.MetricValue.newBuilder() .setMetricId(1) .setDoubleValue(200)) .addMetric( UserBitShared.MetricValue.newBuilder() .setMetricId(3) .setDoubleValue(21)) .setOperatorId(UserBitShared.CoreOperatorType.PARQUET_ROW_GROUP_SCAN.getNumber()) .build(); ImmutablePair<OperatorProfile, Integer> pair = new ImmutablePair<>(op, 1); OperatorWrapper ow = new OperatorWrapper(1, ImmutableList.of(pair), OperatorMetricRegistry.getCoreOperatorTypeMetricsMap()); String html = ow.getMetricsTable(); Pattern columnPattern = Pattern.compile("<td>.*?</td>"); Matcher matcher = columnPattern.matcher(html); int count = 0; while (matcher.find()){ count++; } // Unregistered metrics should not appear on profiles screen assertEquals(2, count); }
profileBuilder.setOperatorTypeMetricsMap(OperatorMetricRegistry.getCoreOperatorTypeMetricsMap());
/** * @param coreOperatorTypeMetricsMap * @param operatorType * @return Array of Metric Ids if operator type is present in the map else empty list */ public static Integer[] getMetricIds(CoreOperatorTypeMetricsMap coreOperatorTypeMetricsMap, int operatorType) { Optional<MetricsDef> metricsDef = getMetricsDef(coreOperatorTypeMetricsMap, operatorType); return metricsDef.map((metricDef) -> metricDef.getMetricDefList().stream().map(UserBitShared.MetricDef::getId).toArray(Integer[]::new)).orElse(ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY); }
/** * @param coreOperatorTypeMetricsMap * @param operatorType * @param metricId * @return Optional<MetricDef> containing metric defination if present in the map */ public static Optional<UserBitShared.MetricDef> getMetricById(CoreOperatorTypeMetricsMap coreOperatorTypeMetricsMap, int operatorType, int metricId) { Optional<MetricsDef> metricsDef = getMetricsDef(coreOperatorTypeMetricsMap, operatorType); if (metricsDef.isPresent() && metricId < metricsDef.get().getMetricDefCount()) { return Optional.ofNullable(metricsDef.get().getMetricDef(metricId)); } return Optional.empty(); }