<commit_before><?php
namespace Www\Controllers;

use \Www\Models;
use \Carbon\Carbon;

class StatsController extends \Controller {
    public function __construct()
    {
        if (\Config::get('app.stats_secret') != \Input::get('secret'))
        {
            \App::abort(401);
        }
    }

    public function getDonationsBullet()
    {
        $period = in_array(\Input::get('period'), ['day', 'week', 'month', 'year']) ? \Input::get('period') : 'month';
        $period_info = $this->getPeriodInformation($period);


        $campaign_starts_at = strtotime(\Config::get('fundraising.starting'));
        $campaign_ends_at = strtotime(\Config::get('fundraising.by'));
        $campaign_duration = $campaign_ends_at - $campaign_starts_at;
        $campaign_goal = \Config::get('fundraising.amount');
        $campaign_expected_donors = \Config::get('fundraising.expected_quantity');


        $donations_period = iterator_to_array(Models\Donation
            ::where('created_at', '>', $period_info->starts_at)
            ->where('created_at', '<', $period_info->ends_at)
            ->get());
        $donations_campaign = iterator_to_array(Models\Donation
            ::where('created_at', '>', Carbon::createFromTimestamp($campaign_starts_at))
            ->where('created_at', '<', Carbon::createFromTimestamp($campaign_ends_at))
            ->get());

        return json_encode((object)[
            'orientation' => 'horizontal',
            'item' => [
                $this->getBulletChartForPeriod(
                    'Rev ('.ucfirst($period).')',
                    'U.S. $ (1000s)',
                    $period,
                    (($campaign_goal / $campaign_duration) * $period_info->duration)/1000,
                    array_reduce($donations_period,
                        function($a, $b) {
                            return (object)['amount' => $a->amount + $b->amount];
                        }, (object)['amount' => 0])->amount/1000
                ),

                $this->getBulletChartForPeriod(
                    'Count ('.ucfirst($period).')',
                    'Absolute #',
                    $period,
                    ($campaign_expected_donors / $campaign_duration) * $period_info->duration,
                    count($donations_period)
                ),


                $this->getBulletChartForPeriod(
                    'Rev (Campaign)',
                    'U.S. $ (1000s)',
                    $period,
                    $campaign_goal/1000,
                    array_reduce($donations_campaign,
                        function($a, $b) {
                            return (object)['amount' => $a->amount + $b->amount];
                        }, (object)['amount' => 0])->amount
                ),

                $this->getBulletChartForPeriod(
                    'Median (Campaign)',
                    'U.S. $',
                    $period,
                    floor($campaign_goal/$campaign_expected_donors),
                    $this->getMedian(array_map(function ($a) { return $a->amount; }, $donations_campaign)),
                    false
                ),
            ]
        ]);
    }

    private function getBulletChartForPeriod($title, $units, $period,  $goal, $value, $do_forecast = true, $time = null)
    {
        $forecast = $this->getNaiveDeltaForecast($period, $value, $time);
        $max = ceil(max($value, $goal, $forecast)) * 1.1;


        $measure = [
            'current' => (object)[
                    'start' => 0,
                    'end' => $value
                ]
        ];
        if ($do_forecast) {
            $measure['projected'] = (object)[<commit_msg>Fix campaign revenue goal (geckoboard)<commit_after><?php
namespace Www\Controllers;

use \Www\Models;
use \Carbon\Carbon;

class StatsController extends \Controller {
    public function __construct()
    {
        if (\Config::get('app.stats_secret') != \Input::get('secret'))
        {
            \App::abort(401);
        }
    }

    public function getDonationsBullet()
    {
        $period = in_array(\Input::get('period'), ['day', 'week', 'month', 'year']) ? \Input::get('period') : 'month';
        $period_info = $this->getPeriodInformation($period);


        $campaign_starts_at = strtotime(\Config::get('fundraising.starting'));
        $campaign_ends_at = strtotime(\Config::get('fundraising.by'));
        $campaign_duration = $campaign_ends_at - $campaign_starts_at;
        $campaign_goal = \Config::get('fundraising.amount');
        $campaign_expected_donors = \Config::get('fundraising.expected_quantity');


        $donations_period = iterator_to_array(Models\Donation
            ::where('created_at', '>', $period_info->starts_at)
            ->where('created_at', '<', $period_info->ends_at)
            ->get());
        $donations_campaign = iterator_to_array(Models\Donation
            ::where('created_at', '>', Carbon::createFromTimestamp($campaign_starts_at))
            ->where('created_at', '<', Carbon::createFromTimestamp($campaign_ends_at))
            ->get());

        return json_encode((object)[
            'orientation' => 'horizontal',
            'item' => [
                $this->getBulletChartForPeriod(
                    'Rev ('.ucfirst($period).')',
                    'U.S. $ (1000s)',
                    $period,
                    (($campaign_goal / $campaign_duration) * $period_info->duration)/1000,
                    array_reduce($donations_period,
                        function($a, $b) {
                            return (object)['amount' => $a->amount + $b->amount];
                        }, (object)['amount' => 0])->amount/1000
                ),

                $this->getBulletChartForPeriod(
                    'Count ('.ucfirst($period).')',
                    'Absolute #',
                    $period,
                    ($campaign_expected_donors / $campaign_duration) * $period_info->duration,
                    count($donations_period)
                ),


                $this->getBulletChartForPeriod(
                    'Rev (Campaign)',
                    'U.S. $ (1000s)',
                    $period,
                    $campaign_goal/1000,
                    array_reduce($donations_campaign,
                        function($a, $b) {
                            return (object)['amount' => $a->amount + $b->amount];
                        }, (object)['amount' => 0])->amount/1000
                ),

                $this->getBulletChartForPeriod(
                    'Median (Campaign)',
                    'U.S. $',
                    $period,
                    floor($campaign_goal/$campaign_expected_donors),
                    $this->getMedian(array_map(function ($a) { return $a->amount; }, $donations_campaign)),
                    false
                ),
            ]
        ]);
    }

    private function getBulletChartForPeriod($title, $units, $period,  $goal, $value, $do_forecast = true, $time = null)
    {
        $forecast = $this->getNaiveDeltaForecast($period, $value, $time);
        $max = ceil(max($value, $goal, $forecast)) * 1.1;


        $measure = [
            'current' => (object)[
                    'start' => 0,
                    'end' => $value
                ]
        ];
        if ($do_forecast) {
            $measure['projected'] = (object)[
{'fraction_non_alphanumeric': 0.15074626865671642, 'fraction_numerical': 0.008141112618724558, 'mean_word_length': 1.6725888324873097, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 69, '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': '4650591', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1968, 'n_words': 297}