Project

General

Profile

Support #21016 ยป cost.py

Peter Amstutz, 09/28/2023 03:02 PM

 
1
#!/usr/bin/env python3
2

    
3
# arv container_request list --filters '[["properties", "exists", "template_uuid"], 
4
# ["container.state", "=", "Complete"], ["container.exit_code", "=", 0]]' --select '["name", "uuid", "cumulative_cost"]'
5

    
6
import arvados
7
import arvados.util
8
import os
9
import json
10
import csv
11

    
12
api = arvados.api()
13

    
14
by_workflow = {}
15

    
16
if os.path.exists("costs.txt"):
17
    with open("costs.txt", "rt") as f:
18
        by_workflow = json.load(f)
19

    
20
if not by_workflow:
21
    for item in arvados.util.keyset_list_all(api.container_requests().list,
22
      filters=[["properties", "exists", "template_uuid"], ["container.state", "=", "Complete"], ["container.exit_code", "=", 0]],
23
      select=["name", "uuid", "properties", "cumulative_cost", "created_at"]):
24
        template_uuid = item["properties"]["template_uuid"]
25
        by_workflow.setdefault(template_uuid, [])
26
        by_workflow[template_uuid].append(item)
27
        print(item)
28

    
29
with open("costs.txt", "wt") as f:
30
    json.dump(by_workflow, f)
31

    
32
costs_out = open("costs.csv", "wt")
33
costs_csv = csv.writer(costs_out)
34
costs_csv.writerow(("uuid", "name", "count", "total", "average"))
35

    
36
for wf in by_workflow:
37
    try:
38
        wfitem = api.workflows().get(uuid=wf).execute()
39
    except:
40
        wfitem = {"uuid": wf, "name": ""}
41
    count = 0
42
    total = 0
43
    for cr in by_workflow[wf]:
44
       cost = cr["cumulative_cost"]
45
       if cost == 0:
46
           continue
47
       total += cost
48
       count += 1
49
    costs_csv.writerow((wfitem["uuid"], wfitem["name"], count, total, total/count if count > 0 else 0))
    (1-1/1)