From 4f87be9aee6f0f9b4feae56528af02b49cc5c7c9 Mon Sep 17 00:00:00 2001 From: ABCxFF <79597906+ABCxFF@users.noreply.github.com> Date: Wed, 14 Aug 2024 00:07:22 -0400 Subject: [PATCH] Setup analytics module and schema --- .../20240813023547_init/migration.sql | 16 ++++++++++++++++ .../analytics/db/migrations/migration_lock.toml | 3 +++ modules/analytics/db/schema.prisma | 17 +++++++++++++++++ modules/analytics/module.json | 15 +++++++++++++++ tests/basic/backend.json | 3 +++ 5 files changed, 54 insertions(+) create mode 100644 modules/analytics/db/migrations/20240813023547_init/migration.sql create mode 100644 modules/analytics/db/migrations/migration_lock.toml create mode 100644 modules/analytics/db/schema.prisma create mode 100644 modules/analytics/module.json diff --git a/modules/analytics/db/migrations/20240813023547_init/migration.sql b/modules/analytics/db/migrations/20240813023547_init/migration.sql new file mode 100644 index 00000000..ead4ba38 --- /dev/null +++ b/modules/analytics/db/migrations/20240813023547_init/migration.sql @@ -0,0 +1,16 @@ +-- Add timescale +CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; + +-- CreateTable +CREATE TABLE "event" ( + "id" UUID NOT NULL, + "timestamp" TIMESTAMPTZ NOT NULL, + "name" TEXT NOT NULL, + "metadata" JSONB +); + +-- CreateIndex +CREATE UNIQUE INDEX "event_id_key" ON "event"("id"); + +-- CreateIndex +CREATE INDEX "event_name_time_idx" ON "event"("name", "timestamp" DESC); \ No newline at end of file diff --git a/modules/analytics/db/migrations/migration_lock.toml b/modules/analytics/db/migrations/migration_lock.toml new file mode 100644 index 00000000..fbffa92c --- /dev/null +++ b/modules/analytics/db/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql" \ No newline at end of file diff --git a/modules/analytics/db/schema.prisma b/modules/analytics/db/schema.prisma new file mode 100644 index 00000000..989e1121 --- /dev/null +++ b/modules/analytics/db/schema.prisma @@ -0,0 +1,17 @@ +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") + +} + +model Event { + id String @db.Uuid @default(uuid()) @unique + timestamp DateTime @db.Timestamptz + name String + metadata Json? + + // in the init migration, we add the timescale extension and call create_hypertable(). + + @@index(fields: [name, timestamp(sort: Desc)], map: "event_name_time_idx") + @@map("event") +} diff --git a/modules/analytics/module.json b/modules/analytics/module.json new file mode 100644 index 00000000..0594c10a --- /dev/null +++ b/modules/analytics/module.json @@ -0,0 +1,15 @@ +{ + "status": "stable", + "name": "Analytics", + "description": "Track and visualize user behavior data", + "icon": "chart-line", + "tags": [], + "authors": [ + "rivet-gg", + "ABCxFF" + ], + "scripts": { + }, + "errors": {}, + "dependencies": {} +} \ No newline at end of file diff --git a/tests/basic/backend.json b/tests/basic/backend.json index 897a398e..fa86a5e2 100644 --- a/tests/basic/backend.json +++ b/tests/basic/backend.json @@ -7,6 +7,9 @@ } }, "modules": { + "analytics": { + "registry": "local" + }, "currency": { "registry": "local" },