F
Filament2mo ago
Shaza

performance issues

Hi , my widget in dashboard take more than 40 sec to update the data when I change the filter in chart, can I improve the performance?
6 Replies
Shaza
ShazaOP2mo ago
please can anyone help me ?
CodeWithDennis
CodeWithDennis2mo ago
Show some code so people can help you 😄
Dennis Koch
Dennis Koch2mo ago
Is it the database that takes so long?
Shaza
ShazaOP2mo ago
protected function getData(): array { $organizationId = $this->filters['organization'] ?? null; $dateRange = $this->filters['date_range'] ?? 'today'; $userType = $this->filters['type'] ?? 'visit'; $period = match ($dateRange) { 'today' => 'day', 'week' => 'week', 'month' => 'month', default => 'day', }; if ($userType === 'old') { $url = env('ORGANIZATION_URL') . '/api/user-count'; $response = Http::get($url, [ 'organization_id' => $organizationId, 'period' => $period, ]); $responseData = $response->successful() ? $response->json() : [ 'period' => $period, 'data' => [], ]; $data = $responseData['data'] ?? []; } else { $query = \App\Models\User::query(); if ($organizationId) { $query->where('organization', $organizationId); } switch ($period) { } return [ 'labels' => array_keys($chartData), 'datasets' => [ [ 'label' => ucfirst($userType) . ' users', 'data' => array_values($chartData),
], ], ]; } this is my function
Dennis Koch
Dennis Koch2mo ago
Please format your code properly. See #✅┊rules How long is your Http request taking?!
Shaza
ShazaOP2mo ago
protected function getData(): array
{
$organizationId = $this->filters['organization'] ?? null;
$dateRange = $this->filters['date_range'] ?? 'today';
$userType = $this->filters['type'] ?? 'visit';

$period = match ($dateRange) {
'today' => 'day',
'week' => 'week',
'month' => 'month',
default => 'day',
};

if ($userType === 'old') {
$url = env('ORGANIZATION_URL') . '/api/user-count';

$response = Http::get($url, [
'organization_id' => $organizationId,
'period' => $period,
]);

$responseData = $response->successful() ? $response->json() : [
'period' => $period,
'data' => [],
];

$data = $responseData['data'] ?? [];
}

else {
$query = \App\Models\User::query();

if ($organizationId) {
$query->where('organization', $organizationId);
}

switch ($period) {

}

return [
'labels' => array_keys($chartData),
'datasets' => [
[
'label' => ucfirst($userType) . ' users',
'data' => array_values($chartData),

],
],
];
}
protected function getData(): array
{
$organizationId = $this->filters['organization'] ?? null;
$dateRange = $this->filters['date_range'] ?? 'today';
$userType = $this->filters['type'] ?? 'visit';

$period = match ($dateRange) {
'today' => 'day',
'week' => 'week',
'month' => 'month',
default => 'day',
};

if ($userType === 'old') {
$url = env('ORGANIZATION_URL') . '/api/user-count';

$response = Http::get($url, [
'organization_id' => $organizationId,
'period' => $period,
]);

$responseData = $response->successful() ? $response->json() : [
'period' => $period,
'data' => [],
];

$data = $responseData['data'] ?? [];
}

else {
$query = \App\Models\User::query();

if ($organizationId) {
$query->where('organization', $organizationId);
}

switch ($period) {

}

return [
'labels' => array_keys($chartData),
'datasets' => [
[
'label' => ucfirst($userType) . ' users',
'data' => array_values($chartData),

],
],
];
}
it takes only 1.83 s

Did you find this page helpful?