Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimryndin committed Feb 24, 2024
1 parent 8985263 commit 2767b87
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 217 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
* 0.1.4
* releases for other platforms
* fine grained notifications control
* installer shell scripts

* 0.1.3
* fix version message for telegram
* releases for other platforms
* fine grained notifications control
* installer shell script

* 0.1.2
* improve an append error handling and reporting
Expand Down
4 changes: 2 additions & 2 deletions src/spreadsheet/datavalue.rs → src/google/datavalue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::google::sheet::{str_to_id, Header, SheetId};
use crate::google::{DEFAULT_FONT, DEFAULT_FONT_TEXT};
use crate::rules::RuleApplicant;
use crate::spreadsheet::sheet::{str_to_id, Header, SheetId};
use crate::spreadsheet::{DEFAULT_FONT, DEFAULT_FONT_TEXT};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use google_sheets4::api::{
CellData, CellFormat, Color, ColorStyle, ExtendedValue, NumberFormat, RowData, TextFormat,
Expand Down
1 change: 0 additions & 1 deletion src/spreadsheet/mod.rs → src/google/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod datavalue;
pub mod sheet;
#[allow(clippy::module_inception)]
pub mod spreadsheet;
use crate::HyperConnector;
use google_sheets4::oauth2;
Expand Down
2 changes: 1 addition & 1 deletion src/spreadsheet/sheet.rs → src/google/sheet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::spreadsheet::{Metadata, DEFAULT_FONT};
use crate::google::{Metadata, DEFAULT_FONT};
use google_sheets4::api::Sheet as GoogleSheet;
use google_sheets4::api::{
AddSheetRequest, AppendCellsRequest, BasicFilter, BooleanCondition, CellData, CellFormat,
Expand Down
10 changes: 5 additions & 5 deletions src/spreadsheet/spreadsheet.rs → src/google/spreadsheet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::spreadsheet::sheet::{CleanupSheet, Rows, Sheet, SheetId, UpdateSheet, VirtualSheet};
use crate::spreadsheet::Metadata;
use crate::google::sheet::{CleanupSheet, Rows, Sheet, SheetId, UpdateSheet, VirtualSheet};
use crate::google::Metadata;
use crate::storage::StorageError;

use crate::notifications::Sender;
#[cfg(not(test))]
use crate::HyperConnector;
use crate::Sender;
use chrono::Utc;
use google_sheets4::api::{
BatchUpdateSpreadsheetRequest, BatchUpdateSpreadsheetResponse, Spreadsheet,
Expand All @@ -25,7 +25,7 @@ use http::response::Response;
use serde_json::Value;

#[cfg(test)]
use crate::spreadsheet::spreadsheet::tests::TestState;
use crate::google::spreadsheet::tests::TestState;

// https://support.google.com/docs/thread/181288162/whats-the-maximum-amount-of-rows-in-google-sheets?hl=en
pub(crate) const GOOGLE_SPREADSHEET_MAXIMUM_CELLS: u64 = 10_000_000;
Expand Down Expand Up @@ -342,7 +342,7 @@ impl SpreadsheetAPI {
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::spreadsheet::sheet::tests::mock_sheet_with_properties;
use crate::google::sheet::tests::mock_sheet_with_properties;
use google_sheets4::api::Sheet as GoogleSheet;
use google_sheets4::api::{
AddSheetRequest, AppendCellsRequest, BasicFilter, CreateDeveloperMetadataRequest,
Expand Down
140 changes: 10 additions & 130 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
pub mod configuration;
pub mod google;
pub mod messenger;
pub mod notifications;
pub mod rules;
pub mod services;
pub mod spreadsheet;
pub mod storage;
use crate::messenger::configuration::MessengerConfig;
use chrono::{DateTime, NaiveDateTime, Utc};
pub use configuration::*;
use google::sheet::TabColorRGB;
pub use google::*;
use google_sheets4::hyper_rustls::HttpsConnector;
use hyper::{client::connect::HttpConnector, Client};
use lazy_static::lazy_static;
pub use messenger::*;
pub use notifications::*;
use regex::Regex;
use services::general::{GeneralService, GENERAL_SERVICE_NAME};
use services::healthcheck::{HealthcheckService, HEALTHCHECK_SERVICE_NAME};
Expand All @@ -19,16 +22,13 @@ use services::logs::{LogsService, LOGS_SERVICE_NAME};
use services::metrics::{MetricsService, METRICS_SERVICE_NAME};
use services::system::{SystemService, SYSTEM_SERVICE_NAME};
pub use services::*;
use spreadsheet::sheet::TabColorRGB;
pub use spreadsheet::*;
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::sync::Arc;
use std::time::Duration;
pub use storage::*;
use sysinfo::System;
use tokio::sync::mpsc::{self, error::TrySendError, Receiver, Sender as TokioSender};
use tracing::Level;
use tokio::sync::mpsc::Receiver;

pub(crate) type HyperConnector = HttpsConnector<HttpConnector>;
pub(crate) type HttpsClient = Client<HyperConnector>;
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn collect_services(
config: Configuration,
shared: Shared,
mut messengers: HashMap<&'static str, Option<Arc<BoxedMessenger>>>,
channel: Receiver<Notification>,
channel: Receiver<notifications::Notification>,
) -> Vec<Box<dyn Service + Sync + Send + 'static>> {
let mut services = Vec::with_capacity(5);
let assertion =
Expand Down Expand Up @@ -194,134 +194,14 @@ pub fn collect_services(
services
}

#[derive(Debug)]
pub struct Notification {
pub(crate) message: String,
pub(crate) level: Level,
}

impl Notification {
pub(crate) fn new(message: String, level: Level) -> Self {
Self { message, level }
}
}

#[derive(Debug, Clone)]
pub struct Sender {
tx: TokioSender<Notification>,
service: &'static str,
}

impl Sender {
pub fn new(tx: TokioSender<Notification>, service: &'static str) -> Self {
Self { tx, service }
}

pub async fn send(&self, notification: Notification) {
if self.tx.send(notification).await.is_err() {
panic!(
"failed to send notification - `{}` service doesn't accept notifications",
self.service
);
}
}

pub fn send_nonblock(&self, notification: Notification) {
match self.tx.try_send(notification) {
Err(TrySendError::Closed(_)) => {
panic!(
"failed to send notification - `{}` service doesn't accept notifications",
self.service
)
}
Err(TrySendError::Full(n)) => {
tracing::error!(
"failed to send notification {:?} - `{}` service notifications queue is full",
n,
self.service
);
}
Ok(_) => (),
}
}

pub async fn info(&self, message: String) {
let notification = Notification::new(message, Level::INFO);
self.send(notification).await
}

pub fn try_info(&self, message: String) {
let notification = Notification::new(message, Level::INFO);
self.send_nonblock(notification)
}

pub async fn warn(&self, message: String) {
let notification = Notification::new(message, Level::WARN);
self.send(notification).await
}

pub fn try_warn(&self, message: String) {
let notification = Notification::new(message, Level::WARN);
self.send_nonblock(notification)
}

pub async fn error(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send(notification).await
}

pub fn try_error(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send_nonblock(notification)
}

pub async fn fatal(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send(notification).await;
// for fatal errors we need some time to send error
// It is more important to notify user via messenger than to
// restart quickly because restart doesn't help for recovery
// user is required to fix a problem
tokio::time::sleep(Duration::from_millis(500)).await;
}
}

pub(crate) fn setup_messenger_channel(service: &'static str) -> (Sender, Receiver<Notification>) {
let (tx, rx) = mpsc::channel(60); // 60 simultaneous messages is enough for any rate limiting messenger
let tx = Sender::new(tx, service);
(tx, rx)
}

pub fn setup_general_messenger_channel() -> (Sender, Receiver<Notification>) {
setup_messenger_channel(GENERAL_SERVICE_NAME)
}

#[derive(Debug)]
pub(crate) struct MessengerApi {
pub(crate) config: MessengerConfig,
pub(crate) message_tx: Sender,
pub(crate) message_rx: Option<Receiver<Notification>>,
}

impl MessengerApi {
fn new(config: MessengerConfig, service: &'static str) -> Self {
let (message_tx, message_rx) = setup_messenger_channel(service);
Self {
config,
message_tx,
message_rx: Some(message_rx),
}
}
}

#[derive(Clone)]
pub struct Shared {
pub(crate) messenger: Option<Arc<BoxedMessenger>>,
pub(crate) send_notification: Sender,
pub(crate) send_notification: notifications::Sender,
}

impl Shared {
pub fn new(send_notification: Sender) -> Shared {
pub fn new(send_notification: notifications::Sender) -> Shared {
Self {
messenger: None,
send_notification,
Expand All @@ -342,7 +222,7 @@ impl Debug for Shared {
}

pub async fn welcome(
send_notification: Sender,
send_notification: notifications::Sender,
project_id: String,
truncation_check: Result<(), String>,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clap::Parser;
use futures::future::try_join_all;
use goral::configuration::{Configuration, APP_NAME};
use goral::spreadsheet::{get_google_auth, SpreadsheetAPI};
use goral::google::{get_google_auth, SpreadsheetAPI};
use goral::storage::{AppendableLog, Storage};
use goral::{
collect_messengers, collect_services, setup_general_messenger_channel, welcome, Shared,
Expand Down
126 changes: 126 additions & 0 deletions src/notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use crate::messenger::configuration::MessengerConfig;
use crate::services::general::GENERAL_SERVICE_NAME;
use std::fmt::Debug;
use std::time::Duration;
use tokio::sync::mpsc::{self, error::TrySendError, Receiver, Sender as TokioSender};
use tracing::Level;

#[derive(Debug)]
pub struct Notification {
pub(crate) message: String,
pub(crate) level: Level,
}

impl Notification {
pub(crate) fn new(message: String, level: Level) -> Self {
Self { message, level }
}
}

#[derive(Debug, Clone)]
pub struct Sender {
tx: TokioSender<Notification>,
service: &'static str,
}

impl Sender {
pub fn new(tx: TokioSender<Notification>, service: &'static str) -> Self {
Self { tx, service }
}

pub async fn send(&self, notification: Notification) {
if self.tx.send(notification).await.is_err() {
panic!(
"failed to send notification - `{}` service doesn't accept notifications",
self.service
);
}
}

pub fn send_nonblock(&self, notification: Notification) {
match self.tx.try_send(notification) {
Err(TrySendError::Closed(_)) => {
panic!(
"failed to send notification - `{}` service doesn't accept notifications",
self.service
)
}
Err(TrySendError::Full(n)) => {
tracing::error!(
"failed to send notification {:?} - `{}` service notifications queue is full",
n,
self.service
);
}
Ok(_) => (),
}
}

pub async fn info(&self, message: String) {
let notification = Notification::new(message, Level::INFO);
self.send(notification).await
}

pub fn try_info(&self, message: String) {
let notification = Notification::new(message, Level::INFO);
self.send_nonblock(notification)
}

pub async fn warn(&self, message: String) {
let notification = Notification::new(message, Level::WARN);
self.send(notification).await
}

pub fn try_warn(&self, message: String) {
let notification = Notification::new(message, Level::WARN);
self.send_nonblock(notification)
}

pub async fn error(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send(notification).await
}

pub fn try_error(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send_nonblock(notification)
}

pub async fn fatal(&self, message: String) {
let notification = Notification::new(message, Level::ERROR);
self.send(notification).await;
// for fatal errors we need some time to send error
// It is more important to notify user via messenger than to
// restart quickly because restart doesn't help for recovery
// user is required to fix a problem
tokio::time::sleep(Duration::from_millis(500)).await;
}
}

pub(crate) fn setup_messenger_channel(service: &'static str) -> (Sender, Receiver<Notification>) {
let (tx, rx) = mpsc::channel(60); // 60 simultaneous messages is enough for any rate limiting messenger
let tx = Sender::new(tx, service);
(tx, rx)
}

pub fn setup_general_messenger_channel() -> (Sender, Receiver<Notification>) {
setup_messenger_channel(GENERAL_SERVICE_NAME)
}

#[derive(Debug)]
pub(crate) struct MessengerApi {
pub(crate) config: MessengerConfig,
pub(crate) message_tx: Sender,
pub(crate) message_rx: Option<Receiver<Notification>>,
}

impl MessengerApi {
pub(crate) fn new(config: MessengerConfig, service: &'static str) -> Self {
let (message_tx, message_rx) = setup_messenger_channel(service);
Self {
config,
message_tx,
message_rx: Some(message_rx),
}
}
}
Loading

0 comments on commit 2767b87

Please sign in to comment.