Search in sources :

Example 1 with Metric

use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.

the class Metrics method initializeMetrics.

private void initializeMetrics(Set<Class> metricsClassSet, Context context) {
    for (Class clazz : metricsClassSet) {
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors) {
            Class[] parameters = constructor.getParameterTypes();
            try {
                Metric metric;
                if (parameters.length == 0) {
                    metric = (Metric) constructor.newInstance();
                    classNameToMetric.put(metric.getClass().getName(), metric);
                } else if (parameters.length == 1 && parameters[0].equals(Context.class)) {
                    metric = (Metric) constructor.newInstance(context);
                    classNameToMetric.put(metric.getClass().getName(), metric);
                } else {
                    logger.warn("Can't initialize the Metric, please initialize it and add it to Metrics: {}", constructor);
                }
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                logger.error("Fail to initialize the metric instance: {}", constructor);
            }
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Metric(org.terasology.engine.telemetry.metrics.Metric) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Metric

use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.

the class TelemetrySystem method refreshBindingMap.

/**
 * Refresh the bindingMap who notes user's authorization.
 * If a new map is added during the game, the authorization functionality could also be used.
 */
private void refreshBindingMap() {
    Map<String, Metric> classNameToMetricMap = metrics.getClassNameToMetric();
    for (Metric metric : classNameToMetricMap.values()) {
        TelemetryCategory telemetryCategory = metric.getClass().getAnnotation(TelemetryCategory.class);
        if (!bindingMap.containsKey(telemetryCategory.id())) {
            bindingMap.put(telemetryCategory.id(), config.getTelemetryConfig().isTelemetryEnabled());
        }
        List<String> fields = metric.createTelemetryFieldList();
        for (String telemetryField : fields) {
            if (!bindingMap.containsKey(telemetryField)) {
                bindingMap.put(telemetryField, config.getTelemetryConfig().isTelemetryEnabled());
            }
        }
    }
}
Also used : CreatureKilledMetric(org.terasology.engine.telemetry.metrics.CreatureKilledMetric) GamePlayMetric(org.terasology.engine.telemetry.metrics.GamePlayMetric) ModulesMetric(org.terasology.engine.telemetry.metrics.ModulesMetric) BlockPlacedMetric(org.terasology.engine.telemetry.metrics.BlockPlacedMetric) SystemContextMetric(org.terasology.engine.telemetry.metrics.SystemContextMetric) Metric(org.terasology.engine.telemetry.metrics.Metric) BlockDestroyedMetric(org.terasology.engine.telemetry.metrics.BlockDestroyedMetric) GameConfigurationMetric(org.terasology.engine.telemetry.metrics.GameConfigurationMetric)

Example 3 with Metric

use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.

the class ModulesJsonProvider method writeTo.

@Override
public void writeTo(JsonGenerator generator, ILoggingEvent iLoggingEvent) throws IOException {
    TelemetryLogstashAppender appender = TelemetryUtils.fetchTelemetryLogstashAppender();
    Context context = appender.getGameContext();
    if (context != null) {
        Metrics metrics = context.get(Metrics.class);
        Optional<Metric> optional = metrics.getMetric(ModulesMetric.class);
        if (optional.isPresent()) {
            Metric modulesMetric = optional.get();
            Map<String, ?> map = modulesMetric.createTelemetryFieldToValue();
            Map<String, String> stringMap = TelemetryUtils.toStringMap(map);
            JsonWritingUtils.writeMapStringFields(generator, getFieldName(), stringMap);
        }
    }
}
Also used : Context(org.terasology.engine.context.Context) Metrics(org.terasology.engine.telemetry.Metrics) ModulesMetric(org.terasology.engine.telemetry.metrics.ModulesMetric) Metric(org.terasology.engine.telemetry.metrics.Metric)

Example 4 with Metric

use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.

the class TelemetryScreen method refreshContent.

private void refreshContent() {
    ColumnLayout mainLayout = new ColumnLayout();
    mainLayout.setHorizontalSpacing(8);
    mainLayout.setVerticalSpacing(8);
    fetchTelemetryCategoriesFromEngineOnlyEnvironment();
    for (Map.Entry<TelemetryCategory, Class> telemetryCategory : telemetryCategories.entrySet()) {
        Class metricClass = telemetryCategory.getValue();
        Optional<Metric> optional = metrics.getMetric(metricClass);
        if (optional.isPresent()) {
            Metric metric = optional.get();
            Map<String, ?> map = metric.createTelemetryFieldToValue();
            if (map != null) {
                addTelemetrySection(telemetryCategory.getKey(), mainLayout, map);
            }
        }
    }
    ScrollableArea area = find("area", ScrollableArea.class);
    if (area != null) {
        area.setContent(mainLayout);
    }
}
Also used : ScrollableArea(org.terasology.nui.layouts.ScrollableArea) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Metric(org.terasology.engine.telemetry.metrics.Metric) Map(java.util.Map)

Example 5 with Metric

use of org.terasology.engine.telemetry.metrics.Metric in project Terasology by MovingBlocks.

the class TelemetryUtils method fetchMetricAndSend.

/**
 * Fetch metric in {@link org.terasology.engine.context.Context} and send to the server.
 * This method could be used in the modules.
 * @param context The game context
 * @param metricClass The class of the metric which we want to track
 * @param nameSpace The name the class tracking this metric.
 */
public static void fetchMetricAndSend(Context context, Class metricClass, String nameSpace) {
    Emitter emitter = context.get(Emitter.class);
    Metrics metrics = context.get(Metrics.class);
    TelemetryConfiguration telemetryConfiguration = context.get(TelemetryConfiguration.class);
    if (emitter != null && metrics != null && telemetryConfiguration != null) {
        Optional<Metric> metricOptional = metrics.getMetric(metricClass);
        if (metricOptional.isPresent()) {
            Metric metric = metricOptional.get();
            Optional<Unstructured> unstructuredOptional = metric.getUnstructuredMetric();
            if (unstructuredOptional.isPresent()) {
                Unstructured unstructured = unstructuredOptional.get();
                trackMetric(emitter, nameSpace, unstructured, metric, telemetryConfiguration);
            }
        }
    } else {
        logger.error("Emitter or metrics or telemetryConfiguration is not in context");
    }
}
Also used : Emitter(com.snowplowanalytics.snowplow.tracker.emitter.Emitter) TelemetryConfiguration(org.terasology.engine.config.facade.TelemetryConfiguration) Unstructured(com.snowplowanalytics.snowplow.tracker.events.Unstructured) Metric(org.terasology.engine.telemetry.metrics.Metric)

Aggregations

Metric (org.terasology.engine.telemetry.metrics.Metric)7 Unstructured (com.snowplowanalytics.snowplow.tracker.events.Unstructured)2 Context (org.terasology.engine.context.Context)2 Metrics (org.terasology.engine.telemetry.Metrics)2 ModulesMetric (org.terasology.engine.telemetry.metrics.ModulesMetric)2 SystemContextMetric (org.terasology.engine.telemetry.metrics.SystemContextMetric)2 Emitter (com.snowplowanalytics.snowplow.tracker.emitter.Emitter)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 TelemetryConfiguration (org.terasology.engine.config.facade.TelemetryConfiguration)1 BlockDestroyedMetric (org.terasology.engine.telemetry.metrics.BlockDestroyedMetric)1 BlockPlacedMetric (org.terasology.engine.telemetry.metrics.BlockPlacedMetric)1 CreatureKilledMetric (org.terasology.engine.telemetry.metrics.CreatureKilledMetric)1 GameConfigurationMetric (org.terasology.engine.telemetry.metrics.GameConfigurationMetric)1 GamePlayMetric (org.terasology.engine.telemetry.metrics.GamePlayMetric)1 ColumnLayout (org.terasology.nui.layouts.ColumnLayout)1 ScrollableArea (org.terasology.nui.layouts.ScrollableArea)1