Skip to content

Commit

Permalink
Merge pull request #123 from 2ndWatch/add/has_lag
Browse files Browse the repository at this point in the history
Added lag check; Refactored time audit code to prevent repeated code
  • Loading branch information
twarnock authored Jun 4, 2020
2 parents 134a42e + 8e4b04d commit 5ce585d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SHA1 := $$(git log -1 --pretty=%h)
CURRENT_BRANCH := $$(git symbolic-ref -q --short HEAD)
LATEST_TAG := ${REPO_NAME}:latest
GIT_TAG := ${REPO_NAME}:${SHA1}
VERSION := v0.3.2
VERSION := v0.3.3

info: ## Show information about the current git state.
@echo "Github Project: https://github.com/${REPO_NAME}\nCurrent Branch: ${CURRENT_BRANCH}\nSHA1: ${SHA1}\n"
Expand Down
2 changes: 1 addition & 1 deletion cloudendure/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.2"
__version__ = "0.3.3"
33 changes: 23 additions & 10 deletions cloudendure/cloudendure.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,16 @@ def get_machine_sync_details(self) -> Dict[Any]:
}
if replication_info.get("lastSeenDateTime"):
response_dict[machine_id]["last_seen_utc"] = replication_info.get("lastSeenDateTime")
if replication_info.get("lastConsistencyDateTime"):
response_dict[machine_id]["last_consistency_utc"] = replication_info.get("lastConsistencyDateTime")
# Project is still printing to console as a convention; Emitting an
# output to stdout for interactive usage
return response_dict

def inspect_ce_project(self, check_type: str) -> Dict[str, Any]:
valid_check_types: List[str] = ["not_synced", "not_started", "not_current"]
state_check_types: List[str] = ["not_synced", "not_started"]
time_check_types: List[str] = ["not_current", "is_lagged"]
valid_check_types: List[str] = state_check_types + time_check_types
if check_type not in valid_check_types:
print(
f'ERROR: Unknown check_type of "{check_type}"; Please use a valid check_type: [ {", ".join(valid_check_types)} ] ...'
Expand All @@ -485,9 +489,15 @@ def inspect_ce_project(self, check_type: str) -> Dict[str, Any]:
result: Dict[str, Any] = {}
sync_report: Dict[str, Any] = self.get_machine_sync_details()
print(f"INFO: Using check '{check_type}' on Project: ({self.project_name})")
inspector = getattr(self, check_type)
if check_type in state_check_types:
inspector1 = getattr(self, check_type)
else:
inspector2 = getattr(self, "time_delta_context")
for item in sync_report.keys():
mcheck = inspector(machine=sync_report[item])
if inspector2:
mcheck = inspector2(machine=sync_report[item], check_type=check_type)
else:
mcheck = inspector1(machine=sync_report[item])
if mcheck:
result[item] = sync_report[item]
print(f"INFO: Check '{check_type}' completed; {len(result)} machines matched in Project: ({self.project_name})")
Expand All @@ -505,15 +515,18 @@ def not_started(self, machine) -> bool:
else:
return True

def not_current(self, machine, delta_seconds: int = 86400) -> bool:
if machine.get("last_seen_utc"):
def time_delta_context(self, machine: Dict[str, Any], check_type: str) -> bool:
time_contexts: Dict[str, dict] = {
"not_current": {"delta_seconds": 86400, "property": "last_seen_utc"},
"is_lagged": {"delta_seconds": 120, "property": "last_consistency_utc"},
}
if machine.get(time_contexts[check_type].get("property")):
now: datetime = datetime.datetime.now(datetime.timezone.utc)
machine_last_seen: datetime = datetime.datetime.fromisoformat(machine["last_seen_utc"])
last_seen_delta: datetime = now - machine_last_seen
if int(last_seen_delta.total_seconds()) >= delta_seconds:
last_seen: datetime = datetime.datetime.fromisoformat(machine[time_contexts[check_type].get("property")])
last_seen_delta: datetime = now - last_seen
if int(last_seen_delta.total_seconds()) >= time_contexts[check_type].get("delta_seconds"):
return True
else:
return False
return False

def update_blueprint(self) -> bool:
"""Update the blueprint associated with the specified machines."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cloudendure"
version = "0.3.2"
version = "0.3.3"
description = "Python wrapper and CLI for CloudEndure"
authors = ["Mark Beacom <mark@markbeacom.com>", "Tom Warnock <twarnock@2ndwatch.com>"]
maintainers = ["Evan Lucchesi <evan@2ndwatch.com>", "Nick Selpa <nselpa@2ndwatch.com>"]
Expand Down

0 comments on commit 5ce585d

Please sign in to comment.