1
0
Fork 0
mirror of synced 2024-05-20 12:42:39 +12:00

refactor to sync usage for 1d and 30m periods

This commit is contained in:
Damodar Lohani 2021-08-12 11:38:02 +05:45
parent 949abf876b
commit dfde2b970f

View file

@ -5,7 +5,7 @@ global $cli, $register;
require_once __DIR__ . '/../workers.php';
use Utopia\App;
use Utopia\Cache\Adapter\Redis;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\CLI\Console;
use Utopia\Database\Adapter\MariaDB;
@ -23,7 +23,7 @@ $cli
$interval = (int) App::getEnv('_APP_USAGE_SYNC_INTERVAL', '30'); //30 seconds
$cacheAdapter = new Cache(new Redis($register->get('cache')));
$cacheAdapter = new Cache(new None());
$dbForConsole = new Database(new MariaDB($register->get('db')), $cacheAdapter);
$dbForConsole->setNamespace('project_console_internal');
@ -31,13 +31,13 @@ $cli
$projects = $dbForConsole->find('projects');
$projectIds = [];
foreach ($projects as $project) {
$projectIds[] = $project['$id'];
$projectIds[$project['$id']] = true;
}
$projects = null;
$latestData = [];
$dbForProject = new Database(new MariaDB($register->get('db')), $cacheAdapter);
foreach ($projectIds as $id) {
foreach ($projectIds as $id => $value) {
$dbForProject->setNamespace("project_{$id}_internal");
$doc = $dbForProject->find('stats', [new Query("period", Query::TYPE_EQUAL, ["1d"])], 1, 0, ['time'], [Database::ORDER_DESC]);
$doc = reset($doc);
@ -48,123 +48,144 @@ $cli
}
$firstRun = true;
Console::loop(function () use ($interval, $register, $projectIds, $latestData, $dbForProject, &$firstRun) {
Console::loop(function () use ($interval, $register, &$projectIds, &$latestData, $dbForProject, $dbForConsole, &$firstRun) {
$time = date('d-m-Y H:i:s', time());
Console::info("[{$time}] Syncing usage data from influxdb to Appwrite Console DB every {$interval} seconds");
$client = $register->get('influxdb');
$requests = [];
$network = [];
$functions = [];
//fetch delta projects
if (!$firstRun) {
$projects = $dbForConsole->find('projects');
foreach ($projects as $project) {
$id = $project['$id'];
if (!$projectIds[$id]) {
$projectIds[$id] = true;
if ($latestData[$id] == null) {
$dbForProject->setNamespace("project_{$id}_internal");
$doc = $dbForProject->find('stats', [new Query("period", Query::TYPE_EQUAL, ["1d"])], 1, 0, ['time'], [Database::ORDER_DESC]);
$doc = reset($doc);
$latestData[$id]["1d"] = $doc ? $doc->getAttribute('time') : null;
$doc = $dbForProject->find('stats', [new Query("period", Query::TYPE_EQUAL, ["30m"])], 1, 0, ['time'], [Database::ORDER_DESC]);
$doc = reset($doc);
$latestData[$id]["30m"] = $doc ? $doc->getAttribute('time') : null;
}
}
}
}
if ($client) {
foreach ($projectIds as $id) {
if ($firstRun) {
$start = DateTime::createFromFormat('U', \strtotime('-1 days'))->format(DateTime::RFC3339);
if (!empty($latestData[$id]["30m"])) {
$start = DateTime::createFromFormat('U', $latestData[$id]['30m'])->format(DateTime::RFC3339);
}
} else {
$start = DateTime::createFromFormat('U', \strtotime("-{$interval} seconds"))->format(DateTime::RFC3339);
}
$end = DateTime::createFromFormat('U', \strtotime('now'))->format(DateTime::RFC3339);
$database = $client->selectDB('telegraf');
// Requests
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $id . '\' GROUP BY time(' . '30m' . ') FILL(null)');
$points = $result->getPoints();
$dbForProject->setNamespace("project_{$id}_internal");
foreach ($points as $point) {
$requests[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . '30m' . '_requests');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => '30m',
'time' => $time,
'metric' => 'requests',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $firstRun ? $value : $document->getAttribute('value') + $value));
}
}
// Network
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $id . '\'GROUP BY time(' . '30m' . ') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$network[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . '30m' . '_network');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => '30m',
'time' => $time,
'metric' => 'network',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $firstRun ? $value : $document->getAttribute('value') + $value));
}
}
// Functions
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $id . '\'GROUP BY time(' . '30m' . ') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$functions[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . '30m' . '_functions');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => '30m',
'time' => $time,
'metric' => 'functions',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $firstRun ? $value : $document->getAttribute('value') + $value));
}
}
foreach ($projectIds as $id => $value) {
syncData($client, $id, '30m', $latestData, $dbForProject);
syncData($client, $id, '1d', $latestData, $dbForProject);
}
}
$firstRun = false;
}, $interval);
});
function syncData($client, $projectId, $period, &$latestData, $dbForProject)
{
$requests = [];
$network = [];
$functions = [];
$start = DateTime::createFromFormat('U', \strtotime($period == '1d' ? '-90 days' : '-1 days'))->format(DateTime::RFC3339);
if (!empty($latestData[$projectId][$period])) {
$start = DateTime::createFromFormat('U', $latestData[$projectId][$period])->format(DateTime::RFC3339);
}
$end = DateTime::createFromFormat('U', \strtotime('now'))->format(DateTime::RFC3339);
$database = $client->selectDB('telegraf');
// Requests
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $projectId . '\' GROUP BY time(' . $period . ') FILL(null)');
$points = $result->getPoints();
$dbForProject->setNamespace("project_{$projectId}_internal");
foreach ($points as $point) {
$requests[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . $period . '_requests');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => $period,
'time' => $time,
'metric' => 'requests',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $value));
}
$latestData[$id]["30m"] = $time;
}
// Network
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $projectId . '\'GROUP BY time(' . $period . ') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$network[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . $period . '_network');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => $period,
'time' => $time,
'metric' => 'network',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $value));
}
$latestData[$id]["30m"] = $time;
}
// Functions
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "project"=\'' . $id . '\'GROUP BY time(' . $period . ') FILL(null)');
$points = $result->getPoints();
foreach ($points as $point) {
$functions[] = [
'value' => (!empty($point['value'])) ? $point['value'] : 0,
'date' => \strtotime($point['time']),
'dateStr' => $point['time'],
'point' => $point,
];
$time = \strtotime($point['time']);
$id = \md5($time . '_' . $period . '_functions');
$value = (!empty($point['value'])) ? $point['value'] : 0;
$document = $dbForProject->getDocument('stats', $id);
if ($document->isEmpty()) {
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
'period' => $period,
'time' => $time,
'metric' => 'functions',
'value' => $value,
'type' => 0,
]));
} else {
$dbForProject->updateDocument('stats', $document->getId(),
$document->setAttribute('value', $value));
}
$latestData[$id]["30m"] = $time;
}
}