Guillermo - stats architecture [skip ci]
This commit is contained in:
parent
845939f089
commit
868d08ecb2
|
@ -1,34 +1,140 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
|
||||
class GetStatsController extends Controller {
|
||||
const PATH = '/get-stats';
|
||||
|
||||
public function validations() {
|
||||
return [
|
||||
'permission' => 'staff_1',
|
||||
'requestData' => []
|
||||
'requestData' => [
|
||||
'period' => [
|
||||
'validation' => DataValidator::in(['week', 'month', 'quarter', 'year']),
|
||||
'error' => ERRORS::INVALID_PERIOD
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function handler() {
|
||||
$begin = new DateTime(Setting::getSetting('last-stat-day')->value);
|
||||
$end = new DateTime(Date::getCurrentDate());
|
||||
$this->generationNewStats();
|
||||
|
||||
$interval = new DateInterval('P1D');
|
||||
$dateRange = new DatePeriod($begin, $interval ,$end);
|
||||
$staffId = Controller::request('staffId');
|
||||
|
||||
foreach($dateRange as $date){
|
||||
$numberOfTickets = Log::count('type=? AND date LIKE ?',['CREATE_TICKET', $date->format('Ymd') . '%']);
|
||||
$stat = new Stat();
|
||||
if($staffId) {
|
||||
if($staffId !== Controller::getLoggedUser()->id && !Controller::isStaffLogged(3)) {
|
||||
Response::respondError(ERRORS::NO_PERMISSION);
|
||||
return;
|
||||
}
|
||||
|
||||
$stat->setProperties([
|
||||
'date' => $date->format('Ymd'),
|
||||
'type' => 'CREATE_TICKET',
|
||||
'general' => 1,
|
||||
'value' => $numberOfTickets,
|
||||
]);
|
||||
$stat->store();
|
||||
$this->getStaffStat();
|
||||
} else {
|
||||
$this->getGeneralStat();
|
||||
}
|
||||
}
|
||||
|
||||
public function generationNewStats() {
|
||||
$lastStatDay = Setting::getSetting('last-stat-day');
|
||||
$previousCurrentDate = floor(Date::getCurrentDate()/10000)-1;
|
||||
|
||||
if($lastStatDay->value !== $previousCurrentDate) {
|
||||
|
||||
$begin = new DateTime($lastStatDay->value);
|
||||
$end = new DateTime($previousCurrentDate);
|
||||
|
||||
$interval = new DateInterval('P1D');
|
||||
$dateRange = new DatePeriod($begin, $interval ,$end);
|
||||
|
||||
$staffList = Staff::getAll();
|
||||
|
||||
foreach($dateRange as $date) {
|
||||
$this->generateGeneralStat('CREATE_TICKET', $date);
|
||||
$this->generateGeneralStat('CLOSE', $date);
|
||||
$this->generateGeneralStat('SIGNUP', $date);
|
||||
$this->generateGeneralStat('COMMENT', $date);
|
||||
|
||||
foreach($staffList as $staff) {
|
||||
$assignments = Ticketevent::count('type=? AND author_staff_id=? AND date LIKE ?',['ASSIGN',$staff->id, $date->format('Ymd') . '%']);
|
||||
$closed = Ticketevent::count('type=? AND author_staff_id=? AND date LIKE ?',['CLOSE',$staff->id, $date->format('Ymd') . '%']);
|
||||
|
||||
$statAssign = new Stat();
|
||||
$statAssign->setProperties([
|
||||
'date' => $date->format('Ymd'),
|
||||
'type' => 'ASSIGN',
|
||||
'general' => 0,
|
||||
'value' => $assignments,
|
||||
]);
|
||||
|
||||
$statClose = new Stat();
|
||||
$statClose->setProperties([
|
||||
'date' => $date->format('Ymd'),
|
||||
'type' => 'CLOSE',
|
||||
'general' => 0,
|
||||
'value' => $closed,
|
||||
]);
|
||||
|
||||
$staff->ownStatList->add($statAssign);
|
||||
$staff->ownStatList->add($statClose);
|
||||
|
||||
$staff->store();
|
||||
}
|
||||
}
|
||||
|
||||
$lastStatDay->value = $previousCurrentDate;
|
||||
$lastStatDay->store();
|
||||
}
|
||||
}
|
||||
|
||||
public function generateGeneralStat($type, $date) {
|
||||
$value = Log::count('type=? AND date LIKE ?',[$type, $date->format('Ymd') . '%']);
|
||||
$stat = new Stat();
|
||||
|
||||
$stat->setProperties([
|
||||
'date' => $date->format('Ymd'),
|
||||
'type' => $type,
|
||||
'general' => 1,
|
||||
'value' => $value,
|
||||
]);
|
||||
|
||||
$stat->store();
|
||||
}
|
||||
|
||||
public function getGeneralStat() {
|
||||
$daysToRetrieve = $this->getDaysToRetrieve();
|
||||
|
||||
$statList = Stat::find('general=\'1\' ORDER BY id desc LIMIT ? ', [4 * $daysToRetrieve]);
|
||||
|
||||
Response::respondSuccess($statList->toArray());
|
||||
}
|
||||
|
||||
public function getStaffStat() {
|
||||
$staffId = Controller::request('staffId');
|
||||
$daysToRetrieve = $this->getDaysToRetrieve();
|
||||
|
||||
$statList = Stat::find('general=\'0\' AND staff_id=? ORDER BY id desc LIMIT ? ', [$staffId, 4 * $daysToRetrieve]);
|
||||
|
||||
Response::respondSuccess($statList->toArray());
|
||||
}
|
||||
|
||||
public function getDaysToRetrieve() {
|
||||
$period = Controller::request('period');
|
||||
$daysToRetrieve = 0;
|
||||
|
||||
switch ($period) {
|
||||
case 'week':
|
||||
$daysToRetrieve = 7;
|
||||
break;
|
||||
case 'month':
|
||||
$daysToRetrieve = 30;
|
||||
break;
|
||||
case 'quarter':
|
||||
$daysToRetrieve = 90;
|
||||
break;
|
||||
case 'year':
|
||||
$daysToRetrieve = 365;
|
||||
break;
|
||||
}
|
||||
|
||||
Response::respondSuccess();
|
||||
return $daysToRetrieve;
|
||||
}
|
||||
}
|
|
@ -35,4 +35,5 @@ class ERRORS {
|
|||
const INVALID_TEMPLATE = 'INVALID_TEMPLATE';
|
||||
const INVALID_SUBJECT = 'INVALID_SUBJECT';
|
||||
const INVALID_BODY = 'INVALID_BODY';
|
||||
const INVALID_PERIOD = 'INVALID_PERIOD';
|
||||
}
|
||||
|
|
|
@ -19,13 +19,14 @@ class Staff extends DataStore {
|
|||
'sharedDepartmentList',
|
||||
'sharedTicketList',
|
||||
'lastLogin',
|
||||
'sharedStatList'
|
||||
'ownStatList'
|
||||
];
|
||||
}
|
||||
|
||||
public function getDefaultProps() {
|
||||
return [
|
||||
'level' => 1
|
||||
'level' => 1,
|
||||
'ownStatList' => new DataStoreList()
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -14,4 +14,12 @@ class Stat extends DataStore {
|
|||
public function getDefaultProps() {
|
||||
return array();
|
||||
}
|
||||
public function toArray() {
|
||||
return [
|
||||
'date' => $this->date,
|
||||
'type' => $this->type,
|
||||
'general' => $this->general,
|
||||
'value' => $this->value
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue