74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
import os
|
|
import dynatraceAPI
|
|
import logging
|
|
from decouple import config
|
|
import yaml
|
|
import pandas as pd
|
|
from dynatrace import Dynatrace
|
|
import logging
|
|
|
|
|
|
def calculateDifference(dashboards, viewCounts):
|
|
ids = []
|
|
for stub in getattr(dashboards, "_PaginatedList__elements"):
|
|
ids.append(getattr(stub, "id"))
|
|
|
|
viewIds = []
|
|
for metricSeries in getattr(viewCounts, "_PaginatedList__elements"):
|
|
for metric in getattr(metricSeries, "data"):
|
|
viewIds.append(getattr(metric, "dimension_map")["id"])
|
|
|
|
obsolete = []
|
|
for value in ids:
|
|
if value not in viewIds:
|
|
obsolete.append(value)
|
|
return obsolete
|
|
|
|
|
|
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(DT_CLIENT):
|
|
dashboards = DT_CLIENT.dashboards.list(owner=None, tags=None)
|
|
return dashboards
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
|
|
|
with open(os.path.basename("./environment.yaml")) as env_cfg:
|
|
environment = yaml.safe_load(env_cfg)
|
|
|
|
for item, doc in environment.items():
|
|
logging.info("%s checking token...", str(item))
|
|
|
|
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("%s init Dynatrace client...", str(item))
|
|
DT_CLIENT = Dynatrace(DT_URL, DT_TOKEN, logging.Logger("ERROR"),
|
|
None, None, 0, 10*1000)
|
|
|
|
logging.info("%s get all dashboards...", str(item))
|
|
dashboards = getDashboards(DT_CLIENT)
|
|
|
|
logging.info("%s get viewcount of dashboards older than 6 months..."
|
|
, str(item))
|
|
viewCounts = getDashboardsWithViewCount(DT_CLIENT, METRIC_SELECTOR,
|
|
RESOLUTION, FROM_DATE,
|
|
TO_DATE)
|
|
|
|
logging.info("%s calculate difference...", str(item))
|
|
result = calculateDifference(dashboards, viewCounts)
|
|
|
|
logging.info("%s calculated!", str(item)) |