builds, stats

This commit is contained in:
fogelito
2022-07-11 18:12:41 +03:00
parent bc33356a57
commit 7e08575245
14 changed files with 160 additions and 131 deletions
+18 -21
View File
@@ -8,10 +8,10 @@ use Utopia\Database\Document;
class Delete extends Event
{
protected string $type = '';
protected ?int $timestamp = null;
protected ?int $timestamp1d = null;
protected ?int $timestamp30m = null;
protected ?Document $document = null;
protected ?string $datetime = null;
protected ?string $datetime1d = null;
protected ?string $datetime30m = null;
public function __construct()
{
@@ -42,41 +42,38 @@ class Delete extends Event
}
/**
* Set timestamp.
* set Datetime.
*
* @param int $timestamp
* @param string $datetime
* @return self
*/
public function setTimestamp(int $timestamp): self
public function setDatetime(string $datetime): self
{
$this->timestamp = $timestamp;
$this->datetime = $datetime;
return $this;
}
/**
* Set timestamp for 1 day interval.
* Set datetime for 1 day interval.
*
* @param int $timestamp
* @param string $datetime
* @return self
*/
public function setTimestamp1d(int $timestamp): self
public function setDatetime1d(string $datetime): self
{
$this->timestamp1d = $timestamp;
$this->datetime1d = $datetime;
return $this;
}
/**
* Sets timestamp for 30m interval.
* Sets datetime for 30m interval.
*
* @param int $timestamp
* @param string $datetime
* @return self
*/
public function setTimestamp30m(int $timestamp): self
public function setDatetime30m(string $datetime): self
{
$this->timestamp30m = $timestamp;
$this->datetime30m = $datetime;
return $this;
}
@@ -115,9 +112,9 @@ class Delete extends Event
'project' => $this->project,
'type' => $this->type,
'document' => $this->document,
'timestamp' => $this->timestamp,
'timestamp1d' => $this->timestamp1d,
'timestamp30m' => $this->timestamp30m
'datetime' => $this->datetime,
'datetime1d' => $this->datetime1d,
'datetime30m' => $this->datetime30m
]);
}
}
+9 -5
View File
@@ -191,12 +191,12 @@ class Usage
protected array $periods = [
[
'key' => '30m',
'multiplier' => 1800,
//'multiplier' => 1800,
'startTime' => '-24 hours',
],
[
'key' => '1d',
'multiplier' => 86400,
//'multiplier' => 86400,
'startTime' => '-90 days',
],
];
@@ -213,7 +213,7 @@ class Usage
* Create or update each metric in the stats collection for the given project
*
* @param string $projectId
* @param int $time
* @param string $time
* @param string $period
* @param string $metric
* @param int $value
@@ -221,7 +221,7 @@ class Usage
*
* @return void
*/
private function createOrUpdateMetric(string $projectId, int $time, string $period, string $metric, int $value, int $type): void
private function createOrUpdateMetric(string $projectId, string $time, string $period, string $metric, int $value, int $type): void
{
$id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_console');
@@ -246,7 +246,10 @@ class Usage
$document->setAttribute('value', $value)
);
}
$time = (new DateTime($time))->getTimestamp(); //todo: What about this timestamp?
$this->latestTime[$metric][$period] = $time;
} catch (\Exception $e) { // if projects are deleted this might fail
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "sync_project_{$projectId}_metric_{$metric}");
@@ -311,7 +314,8 @@ class Usage
}
}
$time = \strtotime($point['time']);
//$time = \strtotime($point['time']);
$time = $point['time']; //todo: check is this datetime format?
$value = (!empty($point['value'])) ? $point['value'] : 0;
$this->createOrUpdateMetric(
+19 -5
View File
@@ -2,6 +2,7 @@
namespace Appwrite\Stats;
use Exception;
use Utopia\Database\Database;
use Utopia\Database\Document;
@@ -12,6 +13,7 @@ class UsageDB extends Usage
$this->database = $database;
$this->errorHandler = $errorHandler;
}
/**
* Create or Update Mertic
* Create or update each metric in the stats collection for the given project
@@ -21,12 +23,22 @@ class UsageDB extends Usage
* @param int $value
*
* @return void
* @throws Exception
*/
private function createOrUpdateMetric(string $projectId, string $metric, int $value): void
{
foreach ($this->periods as $options) {
$period = $options['key'];
$time = (int) (floor(time() / $options['multiplier']) * $options['multiplier']);
$date = new \DateTime();
if ($period === '30m') {
$minutes = $date->format('i') >= '30' ? "30" : "00";
$time = $date->format('Y-m-d H:' . $minutes . ':00');
} elseif ($period === '1d') {
$time = $date->format('Y-m-d 00:00:00');
} else {
throw new Exception("Period type not found", 500);
}
$id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_' . $projectId);
@@ -48,7 +60,7 @@ class UsageDB extends Usage
$document->setAttribute('value', $value)
);
}
} catch (\Exception$e) { // if projects are deleted this might fail
} catch (Exception$e) { // if projects are deleted this might fail
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "sync_project_{$projectId}_metric_{$metric}");
} else {
@@ -80,7 +92,7 @@ class UsageDB extends Usage
while ($sum === $limit) {
try {
$results = $this->database->find($collection, $queries, $limit, cursor:$latestDocument);
} catch (\Exception $e) {
} catch (Exception $e) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "fetch_documents_project_{$projectId}_collection_{$collection}");
return;
@@ -113,6 +125,7 @@ class UsageDB extends Usage
* @param string $metric
*
* @return int
* @throws Exception
*/
private function sum(string $projectId, string $collection, string $attribute, string $metric): int
{
@@ -122,7 +135,7 @@ class UsageDB extends Usage
$sum = (int) $this->database->sum($collection, $attribute);
$this->createOrUpdateMetric($projectId, $metric, $sum);
return $sum;
} catch (\Exception $e) {
} catch (Exception $e) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "fetch_sum_project_{$projectId}_collection_{$collection}");
} else {
@@ -140,6 +153,7 @@ class UsageDB extends Usage
* @param string $metric
*
* @return int
* @throws Exception
*/
private function count(string $projectId, string $collection, string $metric): int
{
@@ -149,7 +163,7 @@ class UsageDB extends Usage
$count = $this->database->count($collection);
$this->createOrUpdateMetric($projectId, $metric, $count);
return $count;
} catch (\Exception $e) {
} catch (Exception $e) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e, "fetch_count_project_{$projectId}_collection_{$collection}");
} else {
+10 -10
View File
@@ -16,12 +16,6 @@ class Build extends Model
'default' => '',
'example' => '5e5ea5c16897e',
])
->addRule('startTime', [
'type' => self::TYPE_INTEGER,
'description' => 'The deployment creation date in Unix timestamp.',
'default' => 0,
'example' => 1592981250,
])
->addRule('deploymentId', [
'type' => self::TYPE_STRING,
'description' => 'The deployment that created this build.',
@@ -51,11 +45,17 @@ class Build extends Model
'default' => '',
'example' => '',
])
->addRule('startTime', [
'type' => self::TYPE_DATETIME,
'description' => 'The deployment creation date in Datetime.',
'default' => '',
'example' => '1975-12-06 13:30:59',
])
->addRule('endTime', [
'type' => self::TYPE_INTEGER,
'description' => 'The time the build was finished in Unix timestamp.',
'default' => 0,
'example' => 0,
'type' => self::TYPE_DATETIME,
'description' => 'The time the build was finished in Datetime.',
'default' => '',
'example' => '1975-12-06 13:30:59',
])
->addRule('duration', [
'type' => self::TYPE_INTEGER,