<commit_before><?php

namespace Nodes\Backend\Dashboard\Types\NodesStatistics;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use Nodes\Backend\Dashboard\Exceptions\MissingConfigException;

/**
 * Class Statistic
 *
 * @author  <NAME> <<EMAIL>>
 * @package Nodes\Backend\Dashboard\Types\NodesStatistics
 */
abstract class Statistic
{
    /**
     * @var string
     */
    protected $period;

    /**
     * @var string
     */
    protected $type = 'line-chart';

    /**
     * @var string
     */
    protected $title;

    /**
     * @var string
     */
    protected $gaId;

    /**
     * @var array
     */
    protected $chartData;

    /**
     * @var string
     */
    protected $id;

    /**
     * IFrame constructor.
     *
     * @param array $config
     * @throws \Nodes\Backend\Dashboard\Exceptions\MissingConfigException
     */
    public function __construct(array $config)
    {
        // Guard title param
        if (empty($config['title']) || !is_string($config['title'])) {
            throw new MissingConfigException('Missing title');
        }

        // Guard title url param
        if (empty($config['gaId']) || !is_string($config['gaId'])) {
            throw new MissingConfigException('Missing gaId');
        }

        // Guard type
        if (empty($this->period)) {
            throw new MissingConfigException('Period is not set in the child object');
        }

        // Set params
        $this->title = $config['title'];
        $this->gaId = $config['gaId'];

        // Assign random id
//        $this->id = Str::random();
        $this->id = self::randomString();

        $this->prepareChartData();
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @author <NAME>asmussen <<EMAIL>>
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getPeriod()
    {
        return $this->period;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return array
     */
    public function getChartData()
    {
        return $this->chartData;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return bool
     * @throws \Nodes\Backend\Dashboard\Exceptions\UnsupportedTypeException
     */
    protected function prepareChartData()
    {
        $url = sprintf(env('NODES_STATISTICS_HISTORY'), $this->gaId);

        // Generate query
        if ($this->period == 'monthly') {
            $query = [
                'from' => Carbon::now()->subMonth()->format('Y-m-d'),
                'to' => Carbon::now()->format('Y-m-d'),
                'group' => 'day'
            ];

        } else {
            $query = [
                'from' => Carbon::now()->format('Y-m-d'),
                'to' => Carbon::now()->addDay()->format('Y-m-d'),
                'group' => 'hour'
            ];
        }

        // Append query to url
        $url .= '?' . http_build_query($query);

        $chartData = [
            'id' => $this->id,
            'title' => $this->title,
            'data' => [],
            'labels' => []
        ];

        // Do api call
        try{
            $client = new Client();
            $response = json_decode($client->get($url)->getBody(), true);
        } catch(\Exception $e) {
            return false;
        }

        // Now get total visitors and from those platforms
        foreach($response['data'] as $data) {
            $time = Carbon::createFromFormat('Y-m-d H:i:s', $data['time']);
            if($this->period == 'monthly') {
                $label = $time->format('m/d');
            } else {
                $label = $time->format('m/d H:00');
            }

            // Append
            $chartData['labels'][] = $label;
            $chartData['data'][] = !empty($data['visit_count']) ? $data['visit_count'] : 0;
        }

        $this->chartData = $chartData;

    }

    /**
     * @author <NAME> <<EMAIL>>
     * @param int $length
     * @return string
     */
    private static function randomString($length = 16) {
        $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
    }
}<commit_msg>added cache for dashboard statistics<commit_after><?php

namespace Nodes\Backend\Dashboard\Types\NodesStatistics;

use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use Nodes\Backend\Dashboard\Exceptions\MissingConfigException;

/**
 * Class Statistic
 *
 * @author  <NAME> <<EMAIL>>
 * @package Nodes\Backend\Dashboard\Types\NodesStatistics
 */
abstract class Statistic
{
    /**
     * @var string
     */
    protected $period;

    /**
     * @var string
     */
    protected $type = 'line-chart';

    /**
     * @var string
     */
    protected $title;

    /**
     * @var string
     */
    protected $gaId;

    /**
     * @var array
     */
    protected $chartData;

    /**
     * @var string
     */
    protected $id;

    /**
     * IFrame constructor.
     *
     * @param array $config
     * @throws \Nodes\Backend\Dashboard\Exceptions\MissingConfigException
     */
    public function __construct(array $config)
    {
        // Guard title param
        if (empty($config['title']) || !is_string($config['title'])) {
            throw new MissingConfigException('Missing title');
        }

        // Guard title url param
        if (empty($config['gaId']) || !is_string($config['gaId'])) {
            throw new MissingConfigException('Missing gaId');
        }

        // Guard type
        if (empty($this->period)) {
            throw new MissingConfigException('Period is not set in the child object');
        }

        // Set params
        $this->title = $config['title'];
        $this->gaId = $config['gaId'];

        // Assign random id
        $this->id = self::randomString();

        $this->prepareChartData();
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return string
     */
    public function getPeriod()
    {
        return $this->period;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return array
     */
    public function getChartData()
    {
        return $this->chartData;
    }

    /**
     * @author <NAME> <<EMAIL>>
     * @return bool
     * @throws \Nodes\Backend\Dashboard\Exceptions\UnsupportedTypeException
     */
    protected function prepareChartData()
    {
        $url = sprintf(env('NODES_STATISTICS_HISTORY'), $this->gaId);

        // Generate query
        if ($this->period == 'monthly') {
            $query = [
                'from' => Carbon::now()->subMonth()->format('Y-m-d'),
                'to' => Carbon::now()->format('Y-m-d'),
                'group' => 'day'
            ];

        } else {
            $query = [
                'from' => Carbon::now()->format('Y-m-d'),
                'to' => Carbon::now()->addDay()->format('Y-m-d'),
                'group' => 'hour'
            ];
        }

        // Append query to url
        $url .= '?' . http_build_query($query);

        $chartData = [
            'id' => $this->id,
            'title' => $this->title,
            'data' => [],
            'labels' => []
        ];

        if(!$response = \Cache::get($url)) {
            // Do api call
            try{
                $client = new Client();
                $response = json_decode($client->get($url)->getBody(), true);

                \Cache::put($url, $response, 1440);
            } catch(\Exception $e) {
                return false;
            }
        }

        // Now get total visitors and from those platforms
        foreach($response['data'] as $data) {
            $time = Carbon::createFromFormat('Y-m-d H:i:s', $data['time']);
            if($this->period == 'monthly') {
                $label = $time->format('m/d');
            } else {
                $label = $time->format('m/d H:00');
            }

            // Append
            $chartData['labels'][] = $label;
            $chartData['data'][] = !empty($data['visit_count']) ? $data['visit_count'] : 0;
        }

        $this->chartData = $chartData;

    }

    /**
     * @author <NAME> <<EMAIL>>
     * @param int $length
     * @return string
     */
    private static function randomString($length = 16) {
        $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
    }
}
{'fraction_non_alphanumeric': 0.16443762332821749, 'fraction_numerical': 0.0019732514799386098, 'mean_word_length': 1.7135633551457465, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 124, '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': '365257', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2718, 'n_words': 543}