cleanup analysis in sqlitebrowser
authorDan White <dan@whiteaudio.com>
Sat, 24 Dec 2022 23:29:11 +0000 (17:29 -0600)
committerDan White <dan@whiteaudio.com>
Sat, 24 Dec 2022 23:29:11 +0000 (17:29 -0600)
observations_analysis.sqbpro

index 77a9813bd676b266ec61f0ba94cdcc660818dd17..29e5ba64f36a9c4cbe3f21c4431e57c36ca1626d 100644 (file)
@@ -1,4 +1,5 @@
-<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="observations.db" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><current_tab id="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="6286"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="observations"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="times_index" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_node" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_parent" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_rowid" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 6">create index if not exists gs_norad_index
+<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="observations.db" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><current_tab id="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="7736"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="observations"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="observations" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_node" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_parent" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table><table schema="main" name="times_index_rowid" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk=""><sort/><column_widths/><filter_values/><display_formats/><hidden_columns/><plot_y_axes/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 6">-- Indexes make the world go 'round ... faster.
+create index if not exists gs_norad_index
 on observations(ground_station, norad_cat_id);
 
 create index if not exists gs_index
@@ -12,26 +13,84 @@ on observations(observer);
 
 create index if not exists observer_gs_index
 on observations(observer, ground_station);
-</sql><sql name="SQL 2">CREATE VIRTUAL TABLE IF NOT EXISTS times_index
+
+create index if not exists gs_observer_index
+on observations(ground_station, observer);
+</sql><sql name="SQL 2">-- Special index for speeding up time range queries.
+CREATE VIRTUAL TABLE IF NOT EXISTS times_index
 USING rtree(id, start, end);
 
 INSERT OR REPLACE INTO times_index
 SELECT id, strftime(&quot;%s&quot;, start), strftime(&quot;%s&quot;, end)
 FROM observations
 WHERE strftime(&quot;%s&quot;, start) &lt;= strftime(&quot;%s&quot;, end);
-</sql><sql name="SQL 4">select observer, count(vetted_status) from observations
+</sql><sql name="SQL 10">-- Add triggers to keep the times Rtree updated
+
+CREATE TRIGGER IF NOT EXISTS times_insert_trigger
+AFTER INSERT
+ON observations
+WHEN strftime(&quot;%s&quot;, NEW.start) &lt;= strftime(&quot;%s&quot;, NEW.end)
+BEGIN
+       INSERT OR REPLACE INTO times_index(id, start, end)
+       VALUES (NEW.id, strftime(&quot;%s&quot;, NEW.start), strftime(&quot;%s&quot;, NEW.end));
+END;
+
+
+CREATE TRIGGER IF NOT EXISTS times_update_trigger
+AFTER UPDATE
+ON observations
+WHEN strftime(&quot;%s&quot;, NEW.start) &lt;= strftime(&quot;%s&quot;, NEW.end)
+BEGIN
+       INSERT OR REPLACE INTO times_index(id, start, end)
+       VALUES (NEW.id, strftime(&quot;%s&quot;, NEW.start), strftime(&quot;%s&quot;, NEW.end));
+END;
+
+CREATE TRIGGER IF NOT EXISTS times_delete_trigger
+AFTER DELETE
+ON observations
+BEGIN
+       DELETE FROM times_index
+       WHERE id = OLD.id;
+END;
+</sql><sql name="SQL 4">-- Observers with un-vetted observations
+-- LONG
+SELECT
+       observer,
+       count(vetted_status) AS num_unknown
+FROM observations
 WHERE vetted_status = &quot;unknown&quot;
 GROUP BY observer
-ORDER BY observer;</sql><sql name="SQL 5">select ground_station, norad_cat_id, count(id) from observations
+ORDER BY num_unknown;</sql><sql name="SQL 5">-- Which satellites are listened to by which GS?
+select ground_station, norad_cat_id, count(id) from observations
 GROUP BY ground_station, norad_cat_id
-ORDER BY ground_station, count(id);</sql><sql name="SQL 7">select observer, ground_station, count(id) from observations
+ORDER BY ground_station, count(id);</sql><sql name="SQL 7">-- Favorite ground stations used by observers
+select observer, ground_station, count(id) from observations
 GROUP BY observer, ground_station
 ORDER BY observer, count(id) DESC, ground_station;
-</sql><sql name="SQL 3">select ground_station, observer, count(id) from observations
+</sql><sql name="SQL 3">-- Observations on each ground station, by total number per observer
+-- Largest number is likely the GS owner?
+SELECT
+       ground_station,
+       observer,
+       count(id)
+FROM observations
+WHERE ground_station = 2
 GROUP BY ground_station, observer
 ORDER BY ground_station, count(id) DESC, observer;
-</sql><sql name="SQL 1">-- WIP: how many different observers have used each GS?
-select ground_station, count(*) from observations
-GROUP BY ground_station, observer
-ORDER BY count(distinct observer) DESC, ground_station;
-</sql><current_tab id="6"/></tab_sql></sqlb_project>
+</sql><sql name="SQL 1">-- How many different observers have scheduled on each GS?
+SELECT
+       ground_station,
+       count(distinct observer) as num_observers,
+       count(id) AS num_obs
+FROM observations
+GROUP BY ground_station
+ORDER BY ground_station;
+</sql><sql name="SQL 9">-- How many different GS have observers scheduled on?
+SELECT
+       observer,
+       count(distinct ground_station) AS num_gs,
+count(id) AS num_obs
+FROM observations
+GROUP BY observer
+ORDER BY num_gs DESC;
+</sql><current_tab id="3"/></tab_sql></sqlb_project>