Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/Admin/StatsController.php
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public function chart($chart) | ||||
return response()->json($result); | return response()->json($result); | ||||
} | } | ||||
/** | /** | ||||
* Get discounts chart | * Get discounts chart | ||||
*/ | */ | ||||
protected function chartDiscounts(): array | protected function chartDiscounts(): array | ||||
{ | { | ||||
// TODO: Exclude deleted account wallets | |||||
$discounts = DB::table('wallets') | $discounts = DB::table('wallets') | ||||
->join('discounts', 'discounts.id', '=', 'wallets.discount_id') | |||||
->selectRaw("discount, count(discount_id) as cnt") | ->selectRaw("discount, count(discount_id) as cnt") | ||||
->join('discounts', 'discounts.id', '=', 'wallets.discount_id') | |||||
->join('users', 'users.id', '=', 'wallets.user_id') | |||||
->where('discount', '>', 0) | |||||
->whereNull('users.deleted_at') | |||||
->groupBy('discounts.discount') | ->groupBy('discounts.discount') | ||||
->get() | |||||
->pluck('cnt', 'discount') | ->pluck('cnt', 'discount') | ||||
->all(); | ->all(); | ||||
$labels = array_keys($discounts); | $labels = array_keys($discounts); | ||||
$discounts = array_values($discounts); | $discounts = array_values($discounts); | ||||
// $labels = [10, 25, 30, 100]; | // $labels = [10, 25, 30, 100]; | ||||
// $discounts = [100, 120, 30, 50]; | // $discounts = [100, 120, 30, 50]; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | protected function chartIncome(): array | ||||
$start->subWeeks(1); | $start->subWeeks(1); | ||||
$weeks--; | $weeks--; | ||||
} | } | ||||
$labels = array_reverse($labels); | $labels = array_reverse($labels); | ||||
$start->startOfWeek(Carbon::MONDAY); | $start->startOfWeek(Carbon::MONDAY); | ||||
$payments = DB::table('payments') | $payments = DB::table('payments') | ||||
->selectRaw("concat(year(updated_at), '-', week(updated_at, 3)) as period, sum(amount) as amount") | ->selectRaw("date_format(updated_at, '%Y-%v') as period, sum(amount) as amount") | ||||
->where('updated_at', '>=', $start->toDateString()) | ->where('updated_at', '>=', $start->toDateString()) | ||||
->where('status', PaymentProvider::STATUS_PAID) | ->where('status', PaymentProvider::STATUS_PAID) | ||||
->whereIn('type', [PaymentProvider::TYPE_ONEOFF, PaymentProvider::TYPE_RECURRING]) | ->whereIn('type', [PaymentProvider::TYPE_ONEOFF, PaymentProvider::TYPE_RECURRING]) | ||||
->groupByRaw('1') | ->groupByRaw('1') | ||||
->get() | |||||
->pluck('amount', 'period') | ->pluck('amount', 'period') | ||||
->map(function ($amount) { | ->map(function ($amount) { | ||||
return $amount / 100; | return $amount / 100; | ||||
}); | }); | ||||
// TODO: exclude refunds/chargebacks | // TODO: exclude refunds/chargebacks | ||||
$empty = array_fill_keys($labels, 0); | $empty = array_fill_keys($labels, 0); | ||||
$payments = array_values(array_merge($empty, $payments->all())); | $payments = array_values(array_merge($empty, $payments->all())); | ||||
// $payments = [1000, 1200.25, 3000, 1897.50, 2000, 1900, 2134, 3330]; | // $payments = [1000, 1200.25, 3000, 1897.50, 2000, 1900, 2134, 3330]; | ||||
$avg = collect($payments)->avg(); | |||||
// See https://frappe.io/charts/docs for format/options description | // See https://frappe.io/charts/docs for format/options description | ||||
return [ | return [ | ||||
'title' => 'Income in CHF - last 8 weeks', | 'title' => 'Income in CHF - last 8 weeks', | ||||
'type' => 'bar', | 'type' => 'bar', | ||||
'colors' => [self::COLOR_BLUE], | 'colors' => [self::COLOR_BLUE], | ||||
'axisOptions' => [ | 'axisOptions' => [ | ||||
'xIsSeries' => true, | 'xIsSeries' => true, | ||||
], | ], | ||||
'data' => [ | 'data' => [ | ||||
'labels' => $labels, | 'labels' => $labels, | ||||
'datasets' => [ | 'datasets' => [ | ||||
[ | [ | ||||
// 'name' => 'Payments', | // 'name' => 'Payments', | ||||
'values' => $payments | 'values' => $payments | ||||
] | ] | ||||
], | ], | ||||
'yMarkers' => [ | 'yMarkers' => [ | ||||
[ | [ | ||||
'label' => 'average', | 'label' => sprintf('average = %.2f', $avg), | ||||
'value' => collect($payments)->avg(), | 'value' => $avg, | ||||
'options' => [ 'labelPos' => 'left' ] // default: 'right' | 'options' => [ 'labelPos' => 'left' ] // default: 'right' | ||||
] | ] | ||||
] | ] | ||||
] | ] | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
Show All 10 Lines | protected function chartUsers(): array | ||||
$start->subWeeks(1); | $start->subWeeks(1); | ||||
$weeks--; | $weeks--; | ||||
} | } | ||||
$labels = array_reverse($labels); | $labels = array_reverse($labels); | ||||
$start->startOfWeek(Carbon::MONDAY); | $start->startOfWeek(Carbon::MONDAY); | ||||
$created = DB::table('users') | $created = DB::table('users') | ||||
->selectRaw("concat(year(created_at), '-', week(created_at, 3)) as period, count(*) as cnt") | ->selectRaw("date_format(created_at, '%Y-%v') as period, count(*) as cnt") | ||||
->where('created_at', '>=', $start->toDateString()) | ->where('created_at', '>=', $start->toDateString()) | ||||
->groupByRaw('1') | ->groupByRaw('1') | ||||
->get(); | ->get(); | ||||
$deleted = DB::table('users') | $deleted = DB::table('users') | ||||
->selectRaw("concat(year(deleted_at), '-', week(deleted_at, 3)) as period, count(*) as cnt") | ->selectRaw("date_format(deleted_at, '%Y-%v') as period, count(*) as cnt") | ||||
->where('deleted_at', '>=', $start->toDateString()) | ->where('deleted_at', '>=', $start->toDateString()) | ||||
->groupByRaw('1') | ->groupByRaw('1') | ||||
->get(); | ->get(); | ||||
$empty = array_fill_keys($labels, 0); | $empty = array_fill_keys($labels, 0); | ||||
$created = array_values(array_merge($empty, $created->pluck('cnt', 'period')->all())); | $created = array_values(array_merge($empty, $created->pluck('cnt', 'period')->all())); | ||||
$deleted = array_values(array_merge($empty, $deleted->pluck('cnt', 'period')->all())); | $deleted = array_values(array_merge($empty, $deleted->pluck('cnt', 'period')->all())); | ||||
// $created = [5, 2, 4, 2, 0, 5, 2, 4]; | // $created = [5, 2, 4, 2, 0, 5, 2, 4]; | ||||
// $deleted = [1, 2, 3, 1, 2, 1, 2, 3]; | // $deleted = [1, 2, 3, 1, 2, 1, 2, 3]; | ||||
$avg = collect($created)->avg(); | |||||
// See https://frappe.io/charts/docs for format/options description | // See https://frappe.io/charts/docs for format/options description | ||||
return [ | return [ | ||||
'title' => 'Users - last 8 weeks', | 'title' => 'Users - last 8 weeks', | ||||
// 'type' => 'axis-mixed', | // 'type' => 'axis-mixed', | ||||
'colors' => [self::COLOR_GREEN, self::COLOR_RED], | 'colors' => [self::COLOR_GREEN, self::COLOR_RED], | ||||
'axisOptions' => [ | 'axisOptions' => [ | ||||
'xIsSeries' => true, | 'xIsSeries' => true, | ||||
Show All 9 Lines | protected function chartUsers(): array | ||||
[ | [ | ||||
'name' => 'Deleted', | 'name' => 'Deleted', | ||||
'chartType' => 'line', | 'chartType' => 'line', | ||||
'values' => $deleted | 'values' => $deleted | ||||
] | ] | ||||
], | ], | ||||
'yMarkers' => [ | 'yMarkers' => [ | ||||
[ | [ | ||||
'label' => 'average', | 'label' => sprintf('average = %.1f', $avg), | ||||
'value' => collect($created)->avg(), | 'value' => collect($created)->avg(), | ||||
'options' => [ 'labelPos' => 'left' ] // default: 'right' | 'options' => [ 'labelPos' => 'left' ] // default: 'right' | ||||
] | ] | ||||
] | ] | ||||
] | ] | ||||
]; | ]; | ||||
} | } | ||||
Show All 11 Lines | protected function chartUsersAll(): array | ||||
$start->subWeeks(1); | $start->subWeeks(1); | ||||
$weeks--; | $weeks--; | ||||
} | } | ||||
$labels = array_reverse($labels); | $labels = array_reverse($labels); | ||||
$start->startOfWeek(Carbon::MONDAY); | $start->startOfWeek(Carbon::MONDAY); | ||||
$created = DB::table('users') | $created = DB::table('users') | ||||
->selectRaw("concat(year(created_at), '-', week(created_at, 3)) as period, count(*) as cnt") | ->selectRaw("date_format(created_at, '%Y-%v') as period, count(*) as cnt") | ||||
->where('created_at', '>=', $start->toDateString()) | ->where('created_at', '>=', $start->toDateString()) | ||||
->groupByRaw('1') | ->groupByRaw('1') | ||||
->get(); | ->get(); | ||||
$deleted = DB::table('users') | $deleted = DB::table('users') | ||||
->selectRaw("concat(year(deleted_at), '-', week(deleted_at, 3)) as period, count(*) as cnt") | ->selectRaw("concat(year(deleted_at), '-', week(deleted_at, 3)) as period, count(*) as cnt") | ||||
->where('deleted_at', '>=', $start->toDateString()) | ->where('deleted_at', '>=', $start->toDateString()) | ||||
->groupByRaw('1') | ->groupByRaw('1') | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |