uawdijnntqw1x1x1
IP : 216.73.216.130
Hostname : it-staging-server
Kernel : Linux it-staging-server 5.15.0-131-generic #141-Ubuntu SMP Fri Jan 10 21:18:28 UTC 2025 x86_64
Disable Function : None :)
OS : Linux
PATH:
/
home
/
forge
/
stage.herta-bht.smartcon-survey.com
/
app
/
Services
/
KPITimelineService.php
/
/
<?php namespace App\Services; use App\Models\Brand; use App\Models\DataAggregation; use App\Models\DataUpload; use App\Models\Interview; use App\Models\Product; use Illuminate\Database\Eloquent\Collection as EloquentCollection; /** * Class KPITimelineService * * This service class handles the KPI timeline data processing for a given country and row ID. */ class KPITimelineService { /** @var EloquentCollection Collection of interview data */ private EloquentCollection $aggregatedInterviews; /** @var EloquentCollection collection of unique waves */ private EloquentCollection $quarters; /** @var array The published waves */ private array $publishedWaves; /** * KPITimelineService constructor. * * @param int $countryId The country ID */ public function __construct( private readonly string $title, private readonly int $countryId, private readonly Brand|Product $model, ) { $this->publishedWaves = DataUpload::getPublishedWaves($this->countryId); $this->aggregatedInterviews = $this->getAggregatedInterviews(); $this->quarters = Interview::getQuarterInfo($this->countryId, $this->publishedWaves); } /** * Get the collection of interview data. * * @return EloquentCollection The collection of interview data */ private function getAggregatedInterviews(): EloquentCollection { return DataAggregation::where([ 'country_id' => $this->countryId, 'model_id' => $this->model->sid, ]) ->whereIn('wave', $this->publishedWaves) ->get(); } /** * Get the values for a specific filter. * * @param callable $filter The filter function * @return array The filtered values */ private function getValues(callable $filter): array { $data = $this->quarters->reduce(function ($carry, $quarterInfo) use ($filter) { $key = "Q$quarterInfo->quarter $quarterInfo->year"; if (! isset($data[$key])) { $data[$key] = [ 'x' => $key, 'y' => 0, 'n' => 0, 'base' => $quarterInfo->aggregate, ]; } $filteredData = $this->aggregatedInterviews ->filter($filter) ->filter(fn ($i) => $i->quarter === $quarterInfo->quarter && $i->year === $quarterInfo->year); foreach ($filteredData as $item) { $data[$key]['y'] += $quarterInfo->aggregate > 0 ? round($item->aggregate * 100 / $quarterInfo->aggregate) : 0; $data[$key]['n'] += $item->aggregate; } return $carry + $data; }, []); return array_values($this->fillMissing($data)); } private function fillMissing(array $data): array { foreach ($this->quarters as $quarterInfo) { $key = "Q$quarterInfo->quarter $quarterInfo->year"; if (! isset($data[$key])) { $data[$key] = [ 'x' => $key, 'y' => 0, 'n' => 0, ]; } } return $data; } /** * Get the awareness KPI data. * * @return array The awareness data */ public function awareness(): array { return [ 'label' => 'Awareness', 'data' => $this->getValues( fn ($i) => ($i->question_name === 'Q2' || $i->question_name === 'Q2a') && $i->value === '1' ), 'borderColor' => '#AEDFFF', 'id' => $this->model->sid, ]; } /** * Get the consideration KPI data. * * @return array The consideration data */ public function consideration(): array { return [ 'label' => 'Consideration', 'data' => $this->getValues( fn ($i) => ($i->question_name === 'Q8' || $i->question_name === 'Q8a') && in_array($i->value, ['1', '2', '3']) ), 'borderColor' => '#5FA3F7', 'id' => $this->model->sid, ]; } /** * Get the trial KPI data. * * @return array The trial data */ public function trial(): array { return [ 'label' => 'Trial', 'data' => $this->getValues( fn ($i) => ($i->question_name === 'Q4' || $i->question_name === 'Q4a') && $i->value === '1' ), 'borderColor' => '#0064D2', 'id' => $this->model->sid, ]; } /** * Get the purchase KPI data. * * @return array The purchase data */ public function purchase(): array { return [ 'label' => 'Repurchase', 'data' => $this->getValues( fn ($i) => ($i->question_name === 'Q5' || $i->question_name === 'Q5a') && $i->value === '1' ), 'borderColor' => '#003F6B', 'id' => $this->model->sid, ]; } /** * Get the loyalty KPI data. * * @return array The loyalty data */ public function loyalty(): array { return [ 'label' => 'Loyalty', 'data' => $this->getValues( fn ($i) => ($i->question_name === 'Q7' || $i->question_name === 'Q7a') && (int) $i->value === $this->model->sid ), 'borderColor' => '#002337', 'id' => $this->model->sid, ]; } /** * Get all KPI data. * * @return array The total number of interviews and the KPI data */ public function getData(): array { return [ 'totalN' => $this->quarters->sum('aggregate'), 'title' => $this->title, 'data' => [ 'labels' => $this->quarters->map(fn ($quarterInfo) => "Q$quarterInfo->quarter $quarterInfo->year")->values()->toArray(), 'datasets' => [ $this->awareness(), $this->consideration(), $this->trial(), $this->purchase(), $this->loyalty(), ], ], ]; } }
/home/forge/stage.herta-bht.smartcon-survey.com/app/Services/KPITimelineService.php