Skip to content

Commit 0edbf93

Browse files
author
Alan Christie
committed
fix: Better load-cr utility
Now reports progress and Jobs that have no rates More 'visual' Needs requirement update
1 parent 185201b commit 0edbf93

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
im-squonk2-client >= 1.13.3, < 2.0.0
1+
im-squonk2-client >= 1.15.2, < 2.0.0
22
python-dateutil == 2.8.2
3-
rich == 12.5.1
3+
rich == 12.6.0
44
pyyaml == 6.0

tools/load-er.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
1717

1818

19-
def main(c_args: argparse.Namespace) -> None:
19+
def main(c_args: argparse.Namespace, filename: StopIteration) -> None:
2020
"""Main function."""
2121

2222
console = Console()
@@ -33,23 +33,65 @@ def main(c_args: argparse.Namespace) -> None:
3333
password=env.admin_password,
3434
)
3535

36-
filename: str = c_args.file
37-
if not filename.endswith('.yaml'):
38-
filename += '.yaml'
39-
4036
# Just read the list from the chosen file
4137
file_content: str = Path(filename).read_text(encoding='utf8')
4238
rates: List[Dict[str, Any]] = yaml.load(file_content, Loader=yaml.FullLoader)
43-
er_rv: DmApiRv = DmApi.set_job_exchange_rates(token, rates=rates)
44-
if not er_rv.success:
45-
console.log(f'[bold red]ERROR[/bold red] {er_rv.msg["error"]}')
46-
sys.exit(1)
39+
# Load the rates one at a time (to handle any errors gracefully)
40+
num_rates: int = 0
41+
num_rates_failed: int = 0
42+
for rate in rates:
43+
# A rate must have a collection, job and version
44+
collection: str = rate.get('collection')
45+
if not collection:
46+
console.log(':boom: File has a rate without a collection')
47+
sys.exit(1)
48+
job: str = rate.get('job')
49+
if not job:
50+
console.log(':boom: File has a rate without a job')
51+
sys.exit(1)
52+
version: str = rate.get('version')
53+
if not version:
54+
console.log(':boom: File has a rate without a version')
55+
sys.exit(1)
56+
rate_value: str = rate.get('rate')
57+
if not rate_value:
58+
console.log(':boom: File has a rate without a rate value')
59+
sys.exit(1)
60+
# Now try and set the rate...
61+
er_rv: DmApiRv = DmApi.set_job_exchange_rates(token, rates=rate)
62+
if er_rv.success:
63+
num_rates += 1
64+
emoji = ':white_check_mark:'
65+
else:
66+
num_rates_failed += 1
67+
emoji = ':cross_mark:'
68+
# Log
69+
console.log(f'{emoji} {collection}/{job}/{version}'
70+
f' :moneybag:[gold3]{rate_value}[/gold3]')
71+
72+
# Now report all the Jobs that still have no rates
73+
er_rv: DmApiRv = DmApi.get_job_exchange_rates(token, only_undefined=True)
74+
num_jobs_without_rate: int = 0
75+
for job in er_rv.msg['exchange_rates']:
76+
if num_jobs_without_rate == 0:
77+
console.log('[bold dark_orange]WARNING Some Jobs have no rates...[/bold dark_orange]')
78+
num_jobs_without_rate += 1
79+
console.log(f':orange_circle: {job["collection"]}/{job["job"]}/{job["version"]}')
4780

48-
num_rates: int = len(rates)
81+
# Summary
4982
if num_rates:
50-
console.log(f'Loaded {num_rates}')
51-
else:
52-
console.log('Loaded [bold red]nothing[/bold red]')
83+
console.log(f'Job rates loaded {num_rates}')
84+
# Error states
85+
if num_rates_failed:
86+
console.log(f'Job rate failures {num_rates_failed}')
87+
if num_jobs_without_rate:
88+
console.log(f'Jobs without rates {num_jobs_without_rate}')
89+
if not num_rates and not num_rates_failed:
90+
console.log('Loaded [bold red1]nothing[/bold red1]')
91+
92+
# Error states
93+
if num_rates_failed or not num_rates and not num_rates_failed:
94+
sys.exit(1)
5395

5496

5597
if __name__ == "__main__":
@@ -63,8 +105,12 @@ def main(c_args: argparse.Namespace) -> None:
63105
parser.add_argument('file', type=str, help='The source file')
64106
args: argparse.Namespace = parser.parse_args()
65107

108+
filename: str = args.file
109+
if not filename.endswith('.yaml'):
110+
filename += '.yaml'
111+
66112
# File must exist
67-
if not Path(args.file).is_file():
68-
parser.error("File does not exist")
113+
if not Path(filename).is_file():
114+
parser.error(f"File '{filename}' does not exist")
69115

70-
main(args)
116+
main(args, filename)

0 commit comments

Comments
 (0)