-- ------------------------------
-- OPTION
-- ------------------------------

OPTION IMPORT;

-- ------------------------------
-- FUNCTIONS
-- ------------------------------

DEFINE FUNCTION fn::app_price_per_minute($app_id: record) {
LET $app = (SELECT * FROM $app_id)[0];
RETURN (($app.vcpus * 5) + ($app.memory_mib / 200) + ($app.disk_size_mib / 10)) * $app.price_per_unit;
} PERMISSIONS FULL;
DEFINE FUNCTION fn::delete_app($app_id: record) {
LET $app = (SELECT * FROM $app_id)[0];
LET $account = $app.in;
LET $deleted_app = $app.patch([{ op: 'replace', path: 'id', value: type::record('deleted_app:' + record::id($app.id)) }]);
IF $app.locked_nano >= 0 { UPDATE $account SET balance += $app.locked_nano; };
INSERT RELATION INTO deleted_app ($deleted_app);
RETURN (DELETE $app.id RETURN BEFORE);
} PERMISSIONS FULL;
DEFINE FUNCTION fn::delete_vm($vm_id: record) {
LET $vm = (SELECT * FROM $vm_id)[0];
LET $account = $vm.in;
LET $deleted_vm = $vm.patch([{ op: 'replace', path: 'id', value: type::record('deleted_vm:' + record::id($vm.id)) }]);
IF $vm.locked_nano >= 0 { UPDATE $account SET balance += $vm.locked_nano; };
INSERT RELATION INTO deleted_vm ($deleted_vm);
RETURN (DELETE $vm.id RETURN BEFORE);
} PERMISSIONS FULL;
DEFINE FUNCTION fn::vm_price_per_minute($vm_id: record) {
LET $vm = (SELECT * FROM $vm_id)[0];
LET $ip_price = IF $vm.public_ipv4.len() > 0 { 10 } ELSE { 0 };
RETURN (($vm.vcpus * 10) + (($vm.memory_mib + 256) / 200) + ($vm.disk_size_mib / 1024 / 10) + $ip_price) * $vm.price_per_unit;
} PERMISSIONS FULL;

-- ------------------------------
-- TABLE: account
-- ------------------------------

DEFINE TABLE account TYPE NORMAL SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD balance ON account TYPE int DEFAULT 0 PERMISSIONS FULL;
DEFINE FIELD email ON account TYPE string DEFAULT '' PERMISSIONS FULL;
DEFINE FIELD escrow ON account TYPE int DEFAULT 0 PERMISSIONS FULL;
DEFINE FIELD tmp_locked ON account TYPE int DEFAULT 0 PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: account
-- ------------------------------

INSERT [ { balance: 9978460000, email: '', escrow: 0, id: account:3BNggj8ZTsoSjfAGdPfmcU2Gobm2qcTEBg9iHXEUPe1t, tmp_locked: 0 }, { balance: 1670441080000, email: '', escrow: 0, id: account:45Pyv9hRfub43NyRrYv95MhZs1Wrm8sj3RhBvA3F1Bvr, tmp_locked: 0 }, { balance: 1076960680000, email: '', escrow: 0, id: account:49JBVzmgsQbUURHzAWax2gxo6jmukqbEQzP97YeeNQyu, tmp_locked: 0 }, { balance: 25949200000, email: 'test_mock@operator', escrow: 5499700480000, id: account:4qFJJJdRrSB9hCn8rrvYTXHLJg371ab36PJmZ4uxHjGQ, tmp_locked: 0 }, { balance: 3271040000, email: '', escrow: 888888888899999, id: account:7V3rEuh6j8VuwMVB5PyGqWKLmjJ4fYSv6WtrTL51NZTB, tmp_locked: 0 }, { balance: 99980200000, email: '', escrow: 0, id: account:B981xPHmHthfKr15J9uJ64qd9zt2KsdiEuDRR7UUCGWi, tmp_locked: 0 }, { balance: 4672207240000, email: 'first_on_detee@proton.me', escrow: 5096692000000, id: account:BFopWmwcZAMF1h2PFECZNdEucdZfnZZ32p6R9ZaBiVsS, tmp_locked: 0 }, { balance: 25000000000, email: '', escrow: 0, id: account:CLYyE6id5876DW69LHDynuH6TjJPvWRBTQC5XDZ6jfT1, tmp_locked: 0 }, { balance: 4794480000, email: '', escrow: 0, id: account:Cnkvn3WuHYfTzh1YK1TAv2VD25sNvstJNnQtxjcdQSL7, tmp_locked: 0 }, { balance: 20293420000, email: '', escrow: 0, id: account:DXXkYSnhP3ijsHYxkedcuMomEyc122WaAbkDX7SaGuUS, tmp_locked: 0 }, { balance: 74660380000, email: '', escrow: 0, id: account:DwfL5iFu32xh2YMCUxg63oEAThLRqehDAumiP9q6zuuX, tmp_locked: 0 }, { balance: 21121600000, email: '', escrow: 0, id: account:E3bgXsWvgichXeC6AqULJCZDp7FbEdTxBD67UaYVWf9y, tmp_locked: 0 }, { balance: 181560160000, email: '', escrow: 0, id: account:FBMWVqME3t1i4R6zWyDQGUuiTeruZ1TxLhTmhaEcFypZ, tmp_locked: 0 }, { balance: 25949200000, email: '', escrow: 0, id: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, tmp_locked: 0 }, { balance: 500000000000, email: '', escrow: 0, id: account:GmE4JH3bL4NpmzwKCBJemJzRTumJAnbcXLGqce5mREgS, tmp_locked: 0 }, { balance: 976000000, email: '', escrow: 0, id: account:H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc, tmp_locked: 0 }, { balance: 979410300000, email: '', escrow: 0, id: account:HQyGWpiteHbxjszngZvmiX7ZFZAmF6nFjEraBa1M6bbM, tmp_locked: 0 }, { balance: 25949200000, email: 'test_mock_extend@operator', escrow: 5499700480000, id: account:Hv5q3enK249RUnLRLi9YNQMrPCRxvL2XnhznkzrtCmkG, tmp_locked: 0 }, { balance: 25000000000, email: '', escrow: 0, id: account:db5ZB6uDbF1mUUgeggBZ9XKbi3mUfX6WHkBpbwUHJpB, tmp_locked: 0 }, { balance: 1000000000, email: '', escrow: 0, id: account:fY3NNjvFTeR1FBh5nXV3ujX7zZqrm3eBUWGEiG75TK1, tmp_locked: 0 }, { balance: 554454460000, email: 'gheo@detee.ltd', escrow: 5499700480000, id: account:x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK, tmp_locked: 547200000 } ];

-- ------------------------------
-- TABLE: active_app
-- ------------------------------

DEFINE TABLE active_app TYPE RELATION IN account OUT app_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD app_name ON active_app TYPE string PERMISSIONS FULL;
DEFINE FIELD collected_at ON active_app TYPE datetime PERMISSIONS FULL;
DEFINE FIELD created_at ON active_app TYPE datetime PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON active_app TYPE int PERMISSIONS FULL;
DEFINE FIELD host_ipv4 ON active_app TYPE string PERMISSIONS FULL;
DEFINE FIELD hratls_pubkey ON active_app TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON active_app TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD locked_nano ON active_app TYPE int PERMISSIONS FULL;
DEFINE FIELD mapped_ports ON active_app TYPE array<[int, int]> PERMISSIONS FULL;
DEFINE FIELD mapped_ports[*] ON active_app TYPE [int, int] PERMISSIONS FULL;
DEFINE FIELD memory_mib ON active_app TYPE int PERMISSIONS FULL;
DEFINE FIELD mr_enclave ON active_app TYPE string PERMISSIONS FULL;
DEFINE FIELD out ON active_app TYPE record<app_node> PERMISSIONS FULL;
DEFINE FIELD package_url ON active_app TYPE string PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON active_app TYPE int PERMISSIONS FULL;
DEFINE FIELD vcpus ON active_app TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: active_app
-- ------------------------------

INSERT RELATION [ { __: true, app_name: 'diligent-seahorse', collected_at: d'2025-04-21T11:28:24.905665571Z', created_at: d'2025-04-21T11:27:28.833236909Z', disk_size_mib: 1000, host_ipv4: '212.95.45.139', hratls_pubkey: '[REDACTED]', id: active_app:e3d01f252b2a410b80e312f44e474334, in: account:H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc, locked_nano: 121200000, mapped_ports: [[27158, 34500], [28667, 8080]], memory_mib: 1000, mr_enclave: '34B766D2FBDBDA8CA8760AC162F0937CF0BD2E5F8AAC0FF6E372469FE8D409EA', out: app_node:BiqoPUEoAxYxMRXUmyofoS9H1TBQgQqvLJ6MbWh88AQg, package_url: 'https://registry.detee.ltd/sgx/packages/base_package_2025-04-17_11-01-08.tar.gz', price_per_unit: 200000, vcpus: 1 } ];

-- ------------------------------
-- TABLE: active_vm
-- ------------------------------

DEFINE TABLE active_vm TYPE RELATION IN account OUT vm_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD collected_at ON active_vm TYPE datetime PERMISSIONS FULL;
DEFINE FIELD created_at ON active_vm TYPE datetime PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON active_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD dtrfs_sha ON active_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD hostname ON active_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON active_vm TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD kernel_sha ON active_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD locked_nano ON active_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD mapped_ports ON active_vm TYPE array<[int, int]> PERMISSIONS FULL;
DEFINE FIELD mapped_ports[*] ON active_vm TYPE [int, int] PERMISSIONS FULL;
DEFINE FIELD memory_mib ON active_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD out ON active_vm TYPE record<vm_node> PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON active_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD public_ipv4 ON active_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD public_ipv6 ON active_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD vcpus ON active_vm TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: active_vm
-- ------------------------------

INSERT RELATION [ { __: true, collected_at: d'2025-04-20T00:34:15.461211040Z', created_at: d'2025-04-07T22:57:57.646151746Z', disk_size_mib: 10485760, dtrfs_sha: 'b5f408d00e2b93dc594fed3a7f2466a9878802ff1c7ae502247471cd06728a45', hostname: 'sofenty', id: active_vm:16577f1c98674a1780a86cf0490f1270, in: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, kernel_sha: 'e49c8587287b21df7600c04326fd7393524453918c14d67f73757dc769a13542', locked_nano: 11867500000, mapped_ports: [], memory_mib: 3000, out: vm_node:Du3UfPSUUZmA5thQmc9Vrxdy7UimpygcpDsQNnwRQPtu, price_per_unit: 20000, public_ipv4: '156.146.63.217', public_ipv6: '', vcpus: 2 }, { __: true, collected_at: d'2025-04-20T00:34:15.461240342Z', created_at: d'2025-04-16T20:37:57.176592933Z', disk_size_mib: 10485760, dtrfs_sha: 'd207644ee60d54009b6ecdfb720e2ec251cde31774dd249fcc7435aca0377990', hostname: 'shadowy-hobo', id: active_vm:1bf3630937744825b023b2a0ef0405ed, in: account:x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK, kernel_sha: 'e765e56166ef321b53399b9638584d1279821dbe3d46191c1f66bbaa075e7919', locked_nano: 12730960000, mapped_ports: [[46393, 8080]], memory_mib: 1000, out: vm_node:3zRxiGRnf46vd3zAEmpaYBJocTV9oJB6yXf5GZFR1Sq4, price_per_unit: 20000, public_ipv4: '', public_ipv6: '', vcpus: 1 }, { __: true, collected_at: d'2025-04-20T00:34:15.461240342Z', created_at: d'2025-04-16T20:37:57.176592933Z', disk_size_mib: 10485760, dtrfs_sha: 'd207644ee60d54009b6ecdfb720e2ec251cde31774dd249fcc7435aca0377990', hostname: 'test-extend', id: active_vm:1d749a816c27a22efa0e574b023a6afef040fe5, in: account:GmE4JH3bL4NpmzwKCBJemJzRTumJAnbcXLGqce5mREgS, kernel_sha: 'e765e56166ef321b53399b9638584d1279821dbe3d46191c1f66bbaa075e7919', locked_nano: 60000, mapped_ports: [[46393, 8080]], memory_mib: 1000, out: vm_node:8ue3VHMnJg2i8pwTQ6mJtvYuS2kd9n1XLLco8GUPfT95, price_per_unit: 20000, public_ipv4: '', public_ipv6: '', vcpus: 1 }, { __: true, collected_at: d'2025-04-20T00:34:15.461201690Z', created_at: d'2025-03-20T14:40:25.557753393Z', disk_size_mib: 10485760, dtrfs_sha: '0bb93443f65c9f4379ed469f94794f5c1bf14d8905b0b2c56a125df4a9ebe83e', hostname: 'astromech-wrench', id: active_vm:1f49a71cf68c4c64a82ef50e0ba0b574, in: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, kernel_sha: '3a68709138bed09c16671949cf1f03acee95a08381ba84fc70fb586001fa6767', locked_nano: 11865620000, mapped_ports: [], memory_mib: 3000, out: vm_node:DgkbsrwttkZXvzxY5kDwQQoDd79GLmZ5tc7fYJUFkQQb, price_per_unit: 20000, public_ipv4: '149.22.95.2', public_ipv6: '', vcpus: 2 }, { __: true, collected_at: d'2025-04-20T00:34:15.461191231Z', created_at: d'2025-03-12T16:28:24.749161605Z', disk_size_mib: 10485760, dtrfs_sha: '03ce24dbbe917fdd4f6347e61036805ddbdded5044c272bab188ef9333093bee', hostname: 'heroic-door', id: active_vm:2309440623074332a642acee718d0186, in: account:DwfL5iFu32xh2YMCUxg63oEAThLRqehDAumiP9q6zuuX, kernel_sha: '14e225e4aaf84cc2e0b5f64206121186ddebc4b378b886da3b2f7515dfd41692', locked_nano: 14134140000, mapped_ports: [[38288, 8080]], memory_mib: 1000, out: vm_node:7Xw3RxbP5pvfjZ8U6yA3HHVSS9YXjKH5Vkas3JRbQYd9, price_per_unit: 20000, public_ipv4: '', public_ipv6: '', vcpus: 1 }, { __: true, collected_at: d'2025-04-20T00:34:15.461219779Z', created_at: d'2025-04-12T13:44:56.957037550Z', disk_size_mib: 30720, dtrfs_sha: 'b5f408d00e2b93dc594fed3a7f2466a9878802ff1c7ae502247471cd06728a45', hostname: 'cloaked-mailbox', id: active_vm:4b6e25ca87ac478b8f16aa8f5c44c704, in: account:DwfL5iFu32xh2YMCUxg63oEAThLRqehDAumiP9q6zuuX, kernel_sha: 'e49c8587287b21df7600c04326fd7393524453918c14d67f73757dc769a13542', locked_nano: 11177760000, mapped_ports: [], memory_mib: 1000, out: vm_node:DgkbsrwttkZXvzxY5kDwQQoDd79GLmZ5tc7fYJUFkQQb, price_per_unit: 20000, public_ipv4: '149.22.95.2', public_ipv6: '', vcpus: 1 }, { __: true, collected_at: d'2025-04-20T00:34:15.461240342Z', created_at: d'2025-04-16T20:37:57.176592933Z', disk_size_mib: 10485760, dtrfs_sha: 'd207644ee60d54009b6ecdfb720e2ec251cde31774dd249fcc7435aca0377990', hostname: 'hallow-hobo', id: active_vm:5af49a714c64a82ef50e574b023b2a0ef0405ed, in: account:4qFJJJdRrSB9hCn8rrvYTXHLJg371ab36PJmZ4uxHjGQ, kernel_sha: 'e765e56166ef321b53399b9638584d1279821dbe3d46191c1f66bbaa075e7919', locked_nano: 12730960000, mapped_ports: [[46393, 8080]], memory_mib: 1000, out: vm_node:7fujZQeTme52RdXTLmQST5jBgAbvzic5iERtH5EWoYjk, price_per_unit: 20000, public_ipv4: '', public_ipv6: '', vcpus: 1 }, { __: true, collected_at: d'2025-04-20T00:34:15.461165181Z', created_at: d'2025-02-28T23:19:41.769423466Z', disk_size_mib: 10485760, dtrfs_sha: '3f6b3e5740f249eedfb2f7248c521a551be8b2676f7fcb040f3f3bc840a5004b', hostname: 'detee-landing-fr', id: active_vm:958165e3dea8407d8c42dd17002ef79c, in: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, kernel_sha: '3ec4fc5aa5729f515967ec71be4a851622785c0080f7191b1b07717149840151', locked_nano: 14875500000, mapped_ports: [], memory_mib: 3000, out: vm_node:Du3UfPSUUZmA5thQmc9Vrxdy7UimpygcpDsQNnwRQPtu, price_per_unit: 20000, public_ipv4: '156.146.63.216', public_ipv6: '', vcpus: 2 }, { __: true, collected_at: d'2025-04-20T00:34:15.461181545Z', created_at: d'2025-03-06T19:51:39.595163157Z', disk_size_mib: 10485760, dtrfs_sha: '3f6b3e5740f249eedfb2f7248c521a551be8b2676f7fcb040f3f3bc840a5004b', hostname: 'detee-landing-gb', id: active_vm:e807a2fdcf904a14bc3a89ce6dc59033, in: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, kernel_sha: '3ec4fc5aa5729f515967ec71be4a851622785c0080f7191b1b07717149840151', locked_nano: 14875500000, mapped_ports: [], memory_mib: 3000, out: vm_node:7Xw3RxbP5pvfjZ8U6yA3HHVSS9YXjKH5Vkas3JRbQYd9, price_per_unit: 20000, public_ipv4: '173.234.136.154', public_ipv6: '', vcpus: 2 }, { __: true, collected_at: d'2025-04-20T00:34:15.461230948Z', created_at: d'2025-04-15T00:46:35.622165457Z', disk_size_mib: 10485760, dtrfs_sha: 'b5f408d00e2b93dc594fed3a7f2466a9878802ff1c7ae502247471cd06728a45', hostname: 'twenty', id: active_vm:eb1a13edd7824b71886073540129cb7d, in: account:FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL, kernel_sha: 'e49c8587287b21df7600c04326fd7393524453918c14d67f73757dc769a13542', locked_nano: 15570720000, mapped_ports: [], memory_mib: 4000, out: vm_node:3zRxiGRnf46vd3zAEmpaYBJocTV9oJB6yXf5GZFR1Sq4, price_per_unit: 20000, public_ipv4: '149.36.48.100', public_ipv6: '', vcpus: 4 } ];

-- ------------------------------
-- TABLE: app_node
-- ------------------------------

DEFINE TABLE app_node TYPE NORMAL SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD avail_mem_mib ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_ports ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_storage_mib ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_vcpus ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD city ON app_node TYPE string PERMISSIONS FULL;
DEFINE FIELD connected_at ON app_node TYPE datetime PERMISSIONS FULL;
DEFINE FIELD country ON app_node TYPE string PERMISSIONS FULL;
DEFINE FIELD disconnected_at ON app_node TYPE datetime PERMISSIONS FULL;
DEFINE FIELD ip ON app_node TYPE string PERMISSIONS FULL;
DEFINE FIELD max_ports_per_app ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD operator ON app_node TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD price ON app_node TYPE int PERMISSIONS FULL;
DEFINE FIELD pub_sub_node ON app_node TYPE string DEFAULT '127.0.0.1:31337' PERMISSIONS FULL;
DEFINE FIELD region ON app_node TYPE string PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: app_node
-- ------------------------------

INSERT [ { avail_mem_mib: 16000, avail_ports: 20000, avail_storage_mib: 200000, avail_vcpus: 16, city: 'Frankfurt am Main', connected_at: d'2025-07-06T15:14:33.228406616Z', country: 'DE', disconnected_at: d'2025-07-06T15:14:33.228406558Z', id: app_node:BiqoPUEoAxYxMRXUmyofoS9H1TBQgQqvLJ6MbWh88AQg, ip: '212.95.45.139', max_ports_per_app: 9, operator: account:7V3rEuh6j8VuwMVB5PyGqWKLmjJ4fYSv6WtrTL51NZTB, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'Hesse' } ];

-- ------------------------------
-- TABLE: ban
-- ------------------------------

DEFINE TABLE ban TYPE RELATION IN account OUT account SCHEMALESS PERMISSIONS NONE;

DEFINE FIELD created_at ON ban TYPE datetime DEFAULT time::now() PERMISSIONS FULL;
DEFINE FIELD in ON ban TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD out ON ban TYPE record<account> PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: ban
-- ------------------------------


-- ------------------------------
-- TABLE: deleted_app
-- ------------------------------

DEFINE TABLE deleted_app TYPE RELATION IN account OUT app_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD app_name ON deleted_app TYPE string PERMISSIONS FULL;
DEFINE FIELD created_at ON deleted_app TYPE datetime PERMISSIONS FULL;
DEFINE FIELD deleted_at ON deleted_app TYPE datetime DEFAULT time::now() PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON deleted_app TYPE int PERMISSIONS FULL;
DEFINE FIELD host_ipv4 ON deleted_app TYPE string PERMISSIONS FULL;
DEFINE FIELD hratls_pubkey ON deleted_app TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON deleted_app TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD mapped_ports ON deleted_app TYPE array<[int, int]> PERMISSIONS FULL;
DEFINE FIELD mapped_ports[*] ON deleted_app TYPE [int, int] PERMISSIONS FULL;
DEFINE FIELD memory_mib ON deleted_app TYPE int PERMISSIONS FULL;
DEFINE FIELD mr_enclave ON deleted_app TYPE string PERMISSIONS FULL;
DEFINE FIELD out ON deleted_app TYPE record<app_node> PERMISSIONS FULL;
DEFINE FIELD package_url ON deleted_app TYPE string PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON deleted_app TYPE int PERMISSIONS FULL;
DEFINE FIELD vcpus ON deleted_app TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: deleted_app
-- ------------------------------


-- ------------------------------
-- TABLE: deleted_vm
-- ------------------------------

DEFINE TABLE deleted_vm TYPE RELATION IN account OUT vm_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD created_at ON deleted_vm TYPE datetime PERMISSIONS FULL;
DEFINE FIELD deleted_at ON deleted_vm TYPE datetime DEFAULT time::now() PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON deleted_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD dtrfs_sha ON deleted_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD hostname ON deleted_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON deleted_vm TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD kernel_sha ON deleted_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD mapped_ports ON deleted_vm TYPE array<[int, int]> PERMISSIONS FULL;
DEFINE FIELD mapped_ports[*] ON deleted_vm TYPE [int, int] PERMISSIONS FULL;
DEFINE FIELD memory_mib ON deleted_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD out ON deleted_vm TYPE record<vm_node> PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON deleted_vm TYPE int PERMISSIONS FULL;
DEFINE FIELD public_ipv4 ON deleted_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD public_ipv6 ON deleted_vm TYPE string PERMISSIONS FULL;
DEFINE FIELD vcpus ON deleted_vm TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: deleted_vm
-- ------------------------------


-- ------------------------------
-- TABLE: kick
-- ------------------------------

DEFINE TABLE kick TYPE RELATION IN account OUT account SCHEMALESS PERMISSIONS NONE;

DEFINE FIELD contract ON kick TYPE record<deleted_vm | deleted_app> PERMISSIONS FULL;
DEFINE FIELD created_at ON kick TYPE datetime PERMISSIONS FULL;
DEFINE FIELD in ON kick TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD node ON kick TYPE record<vm_node | app_node> PERMISSIONS FULL;
DEFINE FIELD out ON kick TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD reason ON kick TYPE string PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: kick
-- ------------------------------


-- ------------------------------
-- TABLE: new_app_req
-- ------------------------------

DEFINE TABLE new_app_req TYPE RELATION IN account OUT app_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD app_name ON new_app_req TYPE string PERMISSIONS FULL;
DEFINE FIELD created_at ON new_app_req TYPE datetime PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON new_app_req TYPE int PERMISSIONS FULL;
DEFINE FIELD error ON new_app_req TYPE string PERMISSIONS FULL;
DEFINE FIELD hratls_pubkey ON new_app_req TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON new_app_req TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD locked_nano ON new_app_req TYPE int PERMISSIONS FULL;
DEFINE FIELD memory_mib ON new_app_req TYPE int PERMISSIONS FULL;
DEFINE FIELD mr_enclave ON new_app_req TYPE string PERMISSIONS FULL;
DEFINE FIELD out ON new_app_req TYPE record<app_node> PERMISSIONS FULL;
DEFINE FIELD package_url ON new_app_req TYPE string PERMISSIONS FULL;
DEFINE FIELD ports ON new_app_req TYPE array<int> PERMISSIONS FULL;
DEFINE FIELD ports[*] ON new_app_req TYPE int PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON new_app_req TYPE int PERMISSIONS FULL;
DEFINE FIELD vcpus ON new_app_req TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: new_app_req
-- ------------------------------


-- ------------------------------
-- TABLE: new_vm_req
-- ------------------------------

DEFINE TABLE new_vm_req TYPE RELATION IN account OUT vm_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD created_at ON new_vm_req TYPE datetime PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON new_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD dtrfs_sha ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD dtrfs_url ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD error ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD extra_ports ON new_vm_req TYPE array<int> PERMISSIONS FULL;
DEFINE FIELD extra_ports[*] ON new_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD hostname ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON new_vm_req TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD kernel_sha ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD kernel_url ON new_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD locked_nano ON new_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD memory_mib ON new_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD out ON new_vm_req TYPE record<vm_node> PERMISSIONS FULL;
DEFINE FIELD price_per_unit ON new_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD public_ipv4 ON new_vm_req TYPE bool PERMISSIONS FULL;
DEFINE FIELD public_ipv6 ON new_vm_req TYPE bool PERMISSIONS FULL;
DEFINE FIELD vcpus ON new_vm_req TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: new_vm_req
-- ------------------------------


-- ------------------------------
-- TABLE: report
-- ------------------------------

DEFINE TABLE report TYPE RELATION IN account OUT vm_node | app_node SCHEMALESS PERMISSIONS NONE;

DEFINE FIELD contract_id ON report TYPE string PERMISSIONS FULL;
DEFINE FIELD created_at ON report TYPE datetime PERMISSIONS FULL;
DEFINE FIELD in ON report TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD out ON report TYPE record<vm_node | app_node> PERMISSIONS FULL;
DEFINE FIELD reason ON report TYPE string PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: report
-- ------------------------------


-- ------------------------------
-- TABLE: update_vm_req
-- ------------------------------

DEFINE TABLE update_vm_req TYPE RELATION IN account OUT vm_node SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD created_at ON update_vm_req TYPE datetime PERMISSIONS FULL;
DEFINE FIELD disk_size_mib ON update_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD dtrfs_sha ON update_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD dtrfs_url ON update_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD error ON update_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD in ON update_vm_req TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD kernel_sha ON update_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD kernel_url ON update_vm_req TYPE string PERMISSIONS FULL;
DEFINE FIELD memory_mib ON update_vm_req TYPE int PERMISSIONS FULL;
DEFINE FIELD out ON update_vm_req TYPE record<vm_node> PERMISSIONS FULL;
DEFINE FIELD vcpus ON update_vm_req TYPE int PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: update_vm_req
-- ------------------------------


-- ------------------------------
-- TABLE: vm_node
-- ------------------------------

DEFINE TABLE vm_node TYPE NORMAL SCHEMAFULL PERMISSIONS NONE;

DEFINE FIELD avail_ipv4 ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_ipv6 ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_mem_mib ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_ports ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_storage_mib ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD avail_vcpus ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD city ON vm_node TYPE string PERMISSIONS FULL;
DEFINE FIELD connected_at ON vm_node TYPE datetime PERMISSIONS FULL;
DEFINE FIELD country ON vm_node TYPE string PERMISSIONS FULL;
DEFINE FIELD disconnected_at ON vm_node TYPE datetime PERMISSIONS FULL;
DEFINE FIELD ip ON vm_node TYPE string PERMISSIONS FULL;
DEFINE FIELD max_ports_per_vm ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD operator ON vm_node TYPE record<account> PERMISSIONS FULL;
DEFINE FIELD price ON vm_node TYPE int PERMISSIONS FULL;
DEFINE FIELD pub_sub_node ON vm_node TYPE string DEFAULT '127.0.0.1:31337' PERMISSIONS FULL;
DEFINE FIELD region ON vm_node TYPE string PERMISSIONS FULL;



-- ------------------------------
-- TABLE DATA: vm_node
-- ------------------------------

INSERT [ { avail_ipv4: 0, avail_ipv6: 0, avail_mem_mib: 30000, avail_ports: 20000, avail_storage_mib: 716800, avail_vcpus: 31, city: 'Montréal', connected_at: d'2025-07-06T15:14:33.228403298Z', country: 'CA', disconnected_at: d'2025-07-06T15:14:33.228403224Z', id: vm_node:2Uf5pxhxKTUm6gRMnpbJHYDuyA6BWUfFsdmPyWfbMV1f, ip: '184.107.169.199', max_ports_per_vm: 5, operator: account:x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK, price: 18000, pub_sub_node: '127.0.0.1:31337', region: 'Quebec' }, { avail_ipv4: 23, avail_ipv6: 0, avail_mem_mib: 120000, avail_ports: 19999, avail_storage_mib: 399360, avail_vcpus: 41, city: 'San Jose', connected_at: d'2025-07-06T15:14:33.228404068Z', country: 'US', disconnected_at: d'2025-07-06T15:14:33.228404010Z', id: vm_node:3zRxiGRnf46vd3zAEmpaYBJocTV9oJB6yXf5GZFR1Sq4, ip: '149.36.48.99', max_ports_per_vm: 5, operator: account:BFopWmwcZAMF1h2PFECZNdEucdZfnZZ32p6R9ZaBiVsS, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'California' }, { avail_ipv4: 2, avail_ipv6: 65516, avail_mem_mib: 26000, avail_ports: 19999, avail_storage_mib: 696320, avail_vcpus: 28, city: 'London', connected_at: d'2025-07-06T15:14:33.228401491Z', country: 'GB', disconnected_at: d'2025-07-06T15:14:33.228399092Z', id: vm_node:7Xw3RxbP5pvfjZ8U6yA3HHVSS9YXjKH5Vkas3JRbQYd9, ip: '173.234.17.2', max_ports_per_vm: 5, operator: account:x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'England' }, { avail_ipv4: 1, avail_ipv6: 0, avail_mem_mib: 28000, avail_ports: 19999, avail_storage_mib: 1720320, avail_vcpus: 24, city: 'London', connected_at: d'2025-07-06T15:14:33.228405169Z', country: 'GB', disconnected_at: d'2025-07-06T15:14:33.228405115Z', id: vm_node:7fujZQeTme52RdXTLmQST5jBgAbvzic5iERtH5EWoYjk, ip: '193.234.17.2', max_ports_per_vm: 10, operator: account:4qFJJJdRrSB9hCn8rrvYTXHLJg371ab36PJmZ4uxHjGQ, price: 24000, pub_sub_node: '127.0.0.1:31337', region: 'England' }, { avail_ipv4: 1, avail_ipv6: 0, avail_mem_mib: 28000, avail_ports: 19999, avail_storage_mib: 1720320, avail_vcpus: 24, city: 'London', connected_at: d'2025-07-06T15:14:33.228405526Z', country: 'GB', disconnected_at: d'2025-07-06T15:14:33.228405472Z', id: vm_node:8ue3VHMnJg2i8pwTQ6mJtvYuS2kd9n1XLLco8GUPfT95, ip: '193.234.17.2', max_ports_per_vm: 10, operator: account:Hv5q3enK249RUnLRLi9YNQMrPCRxvL2XnhznkzrtCmkG, price: 24000, pub_sub_node: '127.0.0.1:31337', region: 'England' }, { avail_ipv4: 25, avail_ipv6: 0, avail_mem_mib: 109000, avail_ports: 20000, avail_storage_mib: 409600, avail_vcpus: 45, city: 'Vancouver', connected_at: d'2025-07-06T15:14:33.228403686Z', country: 'CA', disconnected_at: d'2025-07-06T15:14:33.228403612Z', id: vm_node:DgkbsrwttkZXvzxY5kDwQQoDd79GLmZ5tc7fYJUFkQQb, ip: '149.22.95.1', max_ports_per_vm: 5, operator: account:BFopWmwcZAMF1h2PFECZNdEucdZfnZZ32p6R9ZaBiVsS, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'British Columbia' }, { avail_ipv4: 2, avail_ipv6: 0, avail_mem_mib: 123000, avail_ports: 20000, avail_storage_mib: 450560, avail_vcpus: 46, city: 'Paris', connected_at: d'2025-07-06T15:14:33.228402681Z', country: 'FR', disconnected_at: d'2025-07-06T15:14:33.228402603Z', id: vm_node:Du3UfPSUUZmA5thQmc9Vrxdy7UimpygcpDsQNnwRQPtu, ip: '156.146.63.215', max_ports_per_vm: 5, operator: account:BFopWmwcZAMF1h2PFECZNdEucdZfnZZ32p6R9ZaBiVsS, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'Île-de-France' }, { avail_ipv4: 26, avail_ipv6: 0, avail_mem_mib: 125000, avail_ports: 20000, avail_storage_mib: 409600, avail_vcpus: 46, city: 'Vancouver', connected_at: d'2025-07-06T15:14:33.228404815Z', country: 'CA', disconnected_at: d'2025-07-06T15:14:33.228404760Z', id: vm_node:HiyMp21zaBVbRCjDsD5hEjQnHeHv4e1gpUR6pVfHTKqv, ip: '149.22.95.28', max_ports_per_vm: 5, operator: account:BFopWmwcZAMF1h2PFECZNdEucdZfnZZ32p6R9ZaBiVsS, price: 20000, pub_sub_node: '127.0.0.1:31337', region: 'British Columbia' } ];

