Skip to content

Commit e62e7cc

Browse files
committed
func(Analytics): Add cache.
1 parent c8b9240 commit e62e7cc

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

app/Http/Controllers/AnalyticsController.php

+37-33
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,52 @@ class AnalyticsController extends Controller
1414
public function index()
1515
{
1616

17-
$roundsInThePastYear = Round::where('created_at', '>=', now()->subYear())
18-
->where('name', 'not like', '% test%')
19-
->where('name', 'not like', '%test %')
20-
->get(['id', 'uuid', 'name']);
17+
$roundsInThePastYear = cache()->remember('rounds_in_the_past_year', 86400, function () {
18+
return Round::where('created_at', '>=', now()->subYear())
19+
->where('name', 'not like', '% test%')
20+
->where('name', 'not like', '%test %')
21+
->get(['id', 'uuid', 'name']);
22+
});
2123

22-
$stats = [
23-
'rounds' => 0,
24-
'applications' => 0,
25-
'roundsEvaluatedByHumans' => 0,
26-
'roundApplicationsEvaluatedByHumans' => 0,
27-
'roundsEvaluatedByAI' => 0,
28-
'roundApplicationsEvaluatedByAI' => 0,
29-
];
24+
$stats = cache()->remember('analytics_stats', 86400, function () use ($roundsInThePastYear) {
25+
$stats = [
26+
'rounds' => 0,
27+
'applications' => 0,
28+
'roundsEvaluatedByHumans' => 0,
29+
'roundApplicationsEvaluatedByHumans' => 0,
30+
'roundsEvaluatedByAI' => 0,
31+
'roundApplicationsEvaluatedByAI' => 0,
32+
];
3033

31-
foreach ($roundsInThePastYear as $round) {
32-
$applicationsCount = RoundApplication::where('round_id', $round->id)->count();
33-
$stats['rounds']++;
34-
$stats['applications'] += $applicationsCount;
34+
foreach ($roundsInThePastYear as $round) {
35+
$applicationsCount = RoundApplication::where('round_id', $round->id)->count();
36+
$stats['rounds']++;
37+
$stats['applications'] += $applicationsCount;
3538

36-
$roundApplicationEvaluationAnswers = RoundApplicationEvaluationAnswers::where('round_id', $round->id)
37-
->count();
39+
$roundApplicationEvaluationAnswers = RoundApplicationEvaluationAnswers::where('round_id', $round->id)
40+
->count();
3841

39-
if ($roundApplicationEvaluationAnswers > 0) {
40-
$stats['roundsEvaluatedByHumans']++;
41-
}
42+
if ($roundApplicationEvaluationAnswers > 0) {
43+
$stats['roundsEvaluatedByHumans']++;
44+
}
4245

43-
$stats['roundApplicationsEvaluatedByHumans'] += $roundApplicationEvaluationAnswers;
46+
$stats['roundApplicationsEvaluatedByHumans'] += $roundApplicationEvaluationAnswers;
4447

48+
$roundApplicationPromptResults = RoundApplicationPromptResult::select('application_id')
49+
->where('round_id', $round->id)
50+
->groupBy('application_id')
51+
->havingRaw('COUNT(application_id) = 1')
52+
->get()
53+
->count();
54+
if ($roundApplicationPromptResults > 0) {
55+
$stats['roundsEvaluatedByAI']++;
56+
}
4557

46-
$roundApplicationPromptResults = RoundApplicationPromptResult::select('application_id')
47-
->where('round_id', $round->id)
48-
->groupBy('application_id')
49-
->havingRaw('COUNT(application_id) = 1')
50-
->get()
51-
->count();
52-
if ($roundApplicationPromptResults > 0) {
53-
$stats['roundsEvaluatedByAI']++;
58+
$stats['roundApplicationsEvaluatedByAI'] += $roundApplicationPromptResults;
5459
}
5560

56-
$stats['roundApplicationsEvaluatedByAI'] += $roundApplicationPromptResults;
57-
}
58-
61+
return $stats;
62+
});
5963

6064

6165
return Inertia::render('Analytics/Index', [

0 commit comments

Comments
 (0)