From c5621b82cfeddee23b81871a53035fde747f73a9 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 18 Dec 2018 02:23:44 +0100 Subject: Implemented /metrics endpoint and reimplemented /stats closes #418 (/metrics endpoint) Usage: ```yaml scrape_configs: - job_name: 'engelsystem' static_configs: - targets: ['engelsystem.example.com:80'] ``` --- src/Controllers/Metrics/Stats.php | 144 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/Controllers/Metrics/Stats.php (limited to 'src/Controllers/Metrics/Stats.php') diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php new file mode 100644 index 00000000..891f8c80 --- /dev/null +++ b/src/Controllers/Metrics/Stats.php @@ -0,0 +1,144 @@ +db = $db; + } + + /** + * The number of not arrived users + * + * @param null $working + * @return int + */ + public function arrivedUsers($working = null): int + { + $query = $this + ->getQuery('users') + ->join('users_state', 'user_id', '=', 'id') + ->where('arrived', '=', 1); + + if (!is_null($working)) { + // @codeCoverageIgnoreStart + $query + ->leftJoin('UserWorkLog', 'UserWorkLog.user_id', '=', 'users.id') + ->leftJoin('ShiftEntry', 'ShiftEntry.UID', '=', 'users.id') + ->groupBy('users.id'); + + $query->where(function ($query) use ($working) { + /** @var QueryBuilder $query */ + if ($working) { + $query + ->whereNotNull('ShiftEntry.SID') + ->orWhereNotNull('UserWorkLog.work_hours'); + + return; + } + $query + ->whereNull('ShiftEntry.SID') + ->whereNull('UserWorkLog.work_hours'); + }); + // @codeCoverageIgnoreEnd + } + + return $query + ->count(); + } + + /** + * The number of not arrived users + * + * @return int + */ + public function newUsers(): int + { + return $this + ->getQuery('users') + ->join('users_state', 'user_id', '=', 'id') + ->where('arrived', '=', 0) + ->count(); + } + + /** + * The number of currently working users + * + * @param null $freeloaded + * @return int + * @codeCoverageIgnore + */ + public function currentlyWorkingUsers($freeloaded = null): int + { + $query = $this + ->getQuery('users') + ->join('ShiftEntry', 'ShiftEntry.UID', '=', 'users.id') + ->join('Shifts', 'Shifts.SID', '=', 'ShiftEntry.SID') + ->where('Shifts.start', '<=', time()) + ->where('Shifts.end', '>', time()); + + if (!is_null($freeloaded)) { + $query->where('ShiftEntry.freeloaded', '=', $freeloaded); + } + + return $query->count(); + } + + /** + * The number of worked shifts + * + * @param bool|null $done + * @param bool|null $freeloaded + * @return int + * @codeCoverageIgnore + */ + public function workSeconds($done = null, $freeloaded = null): int + { + $query = $this + ->getQuery('ShiftEntry') + ->join('Shifts', 'Shifts.SID', '=', 'ShiftEntry.SID'); + + if (!is_null($freeloaded)) { + $query->where('freeloaded', '=', $freeloaded); + } + + if (!is_null($done)) { + $query->where('end', ($done == true ? '<' : '>='), time()); + } + + return $query->sum($this->raw('end - start')); + } + + /** + * @param string $table + * @return QueryBuilder + */ + protected function getQuery(string $table): QueryBuilder + { + return $this->db + ->getConnection() + ->table($table); + } + + /** + * @param mixed $value + * @return QueryExpression + * @codeCoverageIgnore + */ + protected function raw($value) + { + return $this->db->getConnection()->raw($value); + } +} -- cgit v1.3.1