Skip to content

Commit

Permalink
Add total counter of identical tables
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoshet committed Sep 16, 2024
1 parent cbe0149 commit 7dd9401
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ target/
.DS_Store
postgres-data1/
postgres-data2/

script.sh
32 changes: 32 additions & 0 deletions src/diff/table/query/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ impl Display for TableSource {
}
}

// Represents a counter for the identical tables in total
#[derive(Debug, Clone)]
pub struct IdenticalTablesCounter {
pub count: i64,
}

impl IdenticalTablesCounter {
pub fn new(count: i64) -> Self {
Self { count }
}

pub fn increment(&mut self) {
self.count += 1;
}
}

/// Represents the difference in table counts between two tables.
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -66,6 +82,22 @@ impl TableDiffOutput {
matches!(self, Self::Diff(_, _) | Self::NotExists(_, _))
}

// Prints the enum value of the table diff output as string
pub fn enum_as_string(&self) -> String {
match self {
Self::NoCountDiff(_table, _count) => "NoCountDiff".to_string(),
Self::NotExists(_table, _source) => "NotExists".to_string(),
Self::Diff(_table, _diffs) => "Diff".to_string(),
TableDiffOutput::NoPrimaryKeyFound(_table) => "NoPrimaryKeyFound".to_string(),
TableDiffOutput::NoDiffWithDuration(_table, _duration) => {
"NoDiffWithDuration".to_string()
}
TableDiffOutput::DataDiffWithDuration(_table_name, _position, _offset, _duration) => {
"DataDiffWithDuration".to_string()
}
}
}

/// Converts the table difference output to a colored string.
pub fn to_string(&self) -> ColoredString {
match self {
Expand Down
46 changes: 45 additions & 1 deletion src/diff/table/table_differ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::diff::diff_payload::DiffPayload;
use crate::diff::table::query::input::{
QueryHashDataInput, QueryPrimaryKeysInput, QueryTableCountInput, QueryTableNamesInput,
};
use crate::diff::table::query::output::{TableCountDiff, TableDiffOutput, TableSource};
use crate::diff::table::query::output::{
IdenticalTablesCounter, TableCountDiff, TableDiffOutput, TableSource,
};

use crate::diff::table::query::table_query_executor::{
TableDualSourceQueryExecutor, TableSingleSourceQueryExecutor,
Expand All @@ -16,6 +18,7 @@ use tracing::{debug, info};

use crate::diff::diff_output::DiffOutput;
use crate::diff::types::SchemaName;
use std::sync::{Arc, Mutex};
use std::time::Instant;

pub struct TableDiffer<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor> {
Expand All @@ -42,6 +45,8 @@ impl<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor>

let sorted_tables = tables.to_owned();

let identical_tables_counter = Arc::new(Mutex::new(IdenticalTablesCounter::new(0)));

let futures = sorted_tables.iter().map(|table_name| async {
let start = Instant::now();

Expand Down Expand Up @@ -82,6 +87,14 @@ impl<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor>

debug!("##############################################");

// Increment identical tables counter
let identical_tables_counter = identical_tables_counter.clone();
self.increase_counter_of_identical_tables(
identical_tables_counter,
table_diff_result.clone(),
)
.await;

// If we only care about counts, return the result
if diff_payload.only_count() {
return table_diff_result;
Expand Down Expand Up @@ -167,6 +180,20 @@ impl<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor>
.bold()
);

let identical_tables_counter = identical_tables_counter.lock().unwrap();
let success_rate = (identical_tables_counter.count as f64 / tables.len() as f64) * 100.0;
info!(
"{}",
format!(
"Identical tables found: {} out of {}, success rate: {}%",
identical_tables_counter.count,
tables.len(),
success_rate
)
.bright_blue()
.bold()
);

info!("##############################################");
info!("{}", "Table analysis results 👇".bright_magenta().bold());

Expand Down Expand Up @@ -197,6 +224,23 @@ impl<TQE: TableSingleSourceQueryExecutor, DTQE: TableDualSourceQueryExecutor>
Ok(tables)
}

pub async fn increase_counter_of_identical_tables(
&self,
identical_tables_counter: Arc<Mutex<IdenticalTablesCounter>>,
table_diff_result: TableDiffOutput,
) {
// Increment identical tables counter for specific cases
let vec: Vec<String> = vec![
"NoCountDiff".into(),
"NoDiffWithDuration".into(),
"NoPrimaryKeyFound".into(), // this is added since we can't compare tables without primary keys
];
if vec.contains(&table_diff_result.enum_as_string()) {
let mut counter = identical_tables_counter.lock().unwrap();
counter.increment();
}
}

fn extract_result(
table_name: &str,
first_result: Result<i64>,
Expand Down

0 comments on commit 7dd9401

Please sign in to comment.