Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Loudbooks committed Dec 12, 2024
1 parent a3aeae0 commit 1e8c9e6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 3 additions & 2 deletions backend/src/database/aws_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use aws_sdk_s3::config::{Credentials, Region, SharedCredentialsProvider};
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::{Client, Config};
use log::info;

pub struct AWSService {
client: Client,
Expand All @@ -10,7 +11,7 @@ pub struct AWSService {

impl AWSService {
pub async fn new(endpoint: &str, bucket_name: &str, access_key: &str, secret_key: &str) -> Result<Self> {
println!("Connecting to AWS S3...");
info!("Connecting to AWS S3...");
let region = Region::new("auto");
let credentials = Credentials::from_keys(access_key, secret_key, None);
let shared_credentials = SharedCredentialsProvider::new(credentials);
Expand All @@ -25,7 +26,7 @@ impl AWSService {

let client = Client::from_conf(config);

println!("Connected to AWS S3.");
info!("Connected to AWS S3.");

Ok(Self {
client,
Expand Down
21 changes: 11 additions & 10 deletions backend/src/database/migration_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::models::paste::Paste;
use crate::models::user::User;
use futures_util::StreamExt;
use log::{error, info};
use mongodb::bson::{doc, Document};
use mongodb::{Collection, Database};

Expand All @@ -22,17 +23,17 @@ impl MigrationService {
}

pub async fn run_migrations(&self) {
self.user_migration_12_11_24().await.map_err(|e| println!("Error migrating users: {}", e)).ok();
self.paste_migration_12_11_24().await.map_err(|e| println!("Error migrating pastes: {}", e)).ok();
self.user_migration_12_11_24().await.map_err(|e| error!("Error migrating users: {}", e)).ok();
self.paste_migration_12_11_24().await.map_err(|e| error!("Error migrating pastes: {}", e)).ok();
}

pub async fn user_migration_12_11_24(&self) -> Result<(), mongodb::error::Error> {
if !self.database.list_collection_names().await?.contains(&"user".to_string()) {
println!("No users to migrate.");
info!("No users to migrate.");
return Ok(());
}

println!("Migrating users...");
info!("Migrating users...");
let old_user_collection = self.database.collection::<Document>("user");

let mut amount = 0;
Expand All @@ -48,7 +49,7 @@ impl MigrationService {
let last_visit = old_user.get_i64("lastVisit").unwrap_or_default();

if user_ip.is_empty() || user_id.is_empty() {
println!("Skipping user with empty IP or ID.");
info!("Skipping user with empty IP or ID.");
continue;
}

Expand All @@ -65,7 +66,7 @@ impl MigrationService {
amount += 1;
}

println!("Migrated {} out of {} users", amount, target_amount);
info!("Migrated {} out of {} users", amount, target_amount);

let qualified_old_database_name = self.database.name().to_owned() + ".user";
let qualified_new_database_name = self.database.name().to_owned() + ".user_migrated_12_11_24";
Expand All @@ -81,11 +82,11 @@ impl MigrationService {

pub async fn paste_migration_12_11_24(&self) -> Result<(), mongodb::error::Error> {
if !self.database.list_collection_names().await?.contains(&"pastePrivateDTO".to_string()) {
println!("No pastes to migrate.");
info!("No pastes to migrate.");
return Ok(());
}

println!("Migrating pastes...");
info!("Migrating pastes...");
let old_paste_collection = self.database.collection::<Document>("pastePrivateDTO");

let mut amount = 0;
Expand All @@ -102,7 +103,7 @@ impl MigrationService {
let paste_wrap = old_paste.get_bool("wrap").unwrap_or_default();

if paste_id.is_empty() {
println!("Skipping paste with empty ID.");
info!("Skipping paste with empty ID.");
continue;
}

Expand All @@ -121,7 +122,7 @@ impl MigrationService {
amount += 1;
}

println!("Migrated {} out of {} pastes", amount, target_amount);
info!("Migrated {} out of {} pastes", amount, target_amount);

let qualified_old_database_name = self.database.name().to_owned() + ".pastePrivateDTO";
let qualified_new_database_name = self.database.name().to_owned() + ".pastePrivateDTO_migrated_12_11_24";
Expand Down
5 changes: 3 additions & 2 deletions backend/src/database/mongodb_service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::info;
use mongodb::bson::{doc, uuid};
use mongodb::{Client, Collection, Cursor};
use mongodb::options::ClientOptions;
Expand All @@ -13,7 +14,7 @@ pub struct MongoService {

impl MongoService {
pub async fn new(uri: &str, db_name: &str) -> MongoResult<Self> {
println!("Connecting to MongoDB...");
info!("Connecting to MongoDB...");
let client_options = ClientOptions::parse(uri).await?;
let client = Client::with_options(client_options)?;

Expand All @@ -22,7 +23,7 @@ impl MongoService {
let user_collection = database.collection::<User>("users");
let paste_collection = database.collection::<Paste>("pastes");

println!("Connected to MongoDB.");
info!("Connected to MongoDB.");

let migration_service = MigrationService::new(&database, &admin_database, &user_collection, &paste_collection);
migration_service.run_migrations().await;
Expand Down

0 comments on commit 1e8c9e6

Please sign in to comment.