Added stats endpoint prototype
This commit is contained in:
parent
2cba0279e6
commit
7c422d58af
4 changed files with 99 additions and 0 deletions
38
public/api/getStats.php
Normal file
38
public/api/getStats.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
session_start();
|
||||
require_once(__DIR__ . "/services/apiBranch.inc.php");
|
||||
require_once(__DIR__ . "/services/jsonBuilder.inc.php");
|
||||
require_once(__DIR__ . "/services/responses.inc.php");
|
||||
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
|
||||
|
||||
class GetStatsBranch extends ApiBranch
|
||||
{
|
||||
function get(ParamCleaner $params)
|
||||
{
|
||||
respondStatus(405);
|
||||
}
|
||||
|
||||
function post(ParamCleaner $params)
|
||||
{
|
||||
$user_id = $params->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();
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue