wip: currently rewriting the project as a full stack application tangled.org/kacaii.dev/sigo
gleam
at main 243 lines 7.5 kB view raw
1//// This module contains the code to run the sql queries defined in 2//// `./src/app/domain/data_analysis/sql`. 3//// > 🐿️ This module was generated automatically using v4.6.0 of 4//// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 5//// 6 7import gleam/dynamic/decode 8import gleam/option.{type Option} 9import gleam/time/timestamp.{type Timestamp} 10import pog 11import youid/uuid.{type Uuid} 12 13/// A row you get from running the `occurrence_dataset` query 14/// defined in `./src/app/domain/data_analysis/sql/occurrence_dataset.sql`. 15/// 16/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 17/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 18/// 19pub type OccurrenceDatasetRow { 20 OccurrenceDatasetRow( 21 occurrence_id: Uuid, 22 reported_timestamp: Timestamp, 23 arrival_timestamp: Option(Timestamp), 24 resolved_timestamp: Option(Timestamp), 25 occurrence_category: OccurrenceCategoryEnum, 26 occurrence_subcategory: Option(OccurrenceSubcategoryEnum), 27 priority: OccurrencePriorityEnum, 28 applicant_name: Option(String), 29 applicant_role: Option(UserRoleEnum), 30 latitude: Float, 31 longitude: Float, 32 ) 33} 34 35/// 󰕮 Occurrence reports 36/// 37/// > 🐿️ This function was generated automatically using v4.6.0 of 38/// > the [squirrel package](https://github.com/giacomocavalieri/squirrel). 39/// 40pub fn occurrence_dataset( 41 db: pog.Connection, 42) -> Result(pog.Returned(OccurrenceDatasetRow), pog.QueryError) { 43 let decoder = { 44 use occurrence_id <- decode.field(0, uuid_decoder()) 45 use reported_timestamp <- decode.field(1, pog.timestamp_decoder()) 46 use arrival_timestamp <- decode.field( 47 2, 48 decode.optional(pog.timestamp_decoder()), 49 ) 50 use resolved_timestamp <- decode.field( 51 3, 52 decode.optional(pog.timestamp_decoder()), 53 ) 54 use occurrence_category <- decode.field( 55 4, 56 occurrence_category_enum_decoder(), 57 ) 58 use occurrence_subcategory <- decode.field( 59 5, 60 decode.optional(occurrence_subcategory_enum_decoder()), 61 ) 62 use priority <- decode.field(6, occurrence_priority_enum_decoder()) 63 use applicant_name <- decode.field(7, decode.optional(decode.string)) 64 use applicant_role <- decode.field( 65 8, 66 decode.optional(user_role_enum_decoder()), 67 ) 68 use latitude <- decode.field(9, decode.float) 69 use longitude <- decode.field(10, decode.float) 70 decode.success(OccurrenceDatasetRow( 71 occurrence_id:, 72 reported_timestamp:, 73 arrival_timestamp:, 74 resolved_timestamp:, 75 occurrence_category:, 76 occurrence_subcategory:, 77 priority:, 78 applicant_name:, 79 applicant_role:, 80 latitude:, 81 longitude:, 82 )) 83 } 84 85 "-- 󰕮 Occurrence reports 86select 87 o.id as occurrence_id, 88 o.created_at as reported_timestamp, 89 o.arrived_at as arrival_timestamp, 90 o.resolved_at as resolved_timestamp, 91 o.occurrence_category, 92 o.occurrence_subcategory, 93 o.priority, 94 u_applicant.full_name as applicant_name, 95 u_applicant.user_role as applicant_role, 96 o.occurrence_location[1] as latitude, 97 o.occurrence_location[2] as longitude 98from 99 public.occurrence as o 100left join 101 public.user_account as u_applicant 102 on o.applicant_id = u_applicant.id 103order by 104 o.created_at desc; 105" 106 |> pog.query 107 |> pog.returning(decoder) 108 |> pog.execute(db) 109} 110 111// --- Enums ------------------------------------------------------------------- 112 113/// Corresponds to the Postgres `occurrence_category_enum` enum. 114/// 115/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 116/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 117/// 118pub type OccurrenceCategoryEnum { 119 Other 120 TrafficAccident 121 Fire 122 MedicEmergency 123} 124 125fn occurrence_category_enum_decoder() -> decode.Decoder(OccurrenceCategoryEnum) { 126 use occurrence_category_enum <- decode.then(decode.string) 127 case occurrence_category_enum { 128 "other" -> decode.success(Other) 129 "traffic_accident" -> decode.success(TrafficAccident) 130 "fire" -> decode.success(Fire) 131 "medic_emergency" -> decode.success(MedicEmergency) 132 _ -> decode.failure(Other, "OccurrenceCategoryEnum") 133 } 134} 135 136/// Corresponds to the Postgres `occurrence_priority_enum` enum. 137/// 138/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 139/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 140/// 141pub type OccurrencePriorityEnum { 142 High 143 Medium 144 Low 145} 146 147fn occurrence_priority_enum_decoder() -> decode.Decoder(OccurrencePriorityEnum) { 148 use occurrence_priority_enum <- decode.then(decode.string) 149 case occurrence_priority_enum { 150 "high" -> decode.success(High) 151 "medium" -> decode.success(Medium) 152 "low" -> decode.success(Low) 153 _ -> decode.failure(High, "OccurrencePriorityEnum") 154 } 155} 156 157/// Corresponds to the Postgres `occurrence_subcategory_enum` enum. 158/// 159/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 160/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 161/// 162pub type OccurrenceSubcategoryEnum { 163 InjuredAnimal 164 Flood 165 TreeCrash 166 MotorcycleCrash 167 Rollover 168 RunOver 169 Collision 170 Vehicle 171 Vegetation 172 Comercial 173 Residential 174 Intoxication 175 SeriousInjury 176 Seizure 177 PreHospitalCare 178 HeartStop 179} 180 181fn occurrence_subcategory_enum_decoder() -> decode.Decoder( 182 OccurrenceSubcategoryEnum, 183) { 184 use occurrence_subcategory_enum <- decode.then(decode.string) 185 case occurrence_subcategory_enum { 186 "injured_animal" -> decode.success(InjuredAnimal) 187 "flood" -> decode.success(Flood) 188 "tree_crash" -> decode.success(TreeCrash) 189 "motorcycle_crash" -> decode.success(MotorcycleCrash) 190 "rollover" -> decode.success(Rollover) 191 "run_over" -> decode.success(RunOver) 192 "collision" -> decode.success(Collision) 193 "vehicle" -> decode.success(Vehicle) 194 "vegetation" -> decode.success(Vegetation) 195 "comercial" -> decode.success(Comercial) 196 "residential" -> decode.success(Residential) 197 "intoxication" -> decode.success(Intoxication) 198 "serious_injury" -> decode.success(SeriousInjury) 199 "seizure" -> decode.success(Seizure) 200 "pre_hospital_care" -> decode.success(PreHospitalCare) 201 "heart_stop" -> decode.success(HeartStop) 202 _ -> decode.failure(InjuredAnimal, "OccurrenceSubcategoryEnum") 203 } 204} 205 206/// Corresponds to the Postgres `user_role_enum` enum. 207/// 208/// > 🐿️ This type definition was generated automatically using v4.6.0 of the 209/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). 210/// 211pub type UserRoleEnum { 212 Sargeant 213 Developer 214 Captain 215 Firefighter 216 Analyst 217 Admin 218} 219 220fn user_role_enum_decoder() -> decode.Decoder(UserRoleEnum) { 221 use user_role_enum <- decode.then(decode.string) 222 case user_role_enum { 223 "sargeant" -> decode.success(Sargeant) 224 "developer" -> decode.success(Developer) 225 "captain" -> decode.success(Captain) 226 "firefighter" -> decode.success(Firefighter) 227 "analyst" -> decode.success(Analyst) 228 "admin" -> decode.success(Admin) 229 _ -> decode.failure(Sargeant, "UserRoleEnum") 230 } 231} 232 233// --- Encoding/decoding utils ------------------------------------------------- 234 235/// A decoder to decode `Uuid`s coming from a Postgres query. 236/// 237fn uuid_decoder() { 238 use bit_array <- decode.then(decode.bit_array) 239 case uuid.from_bit_array(bit_array) { 240 Ok(uuid) -> decode.success(uuid) 241 Error(_) -> decode.failure(uuid.v7(), "Uuid") 242 } 243}