From f1698db0e22bd14eafb71d33f401aac07fe4bb9c Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 11:51:01 +0200 Subject: [PATCH 01/10] Initial version of weather microservice --- microservices/readme.md | 3 ++ microservices/weather/.gitignore | 14 +++++++ microservices/weather/Cargo.toml | 18 ++++++++ microservices/weather/Settings.toml | 6 +++ microservices/weather/readme.md | 3 ++ microservices/weather/rustfmt.toml | 1 + microservices/weather/src/fetch.rs | 62 ++++++++++++++++++++++++++++ microservices/weather/src/input.rs | 15 +++++++ microservices/weather/src/main.rs | 47 +++++++++++++++++++++ microservices/weather/src/mode.rs | 7 ++++ microservices/weather/src/publish.rs | 0 microservices/weather/src/write.rs | 21 ++++++++++ 12 files changed, 197 insertions(+) create mode 100644 microservices/readme.md create mode 100644 microservices/weather/.gitignore create mode 100644 microservices/weather/Cargo.toml create mode 100644 microservices/weather/Settings.toml create mode 100644 microservices/weather/readme.md create mode 100644 microservices/weather/rustfmt.toml create mode 100644 microservices/weather/src/fetch.rs create mode 100644 microservices/weather/src/input.rs create mode 100644 microservices/weather/src/main.rs create mode 100644 microservices/weather/src/mode.rs create mode 100644 microservices/weather/src/publish.rs create mode 100644 microservices/weather/src/write.rs diff --git a/microservices/readme.md b/microservices/readme.md new file mode 100644 index 0000000..d357649 --- /dev/null +++ b/microservices/readme.md @@ -0,0 +1,3 @@ +# Microservices + +TODO diff --git a/microservices/weather/.gitignore b/microservices/weather/.gitignore new file mode 100644 index 0000000..6985cf1 --- /dev/null +++ b/microservices/weather/.gitignore @@ -0,0 +1,14 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/microservices/weather/Cargo.toml b/microservices/weather/Cargo.toml new file mode 100644 index 0000000..99d8736 --- /dev/null +++ b/microservices/weather/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "weather" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.71" +chrono = "0.4.24" +config = "0.13.3" +env_logger = "0.10.0" +influxdb = { version = "0.6.0", features = ["derive"] } +log = "0.4.17" +reqwest = { version = "0.11.17", features = ["json", "serde_json"] } +serde = { version = "1.0.162", features = ["serde_derive"] } +serde_json = "1.0.96" +tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] } diff --git a/microservices/weather/Settings.toml b/microservices/weather/Settings.toml new file mode 100644 index 0000000..e885a66 --- /dev/null +++ b/microservices/weather/Settings.toml @@ -0,0 +1,6 @@ +weather_api_base_url="https://api.openweathermap.org/data/2.5/weather" +weather_api_key="4ce897f7ce77f84919fd990cc68d9d20" +influx_db_base_url="http://jumpbox.io:8092" +influx_db_token="R1BGVaxWrX_ySaovweRz6ZTBDvHa5omQkricJGmn3PrI44jpfU7411PSmHOCjGcNR7OcBjmtE9gA62boHHXoKg==" +latitude="50.854019" +longitude="3.355230" diff --git a/microservices/weather/readme.md b/microservices/weather/readme.md new file mode 100644 index 0000000..d296156 --- /dev/null +++ b/microservices/weather/readme.md @@ -0,0 +1,3 @@ +# Weather microservice + +This microservice will control all weather related actions around the house. diff --git a/microservices/weather/rustfmt.toml b/microservices/weather/rustfmt.toml new file mode 100644 index 0000000..eec13eb --- /dev/null +++ b/microservices/weather/rustfmt.toml @@ -0,0 +1 @@ +edition="2021" diff --git a/microservices/weather/src/fetch.rs b/microservices/weather/src/fetch.rs new file mode 100644 index 0000000..a74edbd --- /dev/null +++ b/microservices/weather/src/fetch.rs @@ -0,0 +1,62 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct WeatherResponse { + pub id: usize, + pub weather: Vec, + pub wind: Wind, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Weather { + pub id: usize, + pub main: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Wind { + pub speed: f32, +} + +fn construct_weather_data_url( + base_url: &str, + latitude: &str, + longitude: &str, + api_key: &str, + current_datetime: &DateTime, +) -> String { + format!( + "{}?lat={}&lon={}&dt={}&appid={}", + base_url, latitude, longitude, current_datetime.timestamp(), api_key + ) +} + +pub async fn fetch_weather_data( + base_url: &str, + latitude: &str, + longitude: &str, + api_key: &str, + current_datetime: &DateTime, +) -> Result { + let url = construct_weather_data_url(base_url, latitude, longitude, api_key, current_datetime); + reqwest::get(url).await?.json().await +} + +#[cfg(test)] +mod test { + use super::*; + use chrono::prelude::*; + + #[test] + fn should_construct_url() { + let result = construct_weather_data_url( + "http://test", + "1.14", + "1.12", + "abc", + &Utc.with_ymd_and_hms(2023, 1, 2, 1, 0, 0).unwrap(), + ); + assert_eq!("http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc", result); + } +} diff --git a/microservices/weather/src/input.rs b/microservices/weather/src/input.rs new file mode 100644 index 0000000..8015d37 --- /dev/null +++ b/microservices/weather/src/input.rs @@ -0,0 +1,15 @@ +use serde::Deserialize; +use crate::mode::Mode; + +#[derive(Deserialize, Debug)] +pub struct Input +{ + #[serde(default)] + pub program_mode: Mode, + pub weather_api_base_url: String, + pub weather_api_key: String, + pub influx_db_base_url: String, + pub influx_db_token: String, + pub latitude: String, + pub longitude: String +} diff --git a/microservices/weather/src/main.rs b/microservices/weather/src/main.rs new file mode 100644 index 0000000..3e12289 --- /dev/null +++ b/microservices/weather/src/main.rs @@ -0,0 +1,47 @@ +mod fetch; +mod input; +mod mode; +mod write; + +use anyhow::Result; +use config::Config; +use log::info; +use chrono::Utc; + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::init(); + + let settings: input::Input = fetch_settings()?; + + match settings.program_mode + { + mode::Mode::ReadWeatherData => read_weather_data(&settings).await? + }; + + Ok(()) +} + +async fn read_weather_data(settings: &input::Input) -> Result<()> { + info!("Start fetching weather data..."); + let result = fetch::fetch_weather_data(&settings.weather_api_base_url, &settings.latitude, &settings.longitude, &settings.weather_api_key, &Utc::now()).await?; + + info!("Start writing weather data..."); + let map = write::WeatherReading { + wind_speed: result.wind.speed, + time: Utc::now(), + }; + write::write_weather_data(map, &settings.influx_db_base_url, "homeassistant", &settings.influx_db_token).await?; + Ok(()) +} + +fn fetch_settings() -> Result { + let settings = Config::builder() + // Add in `./Settings.toml` + .add_source(config::File::with_name("./Settings").required(false)) + .add_source(config::Environment::with_prefix("weather")) + .build() + .unwrap(); + + settings.try_deserialize::() +} diff --git a/microservices/weather/src/mode.rs b/microservices/weather/src/mode.rs new file mode 100644 index 0000000..8187edc --- /dev/null +++ b/microservices/weather/src/mode.rs @@ -0,0 +1,7 @@ +use serde::Deserialize; + +#[derive(Deserialize, Debug, Default)] +pub enum Mode { + #[default] + ReadWeatherData, +} diff --git a/microservices/weather/src/publish.rs b/microservices/weather/src/publish.rs new file mode 100644 index 0000000..e69de29 diff --git a/microservices/weather/src/write.rs b/microservices/weather/src/write.rs new file mode 100644 index 0000000..54c1006 --- /dev/null +++ b/microservices/weather/src/write.rs @@ -0,0 +1,21 @@ +use chrono::{DateTime, Utc}; +use influxdb::{Client, InfluxDbWriteable}; +use anyhow::Result; + +#[derive(InfluxDbWriteable)] +pub struct WeatherReading { + pub time: DateTime, + pub wind_speed: f32, +} + +pub async fn write_weather_data( + reading: WeatherReading, + base_url: &str, + database: &str, + token: &str, +) -> Result<()> { + let client = Client::new(base_url, database).with_token(token); + let query = reading.into_query("WeatherReading"); + client.query(query).await?; + Ok(()) +} From 02c0673e647b4b5eb3a4f1dfb3c7d4a6651f7c12 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 12:02:16 +0200 Subject: [PATCH 02/10] Removed Settings.toml --- microservices/weather/.gitignore | 3 +++ microservices/weather/Settings.toml | 6 ------ microservices/weather/readme.md | 13 +++++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) delete mode 100644 microservices/weather/Settings.toml diff --git a/microservices/weather/.gitignore b/microservices/weather/.gitignore index 6985cf1..ae770c1 100644 --- a/microservices/weather/.gitignore +++ b/microservices/weather/.gitignore @@ -12,3 +12,6 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + +# App settings +Settings.toml diff --git a/microservices/weather/Settings.toml b/microservices/weather/Settings.toml deleted file mode 100644 index e885a66..0000000 --- a/microservices/weather/Settings.toml +++ /dev/null @@ -1,6 +0,0 @@ -weather_api_base_url="https://api.openweathermap.org/data/2.5/weather" -weather_api_key="4ce897f7ce77f84919fd990cc68d9d20" -influx_db_base_url="http://jumpbox.io:8092" -influx_db_token="R1BGVaxWrX_ySaovweRz6ZTBDvHa5omQkricJGmn3PrI44jpfU7411PSmHOCjGcNR7OcBjmtE9gA62boHHXoKg==" -latitude="50.854019" -longitude="3.355230" diff --git a/microservices/weather/readme.md b/microservices/weather/readme.md index d296156..c2607ae 100644 --- a/microservices/weather/readme.md +++ b/microservices/weather/readme.md @@ -1,3 +1,16 @@ # Weather microservice This microservice will control all weather related actions around the house. + +## Test locally + +Add `Settings.toml` with following content: + +```toml +weather_api_base_url="xxx" +weather_api_key="xxx" +influx_db_base_url="xxx" +influx_db_token="xxx" +latitude="xxx" +longitude="xxx" +``` From ab14ca65c596d96560b2047e84bca965d4137fa6 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 12:08:41 +0200 Subject: [PATCH 03/10] Added build & run docker scripts --- microservices/weather/.dockerignore | 17 +++++++++++++++++ microservices/weather/Dockerfile | 9 +++++++++ microservices/weather/build-docker.sh | 3 +++ microservices/weather/readme.md | 2 ++ microservices/weather/run-docker.sh | 0 5 files changed, 31 insertions(+) create mode 100644 microservices/weather/.dockerignore create mode 100644 microservices/weather/Dockerfile create mode 100755 microservices/weather/build-docker.sh create mode 100755 microservices/weather/run-docker.sh diff --git a/microservices/weather/.dockerignore b/microservices/weather/.dockerignore new file mode 100644 index 0000000..ae770c1 --- /dev/null +++ b/microservices/weather/.dockerignore @@ -0,0 +1,17 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# App settings +Settings.toml diff --git a/microservices/weather/Dockerfile b/microservices/weather/Dockerfile new file mode 100644 index 0000000..e1ab623 --- /dev/null +++ b/microservices/weather/Dockerfile @@ -0,0 +1,9 @@ +FROM rust:1.61.0 as builder +# 2. Copy the files in your machine to the Docker image +COPY ./ ./ + +# Build your program for release +RUN cargo build --release + +# Run the binary +CMD ["./target/release/test_weather_data"] diff --git a/microservices/weather/build-docker.sh b/microservices/weather/build-docker.sh new file mode 100755 index 0000000..5bfb4ea --- /dev/null +++ b/microservices/weather/build-docker.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +docker build . -t weather-microservice:latest diff --git a/microservices/weather/readme.md b/microservices/weather/readme.md index c2607ae..4581839 100644 --- a/microservices/weather/readme.md +++ b/microservices/weather/readme.md @@ -14,3 +14,5 @@ influx_db_token="xxx" latitude="xxx" longitude="xxx" ``` + +These settings can also be specified in the environment variables, when running in docker. diff --git a/microservices/weather/run-docker.sh b/microservices/weather/run-docker.sh new file mode 100755 index 0000000..e69de29 From 91537cdac3635d0a92302c1bf561d4e88089e927 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 13:03:15 +0200 Subject: [PATCH 04/10] Added .env file --- microservices/weather/.env | 6 ++++++ microservices/weather/Dockerfile | 16 ++++++++-------- microservices/weather/build-docker.sh | 2 ++ 3 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 microservices/weather/.env diff --git a/microservices/weather/.env b/microservices/weather/.env new file mode 100644 index 0000000..d3c36eb --- /dev/null +++ b/microservices/weather/.env @@ -0,0 +1,6 @@ +weather_weather_api_base_url= +weather_weather_api_key= +weather_influx_db_base_url= +weather_influx_db_token= +weather_latitude= +weather_longitude= diff --git a/microservices/weather/Dockerfile b/microservices/weather/Dockerfile index e1ab623..7369127 100644 --- a/microservices/weather/Dockerfile +++ b/microservices/weather/Dockerfile @@ -1,9 +1,9 @@ -FROM rust:1.61.0 as builder -# 2. Copy the files in your machine to the Docker image -COPY ./ ./ +FROM rust:1.67 as builder +WORKDIR /usr/src/weather +COPY . . +RUN cargo install --path . -# Build your program for release -RUN cargo build --release - -# Run the binary -CMD ["./target/release/test_weather_data"] +FROM debian:bullseye-slim as runtime-base +RUN apt-get update && apt-get install -y libssl1.1 ca-certificates libsasl2-dev && rm -rf /var/lib/apt/lists/* +COPY --from=builder /usr/local/cargo/bin/weather /usr/local/bin/weather +CMD ["weather"] diff --git a/microservices/weather/build-docker.sh b/microservices/weather/build-docker.sh index 5bfb4ea..34d3345 100755 --- a/microservices/weather/build-docker.sh +++ b/microservices/weather/build-docker.sh @@ -1,3 +1,5 @@ #!/usr/bin/env bash docker build . -t weather-microservice:latest +docker tag weather-microservice:latest registry.willemserruys.com/weather-microservice:latest +docker push registry.willemserruys.com/weather-microservice:latest From 3967c16bf75475bd9002ccefd949c9ad28f00d33 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 13:07:23 +0200 Subject: [PATCH 05/10] Run microservices script --- microservices/weather/.gitignore | 2 ++ microservices/weather/run-docker.sh | 3 +++ 2 files changed, 5 insertions(+) diff --git a/microservices/weather/.gitignore b/microservices/weather/.gitignore index ae770c1..80bb9dd 100644 --- a/microservices/weather/.gitignore +++ b/microservices/weather/.gitignore @@ -15,3 +15,5 @@ Cargo.lock # App settings Settings.toml + +.env diff --git a/microservices/weather/run-docker.sh b/microservices/weather/run-docker.sh index e69de29..b8a3b83 100755 --- a/microservices/weather/run-docker.sh +++ b/microservices/weather/run-docker.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +docker run --env-file ./env registry.willemserruys.com/weather-microservice:latest From aa5d13e1d422b7bd253be853b234290b4b451f1e Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 13:32:08 +0200 Subject: [PATCH 06/10] Updated run-docker scripts --- microservices/weather/.env | 12 ++++++------ microservices/weather/run-docker.sh | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/microservices/weather/.env b/microservices/weather/.env index d3c36eb..12c93cb 100644 --- a/microservices/weather/.env +++ b/microservices/weather/.env @@ -1,6 +1,6 @@ -weather_weather_api_base_url= -weather_weather_api_key= -weather_influx_db_base_url= -weather_influx_db_token= -weather_latitude= -weather_longitude= +weather_weather_api_base_url=https://api.openweathermap.org/data/2.5/weather +weather_weather_api_key=4ce897f7ce77f84919fd990cc68d9d20 +weather_influx_db_base_url=http://jumpbox.io:8092 +weather_influx_db_token=R1BGVaxWrX_ySaovweRz6ZTBDvHa5omQkricJGmn3PrI44jpfU7411PSmHOCjGcNR7OcBjmtE9gA62boHHXoKg== +weather_latitude=50.854019 +weather_longitude=3.355230 diff --git a/microservices/weather/run-docker.sh b/microservices/weather/run-docker.sh index b8a3b83..cbacc31 100755 --- a/microservices/weather/run-docker.sh +++ b/microservices/weather/run-docker.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -docker run --env-file ./env registry.willemserruys.com/weather-microservice:latest +docker run --env RUST_LOG=info --env-file ./.env registry.willemserruys.com/weather-microservice:latest &> logs.txt From 76a242f58b68c03180a9b3a181a72f15b956d067 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 13:43:37 +0200 Subject: [PATCH 07/10] Modified docker run script --- microservices/weather/.gitignore | 3 +++ microservices/weather/run-docker.sh | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/microservices/weather/.gitignore b/microservices/weather/.gitignore index 80bb9dd..f5b3191 100644 --- a/microservices/weather/.gitignore +++ b/microservices/weather/.gitignore @@ -17,3 +17,6 @@ Cargo.lock Settings.toml .env + +# Log files +logs.*.txt diff --git a/microservices/weather/run-docker.sh b/microservices/weather/run-docker.sh index cbacc31..43c1d3a 100755 --- a/microservices/weather/run-docker.sh +++ b/microservices/weather/run-docker.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash +file=logs.$(date "+%F").txt -docker run --env RUST_LOG=info --env-file ./.env registry.willemserruys.com/weather-microservice:latest &> logs.txt +docker run --env RUST_LOG=info --env-file ./.env registry.willemserruys.com/weather-microservice:latest &> $file From a57371b2cfa2d0f0f6ff1bb29046c99fc0e82250 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 13:44:58 +0200 Subject: [PATCH 08/10] Formatting --- microservices/weather/.gitignore | 1 - microservices/weather/src/fetch.rs | 11 +++++++++-- microservices/weather/src/input.rs | 7 +++---- microservices/weather/src/main.rs | 24 ++++++++++++++++++------ microservices/weather/src/publish.rs | 0 microservices/weather/src/write.rs | 2 +- 6 files changed, 31 insertions(+), 14 deletions(-) delete mode 100644 microservices/weather/src/publish.rs diff --git a/microservices/weather/.gitignore b/microservices/weather/.gitignore index f5b3191..9f674f0 100644 --- a/microservices/weather/.gitignore +++ b/microservices/weather/.gitignore @@ -15,7 +15,6 @@ Cargo.lock # App settings Settings.toml - .env # Log files diff --git a/microservices/weather/src/fetch.rs b/microservices/weather/src/fetch.rs index a74edbd..26fb593 100644 --- a/microservices/weather/src/fetch.rs +++ b/microservices/weather/src/fetch.rs @@ -28,7 +28,11 @@ fn construct_weather_data_url( ) -> String { format!( "{}?lat={}&lon={}&dt={}&appid={}", - base_url, latitude, longitude, current_datetime.timestamp(), api_key + base_url, + latitude, + longitude, + current_datetime.timestamp(), + api_key ) } @@ -57,6 +61,9 @@ mod test { "abc", &Utc.with_ymd_and_hms(2023, 1, 2, 1, 0, 0).unwrap(), ); - assert_eq!("http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc", result); + assert_eq!( + "http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc", + result + ); } } diff --git a/microservices/weather/src/input.rs b/microservices/weather/src/input.rs index 8015d37..9c7de13 100644 --- a/microservices/weather/src/input.rs +++ b/microservices/weather/src/input.rs @@ -1,9 +1,8 @@ -use serde::Deserialize; use crate::mode::Mode; +use serde::Deserialize; #[derive(Deserialize, Debug)] -pub struct Input -{ +pub struct Input { #[serde(default)] pub program_mode: Mode, pub weather_api_base_url: String, @@ -11,5 +10,5 @@ pub struct Input pub influx_db_base_url: String, pub influx_db_token: String, pub latitude: String, - pub longitude: String + pub longitude: String, } diff --git a/microservices/weather/src/main.rs b/microservices/weather/src/main.rs index 3e12289..4b3f0fc 100644 --- a/microservices/weather/src/main.rs +++ b/microservices/weather/src/main.rs @@ -4,9 +4,9 @@ mod mode; mod write; use anyhow::Result; +use chrono::Utc; use config::Config; use log::info; -use chrono::Utc; #[tokio::main] async fn main() -> Result<()> { @@ -14,9 +14,8 @@ async fn main() -> Result<()> { let settings: input::Input = fetch_settings()?; - match settings.program_mode - { - mode::Mode::ReadWeatherData => read_weather_data(&settings).await? + match settings.program_mode { + mode::Mode::ReadWeatherData => read_weather_data(&settings).await?, }; Ok(()) @@ -24,14 +23,27 @@ async fn main() -> Result<()> { async fn read_weather_data(settings: &input::Input) -> Result<()> { info!("Start fetching weather data..."); - let result = fetch::fetch_weather_data(&settings.weather_api_base_url, &settings.latitude, &settings.longitude, &settings.weather_api_key, &Utc::now()).await?; + let result = fetch::fetch_weather_data( + &settings.weather_api_base_url, + &settings.latitude, + &settings.longitude, + &settings.weather_api_key, + &Utc::now(), + ) + .await?; info!("Start writing weather data..."); let map = write::WeatherReading { wind_speed: result.wind.speed, time: Utc::now(), }; - write::write_weather_data(map, &settings.influx_db_base_url, "homeassistant", &settings.influx_db_token).await?; + write::write_weather_data( + map, + &settings.influx_db_base_url, + "homeassistant", + &settings.influx_db_token, + ) + .await?; Ok(()) } diff --git a/microservices/weather/src/publish.rs b/microservices/weather/src/publish.rs deleted file mode 100644 index e69de29..0000000 diff --git a/microservices/weather/src/write.rs b/microservices/weather/src/write.rs index 54c1006..4345f45 100644 --- a/microservices/weather/src/write.rs +++ b/microservices/weather/src/write.rs @@ -1,6 +1,6 @@ +use anyhow::Result; use chrono::{DateTime, Utc}; use influxdb::{Client, InfluxDbWriteable}; -use anyhow::Result; #[derive(InfluxDbWriteable)] pub struct WeatherReading { From 5ae46ed06fc9233c9fe6707f2316f76fe6eb8745 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 21:36:52 +0200 Subject: [PATCH 09/10] Added additional weather data --- microservices/weather/src/fetch.rs | 22 ++++++++++++++++++---- microservices/weather/src/main.rs | 10 +++++++++- microservices/weather/src/write.rs | 4 ++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/microservices/weather/src/fetch.rs b/microservices/weather/src/fetch.rs index 26fb593..eccb006 100644 --- a/microservices/weather/src/fetch.rs +++ b/microservices/weather/src/fetch.rs @@ -3,22 +3,33 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct WeatherResponse { - pub id: usize, pub weather: Vec, pub wind: Wind, + pub main: Main, + pub clouds: Clouds, } #[derive(Debug, Serialize, Deserialize)] pub struct Weather { - pub id: usize, + pub id: u16, pub main: String, } +#[derive(Debug, Serialize, Deserialize)] +pub struct Main { + pub temp: f32, +} + #[derive(Debug, Serialize, Deserialize)] pub struct Wind { pub speed: f32, } +#[derive(Debug, Serialize, Deserialize)] +pub struct Clouds { + pub all: u16, +} + fn construct_weather_data_url( base_url: &str, latitude: &str, @@ -27,7 +38,7 @@ fn construct_weather_data_url( current_datetime: &DateTime, ) -> String { format!( - "{}?lat={}&lon={}&dt={}&appid={}", + "{}?lat={}&lon={}&dt={}&appid={}&exclude=minutely,hourly,daily,alerts", base_url, latitude, longitude, @@ -44,6 +55,9 @@ pub async fn fetch_weather_data( current_datetime: &DateTime, ) -> Result { let url = construct_weather_data_url(base_url, latitude, longitude, api_key, current_datetime); + let resp = reqwest::get(url.clone()).await?.text().await; + println!("{:?}", resp); + reqwest::get(url).await?.json().await } @@ -62,7 +76,7 @@ mod test { &Utc.with_ymd_and_hms(2023, 1, 2, 1, 0, 0).unwrap(), ); assert_eq!( - "http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc", + "http://test?lat=1.14&lon=1.12&dt=1672621200&appid=abc&exclude=minutely,hourly,daily,alerts", result ); } diff --git a/microservices/weather/src/main.rs b/microservices/weather/src/main.rs index 4b3f0fc..7e7b79c 100644 --- a/microservices/weather/src/main.rs +++ b/microservices/weather/src/main.rs @@ -33,10 +33,18 @@ async fn read_weather_data(settings: &input::Input) -> Result<()> { .await?; info!("Start writing weather data..."); - let map = write::WeatherReading { + let mut map = write::WeatherReading { wind_speed: result.wind.speed, time: Utc::now(), + temp: result.main.temp, + clouds: result.clouds.all, + main: None, + id: None, }; + if let Some(weather_element) = result.weather.first() { + map.main = Some(weather_element.main.clone()); + map.id = Some(weather_element.id); + } write::write_weather_data( map, &settings.influx_db_base_url, diff --git a/microservices/weather/src/write.rs b/microservices/weather/src/write.rs index 4345f45..fd1feb4 100644 --- a/microservices/weather/src/write.rs +++ b/microservices/weather/src/write.rs @@ -6,6 +6,10 @@ use influxdb::{Client, InfluxDbWriteable}; pub struct WeatherReading { pub time: DateTime, pub wind_speed: f32, + pub clouds: u16, + pub temp: f32, + pub id: Option, + pub main: Option, } pub async fn write_weather_data( From dafe647a18fbe8d9b62cadf0bc15bdcb1724ea05 Mon Sep 17 00:00:00 2001 From: wserr Date: Sun, 7 May 2023 22:08:28 +0200 Subject: [PATCH 10/10] Remove logging that was too much --- microservices/weather/src/fetch.rs | 3 --- microservices/weather/src/main.rs | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/microservices/weather/src/fetch.rs b/microservices/weather/src/fetch.rs index eccb006..cf1664a 100644 --- a/microservices/weather/src/fetch.rs +++ b/microservices/weather/src/fetch.rs @@ -55,9 +55,6 @@ pub async fn fetch_weather_data( current_datetime: &DateTime, ) -> Result { let url = construct_weather_data_url(base_url, latitude, longitude, api_key, current_datetime); - let resp = reqwest::get(url.clone()).await?.text().await; - println!("{:?}", resp); - reqwest::get(url).await?.json().await } diff --git a/microservices/weather/src/main.rs b/microservices/weather/src/main.rs index 7e7b79c..4f5f425 100644 --- a/microservices/weather/src/main.rs +++ b/microservices/weather/src/main.rs @@ -52,6 +52,7 @@ async fn read_weather_data(settings: &input::Input) -> Result<()> { &settings.influx_db_token, ) .await?; + println!(""); Ok(()) }