Skip to content

Commit bf57818

Browse files
committed
Sync Molnix appraisals – v0.2
1 parent e1beb3e commit bf57818

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

api/management/commands/sync_molnix_appraisals.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,63 @@ def log_debug(level, message):
5252
logger.info("[debug-%d] %s" % (level, message))
5353

5454

55+
def remove_tags_and_deployments(value, parent_key=None):
56+
if isinstance(value, dict):
57+
cleaned = {}
58+
for key, item in value.items():
59+
if key == "tags":
60+
continue
61+
if parent_key == "appraisal" and key == "deployment":
62+
continue
63+
cleaned[key] = remove_tags_and_deployments(item, key)
64+
return cleaned
65+
if isinstance(value, list):
66+
return [remove_tags_and_deployments(item, parent_key) for item in value]
67+
return value
68+
69+
70+
def collect_person_ids(value, collected):
71+
if isinstance(value, dict):
72+
for key, item in value.items():
73+
if key == "person_id":
74+
collected.append(item)
75+
else:
76+
collect_person_ids(item, collected)
77+
return
78+
if isinstance(value, list):
79+
for item in value:
80+
collect_person_ids(item, collected)
81+
82+
83+
def find_person_payload(value):
84+
# if isinstance(value, dict):
85+
if any(key in value for key in ("sex", "fullname", "organization", "current_availability")):
86+
return value
87+
for item in value.values():
88+
found = find_person_payload(item)
89+
if found is not None:
90+
return found
91+
# occured never:
92+
# if isinstance(value, list):
93+
# for item in value:
94+
# found = find_person_payload(item)
95+
# if found is not None:
96+
# return found
97+
return None
98+
99+
100+
def filter_person_data(person_data):
101+
payload = find_person_payload(person_data)
102+
if not isinstance(payload, dict):
103+
return {}
104+
return {
105+
"sex": payload.get("sex"),
106+
"fullname": payload.get("fullname"),
107+
"organization": payload.get("organization"),
108+
"current_availability": payload.get("current_availability"),
109+
}
110+
111+
55112
class Command(BaseCommand):
56113
help = "Fetch and print Molnix appraisals"
57114

@@ -67,6 +124,7 @@ def handle(self, *args, **options):
67124

68125
page = 1
69126
total = 0
127+
person_ids = []
70128
while True:
71129
log_debug(1, "Fetching page %d" % page)
72130
data = molnix.call_api(path="appraisals", params={"page": page})
@@ -90,12 +148,27 @@ def handle(self, *args, **options):
90148
log_debug(1, "No appraisals returned, stopping")
91149
break
92150
for appraisal in appraisals:
93-
self.stdout.write(json.dumps(appraisal, indent=2, sort_keys=True))
151+
collect_person_ids(appraisal, person_ids)
152+
cleaned_appraisal = remove_tags_and_deployments(appraisal)
153+
self.stdout.write(json.dumps(cleaned_appraisal, indent=2, sort_keys=True))
94154
total += 1
95155
if not should_continue(data, appraisals):
96156
log_debug(1, "Pagination indicates no more pages")
97157
break
98158
page += 1
99159

160+
unique_person_ids = sorted({pid for pid in person_ids if pid is not None})
161+
# self.stdout.write(json.dumps(unique_person_ids, indent=2, sort_keys=True))
162+
log_debug(1, "Collected %d person_id values" % len(unique_person_ids))
163+
for person_id in unique_person_ids:
164+
log_debug(1, "Fetching person_id %s" % person_id)
165+
person_data = molnix.call_api(path="people/%s" % person_id)
166+
filtered_person_data = filter_person_data(person_data)
167+
if not filtered_person_data:
168+
log_debug(2, "No person payload found for person_id %s" % person_id)
169+
self.stdout.write(json.dumps(filtered_person_data, indent=2, sort_keys=True))
170+
# log_debug(1, "Smoke test: response_capacity endpoint")
171+
# response_capacity_data = molnix.call_api(path="response_capacity")
172+
# self.stdout.write(json.dumps(response_capacity_data, indent=2, sort_keys=True))
100173
logger.info("Printed %d appraisals" % total)
101174
molnix.logout()

0 commit comments

Comments
 (0)