Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Restructure CI output #374

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions data_diff/diff_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import re
import time
import pandas as pd
from tabulate import tabulate
from abc import ABC, abstractmethod
from enum import Enum
from contextlib import contextmanager
Expand Down Expand Up @@ -126,22 +128,66 @@ def _get_stats(self) -> DiffStats:
def get_stats_string(self):

diff_stats = self._get_stats()
string_output = ""
string_output += f"{diff_stats.table1_count} rows in table A\n"
string_output += f"{diff_stats.table2_count} rows in table B\n"
string_output += f"{diff_stats.diff_by_sign['-']} rows exclusive to table A (not present in B)\n"
string_output += f"{diff_stats.diff_by_sign['+']} rows exclusive to table B (not present in A)\n"
string_output += f"{diff_stats.diff_by_sign['!']} rows updated\n"
string_output += f"{diff_stats.unchanged} rows unchanged\n"
string_output += f"{100*diff_stats.diff_percent:.2f}% difference score\n"

if self.stats:
string_output += "\nExtra-Info:\n"
for k, v in sorted(self.stats.items()):
string_output += f" {k} = {v}\n"

return string_output
# Convert result_list into human-readable pandas table.
string_output = "\n\n"

for sign, values in self.result_list:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this replace the behavior of get_stats_string() instead? Or maybe be an additional method like get_stats_tables_string()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can replace get_stats_string(). I don't think there's any drawback at all to fully replacing the existing string_output since it is not structured (and therefore depended on by some downstream system)--it's just a string.

I will refactor this, and I'm very open to suggestions/recommendations, but I'm going to try to settle on a good design of the output before optimizing the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dlawin moved the logic into get_stats_string 👍 (still could be optimized from here)

store_extra_columns = self.info_tree.info.tables[0].extra_columns
if store_extra_columns != None:
break

diff_output = self.result_list.copy()
additional_columns_to_diff = list(store_extra_columns)
primary_key = self.info_tree.info.tables[0].key_columns[0]

df_output = pd.DataFrame(diff_output, columns =['a', 'b'])
column_values = pd.DataFrame(df_output['b'].to_list(), columns = [primary_key] + additional_columns_to_diff)
all_columns_values_tall = pd.concat([df_output, column_values], axis=1).\
drop('b', axis=1)
all_columns_values_tall.loc[all_columns_values_tall['a'] == '-', 'a'] = 'Prod'
all_columns_values_tall.loc[all_columns_values_tall['a'] == '+', 'a'] = 'Dev'
all_columns_values_pivot = all_columns_values_tall.\
pivot(index=primary_key, columns='a',values=[primary_key] + additional_columns_to_diff)

all_columns = [primary_key] + additional_columns_to_diff
each_column_twice = [item for item in all_columns for _ in range(2)]
tuples = [(x, 'Prod' if idx % 2 == 0 else 'Dev') for idx, x in enumerate(each_column_twice)]
index = pd.MultiIndex.from_tuples(tuples, names=[None, "a"])
df_with_index = pd.DataFrame(columns=index)
all_columns_values_pivot_multiindex = pd.concat([df_with_index, all_columns_values_pivot])

all_columns_values_pivot_multiindex.columns = [': '.join(i) for i in all_columns_values_pivot_multiindex.columns]
matching_primary_key_rows = all_columns_values_pivot_multiindex.loc[all_columns_values_pivot_multiindex[primary_key+": Prod"] == \
all_columns_values_pivot_multiindex[primary_key+": Dev"]]

# Display missing primary keys
pks_missing_from_table_a = all_columns_values_pivot_multiindex[[primary_key+": Prod"]].isna().sum()[0]
pks_missing_from_table_b = all_columns_values_pivot_multiindex[[primary_key+": Dev"]].isna().sum()[0]
primary_keys_df = pd.DataFrame([[pks_missing_from_table_a, pks_missing_from_table_b]], columns=['Rows Added', 'Rows Removed'])

#blankIndex=[''] * len(primary_keys_df)
#primary_keys_df.index=blankIndex

string_output += tabulate(primary_keys_df, headers='keys', tablefmt='psql', showindex=False)
string_output += "\n\n"

# Display columns with conflicts
columns_with_conflicts = []
conflicts_df = pd.DataFrame(columns=['Column', 'Values Updated'])

for i in additional_columns_to_diff:
conflicts = (matching_primary_key_rows[i+": Prod"] != matching_primary_key_rows[i+": Dev"]) & \
(matching_primary_key_rows[i+": Prod"].notnull() | matching_primary_key_rows[i+": Dev"].notnull())
sum_conflicts = sum(conflicts)
conflicts_df.loc[len(conflicts_df.index)] = [i, sum_conflicts]
if sum(conflicts) > 0:
columns_with_conflicts += [i+": Prod", i+": Dev"]

string_output += tabulate(conflicts_df, headers='keys', tablefmt='psql', showindex=False)

return string_output

def get_stats_dict(self):

diff_stats = self._get_stats()
Expand Down
16 changes: 16 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ click = "^8.1"
rich = "*"
toml = "^0.10.2"
sqeleton = "^0.0.5"
pandas = "^1.5.0"
tabulate = "^0.9.0"
mysql-connector-python = {version="8.0.29", optional=true}
psycopg2 = {version="*", optional=true}
snowflake-connector-python = {version="^2.7.2", optional=true}
Expand Down