77from api .molnix_utils import MolnixApi
88
99DEBUG_LEVEL = 2 # Set to 0 for no debug, higher numbers for more verbose debug output
10+ OUTPUT = 0 # 0=print only, 1=print + DB (TODO), 2=DB only (TODO)
1011APPRAISALS_PER_PAGE = 15
1112EVENTS_PER_PAGE = 15
1213
@@ -74,6 +75,11 @@ def log_debug(level, message):
7475 logger .info ("[debug-%d] %s" % (level , message ))
7576
7677
78+ def output_record (stdout , payload ):
79+ if OUTPUT in (0 , 1 ):
80+ stdout .write (json .dumps (payload , indent = 2 , sort_keys = True ))
81+
82+
7783def get_deployment_payload (value ):
7884 if isinstance (value , dict ):
7985 return value
@@ -281,13 +287,7 @@ def handle_person_ids(molnix, person_ids, org_lookup, stdout):
281287 for person_id in person_ids :
282288 cached_snapshot = person_snapshot_cache .get (person_id )
283289 if cached_snapshot is not None :
284- stdout .write (
285- json .dumps (
286- {"record_type" : "rrms_person_snapshot" , "data" : cached_snapshot },
287- indent = 2 ,
288- sort_keys = True ,
289- )
290- )
290+ output_record (stdout , {"record_type" : "rrms_person_snapshot" , "data" : cached_snapshot })
291291 continue
292292 log_debug (1 , "Fetching person_id %s" % person_id )
293293 person_data = safe_call_api (molnix , path = "people/%s" % person_id , label = "people/%s" % person_id )
@@ -310,13 +310,7 @@ def handle_person_ids(molnix, person_ids, org_lookup, stdout):
310310 }
311311 )
312312 person_snapshot_cache [person_id ] = filtered_person_data
313- stdout .write (
314- json .dumps (
315- {"record_type" : "rrms_person_snapshot" , "data" : filtered_person_data },
316- indent = 2 ,
317- sort_keys = True ,
318- )
319- )
313+ output_record (stdout , {"record_type" : "rrms_person_snapshot" , "data" : filtered_person_data })
320314
321315
322316class Command (BaseCommand ):
@@ -334,6 +328,9 @@ def handle(self, *args, **options):
334328
335329 org_lookup = build_org_lookup (molnix )
336330
331+ if OUTPUT == 2 :
332+ self .stdout .write ("OUTPUT=2 (DB-only mode) is selected; DB writes are not implemented yet." )
333+
337334 page = 1
338335 total = 0
339336 person_ids = []
@@ -377,15 +374,11 @@ def handle(self, *args, **options):
377374 sending_org_id , receiving_org_id = fetch_deployment_org_ids (molnix , deployment_id , deployment_org_cache )
378375 appraisal_data = normalize_appraisal (appraisal_payload , sending_org_id , receiving_org_id )
379376 if appraisal_data :
380- self .stdout .write (
381- json .dumps ({"record_type" : "molnix_appraisal" , "data" : appraisal_data }, indent = 2 , sort_keys = True )
382- )
377+ output_record (self .stdout , {"record_type" : "molnix_appraisal" , "data" : appraisal_data })
383378 appraisals_stream_count += 1
384379 appraiser_data = normalize_appraiser (appraisal )
385380 if appraiser_data :
386- self .stdout .write (
387- json .dumps ({"record_type" : "molnix_appraiser" , "data" : appraiser_data }, indent = 2 , sort_keys = True )
388- )
381+ output_record (self .stdout , {"record_type" : "molnix_appraiser" , "data" : appraiser_data })
389382 appraisers_stream_count += 1
390383 collect_person_ids ([appraiser_data ], person_ids )
391384 total += 1
@@ -422,13 +415,7 @@ def handle(self, *args, **options):
422415 for event in events :
423416 records = normalize_event_participation (event , org_lookup )
424417 for record in records :
425- self .stdout .write (
426- json .dumps (
427- {"record_type" : "rrms_event_participation" , "data" : record },
428- indent = 2 ,
429- sort_keys = True ,
430- )
431- )
418+ output_record (self .stdout , {"record_type" : "rrms_event_participation" , "data" : record })
432419 events_stream_count += 1
433420 if record .get ("person_id" ) is not None :
434421 event_person_ids .append (record .get ("person_id" ))
@@ -450,14 +437,17 @@ def handle(self, *args, **options):
450437 # log_debug(1, "Smoke test: response_capacity endpoint")
451438 # response_capacity_data = molnix.call_api(path="response_capacity")
452439 # self.stdout.write(json.dumps(response_capacity_data, indent=2, sort_keys=True))
453- logger .info (
454- "Printed %d items (appraisals=%d appraisers=%d events=%d persons=%d)"
455- % (
456- total ,
457- appraisals_stream_count ,
458- appraisers_stream_count ,
459- events_stream_count ,
460- len (unique_person_ids ),
440+ if OUTPUT in (0 , 1 ):
441+ logger .info (
442+ "Printed %d items (appraisals=%d appraisers=%d events=%d persons=%d)"
443+ % (
444+ total ,
445+ appraisals_stream_count ,
446+ appraisers_stream_count ,
447+ events_stream_count ,
448+ len (unique_person_ids ),
449+ )
461450 )
462- )
451+ if OUTPUT == 2 :
452+ self .stdout .write ("Completed DB-only run (writes not implemented yet)." )
463453 molnix .logout ()
0 commit comments