combine snapshot_stats for frontend

This commit is contained in:
simon 2022-10-27 18:40:27 +07:00
parent 9b69a8dc91
commit 904f449d37
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
1 changed files with 39 additions and 12 deletions

View File

@ -70,19 +70,26 @@ class ElasticSnapshot:
def _check_policy_exists(self):
"""check if snapshot policy is set correctly"""
policy = self._get_policy()
expected_policy = self._build_policy_data()
if not policy:
print(f"snapshot: create policy {self.POLICY} {expected_policy}")
return False
if policy != expected_policy:
print(f"snapshot: update policy settings {expected_policy}")
return False
return True
def _get_policy(self):
"""get policy from es"""
path = f"_slm/policy/{self.POLICY}"
response, statuscode = ElasticWrap(path).get()
expected_policy = self._build_policy_data()
if statuscode == 200:
print(f"snapshot: policy {self.POLICY} exists")
matching = response["ta_daily"]["policy"] == expected_policy
if not matching:
print(f"snapshot: update policy settings {expected_policy}")
return matching
print(f"snapshot: create policy {self.POLICY} {expected_policy}")
return False
if statuscode != 200:
return False
else:
return response[self.POLICY]
def create_policy(self):
"""create snapshot lifetime policy"""
@ -116,7 +123,14 @@ class ElasticSnapshot:
if statuscode == 200:
print(f"snapshot: executing now: {response}")
def get_all_snapshots(self):
def get_snapshot_stats(self):
"""get snapshot info for frontend"""
snapshot_info = self._build_policy_details()
snapshot_info.update({"snapshots": self._get_all_snapshots()})
return snapshot_info
def _get_all_snapshots(self):
"""get a list of all registered snapshots"""
path = f"_snapshot/{self.REPO}/*?sort=start_time&order=desc"
response, statuscode = ElasticWrap(path).get()
@ -141,6 +155,19 @@ class ElasticSnapshot:
return snap_dicts
def _build_policy_details(self):
"""get additional policy details"""
policy = self._get_policy()
next_exec = policy["next_execution_millis"]
next_exec_date = datetime.fromtimestamp(next_exec // 1000)
next_exec_str = next_exec_date.strftime("%Y-%m-%d %H:%M")
expire_after = policy["policy"]["retention"]["expire_after"]
policy_metadata = {
"next_exec_str": next_exec_str,
"expire_after": expire_after,
}
return policy_metadata
@staticmethod
def _date_converter(date_utc):
"""convert datetime string"""