-
-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathSeedDevDashboardDataCommand.php
More file actions
262 lines (224 loc) · 10.6 KB
/
Copy pathSeedDevDashboardDataCommand.php
File metadata and controls
262 lines (224 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace HiEvents\Console\Commands;
use Carbon\CarbonImmutable;
use HiEvents\Helper\IdHelper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SeedDevDashboardDataCommand extends Command
{
protected $signature = 'dev:seed-dashboard
{eventId : Event ID to seed dummy historical data for}
{--days=60 : Days of historical data to generate}
{--force : Skip the non-production environment check}';
protected $description = 'Seed dummy historical orders and daily statistics so the dashboard renders with realistic data. Idempotent — re-running replaces previously seeded rows for the event.';
private const SEED_NOTE = '[dev-seed]';
private const FIRST_NAMES = [
'Sarah', 'Liam', 'Aoife', 'Noah', 'Mia', 'Oisin', 'Emma', 'Conor',
'Sophie', 'Sean', 'Ella', 'Cian', 'Ava', 'Patrick', 'Saoirse', 'Diarmuid',
'Niamh', 'Eoin', 'Caoimhe', 'Ciaran',
];
private const LAST_NAMES = [
'O\'Connor', 'Murphy', 'Kelly', 'Byrne', 'Walsh', 'O\'Brien', 'Ryan',
'O\'Sullivan', 'Doyle', 'Kennedy', 'Lynch', 'Quinn', 'McCarthy', 'Brady',
'Reilly',
];
public function handle(): int
{
if (app()->environment('production') && !$this->option('force')) {
$this->error('Refusing to run in production. Pass --force to override.');
return self::FAILURE;
}
$eventId = (int)$this->argument('eventId');
$days = (int)$this->option('days');
$event = DB::table('events')->where('id', $eventId)->first();
if ($event === null) {
$this->error("Event {$eventId} not found.");
return self::FAILURE;
}
$occurrence = DB::table('event_occurrences')
->where('event_id', $eventId)
->whereNull('deleted_at')
->first();
if ($occurrence === null) {
$this->error("Event {$eventId} has no event_occurrence rows. Cannot seed daily statistics.");
return self::FAILURE;
}
$this->info("Seeding {$days} days of dummy data for event {$eventId} ({$event->title}, {$event->currency})");
DB::transaction(function () use ($eventId, $days, $event, $occurrence) {
$this->cleanup($eventId);
$today = CarbonImmutable::today();
$aggregateGross = 0.0;
$aggregateTax = 0.0;
$aggregateFee = 0.0;
$aggregateRefunded = 0.0;
$aggregateOrders = 0;
$aggregateProducts = 0;
$aggregateAttendees = 0;
$aggregateViews = 0;
$aggregateCancelled = 0;
$bar = $this->output->createProgressBar($days);
$bar->start();
for ($i = $days - 1; $i >= 0; $i--) {
$date = $today->subDays($i);
$isWeekend = in_array($date->dayOfWeek, [0, 6], true);
$orderCount = $this->randomOrderCount($i, $isWeekend);
$dayProducts = 0;
$dayAttendees = 0;
$dayGross = 0.0;
$dayTax = 0.0;
$dayFee = 0.0;
$dayRefunded = 0.0;
$dayOrdersCreated = 0;
$dayOrdersCancelled = 0;
$dayViews = random_int(20, 180) + ($isWeekend ? 50 : 0);
for ($n = 0; $n < $orderCount; $n++) {
$items = random_int(1, 4);
$unitPrice = $this->randomChoice([15.00, 25.00, 35.00, 45.00, 75.00]);
$beforeAdditions = round($unitPrice * $items, 2);
$tax = round($beforeAdditions * 0.135, 2);
$fee = round($beforeAdditions * 0.025, 2);
$gross = round($beforeAdditions + $tax + $fee, 2);
$isCancelled = random_int(1, 100) <= 8;
$refundedAmount = 0.0;
if (!$isCancelled && random_int(1, 100) <= 6) {
$refundedAmount = $gross;
}
$createdAt = $date->setTime(random_int(8, 22), random_int(0, 59), random_int(0, 59));
$status = $isCancelled ? 'CANCELLED' : 'COMPLETED';
$paymentStatus = $isCancelled ? null : 'PAYMENT_RECEIVED';
$refundStatus = $refundedAmount > 0 ? 'REFUNDED' : null;
DB::table('orders')->insert([
'short_id' => IdHelper::shortId(IdHelper::ORDER_PREFIX),
'public_id' => IdHelper::publicId(IdHelper::ORDER_PREFIX),
'event_id' => $eventId,
'currency' => $event->currency,
'first_name' => $this->randomChoice(self::FIRST_NAMES),
'last_name' => $this->randomChoice(self::LAST_NAMES),
'email' => 'seed' . random_int(1000, 9999) . '@example.com',
'status' => $status,
'payment_status' => $paymentStatus,
'refund_status' => $refundStatus,
'total_before_additions' => $beforeAdditions,
'total_gross' => $gross,
'total_tax' => $tax,
'total_fee' => $fee,
'total_refunded' => $refundedAmount,
'is_manually_created' => false,
'notes' => self::SEED_NOTE,
'locale' => 'en',
'payment_provider' => 'STRIPE',
'created_at' => $createdAt,
'updated_at' => $createdAt,
]);
if ($isCancelled) {
$dayOrdersCancelled++;
continue;
}
$dayOrdersCreated++;
$dayProducts += $items;
$dayAttendees += $items;
$dayGross += $gross;
$dayTax += $tax;
$dayFee += $fee;
$dayRefunded += $refundedAmount;
}
DB::table('event_occurrence_daily_statistics')->upsert(
[
[
'event_id' => $eventId,
'event_occurrence_id' => $occurrence->id,
'date' => $date->toDateString(),
'products_sold' => $dayProducts,
'attendees_registered' => $dayAttendees,
'sales_total_gross' => $dayGross,
'sales_total_before_additions' => round($dayGross - $dayTax - $dayFee, 2),
'total_tax' => $dayTax,
'total_fee' => $dayFee,
'orders_created' => $dayOrdersCreated,
'orders_cancelled' => $dayOrdersCancelled,
'total_refunded' => $dayRefunded,
'version' => 0,
'created_at' => $date,
'updated_at' => $date,
],
],
['event_occurrence_id', 'date'],
[
'products_sold', 'attendees_registered',
'sales_total_gross', 'sales_total_before_additions',
'total_tax', 'total_fee',
'orders_created', 'orders_cancelled', 'total_refunded',
'updated_at',
],
);
$aggregateGross += $dayGross;
$aggregateTax += $dayTax;
$aggregateFee += $dayFee;
$aggregateRefunded += $dayRefunded;
$aggregateOrders += $dayOrdersCreated;
$aggregateProducts += $dayProducts;
$aggregateAttendees += $dayAttendees;
$aggregateViews += $dayViews;
$aggregateCancelled += $dayOrdersCancelled;
$bar->advance();
}
$bar->finish();
$this->newLine();
DB::table('event_statistics')
->where('event_id', $eventId)
->update([
'sales_total_gross' => $aggregateGross,
'sales_total_before_additions' => round($aggregateGross - $aggregateTax - $aggregateFee, 2),
'total_tax' => $aggregateTax,
'total_fee' => $aggregateFee,
'total_refunded' => $aggregateRefunded,
'orders_created' => $aggregateOrders,
'orders_cancelled' => $aggregateCancelled,
'products_sold' => $aggregateProducts,
'attendees_registered' => $aggregateAttendees,
'total_views' => $aggregateViews,
'unique_views' => (int)round($aggregateViews * 0.65),
'updated_at' => now(),
]);
$this->table(
['Metric', 'Total over period'],
[
['Orders (completed)', $aggregateOrders],
['Orders (cancelled)', $aggregateCancelled],
['Products sold', $aggregateProducts],
['Attendees', $aggregateAttendees],
['Gross sales', number_format($aggregateGross, 2) . ' ' . $event->currency],
['Tax', number_format($aggregateTax, 2) . ' ' . $event->currency],
['Fees', number_format($aggregateFee, 2) . ' ' . $event->currency],
['Refunded', number_format($aggregateRefunded, 2) . ' ' . $event->currency],
['Page views', $aggregateViews],
],
);
});
$this->info('Done.');
return self::SUCCESS;
}
private function cleanup(int $eventId): void
{
$deletedOrders = DB::table('orders')
->where('event_id', $eventId)
->where('notes', self::SEED_NOTE)
->delete();
$this->line("Cleaned up {$deletedOrders} prior seed orders. Daily statistics will be upserted for the seeded window.");
}
private function randomOrderCount(int $daysAgo, bool $isWeekend): int
{
$base = $isWeekend ? random_int(4, 12) : random_int(1, 7);
if ($daysAgo > 30) {
$base = (int)round($base * 0.6);
}
if (random_int(1, 100) <= 4) {
$base += random_int(8, 20);
}
return max(0, $base);
}
private function randomChoice(array $items)
{
return $items[array_rand($items)];
}
}