Skip to content

Commit 193002a

Browse files
author
Nick Selpa
committed
Added lag check; Refactored time audit code to prevent repeated code
1 parent 134a42e commit 193002a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

cloudendure/cloudendure.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,16 @@ def get_machine_sync_details(self) -> Dict[Any]:
471471
}
472472
if replication_info.get("lastSeenDateTime"):
473473
response_dict[machine_id]["last_seen_utc"] = replication_info.get("lastSeenDateTime")
474+
if replication_info.get("lastConsistencyDateTime"):
475+
response_dict[machine_id]["last_consistency_utc"] = replication_info.get("lastConsistencyDateTime")
474476
# Project is still printing to console as a convention; Emitting an
475477
# output to stdout for interactive usage
476478
return response_dict
477479

478480
def inspect_ce_project(self, check_type: str) -> Dict[str, Any]:
479-
valid_check_types: List[str] = ["not_synced", "not_started", "not_current"]
481+
state_check_types: List[str] = ["not_synced", "not_started"]
482+
time_check_types: List[str] = ["not_current", "is_lagged"]
483+
valid_check_types: List[str] = state_check_types + time_check_types
480484
if check_type not in valid_check_types:
481485
print(
482486
f'ERROR: Unknown check_type of "{check_type}"; Please use a valid check_type: [ {", ".join(valid_check_types)} ] ...'
@@ -485,9 +489,15 @@ def inspect_ce_project(self, check_type: str) -> Dict[str, Any]:
485489
result: Dict[str, Any] = {}
486490
sync_report: Dict[str, Any] = self.get_machine_sync_details()
487491
print(f"INFO: Using check '{check_type}' on Project: ({self.project_name})")
488-
inspector = getattr(self, check_type)
492+
if check_type in state_check_types:
493+
inspector1 = getattr(self, check_type)
494+
else:
495+
inspector2 = getattr(self, "time_delta_context")
489496
for item in sync_report.keys():
490-
mcheck = inspector(machine=sync_report[item])
497+
if inspector2:
498+
mcheck = inspector2(machine=sync_report[item], check_type=check_type)
499+
else:
500+
mcheck = inspector1(machine=sync_report[item])
491501
if mcheck:
492502
result[item] = sync_report[item]
493503
print(f"INFO: Check '{check_type}' completed; {len(result)} machines matched in Project: ({self.project_name})")
@@ -505,15 +515,18 @@ def not_started(self, machine) -> bool:
505515
else:
506516
return True
507517

508-
def not_current(self, machine, delta_seconds: int = 86400) -> bool:
509-
if machine.get("last_seen_utc"):
518+
def time_delta_context(self, machine: Dict[str, Any], check_type: str) -> bool:
519+
time_contexts: Dict[str, dict] = {
520+
"not_current": {"delta_seconds": 86400, "property": "last_seen_utc"},
521+
"is_lagged": {"delta_seconds": 120, "property": "last_consistency_utc"},
522+
}
523+
if machine.get(time_contexts[check_type].get("property")):
510524
now: datetime = datetime.datetime.now(datetime.timezone.utc)
511-
machine_last_seen: datetime = datetime.datetime.fromisoformat(machine["last_seen_utc"])
512-
last_seen_delta: datetime = now - machine_last_seen
513-
if int(last_seen_delta.total_seconds()) >= delta_seconds:
525+
last_seen: datetime = datetime.datetime.fromisoformat(machine[time_contexts[check_type].get("property")])
526+
last_seen_delta: datetime = now - last_seen
527+
if int(last_seen_delta.total_seconds()) >= time_contexts[check_type].get("delta_seconds"):
514528
return True
515-
else:
516-
return False
529+
return False
517530

518531
def update_blueprint(self) -> bool:
519532
"""Update the blueprint associated with the specified machines."""

0 commit comments

Comments
 (0)