Skip to content

Commit

Permalink
grr
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Jan 28, 2024
1 parent 7909de1 commit 98259a2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 172 deletions.
123 changes: 0 additions & 123 deletions glued/Classes/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,129 +7,6 @@ class Sql
public $q;
public function __construct()
{
$q['runs'] = "
SELECT * FROM (
SELECT
BIN_TO_UUID(`c_uuid`,1) AS `uuid`,
BIN_TO_UUID(`c_act_uuid`,1) AS `act_uuid`,
c_ts_requested as `ts_requested`,
c_ts_responded as `ts_responded`,
c_data as `data`,
TIMESTAMPDIFF(SECOND, c_ts_requested, c_ts_responded) AS duration,
c_status AS ok,
c_response_hash as response_hash,
c_response_fid as response_fid,
TIMESTAMPDIFF(SECOND, c_ts_requested, NOW()) AS requested_ago
FROM `t_if__runs`
) q
";
$q['queue'] = "
SELECT
svc_uuid,
svc_data,
svc_type,
svc_name,
svc_host,
act_uuid,
act_type,
act_freq,
run_req,
run_res,
run_duration,
run_ok,
run_hash,
run_fid,
run_ago,
next_in,
hash_count
FROM (
SELECT
bin_to_uuid(svc.c_uuid, true) AS svc_uuid,
svc.c_data AS svc_data,
svc.c_service AS svc_type,
svc.c_deployment AS svc_name,
svc.c_remote AS svc_host,
bin_to_uuid(act.c_uuid, true) AS act_uuid,
act.c_scheme AS act_type,
act.c_freq AS act_freq,
bin_to_uuid(run.c_uuid, true) as run_uuid,
IFNULL(run.c_ts_start, 0) AS run_req,
IFNULL(run.c_ts_finish, 0) AS run_res,
TIMESTAMPDIFF(SECOND, run.c_ts_start, c_ts_finish) AS run_duration,
run.c_status AS run_ok,
run.c_hash AS run_hash,
run.c_fid AS run_fid,
TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW()) AS run_ago,
ifnull(act.c_freq - TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW()),-1) AS next_in,
(SELECT COUNT(*) FROM t_if__runs WHERE c_hash = run.c_hash) AS hash_count,
ROW_NUMBER() OVER (PARTITION BY act.c_uuid ORDER BY TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW())) AS row_num
FROM t_if__services svc
LEFT JOIN t_if__actions act ON svc.c_uuid = act.c_service_uuid
LEFT JOIN t_if__runs run ON act.c_uuid = run.c_action_uuid
ORDER BY run_ago ASC, run_duration ASC
) subquery";

$q['base'] = "
SELECT COALESCE(
JSON_ARRAYAGG(
JSON_OBJECT(
'service', service,
'deployment', JSON_OBJECT(
'uuid', deployment_uuid,
'conf', deployment_data
),
'action', JSON_OBJECT(
'uuid', act_uuid,
'scheme', act_scheme,
'freq', act_freq
),
'run', JSON_OBJECT(
'uuid', run_uuid,
'req', run_req,
'res', run_res,
'duration', run_duration,
'ok', run_ok,
'hash', run_hash,
'fid', run_fid,
'ago', run_ago,
'in', run_in,
'hash_count', run_hash_count
)
)
),
JSON_ARRAY()
) AS json_result
FROM (
SELECT
bin_to_uuid(svc.c_uuid, true) AS deployment_uuid,
svc.c_data AS deployment_data,
svc.c_service AS service,
svc.c_deployment AS deployment_name,
svc.c_remote AS deployment_host,
bin_to_uuid(act.c_uuid, true) AS act_uuid,
act.c_scheme AS act_scheme,
act.c_freq AS act_freq,
bin_to_uuid(run.c_uuid, true) as run_uuid,
IFNULL(run.c_ts_start, 0) AS run_req,
IFNULL(run.c_ts_finish, 0) AS run_res,
TIMESTAMPDIFF(SECOND, run.c_ts_start, c_ts_finish) AS run_duration,
run.c_status AS run_ok,
run.c_hash AS run_hash,
run.c_fid AS run_fid,
TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW()) AS run_ago,
IFNULL(act.c_freq - TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW()), -1) AS run_in,
(SELECT COUNT(*) FROM t_if__runs WHERE c_hash = run.c_hash) AS run_hash_count,
ROW_NUMBER() OVER (PARTITION BY COALESCE(act.c_uuid, UUID()) ORDER BY TIMESTAMPDIFF(SECOND, run.c_ts_finish, NOW())) AS row_num
-- Row_num is = 1 is used to select the latest run of an action.
-- Row_num is = null ensures we don't ge a null on actions that never had a run
-- COALESCE(act.c_uuid, UUID()) ensures listing in case service deployment doesn't have an action stored
FROM t_if__services svc
LEFT JOIN t_if__actions act ON svc.c_uuid = act.c_service_uuid
LEFT JOIN t_if__runs run ON act.c_uuid = run.c_action_uuid
ORDER BY run_ago ASC, run_duration ASC
) subquery
";
$q['base1'] = "{$q['base']} where (subquery.row_num = 1 or subquery.row_num is NULL)";

