-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.sql
More file actions
170 lines (150 loc) · 5.02 KB
/
Copy pathselect.sql
File metadata and controls
170 lines (150 loc) · 5.02 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
-- Sentinel AI — Snowflake verification, direct SQL queries, and Cortex Agent sample calls
USE ROLE ACCOUNTADMIN;
USE WAREHOUSE COMPUTE_WH;
CREATE DATABASE IF NOT EXISTS SENTINEL_AI_DB;
USE DATABASE SENTINEL_AI_DB;
CREATE SCHEMA IF NOT EXISTS PUBLIC;
USE SCHEMA PUBLIC;
-- ============================================================
-- Session variables — reusable AGENT_RUN tool configurations
-- NOTE: These SET statements require an active Snowflake session.
-- Run this file interactively in Snowsight or via `snow sql -f`
-- (not as individual statement snippets).
-- ============================================================
SET TOOLS_BOTH = '[
{"tool_spec": {"type": "cortex_search", "name": "sop_search"}},
{"tool_spec": {"type": "cortex_analyst_text_to_sql", "name": "sentinel_data"}}
]';
SET TOOLS_DATA_ONLY = '[
{"tool_spec": {"type": "cortex_analyst_text_to_sql", "name": "sentinel_data"}}
]';
SET TOOL_RESOURCES = '{
"sop_search": {"search_service": "SENTINEL_AI_DB.PUBLIC.SENTINEL_SOP_SEARCH_SERVICE"},
"sentinel_data": {
"semantic_model_file": "@SENTINEL_AI_DB.PUBLIC.AGENT_SKILLS_STAGE/sentinel_semantic_model.yaml",
"execution_environment": {"type": "warehouse", "warehouse": "COMPUTE_WH"}
}
}';
SET TOOL_RESOURCES_DATA_ONLY = '{
"sentinel_data": {
"semantic_model_file": "@SENTINEL_AI_DB.PUBLIC.AGENT_SKILLS_STAGE/sentinel_semantic_model.yaml",
"execution_environment": {"type": "warehouse", "warehouse": "COMPUTE_WH"}
}
}';
-- ============================================================
-- Direct SQL Queries (sections 1–9)
-- These query Snowflake tables directly without the Cortex Agent.
-- Use these for raw data inspection, dashboarding, and debugging.
-- ============================================================
-- 1. Active Weather & Meteorological Overview (select only needed columns)
SELECT
timestamp,
location,
rainfall,
wind_speed,
storm_name,
forecast
FROM weather_data
WHERE timestamp >= DATEADD('hour', -24, CURRENT_TIMESTAMP())
ORDER BY timestamp DESC
LIMIT 5;
-- 2. Live Telemetry & Risk Assessment (filtered to latest readings only)
SELECT
r.sensor_id,
r.barangay,
r.water_level,
r.alert_level,
CASE
WHEN r.water_level >= r.critical_threshold THEN 'Critical (Red Alert)'
WHEN r.water_level >= r.warning_threshold THEN 'High Warning (Orange Alert)'
ELSE 'Normal'
END AS status,
b.population,
r.latitude,
r.longitude
FROM river_sensors r
JOIN barangays b ON r.barangay = b.barangay
WHERE r.timestamp >= DATEADD('hour', -1, CURRENT_TIMESTAMP())
ORDER BY r.water_level DESC;
-- 3. Evacuation Centers Capacity & Occupancy Rates
SELECT
name,
barangay,
capacity,
current_occupancy,
ROUND((current_occupancy / capacity) * 100, 1) AS occupancy_pct
FROM evacuation_centers
ORDER BY occupancy_pct DESC;
-- 4. Healthcare Infrastructure & Hospital Bed Availability
SELECT
hospital,
barangay,
beds_available
FROM hospitals
ORDER BY beds_available DESC;
-- 5. Historical Flood Events & Peak Levels
SELECT
barangay,
COUNT(*) AS past_floods,
MAX(water_level) AS peak_water_level
FROM flood_history
GROUP BY barangay
ORDER BY peak_water_level DESC;
-- 6. Recent Audit Events & Directive Approvals
SELECT
event,
event_type,
timestamp
FROM audit_logs
ORDER BY timestamp DESC
LIMIT 20;
-- 7. Emergency Job Dispatch Execution Records
SELECT
job_id,
status,
recipients_filter,
counts,
created_at
FROM execution_jobs
ORDER BY created_at DESC
LIMIT 10;
-- 8. Copilot Chat Session History Records
SELECT
id,
session_id,
role,
content,
metadata,
created_at
FROM chat_history
ORDER BY created_at ASC
LIMIT 50;
-- 9. Standard Operating Procedures (limited scan)
SELECT content, category, created_at
FROM SENTINEL_SOPS
ORDER BY created_at ASC
LIMIT 100;
-- Cortex Search verification
SELECT SNOWFLAKE.CORTEX.SEARCH_PREVIEW(
'SENTINEL_SOP_SEARCH_SERVICE',
'{"query": "evacuation thresholds", "columns": ["content"]}'
);
-- ============================================================
-- 10. Snowflake Cortex Agent — Sample AGENT_RUN Query
-- ============================================================
-- ARCHITECTURAL NOTE:
-- • Production Web/Copilot UI & Dispatch Engine use SNOWFLAKE.CORTEX.AI_COMPLETE
-- + Cortex Search RAG & direct SQL for 1–3s instant response times and human-in-the-loop safety.
-- • SNOWFLAKE.CORTEX.AGENT_RUN is kept below as a 1-query reference sample for testing
-- autonomous multi-tool reasoning (SOP search + Text-to-SQL).
-- Sample AGENT_RUN Reference Query:
SELECT SNOWFLAKE.CORTEX.AGENT_RUN(
'{
"agent": "SentinelAI",
"tools": ' || $TOOLS_BOTH || ',
"tool_resources": ' || $TOOL_RESOURCES || ',
"messages": [{"role": "user", "content": [{"type": "text",
"text": "What is the current water level and flood risk status for all monitored rivers from the latest sensor readings? Cross-reference with SOP thresholds."}]}]
}',
FALSE
) AS sample_agent_run;