mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Improve dashboard rendering speed by using ajax for graphs. Improve perfs by using more index on query. Add function to find invalid numbers and export as csv
This commit is contained in:
parent
52c849e043
commit
2be8242d5e
16 changed files with 494 additions and 56 deletions
|
@ -1189,4 +1189,68 @@ namespace controllers\publics;
|
|||
|
||||
return $this->json($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return statistics about invalid numbers
|
||||
*
|
||||
* @param int $page : Pagination number, Default = 0. Group of 25 results.
|
||||
* @param int $_GET['volume'] : Minimum number of SMS sent to the number
|
||||
* @param int $_GET['percent_failed'] : Minimum percentage of failed SMS to the number
|
||||
* @param int $_GET['percent_unknown'] : Minimum percentage of unknown SMS to the number
|
||||
*
|
||||
* @return : List of entries
|
||||
*/
|
||||
public function get_invalid_numbers($page = 0)
|
||||
{
|
||||
$page = (int) $page;
|
||||
$limit = 25;
|
||||
$volume = $_GET['volume'] ?? false;
|
||||
$percent_failed = $_GET['percent_failed'] ?? false;
|
||||
$percent_unknown = $_GET['percent_unknown'] ?? false;
|
||||
|
||||
if ($volume === false || $percent_failed === false || $percent_unknown === false)
|
||||
{
|
||||
$return = self::DEFAULT_RETURN;
|
||||
$return['error'] = self::ERROR_CODES['MISSING_PARAMETER'];
|
||||
$return['message'] = self::ERROR_MESSAGES['MISSING_PARAMETER'] . 'volume, percent_failed and percent_unknown are required.';
|
||||
$this->auto_http_code(false);
|
||||
|
||||
return $this->json($return);
|
||||
}
|
||||
|
||||
$volume = (int) $volume;
|
||||
$percent_failed = ((float) $percent_failed) / 100;
|
||||
$percent_unknown = ((float) $percent_unknown) / 100;
|
||||
|
||||
$return = self::DEFAULT_RETURN;
|
||||
|
||||
$invalid_numbers = $this->internal_sended->get_invalid_numbers($this->user['id'], $volume, $percent_failed, $percent_unknown, $limit, $page);
|
||||
|
||||
$return = self::DEFAULT_RETURN;
|
||||
|
||||
if (\count($invalid_numbers) === $limit)
|
||||
{
|
||||
$return['next'] = \descartes\Router::url('Api', __FUNCTION__, ['page' => $page + 1], [
|
||||
'api_key' => $this->user['api_key'],
|
||||
'volume' => $volume,
|
||||
'percent_failed' => $percent_failed * 100,
|
||||
'percent_unknown' => $percent_unknown * 100
|
||||
]);
|
||||
}
|
||||
|
||||
if ($page > 0)
|
||||
{
|
||||
$return['prev'] = \descartes\Router::url('Api', __FUNCTION__, ['page' => $page - 1], [
|
||||
'api_key' => $this->user['api_key'],
|
||||
'volume' => $volume,
|
||||
'percent_failed' => $percent_failed * 100,
|
||||
'percent_unknown' => $percent_unknown * 100
|
||||
]);
|
||||
}
|
||||
|
||||
$return['response'] = $invalid_numbers;
|
||||
$this->auto_http_code(true);
|
||||
|
||||
return $this->json($return, false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,16 +87,50 @@ namespace controllers\publics;
|
|||
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
|
||||
}
|
||||
|
||||
$this->render('dashboard/show', [
|
||||
'nb_contacts' => $nb_contacts,
|
||||
'nb_groups' => $nb_groups,
|
||||
'nb_scheduleds' => $nb_scheduleds,
|
||||
'nb_sendeds' => $nb_sendeds,
|
||||
'nb_receiveds' => $nb_receiveds,
|
||||
'nb_unreads' => $nb_unreads,
|
||||
'quota_unused' => $quota_unused,
|
||||
'sendeds' => $sendeds,
|
||||
'receiveds' => $receiveds,
|
||||
'events' => $events,
|
||||
'stats_start_date_formated' => $stats_start_date_formated,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return stats about sended sms
|
||||
*/
|
||||
public function stats_sended()
|
||||
{
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
|
||||
//Création de la date d'il y a 30 jours
|
||||
$now = new \DateTime();
|
||||
$one_month = new \DateInterval('P1M');
|
||||
$stats_start_date = clone $now;
|
||||
$stats_start_date->sub($one_month);
|
||||
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
|
||||
|
||||
//If user have a quota and the quota start before today, use quota start date instead
|
||||
$quota = $this->internal_quota->get_user_quota($id_user);
|
||||
if ($quota && (new \DateTime($quota['start_date']) <= $now) && (new \DateTime($quota['expiration_date']) > $now))
|
||||
{
|
||||
$stats_start_date = new \DateTime($quota['start_date']);
|
||||
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
|
||||
}
|
||||
|
||||
$nb_sendeds_by_day = $this->internal_sended->count_by_day_and_status_since_for_user($id_user, $stats_start_date_formated);
|
||||
$nb_receiveds_by_day = $this->internal_received->count_by_day_since_for_user($id_user, $stats_start_date_formated);
|
||||
|
||||
//On va traduire ces données pour les afficher en graphique
|
||||
$array_bar_chart_sended = [];
|
||||
$array_bar_chart_received = [];
|
||||
|
||||
$date = clone $stats_start_date;
|
||||
$one_day = new \DateInterval('P1D');
|
||||
$i = 0;
|
||||
|
||||
//On va construire un tableau avec la date en clef, et les données pour chaque date
|
||||
while ($date <= $now)
|
||||
|
@ -109,15 +143,13 @@ namespace controllers\publics;
|
|||
'sendeds_delivered' => 0,
|
||||
];
|
||||
|
||||
$array_bar_chart_received[$date_f] = ['period' => $date_f, 'receiveds' => 0];
|
||||
|
||||
$date->add($one_day);
|
||||
}
|
||||
|
||||
$total_sendeds = 0;
|
||||
$total_receiveds = 0;
|
||||
|
||||
//0n remplie le tableau avec les données adaptées
|
||||
//On remplie le tableau avec les données adaptées
|
||||
foreach ($nb_sendeds_by_day as $nb_sended)
|
||||
{
|
||||
$array_bar_chart_sended[$nb_sended['at_ymd']]['sendeds_' . $nb_sended['status']] = $nb_sended['nb'];
|
||||
|
@ -125,6 +157,59 @@ namespace controllers\publics;
|
|||
$total_sendeds += $nb_sended['nb'];
|
||||
}
|
||||
|
||||
$nb_days = $stats_start_date->diff($now)->days + 1;
|
||||
$avg_sendeds = round($total_sendeds / $nb_days, 2);
|
||||
|
||||
$array_bar_chart_sended = array_values($array_bar_chart_sended);
|
||||
|
||||
header('content-type:application/json');
|
||||
echo json_encode([
|
||||
'data_bar_chart_sended' => $array_bar_chart_sended,
|
||||
'avg_sendeds' => $avg_sendeds,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return stats about received sms
|
||||
*/
|
||||
public function stats_received()
|
||||
{
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
|
||||
//Création de la date d'il y a 30 jours
|
||||
$now = new \DateTime();
|
||||
$one_month = new \DateInterval('P1M');
|
||||
$stats_start_date = clone $now;
|
||||
$stats_start_date->sub($one_month);
|
||||
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
|
||||
|
||||
$quota = $this->internal_quota->get_user_quota($id_user);
|
||||
if ($quota && (new \DateTime($quota['start_date']) <= $now) && (new \DateTime($quota['expiration_date']) > $now))
|
||||
{
|
||||
$stats_start_date = new \DateTime($quota['start_date']);
|
||||
$stats_start_date_formated = $stats_start_date->format('Y-m-d');
|
||||
}
|
||||
|
||||
$nb_receiveds_by_day = $this->internal_received->count_by_day_since_for_user($id_user, $stats_start_date_formated);
|
||||
|
||||
//On va traduire ces données pour les afficher en graphique
|
||||
$array_bar_chart_received = [];
|
||||
|
||||
$date = clone $stats_start_date;
|
||||
$one_day = new \DateInterval('P1D');
|
||||
|
||||
//On va construire un tableau avec la date en clef, et les données pour chaque date
|
||||
while ($date <= $now)
|
||||
{
|
||||
$date_f = $date->format('Y-m-d');
|
||||
$array_bar_chart_received[$date_f] = ['period' => $date_f, 'receiveds' => 0];
|
||||
|
||||
$date->add($one_day);
|
||||
}
|
||||
|
||||
$total_receiveds = 0;
|
||||
|
||||
foreach ($nb_receiveds_by_day as $date => $nb_received)
|
||||
{
|
||||
$array_bar_chart_received[$date]['receiveds'] = $nb_received;
|
||||
|
@ -132,28 +217,15 @@ namespace controllers\publics;
|
|||
}
|
||||
|
||||
$nb_days = $stats_start_date->diff($now)->days + 1;
|
||||
$avg_sendeds = round($total_sendeds / $nb_days, 2);
|
||||
$avg_receiveds = round($total_receiveds / $nb_days, 2);
|
||||
|
||||
$array_bar_chart_sended = array_values($array_bar_chart_sended);
|
||||
$array_bar_chart_received = array_values($array_bar_chart_received);
|
||||
|
||||
$this->render('dashboard/show', [
|
||||
'nb_contacts' => $nb_contacts,
|
||||
'nb_groups' => $nb_groups,
|
||||
'nb_scheduleds' => $nb_scheduleds,
|
||||
'nb_sendeds' => $nb_sendeds,
|
||||
'nb_receiveds' => $nb_receiveds,
|
||||
'nb_unreads' => $nb_unreads,
|
||||
'avg_sendeds' => $avg_sendeds,
|
||||
header('content-type:application/json');
|
||||
echo json_encode([
|
||||
'data_bar_chart_received' => $array_bar_chart_received,
|
||||
'avg_receiveds' => $avg_receiveds,
|
||||
'quota_unused' => $quota_unused,
|
||||
'sendeds' => $sendeds,
|
||||
'receiveds' => $receiveds,
|
||||
'events' => $events,
|
||||
'data_bar_chart_sended' => json_encode($array_bar_chart_sended),
|
||||
'data_bar_chart_received' => json_encode($array_bar_chart_received),
|
||||
'stats_start_date_formated' => $stats_start_date_formated,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ namespace controllers\publics;
|
|||
*/
|
||||
public function list_json()
|
||||
{
|
||||
$entities = $this->internal_received->get_discussions_for_user($_SESSION['user']['id']);
|
||||
$entities = $this->internal_received->get_discussions_for_user($_SESSION['user']['id'], 1000);
|
||||
|
||||
foreach ($entities as &$entity)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue