update ai insight migration for all page

This commit is contained in:
Alberto-Audrix committed 2026-09-17 10:11:14 +07:00
1 parent 07d36081ce
commit 18790ab8f6
69 files changed
+9457 -399

No files matched your search

Binary file not shown.

Before

Width:  |  Height:  |  Size: 332 KiB

After

Width:  |  Height:  |  Size: 331 KiB

+31 -17
View File
@@ -16,8 +16,9 @@
-- cycles.status: active | pending_close | closed (GM approves end_date)
-- cycles.close_requested_by_id → user_access (optional)
-- kandang → flock → iot_panel
-- cycles → ai_insight, feed_sacks, chicken_counting, chicken_weight,
-- manual_input, kpi
-- cycles → ai_insight (scoped by kandang/topic/report), feed_sacks,
-- chicken_counting, chicken_weight, manual_input, kpi
-- kandang.feed_in_button_urls: JSON map of feed-in button URLs per slot
-- kpi → chicken_counting, chicken_weight, feed_sacks, manual_input
-- =============================================================================
@@ -99,11 +100,12 @@ CREATE INDEX IF NOT EXISTS idx_sites_pusat_active_site_id ON sites (pusat_active
-- 3. Kandang (Coop / Pen)
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS kandang (
kandang_id SERIAL PRIMARY KEY,
kandang_name VARCHAR(30) NOT NULL,
site_id VARCHAR(64) NOT NULL REFERENCES sites (site_id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
kandang_id SERIAL PRIMARY KEY,
kandang_name VARCHAR(30) NOT NULL,
site_id VARCHAR(64) NOT NULL REFERENCES sites (site_id) ON DELETE CASCADE,
feed_in_button_urls JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_kandang_site_id ON kandang (site_id);
@@ -180,23 +182,35 @@ CREATE UNIQUE INDEX IF NOT EXISTS uniq_iot_panel_flock_timestamp ON iot_panel (f
-- -----------------------------------------------------------------------------
-- 7. AI Insight
-- Persisted per cache scope: cycle + kandang + topic + report_type +
-- report_period. Regenerate overwrites the same row.
-- alert: condition from graded CP707 facts (healthy|warning|critical|unknown).
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS ai_insight (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
date DATE NOT NULL,
insight_text TEXT NOT NULL,
alert VARCHAR(100) NOT NULL,
section VARCHAR(100) NOT NULL,
session VARCHAR(100) NOT NULL,
alert VARCHAR(100) NOT NULL DEFAULT '',
cycle_id BIGINT NOT NULL REFERENCES cycles (cycle_id) ON DELETE CASCADE,
kandang_id BIGINT REFERENCES kandang (kandang_id) ON DELETE CASCADE,
topic VARCHAR(64) NOT NULL DEFAULT '',
report_type VARCHAR(32) NOT NULL DEFAULT 'page',
report_period VARCHAR(64) NOT NULL DEFAULT 'current',
source VARCHAR(32) NOT NULL DEFAULT 'generated', -- generated | cache | local_fallback
summary TEXT NOT NULL DEFAULT '',
citations TEXT NOT NULL DEFAULT '[]', -- JSON array string of RAG citations
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
cycle_id INTEGER NOT NULL REFERENCES cycles (cycle_id) ON DELETE CASCADE
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_ai_insight_cycle_id ON ai_insight (cycle_id);
CREATE INDEX IF NOT EXISTS idx_ai_insight_kandang_id ON ai_insight (kandang_id);
CREATE INDEX IF NOT EXISTS idx_ai_insight_topic ON ai_insight (topic);
CREATE INDEX IF NOT EXISTS idx_ai_insight_report_type ON ai_insight (report_type);
CREATE INDEX IF NOT EXISTS idx_ai_insight_report_period ON ai_insight (report_period);
CREATE INDEX IF NOT EXISTS idx_ai_insight_date ON ai_insight (date);
CREATE UNIQUE INDEX IF NOT EXISTS uniq_ai_insight_cycle_date_section_session
ON ai_insight (cycle_id, date, section, session);
CREATE UNIQUE INDEX IF NOT EXISTS uniq_ai_insight_cache_scope
ON ai_insight (cycle_id, kandang_id, topic, report_type, report_period);
-- -----------------------------------------------------------------------------
-- 8. Feed Sacks
@@ -255,7 +269,7 @@ CREATE TABLE IF NOT EXISTS chicken_weight (
CREATE INDEX IF NOT EXISTS idx_chicken_weight_cycle_id ON chicken_weight (cycle_id);
CREATE INDEX IF NOT EXISTS idx_chicken_weight_date ON chicken_weight (date);
CREATE UNIQUE INDEX IF NOT EXISTS uniq_chicken_weight_cycle_date ON chicken_weight (cycle_id, date);
CREATE UNIQUE INDEX IF NOT EXISTS uniq_cw_cycle_date ON chicken_weight (cycle_id, date);
-- -----------------------------------------------------------------------------
-- 11. Manual Input
@@ -341,7 +355,7 @@ DECLARE
tbl TEXT;
BEGIN
FOREACH tbl IN ARRAY ARRAY[
'user_access', 'sites', 'kandang', 'cycles', 'flock',
'user_access', 'api_keys', 'sites', 'kandang', 'cycles', 'flock',
'iot_panel', 'ai_insight', 'feed_sacks', 'chicken_counting',
'chicken_weight', 'manual_input', 'kpi'
]