Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/Admin/StatsController.php
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | class StatsController extends \App\Http\Controllers\Controller | ||||
protected function chartIncome(): array | protected function chartIncome(): array | ||||
{ | { | ||||
$weeks = 8; | $weeks = 8; | ||||
$start = Carbon::now(); | $start = Carbon::now(); | ||||
$labels = []; | $labels = []; | ||||
while ($weeks > 0) { | while ($weeks > 0) { | ||||
$labels[] = $start->format('Y-W'); | $labels[] = $start->format('Y-W'); | ||||
$start->subWeeks(1); | |||||
$weeks--; | $weeks--; | ||||
if ($weeks) { | |||||
$start->subWeeks(1); | |||||
} | |||||
} | } | ||||
$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("date_format(updated_at, '%Y-%v') 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') | ||||
->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(); | $avg = collect($payments)->slice(0, count($labels) - 1)->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' => [ | ||||
Show All 24 Lines | class StatsController extends \App\Http\Controllers\Controller | ||||
protected function chartUsers(): array | protected function chartUsers(): array | ||||
{ | { | ||||
$weeks = 8; | $weeks = 8; | ||||
$start = Carbon::now(); | $start = Carbon::now(); | ||||
$labels = []; | $labels = []; | ||||
while ($weeks > 0) { | while ($weeks > 0) { | ||||
$labels[] = $start->format('Y-W'); | $labels[] = $start->format('Y-W'); | ||||
$start->subWeeks(1); | |||||
$weeks--; | $weeks--; | ||||
if ($weeks) { | |||||
$start->subWeeks(1); | |||||
} | |||||
} | } | ||||
$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("date_format(created_at, '%Y-%v') 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("date_format(deleted_at, '%Y-%v') 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(); | $avg = collect($created)->slice(0, count($labels) - 1)->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' => [ | ||||
Show All 30 Lines | class StatsController extends \App\Http\Controllers\Controller | ||||
protected function chartUsersAll(): array | protected function chartUsersAll(): array | ||||
{ | { | ||||
$weeks = 54; | $weeks = 54; | ||||
$start = Carbon::now(); | $start = Carbon::now(); | ||||
$labels = []; | $labels = []; | ||||
while ($weeks > 0) { | while ($weeks > 0) { | ||||
$labels[] = $start->format('Y-W'); | $labels[] = $start->format('Y-W'); | ||||
$start->subWeeks(1); | |||||
$weeks--; | $weeks--; | ||||
if ($weeks) { | |||||
$start->subWeeks(1); | |||||
} | |||||
} | } | ||||
$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("date_format(created_at, '%Y-%v') 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()) | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |