From 0234168c1718e3b08d219259708aa10b21c6b346 Mon Sep 17 00:00:00 2001 From: Robert Jansen Date: Sat, 28 Dec 2024 18:45:01 +0000 Subject: [PATCH] add view for telemetry panels to have latest ids --- migrations/0002_stale_maelstrom.sql | 1 + migrations/meta/0002_snapshot.json | 500 ++++++++++++++++++++++++++++ migrations/meta/_journal.json | 7 + src/schema.ts | 20 +- 4 files changed, 526 insertions(+), 2 deletions(-) create mode 100644 migrations/0002_stale_maelstrom.sql create mode 100644 migrations/meta/0002_snapshot.json diff --git a/migrations/0002_stale_maelstrom.sql b/migrations/0002_stale_maelstrom.sql new file mode 100644 index 0000000..09ab6d0 --- /dev/null +++ b/migrations/0002_stale_maelstrom.sql @@ -0,0 +1 @@ +CREATE VIEW "public"."telemetry_panels_with_latest" AS (select "telemetry_panels"."id", "telemetry_panels"."created", "telemetry_panels"."last_update", "latest"."panel_id", "latest_telemetry_data_id" from "telemetry_panels" left join (select "panel_id", max("id") as "latest_telemetry_data_id" from "telemetry_data" group by "telemetry_data"."panel_id") "latest" on "telemetry_panels"."id" = latest.panel_id); \ No newline at end of file diff --git a/migrations/meta/0002_snapshot.json b/migrations/meta/0002_snapshot.json new file mode 100644 index 0000000..1b746f5 --- /dev/null +++ b/migrations/meta/0002_snapshot.json @@ -0,0 +1,500 @@ +{ + "id": "a958b715-472c-4548-85f9-ef91eb16bcf1", + "prevId": "25333ba6-d4c7-46d4-8584-eec1b98b1717", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.advent_calendar": { + "name": "advent_calendar", + "schema": "", + "columns": { + "extension_id": { + "name": "extension_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "day": { + "name": "day", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "year": { + "name": "year", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "advent_calendar_extension_id_extensions_id_fk": { + "name": "advent_calendar_extension_id_extensions_id_fk", + "tableFrom": "advent_calendar", + "tableTo": "extensions", + "columnsFrom": [ + "extension_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "advent_calendar_pk": { + "name": "advent_calendar_pk", + "columns": [ + "day", + "year" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.authors": { + "name": "authors", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "website": { + "name": "website", + "type": "varchar(63)", + "primaryKey": false, + "notNull": false + }, + "key": { + "name": "key", + "type": "char(32)", + "primaryKey": false, + "notNull": true, + "default": "md5(random()::text)" + }, + "created": { + "name": "created", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "authors_name_idx": { + "name": "authors_name_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "authors_key_idx": { + "name": "authors_key_idx", + "columns": [ + { + "expression": "key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.extensions": { + "name": "extensions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "extension_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "hidden": { + "name": "hidden", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "pending": { + "name": "pending", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "identifier": { + "name": "identifier", + "type": "varchar(63)", + "primaryKey": false, + "notNull": true + }, + "summary": { + "name": "summary", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "platforms": { + "name": "platforms", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "banner": { + "name": "banner", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "created": { + "name": "created", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "extensions_name_idx": { + "name": "extensions_name_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "extensions_identifier_idx": { + "name": "extensions_identifier_idx", + "columns": [ + { + "expression": "identifier", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "extensions_author_id_authors_id_fk": { + "name": "extensions_author_id_authors_id_fk", + "tableFrom": "extensions", + "tableTo": "authors", + "columnsFrom": [ + "author_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.telemetry_data": { + "name": "telemetry_data", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "panel_id": { + "name": "panel_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "telemetry_version": { + "name": "telemetry_version", + "type": "smallint", + "primaryKey": false, + "notNull": true + }, + "data": { + "name": "data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "ip": { + "name": "ip", + "type": "char(64)", + "primaryKey": false, + "notNull": true + }, + "continent": { + "name": "continent", + "type": "char(2)", + "primaryKey": false, + "notNull": false + }, + "country": { + "name": "country", + "type": "char(2)", + "primaryKey": false, + "notNull": false + }, + "created": { + "name": "created", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "telemetry_data_panel_id_idx": { + "name": "telemetry_data_panel_id_idx", + "columns": [ + { + "expression": "panel_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "telemetry_data_data_idx": { + "name": "telemetry_data_data_idx", + "columns": [ + { + "expression": "data", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "telemetry_data_ip_idx": { + "name": "telemetry_data_ip_idx", + "columns": [ + { + "expression": "ip", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "telemetry_data_continent_idx": { + "name": "telemetry_data_continent_idx", + "columns": [ + { + "expression": "continent", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "telemetry_data_country_idx": { + "name": "telemetry_data_country_idx", + "columns": [ + { + "expression": "country", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "telemetry_data_created_idx": { + "name": "telemetry_data_created_idx", + "columns": [ + { + "expression": "created", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "telemetry_data_panel_id_telemetry_panels_id_fk": { + "name": "telemetry_data_panel_id_telemetry_panels_id_fk", + "tableFrom": "telemetry_data", + "tableTo": "telemetry_panels", + "columnsFrom": [ + "panel_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.telemetry_panels": { + "name": "telemetry_panels", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "created": { + "name": "created", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "last_update": { + "name": "last_update", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.extension_type": { + "name": "extension_type", + "schema": "public", + "values": [ + "THEME", + "EXTENSION" + ] + } + }, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": { + "public.telemetry_panels_with_latest": { + "columns": {}, + "definition": "select \"telemetry_panels\".\"id\", \"telemetry_panels\".\"created\", \"telemetry_panels\".\"last_update\", \"latest\".\"panel_id\", \"latest_telemetry_data_id\" from \"telemetry_panels\" left join (select \"panel_id\", max(\"id\") as \"latest_telemetry_data_id\" from \"telemetry_data\" group by \"telemetry_data\".\"panel_id\") \"latest\" on \"telemetry_panels\".\"id\" = latest.panel_id", + "name": "telemetry_panels_with_latest", + "schema": "public", + "isExisting": false, + "materialized": false + } + }, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/migrations/meta/_journal.json b/migrations/meta/_journal.json index 0e59210..525412f 100644 --- a/migrations/meta/_journal.json +++ b/migrations/meta/_journal.json @@ -15,6 +15,13 @@ "when": 1734018063049, "tag": "0001_gifted_rafael_vega", "breakpoints": true + }, + { + "idx": 2, + "version": "7", + "when": 1735411095772, + "tag": "0002_stale_maelstrom", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/schema.ts b/src/schema.ts index 5e7f973..5bd0747 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1,5 +1,5 @@ -import { sql } from "drizzle-orm" -import { integer, pgTable, varchar, uniqueIndex, pgEnum, serial, timestamp, jsonb, text, boolean, primaryKey, char, index, uuid, smallint } from "drizzle-orm/pg-core" +import { eq, sql } from "drizzle-orm" +import { integer, pgTable, varchar, uniqueIndex, pgEnum, serial, timestamp, jsonb, text, boolean, primaryKey, char, index, uuid, smallint, pgView } from "drizzle-orm/pg-core" export const platforms = Object.freeze(['SOURCEXCHANGE', 'BUILTBYBIT', 'GITHUB'] as const) export const currency = Object.freeze(['USD', 'EUR'] as const) @@ -37,6 +37,22 @@ export const telemetryData = pgTable('telemetry_data', { index('telemetry_data_created_idx').on(telemetryData.created) ]) +export const telemetryPanelsWithLatest = pgView('telemetry_panels_with_latest') + .as((qb) => qb + .select() + .from(telemetryPanels) + .leftJoin( + qb.select({ + panelId: telemetryData.panelId, + latestTelemetryDataId: sql`max(${telemetryData.id})`.as('latest_telemetry_data_id') + }) + .from(telemetryData) + .groupBy(telemetryData.panelId) + .as('latest'), + eq(telemetryPanels.id, sql`latest.panel_id`) + ) + ) + export const authors = pgTable('authors', { id: serial('id').primaryKey().notNull(),