File size: 3,213 Bytes
611e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- =============================================================
-- Supabase Table Schemas for Analytics Storage
-- =============================================================
-- Run this SQL in the Supabase SQL Editor to mirror the SQLite
-- analytics schema (tool usage, red flags, RAG searches, queries)
-- =============================================================

CREATE TABLE IF NOT EXISTS tool_usage_events (
    id BIGSERIAL PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT,
    tool_name TEXT NOT NULL,
    "timestamp" BIGINT NOT NULL,
    latency_ms INTEGER,
    tokens_used INTEGER,
    success BOOLEAN DEFAULT TRUE,
    error_message TEXT,
    metadata JSONB
);

CREATE INDEX IF NOT EXISTS idx_tool_usage_tenant_timestamp
    ON tool_usage_events (tenant_id, "timestamp");


CREATE TABLE IF NOT EXISTS redflag_violations (
    id BIGSERIAL PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT,
    rule_id TEXT NOT NULL,
    rule_pattern TEXT,
    severity TEXT NOT NULL,
    matched_text TEXT,
    confidence DOUBLE PRECISION,
    message_preview TEXT,
    "timestamp" BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_redflag_tenant_timestamp
    ON redflag_violations (tenant_id, "timestamp");


CREATE TABLE IF NOT EXISTS rag_search_events (
    id BIGSERIAL PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    query TEXT NOT NULL,
    hits_count INTEGER,
    avg_score DOUBLE PRECISION,
    top_score DOUBLE PRECISION,
    "timestamp" BIGINT NOT NULL,
    latency_ms INTEGER
);

CREATE INDEX IF NOT EXISTS idx_rag_search_tenant_timestamp
    ON rag_search_events (tenant_id, "timestamp");


CREATE TABLE IF NOT EXISTS agent_query_events (
    id BIGSERIAL PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT,
    message_preview TEXT,
    intent TEXT,
    tools_used JSONB,
    total_tokens INTEGER,
    total_latency_ms INTEGER,
    success BOOLEAN DEFAULT TRUE,
    "timestamp" BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_agent_query_tenant_timestamp
    ON agent_query_events (tenant_id, "timestamp");


-- =============================================================
-- Optional: Enable Row Level Security and service-role policy
-- =============================================================
ALTER TABLE tool_usage_events ENABLE ROW LEVEL SECURITY;
ALTER TABLE redflag_violations ENABLE ROW LEVEL SECURITY;
ALTER TABLE rag_search_events ENABLE ROW LEVEL SECURITY;
ALTER TABLE agent_query_events ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Service role can manage tool usage"
    ON tool_usage_events FOR ALL
    USING (true) WITH CHECK (true);

CREATE POLICY "Service role can manage red flags"
    ON redflag_violations FOR ALL
    USING (true) WITH CHECK (true);

CREATE POLICY "Service role can manage rag searches"
    ON rag_search_events FOR ALL
    USING (true) WITH CHECK (true);

CREATE POLICY "Service role can manage agent queries"
    ON agent_query_events FOR ALL
    USING (true) WITH CHECK (true);

-- =============================================================
-- After running this script restart your FastAPI/MCP services
-- so they detect the Supabase analytics backend.
-- =============================================================