Search in sources :

Example 1 with MetricFormula

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;
}
Also used : CardinalitySize(com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) MetricProjectionMaker(com.yahoo.elide.datastores.aggregation.query.MetricProjectionMaker) DefaultMetricProjectionMaker(com.yahoo.elide.datastores.aggregation.query.DefaultMetricProjectionMaker) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) TableSource(com.yahoo.elide.datastores.aggregation.annotation.TableSource) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) ReadPermission(com.yahoo.elide.annotation.ReadPermission)

Example 2 with MetricFormula

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.");
    }
}
Also used : MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) ToString(lombok.ToString)

Example 3 with MetricFormula

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());
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with MetricFormula

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());
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) MetricFormula(com.yahoo.elide.datastores.aggregation.annotation.MetricFormula) Test(org.junit.jupiter.api.Test)

Aggregations

MetricFormula (com.yahoo.elide.datastores.aggregation.annotation.MetricFormula)4 ReadPermission (com.yahoo.elide.annotation.ReadPermission)2 Field (com.yahoo.elide.core.type.Field)2 ColumnMeta (com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta)2 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)2 Table (com.yahoo.elide.modelconfig.model.Table)2 Test (org.junit.jupiter.api.Test)2 CardinalitySize (com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize)1 TableSource (com.yahoo.elide.datastores.aggregation.annotation.TableSource)1 DefaultMetricProjectionMaker (com.yahoo.elide.datastores.aggregation.query.DefaultMetricProjectionMaker)1 MetricProjectionMaker (com.yahoo.elide.datastores.aggregation.query.MetricProjectionMaker)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ToString (lombok.ToString)1