mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
Add phone reliability features
This commit is contained in:
parent
44b855dd48
commit
52c849e043
22 changed files with 837 additions and 188 deletions
|
@ -14,10 +14,11 @@ namespace models;
|
|||
class Phone extends StandardModel
|
||||
{
|
||||
|
||||
const STATUS_AVAILABLE = 'available';
|
||||
const STATUS_UNAVAILABLE = 'unavailable';
|
||||
const STATUS_NO_CREDIT = 'no_credit';
|
||||
const STATUS_LIMIT_REACHED = 'limit_reached';
|
||||
const STATUS_AVAILABLE = 'available'; # Everything OK
|
||||
const STATUS_UNAVAILABLE = 'unavailable'; # RaspiSMS cannot communication with the phone
|
||||
const STATUS_DISABLED = 'disabled'; # Phone have been manually or automatically disabled by user/system
|
||||
const STATUS_NO_CREDIT = 'no_credit'; # Phone have no more credit available
|
||||
const STATUS_LIMIT_REACHED = 'limit_reached'; # We reached the limit in of SMS in RaspiSMS for this phone
|
||||
|
||||
/**
|
||||
* Return all phones that belongs to active users
|
||||
|
@ -109,7 +110,6 @@ namespace models;
|
|||
return $this->_delete('phone_limit', ['id_phone' => $id_phone]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
|
|
95
models/PhoneReliabilityHistory.php
Normal file
95
models/PhoneReliabilityHistory.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?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 models;
|
||||
|
||||
class PhoneReliabilityHistory extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Find all unreliable phones for a user, based on sended sms status, rate limit, etc.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $sms_status : Status of SMS to use to calculate rate
|
||||
* @param float $rate_limit : Percentage of SMS matching status after which we consider the phone unreliable
|
||||
* @param int $min_volume : Minimum number of sms we need to have to consider the statistic relevent
|
||||
* @param int $period : The time span in minutes from which SMS counting should begin.
|
||||
* @param int $grace_period : How long in minutes should we wait before including a SMS in counting
|
||||
*
|
||||
* @return array : A list of unreliable phone for the user, with phone id, total number of sms, and rate of failed sms
|
||||
*/
|
||||
public function find_unreliable_phones (int $id_user, string $sms_status, float $rate_limit, int $min_volume, int $period, int $grace_period)
|
||||
{
|
||||
return $this->_run_query("
|
||||
WITH recent_messages AS (
|
||||
SELECT
|
||||
sended.id_phone AS id_phone,
|
||||
COUNT(sended.id) AS total,
|
||||
SUM(sended.status = :sms_status) AS unreliable
|
||||
FROM
|
||||
sended
|
||||
JOIN
|
||||
phone
|
||||
ON
|
||||
sended.id_phone = phone.id
|
||||
LEFT JOIN
|
||||
(
|
||||
SELECT
|
||||
id_phone,
|
||||
MAX(created_at) AS last_alert_time
|
||||
FROM
|
||||
phone_reliability_history
|
||||
WHERE
|
||||
type = :sms_status
|
||||
GROUP BY
|
||||
id_phone
|
||||
) AS last_alerts
|
||||
ON
|
||||
sended.id_phone = last_alerts.id_phone
|
||||
WHERE
|
||||
sended.id_user = :id_user
|
||||
AND
|
||||
phone.status != 'disabled'
|
||||
AND
|
||||
sended.at > IFNULL(last_alerts.last_alert_time, '1970-01-01')
|
||||
AND
|
||||
sended.at BETWEEN NOW() - INTERVAL :period MINUTE AND NOW() - INTERVAL :grace_period MINUTE
|
||||
GROUP BY
|
||||
id_phone
|
||||
)
|
||||
SELECT
|
||||
id_phone,
|
||||
total,
|
||||
unreliable,
|
||||
(unreliable / total) AS rate
|
||||
FROM
|
||||
recent_messages
|
||||
WHERE
|
||||
total >= :min_volume
|
||||
AND
|
||||
(unreliable / total) > :rate_limit;
|
||||
", [
|
||||
'id_user' => $id_user,
|
||||
'sms_status' => $sms_status,
|
||||
'period' => $period,
|
||||
'grace_period' => $grace_period,
|
||||
'min_volume' => $min_volume,
|
||||
'rate_limit' => $rate_limit,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'phone_reliability_history';
|
||||
}
|
||||
}
|
|
@ -338,6 +338,7 @@ namespace models;
|
|||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
|
|
|
@ -31,6 +31,16 @@ namespace models;
|
|||
return $this->_select_one('user', ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all active users.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all_active()
|
||||
{
|
||||
return $this->_select('user', ['status' => self::STATUS_ACTIVE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user by ids.
|
||||
*
|
||||
|
|
|
@ -19,6 +19,7 @@ namespace models;
|
|||
const TYPE_INBOUND_CALL = 'inbound_call';
|
||||
const TYPE_QUOTA_LEVEL_ALERT = 'quota_level';
|
||||
const TYPE_QUOTA_REACHED = 'quota_reached';
|
||||
const TYPE_PHONE_RELIABILITY = 'phone_reliability';
|
||||
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue