nxtgauge-backend-rust/crates/db/migrations/20260608040000_ai_analytics.up.sql
Ashwin Kumar Sivakumar d48983ee21
Some checks failed
build-and-push / detect-changes (push) Successful in 6s
build-and-push / build (catering-services) (push) Failing after 5s
build-and-push / build (companies) (push) Failing after 6s
build-and-push / build (cron) (push) Failing after 5s
build-and-push / build (customers) (push) Failing after 5s
build-and-push / build (developers) (push) Failing after 5s
build-and-push / build (employees) (push) Successful in 11s
build-and-push / build (fitness-trainers) (push) Successful in 11s
build-and-push / build (graphic-designers) (push) Successful in 12s
build-and-push / build (gateway) (push) Successful in 25s
build-and-push / build (jobs) (push) Failing after 18s
build-and-push / build (leads) (push) Failing after 19s
build-and-push / build (makeup-artists) (push) Failing after 4s
build-and-push / build (payments) (push) Failing after 4s
build-and-push / build (photographers) (push) Failing after 18s
build-and-push / build (job-seekers) (push) Failing after 1m30s
build-and-push / build (tutors) (push) Failing after 5s
build-and-push / build (social-media-managers) (push) Failing after 20s
build-and-push / build (ugc-content-creators) (push) Failing after 5s
build-and-push / build (users) (push) Failing after 6s
build-and-push / build (video-editors) (push) Failing after 5s
feat(ai): Phase 4 - multilingual, voice, A/B testing, analytics (with stubs)
2026-06-08 06:41:10 +05:30

54 lines
2.1 KiB
PL/PgSQL

-- Phase 4: Analytics rollup tables + per-user AI prefs + KB article views.
-- The aggregator job in ai_phase4::aggregate_daily_stats rolls up
-- ai_conversations into ai_daily_stats every 5 minutes.
BEGIN;
-- Daily rollup: one row per (date, user, persona, pillar, intent).
-- All metrics come from ai_conversations + ai_feedback, never the raw rows.
CREATE TABLE IF NOT EXISTS ai_daily_stats (
date DATE NOT NULL,
user_id UUID,
persona TEXT,
pillar TEXT,
intent TEXT,
request_count INTEGER NOT NULL DEFAULT 0,
kb_match_count INTEGER NOT NULL DEFAULT 0,
ollama_calls INTEGER NOT NULL DEFAULT 0,
fallback_count INTEGER NOT NULL DEFAULT 0,
avg_response_ms INTEGER,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (date, user_id, persona, pillar, intent)
);
CREATE INDEX IF NOT EXISTS idx_ai_daily_stats_date
ON ai_daily_stats (date DESC);
-- Per-user AI preferences (e.g. preferred model, language override).
-- Kept small and simple; one row per user.
CREATE TABLE IF NOT EXISTS ai_user_prefs (
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
preferred_model TEXT,
preferred_language VARCHAR(8) NOT NULL DEFAULT 'en',
-- Bump when this user changes their prefs so the clients can re-fetch.
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Track which KB articles users actually look at after Ask Ash surfaces
-- them. Used by the enhanced KB matcher for recency boosting.
CREATE TABLE IF NOT EXISTS ai_article_views (
id BIGSERIAL PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
-- kb_articles.id is UUID.
article_id UUID NOT NULL REFERENCES kb_articles(id) ON DELETE CASCADE,
helpful BOOLEAN,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_ai_article_views_article
ON ai_article_views (article_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_article_views_user
ON ai_article_views (user_id, created_at DESC);
COMMIT;