From 7c422d58afb1840148a6fe53bdb4a0f93f8cca9d Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 23 May 2021 00:43:12 +0200 Subject: [PATCH] Added stats endpoint prototype --- public/api/getStats.php | 38 ++++++++++++++++++++++++ public/api/services/dbOperations.inc.php | 16 ++++++++++ public/api/services/jsonBuilder.inc.php | 21 +++++++++++++ public/api/services/jugglDbApi.inc.php | 24 +++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 public/api/getStats.php diff --git a/public/api/getStats.php b/public/api/getStats.php new file mode 100644 index 0000000..e33f8aa --- /dev/null +++ b/public/api/getStats.php @@ -0,0 +1,38 @@ +get("user_id"); + + $from_date = date("Y-m-d"); + if ($params->exists(["from_date"])) { + $from_date = $params->get("from_date"); + } + $until_date = date("Y-m-d"); + if ($params->exists(["until_date"])) { + $until_date = $params->get("until_date"); + } + + $stats = getStats($user_id, $from_date, $until_date); + + $json = new JsonBuilder(); + $json->addStats($stats); + + respondJson($json); + } +} + +$branch = new GetStatsBranch(); +$branch->execute(); diff --git a/public/api/services/dbOperations.inc.php b/public/api/services/dbOperations.inc.php index 6b47ea2..70d9e27 100644 --- a/public/api/services/dbOperations.inc.php +++ b/public/api/services/dbOperations.inc.php @@ -55,6 +55,22 @@ class DbOperations return $this; } + function groupBy(array $attributes, bool $addTableName = true) + { + for ($i = 0; $i < count($attributes); $i++) { + $a = $attributes[$i]; + // Add table name prefix if missing + if ($addTableName && strpos($a, ".") === false) { + $attributes[$i] = "$this->table.$a"; + } + } + $formattedAttributes = implode(', ', $attributes); + + $this->addToQuery("GROUP BY $formattedAttributes"); + + return $this; + } + function orderBy(string $attribute, string $order = Order::ASC) { diff --git a/public/api/services/jsonBuilder.inc.php b/public/api/services/jsonBuilder.inc.php index 6cf5b0d..9bd008f 100644 --- a/public/api/services/jsonBuilder.inc.php +++ b/public/api/services/jsonBuilder.inc.php @@ -97,6 +97,27 @@ class JsonBuilder return $this; } + function addStats(array $stats) + { + if ($stats === null) return; + + $columns = array( + "name" => "", + "project_id" => "", + "color" => "", + "visible" => "", + "duration" => "", + "record_count" => "", + "date" => "" + ); + + $this->jsonData['stats'] = array(); + foreach ($stats as $tag) { + $this->jsonData['stats'][] = $this->createJsonArray($tag, $columns); + } + return $this; + } + private function createJsonArray(array $data, array $columns) { $jsonArray = array(); diff --git a/public/api/services/jugglDbApi.inc.php b/public/api/services/jugglDbApi.inc.php index 76bb136..23ab448 100644 --- a/public/api/services/jugglDbApi.inc.php +++ b/public/api/services/jugglDbApi.inc.php @@ -464,3 +464,27 @@ function removeTagFromRecord($tag_id, $record_id) $db->where("record_id", Comparison::EQUAL, $record_id); $db->execute(); } + +function getStats($user_id, $from_date, $until_date) { + $sum_duration = "SUM(ju_time_records.duration) AS duration"; + $date = "DATE(ju_time_records.start_time) AS date"; + $record_count = "COUNT(*) AS record_count"; + + $where1 = " AND DATE(ju_time_records.start_time) >= DATE( "; + $where2 = " ) AND DATE(ju_time_records.start_time) <= DATE( "; + $where3 = " ) AND ju_time_records.end_time IS NOT NULL "; + + $db = new DbOperations(); + $db->select("projects", ["name", "project_id", "color", "visible", $sum_duration, $date, $record_count], false); + $db->innerJoin("time_records", "project_id"); + + $db->where("user_id", Comparison::EQUAL, $user_id); + $db->addSql($where1); + $db->addValue($from_date); + $db->addSql($where2); + $db->addValue($until_date); + $db->addSql($where3); + + $db->groupBy(["project_id", "date"]); + return $db->execute(); +}