Skip to content

Commit e4c5da0

Browse files
committed
feat: add possibility to output all logs continiously
1 parent b366b56 commit e4c5da0

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed

src/main.rs

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::{arg, Parser};
22
use serde_json::{to_string, Value};
33
use std::time::Duration;
4+
use reqwest::Error;
45
use tokio::time::sleep;
56

67
/// Algolia index size monitor
@@ -16,14 +17,17 @@ struct Args {
1617
/// Name of the index to monitor
1718
index_name: String,
1819

20+
#[arg(short, long, default_value = "false")]
21+
all_logs: bool,
22+
1923
#[arg(short, long, default_value = "0")]
2024
expected_records: u64,
2125

2226
#[arg(short, long, default_value = "30")]
2327
delay: u64,
2428

25-
#[arg(long, default_value = "1000")]
26-
delta: u64,
29+
#[arg(long, default_value = "-1000")]
30+
delta: i64,
2731
}
2832

2933
impl Args {
@@ -55,21 +59,19 @@ struct AlgoliaClient {
5559

5660
struct AlgoliaLog {
5761
timestamp: String,
58-
message: String,
59-
method: String,
62+
message: String
6063
}
6164

6265
impl AlgoliaLog {
6366
fn from_json(json: &Value) -> AlgoliaLog {
6467
AlgoliaLog {
6568
timestamp: json["timestamp"].as_str().unwrap().to_string(),
6669
message: to_string(json).unwrap(),
67-
method: json["method"].as_str().unwrap().to_string(),
6870
}
6971
}
7072

71-
fn is_update(&self) -> bool {
72-
self.method != "GET"
73+
fn is_newer(&self, timestamp: &String) -> bool {
74+
self.timestamp.gt(timestamp)
7375
}
7476
}
7577

@@ -125,32 +127,62 @@ async fn main() -> Result<(), reqwest::Error> {
125127
_ => args.expected_records,
126128
};
127129

128-
eprintln!(
129-
"Monitoring for record count changes, started with expected value of {expected_records}"
130-
);
130+
if !args.all_logs {
131+
eprintln!(
132+
"Monitoring for record count changes, started with expected value of {expected_records}"
133+
);
134+
}
131135

132136
loop {
133-
let total_records = client.total_records().await?;
134-
if (expected_records as i64 - total_records as i64).abs() > args.delta as i64 {
135-
eprintln!("Records count difference is more than {}, waiting for logs...", args.delta);
136-
let logs = client.get_logs().await?;
137-
for log in &logs {
138-
if log.timestamp > last_log_timestamp && log.is_update() {
139-
if log.message.contains("delete") {
140-
println!("DELETE:{}", log.message);
141-
continue;
142-
}
143-
144-
println!("UPDATE:{}", log.message);
145-
}
146-
}
147-
for log in &logs {
148-
if log.timestamp > last_log_timestamp {
149-
last_log_timestamp = log.timestamp.to_string();
150-
}
151-
}
137+
if args.all_logs {
138+
print_all_logs(&client, &mut last_log_timestamp).await?;
139+
} else {
140+
print_logs_when_records_change(&client, expected_records, args.delta, &mut last_log_timestamp).await?;
152141
}
153-
154142
sleep(Duration::from_secs(args.delay)).await;
155143
}
156144
}
145+
146+
async fn print_logs_when_records_change(
147+
client: &AlgoliaClient,
148+
expected_records: u64,
149+
delta: i64,
150+
last_log_timestamp: &mut String,
151+
) -> Result<(), Error> {
152+
let total_records = client.total_records().await?;
153+
let changed_records = total_records as i64 - expected_records as i64;
154+
if (delta < 0 && changed_records < delta) || (delta > 0 && changed_records > delta) {
155+
eprintln!(
156+
"Records count difference is more than {} ({}), waiting for logs...",
157+
delta,
158+
changed_records
159+
);
160+
print_algolia_logs(client, last_log_timestamp).await?;
161+
}
162+
163+
Ok(())
164+
}
165+
166+
167+
async fn print_all_logs(
168+
client: &AlgoliaClient,
169+
last_log_timestamp: &mut String,
170+
) -> Result<(), reqwest::Error> {
171+
print_algolia_logs(client, last_log_timestamp).await?;
172+
Ok(())
173+
}
174+
175+
async fn print_algolia_logs(client: &AlgoliaClient, last_log_timestamp: &mut String) -> Result<(), Error> {
176+
let logs = client.get_logs().await?;
177+
for log in &logs {
178+
if log.is_newer(last_log_timestamp) {
179+
println!("{}", log.message);
180+
}
181+
}
182+
for log in &logs {
183+
if log.is_newer(last_log_timestamp) {
184+
let _ = std::mem::replace(last_log_timestamp, log.timestamp.clone());
185+
}
186+
}
187+
Ok(())
188+
}

0 commit comments

Comments
 (0)