Add page and api for stats about phone sended sms

This commit is contained in:
osaajani 2024-06-25 21:12:48 +02:00
parent 064d6fd941
commit 4f717ef849
11 changed files with 530 additions and 0 deletions

View file

@ -91,6 +91,10 @@ namespace controllers\publics;
{
$this->user = $this->internal_user->get_by_api_key($api_key);
}
elseif ($_SESSION['user'] ?? false)
{
$this->user = $this->internal_user->get($_SESSION['user']['id']);
}
if (!$this->user)
{
@ -1068,4 +1072,76 @@ namespace controllers\publics;
return $this->json($return);
}
/**
* Return statistics about status of sended sms for a period by phone
*
* @param string $_GET['start'] : Date from which to get sms volume, format Y-m-d H:i:s.
* @param string $_GET['end'] : Date up to which to get sms volume, format Y-m-d H:i:s.
* @param ?int $_GET['id_phone'] : Id of the phone we want to check the status for. Default to null will return stats for all phone.
*
* @return : List of entries
*/
public function get_sms_status_stats()
{
$start = $_GET['start'] ?? null;
$end = $_GET['end'] ?? null;
$id_phone = $_GET['id_phone'] ?? null;
if (!$start || !$end)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['MISSING_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['MISSING_PARAMETER'] . 'start and end date are required.';
$this->auto_http_code(false);
return $this->json($return);
}
$return = self::DEFAULT_RETURN;
if (!\controllers\internals\Tool::is_valid_date($start))
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'start must be a date of format "Y-m-d H:i:s".';
$this->auto_http_code(false);
return $this->json($return);
}
$start = new \DateTime($start);
if (!\controllers\internals\Tool::is_valid_date($end))
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'end must be a date of format "Y-m-d H:i:s".';
$this->auto_http_code(false);
return $this->json($return);
}
$end = new \DateTime($end);
if ($id_phone)
{
$phone = $this->internal_phone->get_for_user($this->user['id'], $id_phone);
if (!$phone)
{
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'phone with id ' . $id_phone . ' does not exists.';
$this->auto_http_code(false);
return $this->json($return);
}
}
$stats = $this->internal_sended->get_sended_status_stats($this->user['id'], $start, $end, $id_phone);
$return = self::DEFAULT_RETURN;
$return['response'] = $stats;
$this->auto_http_code(true);
return $this->json($return);
}
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\publics;
/**
* Statistics pages
*/
class Stat extends \descartes\Controller
{
private $internal_sended;
private $internal_phone;
public function __construct()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_sended = new \controllers\internals\Sended($bdd);
$this->internal_phone = new \controllers\internals\Phone($bdd);
\controllers\internals\Tool::verifyconnect();
}
/**
* Show the stats about sms status for a period by phone
*
* @return void;
*/
public function sms_status()
{
$id_user = $_SESSION['user']['id'];
$phones = $this->internal_phone->gets_for_user($id_user);
$now = new \DateTime();
$seven_days_interval = new \DateInterval('P7D');
$seven_days_ago = clone($now);
$seven_days_ago->sub($seven_days_interval);
$this->render('stat/sms-status', [
'phones' => $phones,
'now' => $now,
'seven_days_ago' => $seven_days_ago,
]);
}
}