diff --git a/env-config.yaml b/environment.yaml similarity index 57% rename from env-config.yaml rename to environment.yaml index edae2ed..f426837 100644 --- a/env-config.yaml +++ b/environment.yaml @@ -5,39 +5,67 @@ euprod-coco: - env-token-name: "EUPROD_TOKEN_VAR" - jenkins: "https://jaws.bmwgroup.net/opapm/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" euprod-gcdm: - name: "euprod" - env-url: "https://moh22956.live.dynatrace.com" - env-token-name: "EUPRODSAAS_TOKEN_VAR" - jenkins: "https://jaws.bmwgroup.net/opapm/" - type: "gcdm" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" eupreprod-coco: - name: "eupreprod" - env-url: "https://qqk70169.live.dynatrace.com" - env-token-name: "EUPREPROD_TOKEN_VAR" - jenkins: "https://jaws.bmwgroup.net/opapm/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" naprod-coco: - name: "naprod" - env-url: "https://wgv50241.live.dynatrace.com" - env-token-name: "NAPROD_TOKEN_VAR" - jenkins: "https://jaws.bmwgroup.net/opapm/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" napreprod-coco: - name: "napreprod" - env-url: "https://onb44935.live.dynatrace.com" - env-token-name: "NAPREPROD_TOKEN_VAR" - jenkins: "https://jaws.bmwgroup.net/opapm/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" cnprod-coco: - name: "cnprod" - env-url: "https://dyna-synth-cn.bmwgroup.com.cn/e/b921f1b9-c00e-4031-b9d1-f5a0d530757b" - env-token-name: "CNPROD_TOKEN_VAR" - jenkins: "https://jaws-china.bmwgroup.net/opmaas/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" cnpreprod-coco: - name: "cnpreprod" - env-url: "https://dynatracemgd-tsp.bmwgroup.net/e/ab88c03b-b7fc-45f0-9115-9e9ecc0ced35" - env-token-name: "CNPREPROD_TOKEN_VAR" - jenkins: "https://jaws-china.bmwgroup.net/opmaas/" - type: "coco" + - metricSelector: "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" + - resolution: "1M" + - fromDate: "now-6M" + - toDate: "now" \ No newline at end of file diff --git a/main.py b/main.py index b0febf0..ad98d36 100644 --- a/main.py +++ b/main.py @@ -5,71 +5,49 @@ from decouple import config import yaml import pandas as pd from dynatrace import Dynatrace -from dynatrace import TOO_MANY_REQUESTS_WAIT -from dynatrace.environment_v2.tokens_api import SCOPE_METRICS_READ, SCOPE_METRICS_INGEST - -def getD(DTURL, DTTOKEN, metricSelector, resolution, fromDate, toDate): - # Create a Dynatrace client - dt = Dynatrace(DTURL, DTTOKEN) - for metric in dt.metrics.query(metricSelector, resolution, fromDate, toDate): - with open('metrics.txt', 'w') as f: - f.write(str(metric)) +import logging -def getDashboardsWithViewCount(DTAPIToken, DTENV, metricSelector, resolution, - fromDate, toDate): - - dtclient = dynatraceAPI.Dynatrace(DTENV, DTAPIToken, - logging.Logger("ERROR"), None, None, 0, - 2 * 1000) - params = { - "metricSelector": f"{metricSelector}", - "resolution": f"{resolution}", - "fromDate": f"{fromDate}", - "toDate": f"{toDate}" - } - - api_url_report = "/api/v2/metrics/query" - pages = dtclient.returnPageination(api_url_report, params, "data") - df = pd.DataFrame(pages.elements) - return df +def getDashboardsWithViewCount(DT_CLIENT, METRIC_SELECTOR, RESOLUTION, + FROM_DATE, TO_DATE): + metrics = DT_CLIENT.metrics.query(METRIC_SELECTOR, RESOLUTION, FROM_DATE, + TO_DATE) + return metrics -def getDashboards(DTAPIToken, DTENV): +def getDashboards(DT_CLIENT): + dashboards = DT_CLIENT.dashboards.list(owner=None, tags=None) + return dashboards - dtclient = dynatraceAPI.Dynatrace(DTENV, DTAPIToken, - logging.Logger("ERROR"), None, None, 0, - 2 * 1000) - my_params = { - "owner": "", - "tags": [], - } - - api_url_report = "/api/config/v1/dashboards" - pages = dtclient.returnPageination(api_url_report, my_params, "dashboards") - df = pd.DataFrame(pages.elements) - return df if __name__ == "__main__": - - metricSelector = "builtin:dashboards.viewCount:splitBy(id):sort(value(auto,ascending))" - resolution = "1M" - fromDate = "now-6M" - toDate = "now" - with open(os.path.basename("./env-config.yaml")) as env_cfg: - env_config = yaml.safe_load(env_cfg) + logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') - for item, doc in env_config.items(): - token = dict(doc[2]) - url = dict(doc[1]) - print(item, " crawling through ...") - print(item, " checking token ...") - if config(token.get("env-token-name"), default='') != "": - print(item, " fetching all dashboards ...") - DTTOKEN = config(token.get("env-token-name"), default='') - DTURL = url.get("env-url") - getDashboards(DTTOKEN, DTURL) - getDashboardsWithViewCount(DTTOKEN, DTURL, metricSelector, - resolution, fromDate, toDate) - getD(DTURL, DTTOKEN, metricSelector, resolution, fromDate, toDate) \ No newline at end of file + with open(os.path.basename("./environment.yaml")) as env_cfg: + environment = yaml.safe_load(env_cfg) + + for item, doc in environment.items(): + logging.info(item, " checking token ...") + + if config(dict(doc[2]).get("env-token-name"), default='') != "": + DT_URL = dict(doc[1]).get("env-url") + DT_TOKEN = config(dict(doc[2]).get("env-token-name"), default='') + METRIC_SELECTOR = dict(doc[5]).get("metricSelector") + RESOLUTION = dict(doc[6]).get("resolution") + FROM_DATE= dict(doc[7]).get("fromDate") + TO_DATE= dict(doc[8]).get("toDate") + + logging.info(item, " init Dynatrace client ...") + DT_CLIENT = Dynatrace(DT_URL, DT_TOKEN, logging.Logger("ERROR"), + None, None, 0, 2*1000) + + logging.info(item, " fetching all dashboards ...") + dashboards = getDashboards(DT_CLIENT) + + logging.info(item, " fetching dashboards with view count that are\ + older than 6 Months...") + viewcounts = getDashboardsWithViewCount(DT_CLIENT, METRIC_SELECTOR, + RESOLUTION, FROM_DATE, + TO_DATE) + \ No newline at end of file