$this->q = $q;

Expand Down
6 changes: 3 additions & 3 deletions glued/Config/Migrations/20231009002534_actions.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-- migrate:up

CREATE TABLE `t_if__actions` (
`c_service_uuid` binary(16) NOT NULL COMMENT 'IF service UUID',
`c_deployment_uuid` binary(16) NOT NULL COMMENT 'IF deployment UUID',
`c_uuid` binary(16) NOT NULL DEFAULT (uuid_to_bin(uuid(),true)) COMMENT 'IF action uuid (v4)',
`c_data` json NOT NULL COMMENT 'JSON data',
`c_scheme` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.scheme'))) STORED COMMENT '[STORED] Interface Framework action scheme',
`c_note` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.note'))) STORED COMMENT '[STORED] Interface Framework action note',
`c_freq` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.freq'))) STORED COMMENT '[STORED] Interface Framework action frequency',
PRIMARY KEY (`c_uuid`),
UNIQUE KEY `uniq_service_scheme` (`c_service_uuid`,`c_scheme`),
KEY `idx_service` (`c_service_uuid`),
UNIQUE KEY `uniq_service_scheme` (`c_deployment_uuid`,`c_scheme`),
KEY `idx_service` (`c_deployment_uuid`),
KEY `idx_scheme` (`c_scheme`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='Integration framework service instance actions.';

Expand Down
18 changes: 18 additions & 0 deletions glued/Config/Migrations/20231009002603_deployments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- migrate:up

CREATE TABLE `t_if__deployments` (
`c_uuid` binary(16) NOT NULL DEFAULT (uuid_to_bin(uuid(),true)) COMMENT 'IF service instance uuid (v4), autogenerated on SQL insert if not provided. NOTE to always insert with UUID_TO_BIN(UUID(), true)',
`c_data` json NOT NULL COMMENT 'JSON data',
`c_hash` varchar(32) GENERATED ALWAYS AS (md5(`c_data`)) STORED COMMENT '[STORED] MD5 Hash of the data json (acting as a unique index)',
`c_service` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.service'))) STORED COMMENT '[STORED] Interface Framework service service',
`c_remote` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.remote'))) STORED COMMENT '[STORED] Interface Framework remote remote',
`c_name` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.name'))) STORED COMMENT '[STORED] Interface Framework service deployment name',
`c_note` varchar(255) GENERATED ALWAYS AS (json_unquote(json_extract(`c_data`,_utf8mb4'$.note'))) STORED COMMENT '[STORED] Interface Framework remote note',
PRIMARY KEY (`c_uuid`),
UNIQUE KEY `c_hash` (`c_hash`),
KEY `idx_service` (`c_service`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='Integration framework service instances (service / remote remote / authentication).';

-- migrate:down

DROP TABLE IF EXISTS `t_if__deployments`;
18 changes: 0 additions & 18 deletions glued/Config/Migrations/20231009002603_services.sql

This file was deleted.

12 changes: 11 additions & 1 deletion glued/Config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ routes:
get: Glued\Controllers\IfController:services_r1
post: Glued\Controllers\IfController:services_c1

be_if_deployments_v1:
be_if_service_deployments_v1:
pattern: ${routes.be_if.path}/v1/services/{svc}/deployments[/{deploy}]
path: ${routes.be_if.path}/v1/services
label: IF Services
Expand All @@ -63,6 +63,16 @@ routes:
get: Glued\Controllers\IfController:deployments_r1
post: Glued\Controllers\IfController:deployments_c1

be_if_deployments_v1:
pattern: ${routes.be_if.path}/v1/deployments[/{deploy}]
path: ${routes.be_if.path}/v1/deployments
label: IF Services
dscr: People-friendly navigation.
service: if
methods:
get: Glued\Controllers\IfController:deployments_r1
post: Glued\Controllers\IfController:deployments_c1

be_if_scheduler_v1:
pattern: ${routes.be_if.path}/v1/scheduler
path: ${routes.be_if.path}/v1/scheduler
Expand Down
52 changes: 25 additions & 27 deletions glued/Controllers/IfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function getServices() {
public function runs_r1(Request $request, Response $response, array $args = []): Response
{
$rp = $this->utils->getQueryParams($request) ?? [];
$qs = (new Sql)->q['runs'];
$qs = (new \Glued\Lib\IfSql())->q['json:runs:all'];
$qs = (new \Glued\Lib\QueryBuilder())->select($qs);
$qs = $this->utils->mysqlQueryFromRequest($qs, $rp, 'c_data');
$r = $this->mysqli->execute_query($qs,array_values($rp));
Expand Down Expand Up @@ -84,32 +84,34 @@ public function services_r1(Request $request, Response $response, array $args =
return $response->withJson($payload);
}

public function services_c1(Request $request, Response $response, array $args = []): Response
public function deployments_c1(Request $request, Response $response, array $args = []): Response
{
$payload = (array) $request->getBody()->getContents();
$payload = json_decode($payload[0], true);

$svc_uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
$data = $payload['svc'];
$data = [ 'c_uuid' => $svc_uuid, 'c_data' => json_encode($data) ];
$x = $this->db->rawQuery('INSERT INTO `t_if__services` ( `c_uuid`, `c_data` ) VALUES ( UUID_TO_BIN(?, true), ? )', $data);

foreach ($payload['act'] as $a) {
$act_uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
$data = [ 'c_svc_uuid' => $svc_uuid, 'c_uuid' => $act_uuid, 'c_data' => json_encode($a) ];
$this->db->rawQuery('INSERT INTO `t_if__actions` ( `c_svc_uuid`, `c_uuid`, `c_data` ) VALUES ( UUID_TO_BIN(?, true), UUID_TO_BIN(?, true), ? )', $data);
$params = $request->getQueryParams();
$contentTypeHeader = $request->getHeaderLine('Content-Type') ?? '';
if ($contentTypeHeader !== 'application/json') { throw new \Exception('Invalid Content-Type. Please set `Content-Type: application/json', 400); }
$payload = $request->getParsedBody();
foreach ($payload as $item) {
$svc_uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
$svc_data = json_encode($item['deployment']);
$res = $this->mysqli->execute_query('SELECT bin_to_uuid(`c_uuid`,true) as svc_uuid FROM `t_if__deployments` WHERE JSON_CONTAINS(c_data, ?, "$")', [$svc_data]);
foreach ($res as $r) { $svc_uuid = $r['svc_uuid']; break; }
$this->mysqli->execute_query("INSERT INTO `t_if__deployments` ( `c_uuid`, `c_data` ) VALUES ( UUID_TO_BIN(?, true), ? ) ON DUPLICATE KEY UPDATE `c_data` = values(`c_data`)", [$svc_uuid, $svc_data]);

foreach ($item['actions'] as $a) {
$act_uuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
$data = ['c_deployment_uuid' => $svc_uuid, 'c_uuid' => $act_uuid, 'c_data' => json_encode($a)];
$this->db->rawQuery('INSERT INTO `t_if__actions` ( `c_deployment_uuid`, `c_uuid`, `c_data` ) VALUES ( UUID_TO_BIN(?, true), UUID_TO_BIN(?, true), ? ) ON DUPLICATE KEY UPDATE `c_data` = VALUES(`c_data`)', $data);
}
}

return $response->withJson($payload);
}



public function queue_r1(Request $request, Response $response, array $args = []): Response
{
$rp = $this->utils->getQueryParams($request) ?? [];
$override = [ [ 'next_in', '<', '0' ], [ 'row_num', '=', '1' ] ];
$qs = (new Sql)->q['queue'];
$qs = (new \Glued\Lib\IfSql())->q['json:runs:latest'];;
$qs = (new \Glued\Lib\QueryBuilder())->select($qs);
$qs = $this->utils->mysqlQueryFromRequest($qs, $rp, 'svc_data', override: $override);
$r = $this->mysqli->execute_query($qs,array_values($rp));
Expand All @@ -127,20 +129,16 @@ public function deployments_r1(Request $request, Response $response, array $args

$data = [];
$rp = $this->utils->getQueryParams($request) ?? [];
$qs = (new Sql)->q['base1'];
//if ($args['svc'] ?? false) { $data[] = $args['svc']; $qs.= " and subquery.service = ?"; }
$qs = (new \Glued\Lib\IfSql())->q['json:runs:latest'];
if ($args['svc'] ?? false) { $data[] = $args['svc']; $qs.= " and subquery.service = ?"; }
$data = array_values($rp) + $data;
$data = [];
$qs .= " and subquery.service = 'fio_cz'";
$res = $this->mysqli->execute_query($qs, $data);
foreach ($res as $row) {
// print_r($row); die();
$data = json_decode($row['json_result']); break; }
//print_r($data);
$data = json_decode($row['json_result'], true); break; }
$fin['status'] = 'ok';
foreach ($data as &$i) {
//print_r($i); die();
//$i->runs = $this->settings['glued']['protocol'] . $this->settings['glued']['hostname'] . '/api/if/svc/' . $i['svc_type'] . '/act/' . $i['act_uuid'];
$base = "{$this->settings['glued']['protocol']}{$this->settings['glued']['hostname']}/api/if";
foreach ($data as $k => &$i) {
$data[$k]['links']['start'] = "{$base}/svc/{$i['service']}/v1/act/{$i['action']['uuid']}";
//$i['run'] = $this->settings['glued']['protocol'] . $this->settings['glued']['hostname'] . '/api/if/v1/runs/' . $i['act_uuid'];
}
$fin['data'] = $data;
Expand Down

0 comments on commit 98259a2

Please sign in to comment.