From adf2812dfc2f6c8079d3636f0b5baaaa2a698fe2 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 1 Nov 2022 18:12:32 +0100 Subject: [PATCH] fix: postgres issues ids are i64, converted to bigints. color should be stored as signed. This makes migrating from SQLite to postgres kind of painful, but doable. --- Cargo.toml | 2 +- migration/Cargo.toml | 1 + .../src/m20220101_000001_create_table.rs | 20 ++++++++++--------- src/data/entities/metrics.rs | 8 ++++---- src/data/entities/panels.rs | 2 +- src/data/entities/points.rs | 4 ++-- src/data/entities/sources.rs | 2 +- src/gui/source.rs | 2 +- src/main.rs | 17 ++++++++-------- src/util.rs | 8 ++++---- src/worker/visualizer.rs | 5 ++++- 11 files changed, 38 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2002d31..e45f395 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ eframe = { version = "0.19", features = ["persistence"] } tokio = { version = "1", features = ["full"] } clap = { version = "4", features = ["derive"] } futures = "0.3" -sea-orm = { version = "0.10", features = [ "runtime-tokio-rustls", "sqlx-sqlite", "macros" ] } +sea-orm = { version = "0.10", features = [ "runtime-tokio-rustls", "sqlx-sqlite", "sqlx-postgres", "macros" ] } reqwest = { version = "0.11", features = ["json"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/migration/Cargo.toml b/migration/Cargo.toml index e8ed7f8..b185067 100644 --- a/migration/Cargo.toml +++ b/migration/Cargo.toml @@ -20,4 +20,5 @@ features = [ # e.g. "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature "sqlx-sqlite", # `DATABASE_DRIVER` feature + "sqlx-postgres", ] diff --git a/migration/src/m20220101_000001_create_table.rs b/migration/src/m20220101_000001_create_table.rs index 41ebba7..5e92f53 100644 --- a/migration/src/m20220101_000001_create_table.rs +++ b/migration/src/m20220101_000001_create_table.rs @@ -17,7 +17,7 @@ impl MigrationTrait for Migration { .if_not_exists() .col( ColumnDef::new(Panels::Id) - .integer() + .big_integer() .not_null() .auto_increment() .primary_key(), @@ -26,6 +26,7 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(Panels::Position).integer().not_null()) .col(ColumnDef::new(Panels::Timeserie).boolean().not_null()) .col(ColumnDef::new(Panels::Height).integer().not_null()) + .col(ColumnDef::new(Panels::Width).integer().not_null()) .col(ColumnDef::new(Panels::ViewScroll).boolean().not_null()) .col(ColumnDef::new(Panels::LimitView).boolean().not_null()) .col(ColumnDef::new(Panels::ViewSize).integer().not_null()) @@ -43,7 +44,7 @@ impl MigrationTrait for Migration { .if_not_exists() .col( ColumnDef::new(Sources::Id) - .integer() + .big_integer() .not_null() .auto_increment() .primary_key(), @@ -62,15 +63,15 @@ impl MigrationTrait for Migration { .if_not_exists() .col( ColumnDef::new(Metrics::Id) - .integer() + .big_integer() .not_null() .auto_increment() .primary_key(), ) .col(ColumnDef::new(Metrics::Name).string().not_null()) .col(ColumnDef::new(Metrics::Position).integer().not_null()) - .col(ColumnDef::new(Metrics::PanelId).integer().not_null()) - .col(ColumnDef::new(Metrics::SourceId).integer().not_null()) + .col(ColumnDef::new(Metrics::PanelId).big_integer().not_null()) + .col(ColumnDef::new(Metrics::SourceId).big_integer().not_null()) .col(ColumnDef::new(Metrics::QueryX).string().not_null()) .col(ColumnDef::new(Metrics::QueryY).string().not_null()) .col(ColumnDef::new(Metrics::Color).integer().not_null()) @@ -83,14 +84,14 @@ impl MigrationTrait for Migration { .if_not_exists() .col( ColumnDef::new(Points::Id) - .integer() + .big_integer() .not_null() .auto_increment() .primary_key(), ) - .col(ColumnDef::new(Points::MetricId).integer().not_null()) - .col(ColumnDef::new(Points::X).float().not_null()) - .col(ColumnDef::new(Points::Y).float().not_null()) + .col(ColumnDef::new(Points::MetricId).big_integer().not_null()) + .col(ColumnDef::new(Points::X).double().not_null()) + .col(ColumnDef::new(Points::Y).double().not_null()) .to_owned(), ).await?; Ok(()) @@ -121,6 +122,7 @@ enum Panels { Position, Timeserie, Height, + Width, ViewScroll, LimitView, ViewSize, diff --git a/src/data/entities/metrics.rs b/src/data/entities/metrics.rs index 3a1a3af..a9999b0 100644 --- a/src/data/entities/metrics.rs +++ b/src/data/entities/metrics.rs @@ -10,13 +10,13 @@ use crate::data::FetchError; #[sea_orm(table_name = "metrics")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] - pub id: i32, + pub id: i64, pub name: String, - pub source_id: i32, + pub source_id: i64, pub query_x: Option, pub query_y: String, - pub panel_id: i32, - pub color: u32, + pub panel_id: i64, + pub color: i32, pub position: i32, } diff --git a/src/data/entities/panels.rs b/src/data/entities/panels.rs index f037ccf..17c2f6f 100644 --- a/src/data/entities/panels.rs +++ b/src/data/entities/panels.rs @@ -6,7 +6,7 @@ use sea_orm::entity::prelude::*; #[sea_orm(table_name = "panels")] pub struct Model { #[sea_orm(primary_key, auto_increment = true)] - pub id: i32, + pub id: i64, pub name: String, pub view_scroll: bool, pub view_size: i32, diff --git a/src/data/entities/points.rs b/src/data/entities/points.rs index bb8531c..66b972b 100644 --- a/src/data/entities/points.rs +++ b/src/data/entities/points.rs @@ -6,8 +6,8 @@ use eframe::egui::plot::PlotPoint; #[sea_orm(table_name = "points")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] - pub id: i32, - pub metric_id: i32, + pub id: i64, + pub metric_id: i64, pub x: f64, pub y: f64, } diff --git a/src/data/entities/sources.rs b/src/data/entities/sources.rs index 5508582..6698571 100644 --- a/src/data/entities/sources.rs +++ b/src/data/entities/sources.rs @@ -5,7 +5,7 @@ use chrono::Utc; #[sea_orm(table_name = "sources")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] - pub id: i32, + pub id: i64, pub name: String, pub enabled: bool, pub url: String, diff --git a/src/gui/source.rs b/src/gui/source.rs index 11cad32..aaf0365 100644 --- a/src/gui/source.rs +++ b/src/gui/source.rs @@ -12,7 +12,7 @@ use crate::data::entities; use super::metric::metric_edit_ui; pub fn source_panel(app: &mut App, ui: &mut Ui) { - let mut source_to_put_metric_on : Option = None; + let mut source_to_put_metric_on : Option = None; let mut to_swap: Option = None; let _to_insert: Vec = Vec::new(); // let mut to_delete: Option = None; diff --git a/src/main.rs b/src/main.rs index a435d1c..ace264e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,14 @@ struct CliArgs { log_size: u64, } +// When compiling for web: +#[cfg(target_arch = "wasm32")] +fn setup_tracing(_layer: InternalLoggerLayer) { + // Make sure panics are logged using `console.error`. + console_error_panic_hook::set_once(); + // Redirect tracing to console.log and friends: + tracing_wasm::set_as_global_default(); +} // When compiling natively: #[cfg(not(target_arch = "wasm32"))] @@ -61,15 +69,6 @@ fn setup_tracing(layer: InternalLoggerLayer) { .init(); } -// When compiling for web: -#[cfg(target_arch = "wasm32")] -fn setup_tracing(_layer: InternalLoggerLayer) { - // Make sure panics are logged using `console.error`. - console_error_panic_hook::set_once(); - // Redirect tracing to console.log and friends: - tracing_wasm::set_as_global_default(); -} - fn main() { let args = CliArgs::parse(); diff --git a/src/util.rs b/src/util.rs index 67b36d3..d167849 100644 --- a/src/util.rs +++ b/src/util.rs @@ -83,7 +83,7 @@ pub fn timestamp_to_str(t: i64, date: bool, time: bool) -> String { ) } -pub fn unpack_color(c: u32) -> Color32 { +pub fn unpack_color(c: i32) -> Color32 { let r: u8 = (c >> 0) as u8; let g: u8 = (c >> 8) as u8; let b: u8 = (c >> 16) as u8; @@ -92,11 +92,11 @@ pub fn unpack_color(c: u32) -> Color32 { } #[allow(dead_code)] -pub fn repack_color(c: Color32) -> u32 { - let mut out: u32 = 0; +pub fn repack_color(c: Color32) -> i32 { + let mut out: i32 = 0; let mut offset = 0; for el in c.to_array() { - out |= ((el & 0xFF) as u32) << offset; + out |= ((el & 0xFF) as i32) << offset; offset += 8; } return out; diff --git a/src/worker/visualizer.rs b/src/worker/visualizer.rs index 9139be3..c587784 100644 --- a/src/worker/visualizer.rs +++ b/src/worker/visualizer.rs @@ -1,5 +1,5 @@ use chrono::Utc; -use sea_orm::{DatabaseConnection, EntityTrait, Condition, ColumnTrait, QueryFilter, Set}; +use sea_orm::{DatabaseConnection, EntityTrait, Condition, ColumnTrait, QueryFilter, Set, QueryOrder, Order}; use tokio::sync::{watch, mpsc}; use tracing::info; use std::collections::VecDeque; @@ -174,6 +174,7 @@ impl AppState { .add(entities::points::Column::X.gte((now - new_width) as f64)) .add(entities::points::Column::X.lte(now as f64)) ) + .order_by(entities::points::Column::X, Order::Asc) .all(&db) .await.unwrap().into(); self.tx.points.send(self.points.clone().into()).unwrap(); @@ -194,6 +195,7 @@ impl AppState { .add(entities::points::Column::X.gte(now - new_width)) .add(entities::points::Column::X.lte(now - width)) ) + .order_by(entities::points::Column::X, Order::Asc) .all(&db) .await.unwrap(); info!(target: "worker", "Fetched {} previous points", previous_points.len()); @@ -211,6 +213,7 @@ impl AppState { .add(entities::points::Column::X.gte(last as f64)) .add(entities::points::Column::X.lte(now as f64)) ) + .order_by(entities::points::Column::X, Order::Asc) .all(&db) .await.unwrap(); info!(target: "worker", "Fetched {} new points", new_points.len());