|
| 1 | +import datetime |
| 2 | +import logging |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from argo_config import ArgoConfig |
| 7 | +from argo_web_api import ArgoWebApi, TopoItem |
| 8 | + |
| 9 | +REQUEST_TIMEOUT = 30 |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def check_hdfs(config: ArgoConfig, tenant_id: str, tenant_name: str) -> bool: |
| 15 | + """Checks if data for today exist in hdfs tenant folders""" |
| 16 | + |
| 17 | + logger.debug( |
| 18 | + f"tenant: {tenant_name} ({tenant_id}) - retrieving report information from web-api..." |
| 19 | + ) |
| 20 | + today = datetime.date.today().strftime("%Y-%m-%d") |
| 21 | + url = f"{config.hdfs_check_path}/{tenant_name}/mdata/{today}?op=LISTSTATUS" |
| 22 | + headers = { |
| 23 | + "Accept": "application/json", |
| 24 | + } |
| 25 | + |
| 26 | + try: |
| 27 | + response = requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT) |
| 28 | + response.raise_for_status() |
| 29 | + |
| 30 | + result = response.json().get("FileStatuses").get("FileStatus") |
| 31 | + if len(result) > 0: |
| 32 | + return True |
| 33 | + except requests.exceptions.HTTPError as e: |
| 34 | + if e.response.status_code == 404: |
| 35 | + logger.warning( |
| 36 | + f"tenant: {tenant_name} ({tenant_id}) - tenant path not found in hdfs" |
| 37 | + ) |
| 38 | + return False |
| 39 | + else: |
| 40 | + raise |
| 41 | + |
| 42 | + return False |
| 43 | + |
| 44 | + |
| 45 | +def check_readiness(config: ArgoConfig, tenant_id: str, tenant_name: str) -> object: |
| 46 | + """Checks tenants readiness by doing web-api requests to see if topology and |
| 47 | + reports are defined and also by checking if data are present both in ams and hdfs""" |
| 48 | + |
| 49 | + web_api = ArgoWebApi(config) |
| 50 | + |
| 51 | + # get access token from config file |
| 52 | + tenant_token = config.tenants.get(tenant_name, {}).get("web_api_token") |
| 53 | + |
| 54 | + # check if topology exists |
| 55 | + topology_ready = True |
| 56 | + topology_msg = [] |
| 57 | + topo_endpoints = web_api.get_topology( |
| 58 | + tenant_id, tenant_name, tenant_token, TopoItem.ENDPOINTS |
| 59 | + ) |
| 60 | + topo_groups = web_api.get_topology( |
| 61 | + tenant_id, tenant_name, tenant_token, TopoItem.GROUPS |
| 62 | + ) |
| 63 | + topo_service_types = web_api.get_topology( |
| 64 | + tenant_id, tenant_name, tenant_token, TopoItem.SERVICE_TYPES |
| 65 | + ) |
| 66 | + |
| 67 | + if len(topo_endpoints) > 0: |
| 68 | + topology_msg.append("Topology endpoints are set.") |
| 69 | + else: |
| 70 | + topology_msg.append("Topology endpoints are missing!") |
| 71 | + topology_ready = False |
| 72 | + |
| 73 | + if len(topo_groups) > 0: |
| 74 | + topology_msg.append("Topology groups are set.") |
| 75 | + else: |
| 76 | + topology_msg.append("Topology groups are missing!") |
| 77 | + topology_ready = False |
| 78 | + |
| 79 | + if len(topo_service_types) > 0: |
| 80 | + topology_msg.append("Topology service-types are set.") |
| 81 | + else: |
| 82 | + topology_msg.append("Topology service-types are missing!") |
| 83 | + topology_ready = False |
| 84 | + |
| 85 | + # check reports |
| 86 | + reports_ready = True |
| 87 | + reports_msg = "Tenant has at least one report" |
| 88 | + |
| 89 | + reports = web_api.get_reports(tenant_id, tenant_name, tenant_token) |
| 90 | + |
| 91 | + if len(reports) < 0: |
| 92 | + reports_msg = "Tenant has no reports!" |
| 93 | + |
| 94 | + # check metric data in hdfs |
| 95 | + hdfs_ready = True |
| 96 | + hdfs_msg = "Tenant has metric data in HDFS for today" |
| 97 | + hdfs_check = check_hdfs(config, tenant_id, tenant_name) |
| 98 | + |
| 99 | + if not hdfs_check: |
| 100 | + hdfs_ready = False |
| 101 | + hdfs_msg = "Tenant doesn't have metric data in HDFS for today!" |
| 102 | + |
| 103 | + # update the state |
| 104 | + payload = { |
| 105 | + "data": {"ready": hdfs_ready, "message": hdfs_msg}, |
| 106 | + "topology": {"ready": topology_ready, "message": " ".join(topology_msg)}, |
| 107 | + "reports": {"ready": reports_ready, "message": reports_msg}, |
| 108 | + "last_check": datetime.datetime.now(datetime.timezone.utc).strftime( |
| 109 | + "%Y:%m:%dT%H:%M:%SZ" |
| 110 | + ), |
| 111 | + } |
| 112 | + |
| 113 | + # update the payload to web-api |
| 114 | + result = web_api.update_ready_state(tenant_id, tenant_name, payload) |
| 115 | + if result: |
| 116 | + return True |
| 117 | + return False |
0 commit comments