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(): |
|
|
|
|
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
|
|
|
|