mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-07-22 04:48:45 +02:00
First step of quota and using daemon
This commit is contained in:
parent
3d19c4decb
commit
120f56fad7
12 changed files with 552 additions and 0 deletions
|
@ -26,6 +26,32 @@ namespace models;
|
|||
return $this->_select('event', ['id_user' => $id_user], 'at', true, $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets events for a type, since a date and eventually until a date (both included)
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $type : Event type we want
|
||||
* @param \DateTime $since : Date to get events since
|
||||
* @param ?\DateTime $until (optional) : Date until wich we want events, if not specified no limit
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public get_events_by_type_and_date_for_user (int $id_user, string $type, \DateTime $since, ?\DateTime $until = null)
|
||||
{
|
||||
$where = [
|
||||
'id_user' => $id_user,
|
||||
'type' => $type,
|
||||
'>=at' => $since->format('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
if ($until !== null)
|
||||
{
|
||||
$where['<=at' => $until->format('Y-m-d H:i:s')];
|
||||
}
|
||||
|
||||
return $this->_select('event', $where, 'at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
|
|
96
models/Quota.php
Normal file
96
models/Quota.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?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 Quota extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Get remaining credit for a date
|
||||
* if no quota for this user return max int
|
||||
* @param int $id_user : User id
|
||||
* @param \DateTime $at : date to get credit at
|
||||
* @return int : number of remaining credits
|
||||
*/
|
||||
public function get_remaining_credit (int $id_user, \DateTime $at): int
|
||||
{
|
||||
$query = '
|
||||
SELECT (credit + additional - consumed) AS remaining_credit
|
||||
FROM quota
|
||||
WHERE id_user = :id_user
|
||||
AND start_date <= :at
|
||||
AND end_date > :at';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at->format('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
$result = $this->_run_query($query, $params);
|
||||
|
||||
return ($result[0]['remaining_credit'] ?? PHP_INT_MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get credit usage percent for a date
|
||||
* if no quota for this user return 0
|
||||
* @param int $id_user : User id
|
||||
* @param \DateTime $at : date to get usage percent at
|
||||
* @return float : percent of used credits
|
||||
*/
|
||||
public function get_usage_percentage (int $id_user, \DateTime $at): int
|
||||
{
|
||||
$query = '
|
||||
SELECT (consumed / (credit + additional)) AS usage_percentage
|
||||
FROM quota
|
||||
WHERE id_user = :id_user
|
||||
AND start_date <= :at
|
||||
AND end_date > :at';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'at' => $at->format('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
$result = $this->_run_query($query, $params);
|
||||
|
||||
return ($result[0]['usage_percentage'] ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume some credit for a user
|
||||
* @param int $id_user : User id
|
||||
* @param int $quantity : Number of credits to consume
|
||||
* @return bool
|
||||
*/
|
||||
public function consume_credit (int $id_user, int $quantity): int
|
||||
{
|
||||
$query = '
|
||||
UPDATE quota
|
||||
SET consumed = consumed + :quantity
|
||||
WHERE id_user = :id_user';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'quantity' => $quantity,
|
||||
];
|
||||
|
||||
return (bool) $this->_run_query($query, $params, \descartes\Model::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'quota';
|
||||
}
|
||||
}
|
|
@ -16,6 +16,9 @@ namespace models;
|
|||
const TYPE_SEND_SMS = 'send_sms';
|
||||
const TYPE_RECEIVE_SMS = 'receive_sms';
|
||||
const TYPE_INBOUND_CALL = 'inbound_call';
|
||||
const TYPE_QUOTA_LEVEL_ALERT = 'quota_level';
|
||||
const TYPE_QUOTA_REACHED = 'quota_reached';
|
||||
|
||||
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue