Test Failed
Push — develop ( 95620b...6631b3 )
by Nicolas
21:21 queued 18:00
created

duckdbcheck   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 24.24 %

Importance

Changes 0
Metric Value
eloc 38
dl 16
loc 66
rs 10
c 0
b 0
f 0
wmc 7

2 Functions

Rating   Name   Duplication   Size   Complexity  
A main() 16 16 2
B check_duckdb() 0 28 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
#!/usr/bin/env python
2
#
3
# Check a DuckDB file.
4
# The input DuckDB file should be given as an input with the -i <file> option.
5
# Check the following thinks:
6
# - number of line (without the header) should be equal to the number given with the -l <number of lines> option
7
# - each line should have the same number of columns
8
# - if the optional -c <number of columns> option is given, each line should have the the same number of columns
9
10
import argparse
11
import sys
12
13
import duckdb
14
15
16
def check_duckdb(input_file, expected_lines, expected_columns=None):
17
    try:
18
        db = duckdb.connect(database='/tmp/glances.db', read_only=True)
19
20
        result = db.sql("SELECT * from cpu").fetchall()
21
22
        # Check 1: Number of lines for CPU
23
        row_count = len(result)
24
        if row_count != expected_lines:
25
            print(f"Error: Expected {expected_lines} CPU lines, but found {row_count}")
26
            return False
27
28
        result = db.sql("SELECT * from network").fetchall()
29
30
        # Check 2: Number of lines for Network
31
        row_count = len(result)
32
        if row_count != expected_lines:
33
            print(f"Error: Expected {expected_lines} Network lines, but found {row_count}")
34
            return False
35
36
        return True
37
38
    except FileNotFoundError:
39
        print(f"Error: File '{input_file}' not found")
40
        return False
41
    except Exception as e:
42
        print(f"Error processing DuckDB file: {str(e)}")
43
        return False
44
45
46 View Code Duplication
def main():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
47
    parser = argparse.ArgumentParser(description='Check a DuckDB file for various properties.')
48
    parser.add_argument('-i', '--input', required=True, help='Input CSV file')
49
    parser.add_argument('-l', '--lines', type=int, required=True, help='Expected number of lines (excluding header)')
50
    parser.add_argument('-c', '--columns', type=int, help='Expected number of columns (optional)')
51
52
    args = parser.parse_args()
53
54
    success = check_duckdb(args.input, args.lines, args.columns)
55
56
    if success:
57
        print("DuckDB - All checks passed successfully!")
58
        sys.exit(0)
59
    else:
60
        print("DuckDB validation failed.")
61
        sys.exit(1)
62
63
64
if __name__ == "__main__":
65
    main()
66