Skip to content

Commit 8ecd8da

Browse files
committed
consilidate data
1 parent 6e7abac commit 8ecd8da

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

app/Filament/Widgets/VoteHistoryChart.php

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,45 @@ protected function getData(): array
4848

4949
$query = $this->item->votes();
5050

51-
if ($this->filter && $this->filter !== 'all') {
52-
$days = (int) $this->filter;
51+
$days = $this->filter && $this->filter !== 'all' ? (int) $this->filter : null;
52+
53+
if ($days) {
5354
$query->where('created_at', '>=', Carbon::now()->subDays($days));
5455
}
5556

57+
// Determine grouping based on time range
58+
$groupBy = $this->getGroupingStrategy($days);
59+
60+
if ($groupBy === 'month') {
61+
return $this->getMonthlyData($query);
62+
} elseif ($groupBy === 'week') {
63+
return $this->getWeeklyData($query);
64+
}
65+
66+
return $this->getDailyData($query);
67+
}
68+
69+
protected function getGroupingStrategy(?int $days): string
70+
{
71+
if ($days === null || $days > 365) {
72+
return 'month';
73+
} elseif ($days > 90) {
74+
return 'week';
75+
}
76+
77+
return 'day';
78+
}
79+
80+
protected function getDailyData($query): array
81+
{
5682
$votes = $query
5783
->selectRaw('DATE(created_at) as date, COUNT(*) as count')
5884
->groupByRaw('DATE(created_at)')
5985
->orderBy('date')
6086
->get();
6187

6288
if ($votes->isEmpty()) {
63-
return [
64-
'datasets' => [],
65-
'labels' => [],
66-
];
89+
return ['datasets' => [], 'labels' => []];
6790
}
6891

6992
$startDate = Carbon::parse($votes->first()->date);
@@ -76,11 +99,58 @@ protected function getData(): array
7699
$currentDate = $startDate->copy();
77100
while ($currentDate->lte($endDate)) {
78101
$dateKey = $currentDate->format('Y-m-d');
79-
$labels[] = $currentDate->format('M d, Y');
102+
$labels[] = $currentDate->format('M d');
80103
$data[] = $votesMap[$dateKey] ?? 0;
81104
$currentDate->addDay();
82105
}
83106

107+
return $this->formatChartData($labels, $data);
108+
}
109+
110+
protected function getWeeklyData($query): array
111+
{
112+
$votes = $query->orderBy('created_at')->get();
113+
114+
if ($votes->isEmpty()) {
115+
return ['datasets' => [], 'labels' => []];
116+
}
117+
118+
$grouped = $votes->groupBy(fn ($vote) => Carbon::parse($vote->created_at)->startOfWeek()->format('Y-m-d'));
119+
120+
$labels = [];
121+
$data = [];
122+
123+
foreach ($grouped as $weekStart => $weekVotes) {
124+
$labels[] = Carbon::parse($weekStart)->format('M d');
125+
$data[] = $weekVotes->count();
126+
}
127+
128+
return $this->formatChartData($labels, $data);
129+
}
130+
131+
protected function getMonthlyData($query): array
132+
{
133+
$votes = $query->orderBy('created_at')->get();
134+
135+
if ($votes->isEmpty()) {
136+
return ['datasets' => [], 'labels' => []];
137+
}
138+
139+
$grouped = $votes->groupBy(fn ($vote) => Carbon::parse($vote->created_at)->format('Y-m'));
140+
141+
$labels = [];
142+
$data = [];
143+
144+
foreach ($grouped as $month => $monthVotes) {
145+
$labels[] = Carbon::createFromFormat('Y-m', $month)->format('M Y');
146+
$data[] = $monthVotes->count();
147+
}
148+
149+
return $this->formatChartData($labels, $data);
150+
}
151+
152+
protected function formatChartData(array $labels, array $data): array
153+
{
84154
return [
85155
'datasets' => [
86156
[

tests/Feature/Livewire/Item/VoteHistoryTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
]);
3939

4040
Livewire::test(VoteHistoryChart::class, ['item' => $item])
41+
->set('filter', '30')
4142
->assertStatus(200);
4243
});
4344

@@ -55,7 +56,8 @@
5556
$vote->subscribed = false;
5657
$vote->save();
5758

58-
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item]);
59+
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item])
60+
->set('filter', '30');
5961

6062
$component->assertStatus(200);
6163
});
@@ -86,7 +88,8 @@
8688
$vote2->created_at = $today;
8789
$vote2->save();
8890

89-
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item]);
91+
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item])
92+
->set('filter', '30');
9093

9194
$component->assertStatus(200);
9295
});
@@ -115,7 +118,8 @@
115118
$vote2->created_at = Carbon::today();
116119
$vote2->save();
117120

118-
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item]);
121+
$component = Livewire::test(VoteHistoryChart::class, ['item' => $item])
122+
->set('filter', '30');
119123

120124
$component->assertStatus(200);
121125
});

0 commit comments

Comments
 (0)