From a1cae2ed2ba917043df09cf8f758ce447030baf2 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 3 Aug 2023 00:31:04 -0400 Subject: [PATCH] Recreate the dashboard as a command line app --- dashboard/Cargo.lock | 124 --------------------------------- dashboard/Cargo.toml | 1 - dashboard/src/main.rs | 90 ++++++++++++++---------- dashboard/src/soluna_client.rs | 74 ++++++++++---------- ifc/src/lib.rs | 14 +++- 5 files changed, 99 insertions(+), 204 deletions(-) diff --git a/dashboard/Cargo.lock b/dashboard/Cargo.lock index 20425e6..2280945 100644 --- a/dashboard/Cargo.lock +++ b/dashboard/Cargo.lock @@ -17,15 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "aho-corasick" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" -dependencies = [ - "memchr", -] - [[package]] name = "android-tzdata" version = "0.1.1" @@ -141,28 +132,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "chrono-tz" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c39203181991a7dd4343b8005bd804e7a9a37afb8ac070e43771e8c820bbde" -dependencies = [ - "chrono", - "chrono-tz-build", - "phf", -] - -[[package]] -name = "chrono-tz-build" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f509c3a87b33437b05e2458750a0700e5bdd6956176773e6c7d6dd15a283a0c" -dependencies = [ - "parse-zoneinfo", - "phf", - "phf_codegen", -] - [[package]] name = "core-foundation" version = "0.9.3" @@ -203,7 +172,6 @@ name = "dashboard" version = "0.1.0" dependencies = [ "chrono", - "chrono-tz", "fluent", "fluent-ergonomics", "futures", @@ -931,60 +899,12 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "parse-zoneinfo" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c705f256449c60da65e11ff6626e0c16a0a0b96aaa348de61376b249bc340f41" -dependencies = [ - "regex", -] - [[package]] name = "percent-encoding" version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" -[[package]] -name = "phf" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" -dependencies = [ - "phf_shared", -] - -[[package]] -name = "phf_codegen" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a" -dependencies = [ - "phf_generator", - "phf_shared", -] - -[[package]] -name = "phf_generator" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" -dependencies = [ - "phf_shared", - "rand", -] - -[[package]] -name = "phf_shared" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" -dependencies = [ - "siphasher", - "uncased", -] - [[package]] name = "pin-project" version = "1.1.2" @@ -1086,35 +1006,6 @@ dependencies = [ "bitflags 1.3.2", ] -[[package]] -name = "regex" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" - [[package]] name = "reqwest" version = "0.11.18" @@ -1305,12 +1196,6 @@ dependencies = [ "libc", ] -[[package]] -name = "siphasher" -version = "0.3.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" - [[package]] name = "slab" version = "0.4.8" @@ -1566,15 +1451,6 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "uncased" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b9bc53168a4be7402ab86c3aad243a84dd7381d09be0eddc81280c1da95ca68" -dependencies = [ - "version_check", -] - [[package]] name = "unic-langid" version = "0.9.1" diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index 99c44cd..e128bc9 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] chrono = { version = "0.4", features = ["serde"] } -chrono-tz = { version = "0.6" } fluent = { version = "0.16" } fluent-ergonomics = { path = "../fluent-ergonomics/" } futures = { version = "0.3" } diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index fe052f8..8e2ae4b 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -5,8 +5,7 @@ extern crate horrorshow; #[macro_use] extern crate lazy_static; -use chrono::{Datelike, Utc}; -use chrono_tz::America::New_York; +use chrono::{Datelike, Local, TimeZone, Utc}; use fluent::{FluentArgs, FluentValue}; use geo_types::{Latitude, Longitude}; use horrorshow::helper::doctype; @@ -23,8 +22,9 @@ mod soluna_client; use soluna_client::{SolunaClient, SunMoon}; mod solstices; -use solstices::{Event, Solstices, EVENTS}; +use solstices::{Event, EVENTS}; +/* const EO_TEXT: &'static str = " day = {$day -> *[Sunday] Dimanĉo @@ -75,8 +75,10 @@ fn date(fluent: Arc, today: IFC::IFC) -> impl Ren owned_html! {p : format!("{}, {} {}, {}", tago, monato, today.day(), today.year());} } +*/ -fn soluna_desegno(sun_moon: SunMoon) -> impl Render { +/* +fn soluna_desegno(sun_moon: SunMoon) -> impl Render { owned_html! { table { tr { @@ -86,16 +88,18 @@ fn soluna_desegno(sun_moon: SunMoon) -> impl Render { th : "Lunfalo"; } tr { - td : format!("{}", sun_moon.sunleviĝo.format("%H:%M")); - td : format!("{}", sun_moon.sunfalo.format("%H:%M")); - td : sun_moon.lunleviĝo.map(|v| format!("{}", v.format("%H:%M"))).unwrap_or("".to_string()); - td : sun_moon.lunfalo.map(|v| format!("{}", v.format("%H:%M"))).unwrap_or("".to_string()); - td : format!("{:?}", sun_moon.lunfazo); + td : format!("{}", sun_moon.sunrise.format("%H:%M")); + td : format!("{}", sun_moon.sunset.format("%H:%M")); + td : sun_moon.moonrise.map(|v| format!("{}", v.format("%H:%M"))).unwrap_or("".to_string()); + td : sun_moon.moonset.map(|v| format!("{}", v.format("%H:%M"))).unwrap_or("".to_string()); + td : format!("{:?}", sun_moon.moon_phase); } } } } +*/ +/* fn astronomia_eventa_desegno( fluent: Arc, event: Option, @@ -104,10 +108,10 @@ fn astronomia_eventa_desegno( None => "".to_owned(), Some(event) => { let eventa_str = match event { - Event::SpringEquinox(_) => fluent.tr("printempa_ekvinokso", None), - Event::SummerSolstice(_) => fluent.tr("somera_solstico", None), - Event::AutumnEquinox(_) => fluent.tr("auxtuna_ekvinokso", None), - Event::WinterSolstice(_) => fluent.tr("vintra_solstico", None), + Event::SpringEquinox(_) => fluent.tr("spring_equinox", None), + Event::SummerSolstice(_) => fluent.tr("summer_solstice", None), + Event::AutumnEquinox(_) => fluent.tr("autumn_equinox", None), + Event::WinterSolstice(_) => fluent.tr("winter_solstice", None), } .unwrap(); format!("{} {}", eventa_str, event.date().format("%Y-%m-%d")) @@ -117,6 +121,7 @@ fn astronomia_eventa_desegno( div : s.clone(); } } +*/ /* fn page_template( @@ -164,33 +169,42 @@ async fn page( #[tokio::main] pub async fn main() { - /* - let latitudo = Latitude::from(41.78); - let longitudo = Longitude::from(-71.41); - let eo_id = "eo".parse::().unwrap(); - let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id.clone()]); - let solar_client = Arc::new(SolunaClient::new()); + let now = Local::now(); + let ifc = IFC::IFC::from(now.date_naive().with_year(12023).unwrap()); + let next_event = EVENTS.next_event(now.with_timezone(&Utc)).unwrap(); - fluent.add_from_text(eo_id, EO_TEXT.to_owned()).unwrap(); - let fluent = Arc::new(fluent); + println!( + "{:?}, {:?} {}, {}", + ifc.weekday(), + ifc.month(), + ifc.day(), + ifc.year() + ); - let pageview = { - warp::path::end().and_then(move || { - page( - fluent.clone(), - solar_client.clone(), - latitudo.clone(), - longitudo.clone(), - ) - }) - }; + println!("{:?}", next_event); - let server = warp::serve(paĝvojo); - server - .run(SocketAddr::new( - IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), - 8000, - )) + let latitude = Latitude::from(41.78); + let longitude = Longitude::from(-71.41); + let soluna_client = SolunaClient::new(); + let sun_moon = soluna_client + .request(latitude, longitude, Local::now()) .await; - */ + + println!( + "Solar Transit: {} -> {}", + sun_moon.sunrise.format("%H:%M").to_string(), + sun_moon.sunset.format("%H:%M").to_string() + ); + println!( + "Lunar Transit: {} -> {} [{:?}]", + sun_moon + .moonrise + .map(|time| time.format("%H:%M").to_string()) + .unwrap_or(" -- ".to_owned()), + sun_moon + .moonset + .map(|time| time.format("%H:%M").to_string()) + .unwrap_or(" -- ".to_owned()), + sun_moon.moon_phase + ); } diff --git a/dashboard/src/soluna_client.rs b/dashboard/src/soluna_client.rs index ae9ad2b..60e3e69 100644 --- a/dashboard/src/soluna_client.rs +++ b/dashboard/src/soluna_client.rs @@ -1,8 +1,9 @@ // 41.78, -71.41 // https://api.solunar.org/solunar/41.78,-71.41,20211029,-4 -use chrono::{Date, DateTime, Duration, NaiveTime, Offset, Timelike, Utc}; -use chrono_tz::Tz; +use chrono::{ + DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike, Utc, +}; use geo_types::{Latitude, Longitude}; use memorycache::MemoryCache; use reqwest; @@ -11,32 +12,30 @@ const ENDPOINT: &str = "https://api.solunar.org/solunar"; #[derive(Clone, Debug, PartialEq)] pub struct SunMoon { - pub sunleviĝo: DateTime, - pub sunfalo: DateTime, - pub lunleviĝo: Option>, - pub lunfalo: Option>, - pub lunfazo: LunarPhase, + pub sunrise: NaiveTime, + pub sunset: NaiveTime, + pub moonrise: Option, + pub moonset: Option, + pub moon_phase: LunarPhase, } impl SunMoon { - fn from_js(day: Date, val: SunMoonJs) -> Self { - fn new_time(day: Date, val: String) -> Option> { - NaiveTime::parse_from_str(&val, "%H:%M") - .map(|time| day.and_hms(time.hour(), time.minute(), 0)) - .ok() + fn from_js(val: SunMoonJs) -> Self { + fn parse_time(val: String) -> Option { + NaiveTime::parse_from_str(&val, "%H:%M").ok() } - let sunleviĝo = new_time(day.clone(), val.sunleviĝo).unwrap(); - let sunfalo = new_time(day.clone(), val.sunfalo).unwrap(); - let lunleviĝo = val.lunleviĝo.and_then(|v| new_time(day.clone(), v)); - let lunfalo = val.lunfalo.and_then(|v| new_time(day.clone(), v)); + let sunrise = parse_time(val.sunrise).unwrap(); + let sunset = parse_time(val.sunset).unwrap(); + let moonrise = val.moonrise.and_then(|v| parse_time(v)); + let moonset = val.moonset.and_then(|v| parse_time(v)); Self { - sunleviĝo, - sunfalo, - lunleviĝo, - lunfalo, - lunfazo: val.lunfazo, + sunrise, + sunset, + moonrise, + moonset, + moon_phase: val.moon_phase, } } } @@ -44,15 +43,15 @@ impl SunMoon { #[derive(Clone, Debug, Deserialize)] pub(crate) struct SunMoonJs { #[serde(alias = "sunRise")] - sunleviĝo: String, + sunrise: String, #[serde(alias = "sunSet")] - sunfalo: String, + sunset: String, #[serde(alias = "moonRise")] - lunleviĝo: Option, + moonrise: Option, #[serde(alias = "moonSet")] - lunfalo: Option, + moonset: Option, #[serde(alias = "moonPhase")] - lunfazo: LunarPhase, + moon_phase: LunarPhase, } #[derive(Clone, Debug, Deserialize, PartialEq)] @@ -88,13 +87,13 @@ impl SolunaClient { } } - pub async fn request( + pub async fn request( &self, latitude: Latitude, longitude: Longitude, - day: Date, + day: DateTime, ) -> SunMoon { - let date = day.format("%Y%m%d"); + let date = day.date_naive().format("%Y%m%d"); let url = format!( "{}/{},{},{},{}", ENDPOINT, @@ -119,32 +118,29 @@ impl SolunaClient { }) .await; - SunMoon::from_js(day, js) + SunMoon::from_js(js) } } #[cfg(test)] mod test { use super::*; - use chrono::TimeZone; - use chrono_tz::America::New_York; use serde_json; const EXAMPLE: &str = "{\"sunRise\":\"7:15\",\"sunTransit\":\"12:30\",\"sunSet\":\"17:45\",\"moonRise\":null,\"moonTransit\":\"7:30\",\"moonUnder\":\"19:54\",\"moonSet\":\"15:02\",\"moonPhase\":\"Waning Crescent\",\"moonIllumination\":0.35889454647387764,\"sunRiseDec\":7.25,\"sunTransitDec\":12.5,\"sunSetDec\":17.75,\"moonRiseDec\":null,\"moonSetDec\":15.033333333333333,\"moonTransitDec\":7.5,\"moonUnderDec\":19.9,\"minor1Start\":null,\"minor1Stop\":null,\"minor2StartDec\":14.533333333333333,\"minor2Start\":\"14:32\",\"minor2StopDec\":15.533333333333333,\"minor2Stop\":\"15:32\",\"major1StartDec\":6.5,\"major1Start\":\"06:30\",\"major1StopDec\":8.5,\"major1Stop\":\"08:30\",\"major2StartDec\":18.9,\"major2Start\":\"18:54\",\"major2StopDec\":20.9,\"major2Stop\":\"20:54\",\"dayRating\":1,\"hourlyRating\":{\"0\":20,\"1\":20,\"2\":0,\"3\":0,\"4\":0,\"5\":0,\"6\":20,\"7\":40,\"8\":40,\"9\":20,\"10\":0,\"11\":0,\"12\":0,\"13\":0,\"14\":0,\"15\":20,\"16\":20,\"17\":20,\"18\":40,\"19\":20,\"20\":20,\"21\":20,\"22\":0,\"23\":0}}"; #[test] fn it_parses_a_response() { - let day = New_York.ymd(2021, 10, 29); let sun_moon_js: SunMoonJs = serde_json::from_str(EXAMPLE).unwrap(); - let sun_moon = SunMoon::from_js(day.clone(), sun_moon_js); + let sun_moon = SunMoon::from_js(sun_moon_js); assert_eq!( sun_moon, SunMoon { - sunleviĝo: day.and_hms(7, 15, 0), - sunfalo: day.and_hms(17, 45, 0), - lunleviĝo: None, - lunfalo: Some(day.and_hms(15, 02, 0)), - lunfazo: LunarPhase::WaningCrescent, + sunrise: NaiveTime::from_hms_opt(7, 15, 0).unwrap(), + sunset: NaiveTime::from_hms_opt(17, 45, 0).unwrap(), + moonrise: None, + moonset: Some(NaiveTime::from_hms_opt(15, 02, 0).unwrap()), + moon_phase: LunarPhase::WaningCrescent, } ); } diff --git a/ifc/src/lib.rs b/ifc/src/lib.rs index 4834a7c..5ede3e5 100644 --- a/ifc/src/lib.rs +++ b/ifc/src/lib.rs @@ -158,7 +158,7 @@ impl IFC { } } - pub fn month_ifc(&self) -> Month { + pub fn month(&self) -> Month { match *self { IFC::Day(Day { month, .. }) => Month::from(month), IFC::LeapDay(_) => Month::June, @@ -166,7 +166,15 @@ impl IFC { } } - pub fn weekday_ifc(&self) -> DayOfWeek { + pub fn day(&self) -> u8 { + match *self { + IFC::Day(Day { day, .. }) => day, + IFC::LeapDay(_) => 29, + IFC::YearDay(_) => 29, + } + } + + pub fn weekday(&self) -> DayOfWeek { match *self { IFC::LeapDay(_) => DayOfWeek::LeapDay, IFC::YearDay(_) => DayOfWeek::YearDay, @@ -297,6 +305,7 @@ impl From for IFC { } */ +/* impl Datelike for IFC { fn year(&self) -> i32 { // self.year as i32 @@ -418,6 +427,7 @@ impl Datelike for IFC { unimplemented!() } } +*/ #[cfg(test)] mod tests {