<commit_before>/*
 * Copyright (C) 2013 Codenvy.
 */
package com.codenvy.analytics.metrics;

import com.codenvy.analytics.metrics.value.*;

import java.io.IOException;
import java.util.*;

/** @author <a href="mailto:<EMAIL>"><NAME></a> */
public abstract class AbstractTopReferrersMetric extends CalculatedMetric {

    public static final int LIFE_TIME_PERIOD = -1;
    private final int period;

    public AbstractTopReferrersMetric(MetricType metricType, int period) {
        super(metricType, MetricType.REFERRERS);
        this.period = period;
    }

    /** {@inheritDoc} */
    @Override
    public ValueData getValue(Map<String, String> context) throws IOException {
        context = overrideContext(context);

        MapStringFixedLongListValueData referrers = (MapStringFixedLongListValueData)super.getValue(context);

        List<ListStringValueData> result = new ArrayList<>(100);
        for (Map.Entry<String, FixedListLongValueData> refEntry : keepTopItems(referrers)) {
            List<String> item = new ArrayList<>(13);

            String referrer = refEntry.getKey();
            List<Long> values = refEntry.getValue().getAll();

            Long mins = values.get(0);
            Long tmpWs = values.get(1);
            Long sess = values.get(2);
            Long auth = values.get(3);
            Long conv = values.get(4);
            Long bld = values.get(5);
            Long run = values.get(6);
            Long dpl = values.get(7);

            item.add(referrer);
            item.add(tmpWs.toString());
            item.add(sess.toString());
            item.add("" + 100D * (sess.longValue() - auth.longValue()) / sess.longValue());
            item.add("" + 100D * auth.longValue() / sess.longValue());
            item.add("" + 100D * (sess.longValue() - conv.longValue()) / sess.longValue());
            item.add("" + 100D * conv.longValue() / sess.longValue());
            item.add("" + 100D * bld.longValue() / sess.longValue());
            item.add("" + 100D * run.longValue() / sess.longValue());
            item.add("" + 100D * dpl.longValue() / sess.longValue());
            item.add("" + mins.longValue() / 60);

            addFirstLastSessionsOccurrence(item);

            result.add(new ListStringValueData(item));
        }

        return new ListListStringValueData(result);
    }

    private void addFirstLastSessionsOccurrence(List<String> item) throws IOException {
        Map<String, String> context = Utils.newContext();
        MetricParameter.FROM_DATE.putDefaultValue(context);
        MetricParameter.TO_DATE.putDefaultValue(context);

        MetricFilter.REFERRER_URL.put(context, item.get(0));

        FactorySessionFirstMetric sessionFirstMetric =
                (FactorySessionFirstMetric)MetricFactory.createMetric(MetricType.FACTORY_SESSION_FIRST);
        ListStringValueData firstSession = (ListStringValueData)sessionFirstMetric.getValue(context);
        String date = firstSession.size() == 0 ? "" : sessionFirstMetric.getDate(firstSession);<commit_msg>DASHB-102: top factories<commit_after>/*
 * Copyright (C) 2013 Codenvy.
 */
package com.codenvy.analytics.metrics;

import com.codenvy.analytics.metrics.value.*;

import java.io.IOException;
import java.util.*;

/** @author <a href="mailto:<EMAIL>"><NAME></a> */
public abstract class AbstractTopReferrersMetric extends CalculatedMetric {

    public static final int LIFE_TIME_PERIOD = -1;
    private final int period;

    public AbstractTopReferrersMetric(MetricType metricType, int period) {
        super(metricType, MetricType.REFERRERS);
        this.period = period;
    }

    /** {@inheritDoc} */
    @Override
    public ValueData getValue(Map<String, String> context) throws IOException {
        context = overrideContext(context);

        MapStringFixedLongListValueData referrers = (MapStringFixedLongListValueData)super.getValue(context);

        List<ListStringValueData> result = new ArrayList<>(100);
        for (Map.Entry<String, FixedListLongValueData> refEntry : keepTopItems(referrers)) {
            List<String> item = new ArrayList<>(13);

            String referrer = refEntry.getKey();
            List<Long> values = refEntry.getValue().getAll();

            Long mins = values.get(0);
            Long tmpWs = values.get(1);
            Long sess = values.get(2);
            Long auth = values.get(3);
            Long conv = values.get(4);
            Long bld = values.get(5);
            Long run = values.get(6);
            Long dpl = values.get(7);

            item.add(referrer);
            item.add(tmpWs.toString());
            item.add(sess.toString());
            item.add("" + 100D * (sess - auth) / sess);
            item.add("" + 100D * auth / sess);
            item.add("" + 100D * (sess - conv) / sess);
            item.add("" + 100D * conv / sess);
            item.add("" + 100D * bld / sess);
            item.add("" + 100D * run / sess);
            item.add("" + 100D * dpl / sess);
            item.add("" + mins / 60);

            addFirstLastSessionsOccurrence(item);

            result.add(new ListStringValueData(item));
        }

        return new ListListStringValueData(result);
    }

    private void addFirstLastSessionsOccurrence(List<String> item) throws IOException {
        Map<String, String> context = Utils.newContext();
        MetricParameter.FROM_DATE.putDefaultValue(context);
        MetricParameter.TO_DATE.putDefaultValue(context);

        MetricFilter.REFERRER_URL.put(context, item.get(0));

        FactorySessionFirstMetric sessionFirstMetric =
                (FactorySessionFirstMetric)MetricFactory.createMetric(MetricType.FACTORY_SESSION_FIRST);
        ListStringValueData firstSession = (ListStringValueData)sessionFirstMetric.getValue(context);
        String date = firstSession.size() == 0 ? "" : sessionFirstMetric.getDate(firstSession);
{'fraction_non_alphanumeric': 0.1255308306437914, 'fraction_numerical': 0.015118056735179209, 'mean_word_length': 2.724225173940544, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337247', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1656, 'n_words': 331}