use of com.yahoo.elide.datastores.aggregation.annotation.MetricFormula in project elide by yahoo.
the class TableType method buildAnnotations.
private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(Measure measure) {
Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
annotations.put(ColumnMeta.class, new ColumnMeta() {
@Override
public Class<? extends Annotation> annotationType() {
return ColumnMeta.class;
}
@Override
public String friendlyName() {
return measure.getFriendlyName();
}
@Override
public String description() {
return measure.getDescription();
}
@Override
public String category() {
return measure.getCategory();
}
@Override
public TableSource tableSource() {
return buildTableSource(null);
}
@Override
public String[] tags() {
return measure.getTags().toArray(new String[0]);
}
@Override
public String[] values() {
return new String[0];
}
@Override
public boolean isHidden() {
return measure.getHidden() != null && measure.getHidden();
}
@Override
public String filterTemplate() {
return measure.getFilterTemplate();
}
@Override
public CardinalitySize size() {
return CardinalitySize.UNKNOWN;
}
});
annotations.put(MetricFormula.class, new MetricFormula() {
@Override
public ArgumentDefinition[] arguments() {
return getArgumentDefinitions(measure.getArguments());
}
@Override
public Class<? extends Annotation> annotationType() {
return MetricFormula.class;
}
@Override
public String value() {
if (measure.getDefinition() != null) {
return trimColumnReferences(measure.getDefinition());
} else {
return "";
}
}
@Override
public Class<? extends MetricProjectionMaker> maker() {
if (measure.getMaker() == null || measure.getMaker().isEmpty()) {
return DefaultMetricProjectionMaker.class;
}
try {
return (Class<? extends MetricProjectionMaker>) Class.forName(measure.getMaker());
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
});
String readPermission = measure.getReadAccess();
if (StringUtils.isNotEmpty(readPermission)) {
annotations.put(ReadPermission.class, new ReadPermission() {
@Override
public Class<? extends Annotation> annotationType() {
return ReadPermission.class;
}
@Override
public String expression() {
return readPermission;
}
});
}
return annotations;
}
use of com.yahoo.elide.datastores.aggregation.annotation.MetricFormula in project elide by yahoo.
the class Metric method verifyFormula.
private void verifyFormula(MetricFormula formula) {
if (formula == null) {
throw new IllegalStateException("Trying to construct metric field " + getId() + " without @MetricFormula.");
}
String defaultValue;
Class<?> defaultMaker;
try {
defaultValue = (String) MetricFormula.class.getDeclaredMethod("value").getDefaultValue();
defaultMaker = (Class<?>) MetricFormula.class.getDeclaredMethod("maker").getDefaultValue();
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalStateException("Error encountered while constructing metric field: " + getId() + ". " + e.getMessage());
}
if (formula.value().equals(defaultValue) && formula.maker().equals(defaultMaker)) {
throw new IllegalStateException("Trying to construct metric field " + getId() + " with default values. Provide either value or maker in @MetricFormula.");
}
if (!formula.value().equals(defaultValue) && !formula.maker().equals(defaultMaker)) {
throw new IllegalStateException("Trying to construct metric field " + getId() + " with value and maker. Provide either one in @MetricFormula, both are not allowed.");
}
}
use of com.yahoo.elide.datastores.aggregation.annotation.MetricFormula in project elide by yahoo.
the class TableTypeTest method testMeasureAnnotations.
@Test
void testMeasureAnnotations() throws Exception {
Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
Table testTable = Table.builder().table("table1").name("Table").measure(Measure.builder().type(Type.MONEY).category("category1").definition("SUM{{ price}}").hidden(false).friendlyName("Price").name("price").readAccess("Admin").description("A measure").tags(tags).build()).build();
TableType testType = new TableType(testTable);
Field field = testType.getDeclaredField("price");
assertNotNull(field);
ReadPermission readPermission = field.getAnnotation(ReadPermission.class);
assertEquals("Admin", readPermission.expression());
ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
assertEquals("A measure", columnMeta.description());
assertEquals("category1", columnMeta.category());
assertEquals("Price", columnMeta.friendlyName());
assertEquals(CardinalitySize.UNKNOWN, columnMeta.size());
assertEquals(tags, new HashSet<>(Arrays.asList(columnMeta.tags())));
MetricFormula metricFormula = field.getAnnotation(MetricFormula.class);
assertEquals("SUM{{price}}", metricFormula.value());
assertEquals(DefaultMetricProjectionMaker.class, metricFormula.maker());
}
use of com.yahoo.elide.datastores.aggregation.annotation.MetricFormula in project elide by yahoo.
the class TableTypeTest method testInvalidResolver.
@Test
void testInvalidResolver() throws Exception {
Table testTable = Table.builder().table("table1").name("Table").measure(Measure.builder().name("measure1").type(Type.BOOLEAN).maker("does.not.exist.class").build()).build();
TableType testType = new TableType(testTable);
Field field = testType.getDeclaredField("measure1");
MetricFormula metricFormula = field.getAnnotation(MetricFormula.class);
assertThrows(IllegalStateException.class, () -> metricFormula.maker());
}
Aggregations