File size: 2,809 Bytes
19641b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# ============================================================
# utils/validators.py
# ============================================================

import pandas as pd


def validate_files_loaded(source_df, target_df):
    """

    Checks whether both source and target files are loaded.

    """

    if source_df is None or target_df is None:
        return False, "Please upload both Source and Target files."

    if len(source_df) == 0:
        return False, "Source file has no rows."

    if len(target_df) == 0:
        return False, "Target file has no rows."

    return True, "Files are valid."


def validate_common_columns(source_df, target_df):
    """

    Checks whether source and target have at least one common column.

    """

    source_cols = set(source_df.columns)
    target_cols = set(target_df.columns)

    common_cols = list(source_cols.intersection(target_cols))

    if not common_cols:
        return False, "No common columns found between Source and Target."

    return True, common_cols


def validate_key_columns(source_df, target_df, key_columns):
    """

    Validates selected key columns.

    """

    if not key_columns:
        return False, "Please select at least one key column."

    missing_source = [
        col for col in key_columns
        if col not in source_df.columns
    ]

    missing_target = [
        col for col in key_columns
        if col not in target_df.columns
    ]

    if missing_source:
        return False, f"Key column(s) missing in Source: {missing_source}"

    if missing_target:
        return False, f"Key column(s) missing in Target: {missing_target}"

    source_nulls = source_df[key_columns].isnull().any(axis=1).sum()
    target_nulls = target_df[key_columns].isnull().any(axis=1).sum()

    if source_nulls > 0 or target_nulls > 0:
        return False, (
            f"Null key values found. "
            f"Source null-key rows: {source_nulls}, "
            f"Target null-key rows: {target_nulls}"
        )

    return True, "Key columns are valid."


def find_duplicate_keys(df, key_columns):
    """

    Returns duplicate key rows.

    """

    if not key_columns:
        return pd.DataFrame()

    return df[
        df.duplicated(
            subset=key_columns,
            keep=False
        )
    ].copy()


def validate_scope_range(start, end, total_rows):
    """

    Validates row range selection.

    """

    if start < 1:
        return False, "Start row cannot be less than 1."

    if end > total_rows:
        return False, "End row cannot be greater than total rows."

    if start > end:
        return False, "Start row cannot be greater than end row."

    return True, "Scope range is valid."