<commit_before><?php

namespace Baconfy\Analytics\Services\Visits;

use Baconfy\Analytics\Services\GetByHours;
use Carbon\Carbon;
use DB;

class GetVisitByPeriod
{

    /**
     * @param Carbon $startDate
     * @param Carbon $endDate
     * @return mixed
     */
    public function fire(Carbon $startDate, Carbon $endDate)
    {
        return $this->getData($startDate, $endDate);
    }

    /**
     * @param Carbon $startDate
     * @param Carbon $endDate
     * @return mixed
     */
    public function csv(Carbon $startDate, Carbon $endDate)
    {
        $data = $this->getData($startDate, $endDate);

        $lines[] = [GetByHours::title($startDate, $endDate), trans('analytics::messages.visits'), trans('analytics::messages.unique')];

        if (count($data)) {
            foreach ($data as $record) {
                $lines[] = get_object_vars($record);
            }
        }

        $output = '';
        if (count($lines)) {
            foreach ($lines as $line) {
                $output .= implode(',', $line) . "\n";
            }
        }

        return $output;
    }

    /**
     * @param Carbon $startDate
     * @param Carbon $endDate
     * @return mixed
     */
    private function getData(Carbon $startDate, Carbon $endDate)
    {
        $key = GetByHours::key($startDate, $endDate);

        $Select = DB::table('analytcs_visits')->select(
            DB::raw("date_format(created_at, '$key') as `key`"),
            DB::raw("count(uuid) as total"),
            DB::raw("count(distinct uuid) as uniques")
        );

        $Select->where('created_at', '>=', "$startDate 00:00:00");
        $Select->where('created_at', '<=', "$endDate 23:59:59");
        $Select->groupBy('key');

        return $Select->get();
    }

}<commit_msg>Add method csv<commit_after><?php

namespace Baconfy\Analytics\Services\Visits;

use Baconfy\Analytics\Services\GetByHours;
use Carbon\Carbon;
use DB;

class GetVisitByPeriod
{
    private $data;

    private $start;

    private $end;

    /**
     * @param Carbon $startDate
     * @param Carbon $endDate
     * @return mixed
     */
    public function fire(Carbon $startDate, Carbon $endDate)
    {
        $this->start = $startDate;
        $this->end = $endDate;

        return $this->getData();
    }

    public function getLabels()
    {
        $output = [];

        if ($this->data) {
            foreach ($this->data as $row) {
                $output[] = $row->key;
            }
        }

        return $output;
    }

    public function getTotal()
    {
        $output = [];

        if ($this->data) {
            foreach ($this->data as $row) {
                $output[] = $row->total;
            }
        }

        return $output;
    }

    public function getUniques()
    {
        $output = [];

        if ($this->data) {
            foreach ($this->data as $row) {
                $output[] = $row->uniques;
            }
        }

        return $output;
    }

    /**
     * @return mixed
     */
    private function getData()
    {
        $key = GetByHours::key($this->start, $this->end);

        $Select = DB::table('analytcs_visits')->select(
            DB::raw("date_format(created_at, '$key') as `key`"),
            DB::raw("count(uuid) as total"),
            DB::raw("count(distinct uuid) as uniques")
        );

        $Select->where('created_at', '>=', "{$this->start} 00:00:00");
        $Select->where('created_at', '<=', "{$this->end} 23:59:59");
        $Select->groupBy('key');

        $this->data = $Select->get();

        return $this->data;
    }

}
{'fraction_non_alphanumeric': 0.16545758928571427, 'fraction_numerical': 0.006696428571428571, 'mean_word_length': 1.9603633360858794, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '2636882', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1105, 'n_words': 223}