<commit_before>package info.puzz.graphanything.services;

import android.content.SyncStatusObserver;
import android.util.Log;

import com.jjoe64.graphview.series.DataPoint;

import org.apache.commons.math3.stat.regression.SimpleRegression;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

import info.puzz.graphanything.models.Graph;
import info.puzz.graphanything.models.GraphStats;
import info.puzz.graphanything.models.GraphValue;

/**
 * Created by puzz on 08/10/16.
 */
public class StatsCalculator {

    private static final String TAG = StatsCalculator.class.getSimpleName();

    private StatsCalculator() {
        throw new Error();
    }

    public static GraphStats calculate(Graph graph, List<DataPoint> dataPoints) {
        GraphStats res = new GraphStats();

        int sampleIntervalDays = graph.getStatsPeriod();
        if (sampleIntervalDays <= 0) {
            sampleIntervalDays = Graph.DEFAULT_STATS_SAMPLE_DAYS;
        }

        long thisWeek = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(sampleIntervalDays);
        long lastWeek = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2 * sampleIntervalDays);

        double sum = 0D;

        double sumThisWeek = 0D;
        int entriesThisWeek = 0;

        double sumLastWeek = 0D;
        int entriesLastWeek = 0;

        SimpleRegression regression = new SimpleRegression();

        for (int i = 0; i < dataPoints.size(); i++) {
            DataPoint point = dataPoints.get(i);

            if (i > 0) {
                if (point.getX() < dataPoints.get(i - 1).getX()) {
                    throw new Error("Not ordered by created!");
                }
            }

            sum += point.getY();
            boolean isThisWeek = point.getX() > thisWeek;
            boolean isLastWeek = thisWeek > point.getX() && point.getX() > lastWeek;
            if (isThisWeek) {
                //Log.d(TAG, new Timestamp(point.created) + " this week");
                sumThisWeek += point.getY();
                entriesThisWeek++;
            } else if (isLastWeek) {
                //Log.d(TAG, new Timestamp(point.created) + " last week");
                sumLastWeek += point.getY();
                entriesLastWeek++;
            } else {
                //Log.d(TAG, new Timestamp(point.created) + " older");
            }

            if (graph.calculateGoal() && (isThisWeek || isLastWeek)) {
                //System.out.println("data=" + point.created + "," + point.point);
                regression.addData(point.getX(), point.getY());
            }
        }

        res.setSum(sum);
        res.setAvg(sum / dataPoints.size());

        res.setSumLatestPeriod(sumThisWeek);
        res.setAvgLatestPeriod(sumThisWeek / entriesThisWeek);

        res.setSumPreviousPeriod(sumLastWeek);
        res.setAvgPreviousPeriod(sumLastWeek / entriesLastWeek);

        if (graph.calculateGoal()) {
            // y = intercept + slope * x
            res.setGoalIntercept(regression.getIntercept());
            res.setGoalSlope(regression.getSlope());

            //System.out.println(("intercept=" + intercept + " slope=" + slope));
<commit_msg>Don't calculate goal if too few points<commit_after>package info.puzz.graphanything.services;

import android.content.SyncStatusObserver;
import android.util.Log;

import com.jjoe64.graphview.series.DataPoint;

import org.apache.commons.math3.stat.regression.SimpleRegression;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

import info.puzz.graphanything.models.Graph;
import info.puzz.graphanything.models.GraphStats;
import info.puzz.graphanything.models.GraphValue;

/**
 * Created by puzz on 08/10/16.
 */
public class StatsCalculator {

    private static final String TAG = StatsCalculator.class.getSimpleName();

    private StatsCalculator() {
        throw new Error();
    }

    public static GraphStats calculate(Graph graph, List<DataPoint> dataPoints) {
        GraphStats res = new GraphStats();

        int sampleIntervalDays = graph.getStatsPeriod();
        if (sampleIntervalDays <= 0) {
            sampleIntervalDays = Graph.DEFAULT_STATS_SAMPLE_DAYS;
        }

        long thisWeek = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(sampleIntervalDays);
        long lastWeek = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2 * sampleIntervalDays);

        double sum = 0D;

        double sumThisWeek = 0D;
        int entriesThisWeek = 0;

        double sumLastWeek = 0D;
        int entriesLastWeek = 0;

        SimpleRegression regression = new SimpleRegression();

        for (int i = 0; i < dataPoints.size(); i++) {
            DataPoint point = dataPoints.get(i);

            if (i > 0) {
                if (point.getX() < dataPoints.get(i - 1).getX()) {
                    throw new Error("Not ordered by created!");
                }
            }

            sum += point.getY();
            boolean isThisWeek = point.getX() > thisWeek;
            boolean isLastWeek = thisWeek > point.getX() && point.getX() > lastWeek;
            if (isThisWeek) {
                //Log.d(TAG, new Timestamp(point.created) + " this week");
                sumThisWeek += point.getY();
                entriesThisWeek++;
            } else if (isLastWeek) {
                //Log.d(TAG, new Timestamp(point.created) + " last week");
                sumLastWeek += point.getY();
                entriesLastWeek++;
            } else {
                //Log.d(TAG, new Timestamp(point.created) + " older");
            }

            if (graph.calculateGoal() && (isThisWeek || isLastWeek)) {
                //System.out.println("data=" + point.created + "," + point.point);
                regression.addData(point.getX(), point.getY());
            }
        }

        res.setSum(sum);
        res.setAvg(sum / dataPoints.size());

        res.setSumLatestPeriod(sumThisWeek);
        res.setAvgLatestPeriod(sumThisWeek / entriesThisWeek);

        res.setSumPreviousPeriod(sumLastWeek);
        res.setAvgPreviousPeriod(sumLastWeek / entriesLastWeek);

        if (graph.calculateGoal() && dataPoints.size() >= 3) {
            // y = intercept + slope * x
            res.setGoalIntercept(regression.getIntercept());
            res.setGoalSlope(regression.getSlope());

            //System.out.println(("intercept=" + intercept + " slope=" + slope));

{'fraction_non_alphanumeric': 0.12008088349665579, 'fraction_numerical': 0.006066262249183388, 'mean_word_length': 2.250758341759353, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, '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': '682875', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1802, 'n_words': 366}