def massObjectUpdate(arr, filename, fct, update=False): ## Initialize store = {} if (isFileAvailable(filename)): store = readJSONFromFile(filename).get("data") ## Identify ids that have to be updated idsToUpdate = [] if (update): for o in arr: oId = str(o.getId()) idsToUpdate.append(oId) else: keysInFile = [] for key, val in store.items(): keysInFile.append(key) # print("Keys: {}".format(keysInFile)) for o in arr: oId = str(o.getId()) if (not (oId in keysInFile)): idsToUpdate.append(oId) # print("IDs to update: {}".format(idsToUpdate)) ## Fetch additional information and store them in file output = execFctInParallel(idsToUpdate, fct) store = storeParallelOuputToFile(output, filename) ## Enrich object with information just fetched for o in arr: oId = str(o.getId()) o.setDetailsJSON(store[oId]) return arr
def storeParallelOuputToFile(output, filename): store = {} if (isFileAvailable(filename)): store = readJSONFromFile(filename).get("data") for o in output: oId = o.get('id') store[oId] = o.get('output') writeJSONToFile(filename, store) return store
def _getDashboardsJSON(update=False): if (update): dashboardsJSON = api.Dashboard.get_all() print("load (dashboards)") writeJSONToFile(FILENAME, dashboardsJSON) return {"data": dashboardsJSON} else: if (isFileAvailable(FILENAME)): return readJSONFromFile(FILENAME) else: return _getDashboardsJSON(True)
def _getMonitorsJSON(update=False): if (update): monitorsJSON = api.Monitor.get_all() print("load (monitors)") print("loaded {} monitors".format(len(monitorsJSON))) writeJSONToFile(FILENAME, monitorsJSON) return {"data": monitorsJSON} else: if (isFileAvailable(FILENAME)): return readJSONFromFile(FILENAME) else: return _getMonitorsJSON(True)
def getDashboardDetails(dashboardId, update=False): # print("_getDashboardDetails: {}".format(dashboardId)) if (update): dashboardDetailsJSON = api.Dashboard.get(dashboardId) dashboardDetailsJSONStored = readJSONFromFile(FILENAME_DETAILS).get( "data") dashboardDetailsJSONStored[dashboardId] = dashboardDetailsJSON writeJSONToFile(FILENAME_DETAILS, dashboardDetailsJSONStored) return dashboardDetailsJSON else: if (isFileAvailable(FILENAME_DETAILS)): dashboardDetailsJSONStored = readJSONFromFile( FILENAME_DETAILS).get("data") if (dashboardId in dashboardDetailsJSONStored): return dashboardDetailsJSONStored[dashboardId] else: return getDashboardDetails(dashboardId, True) else: writeJSONToFile(FILENAME_DETAILS, {}) return getDashboardDetails(dashboardId, True)
def enrichDashboardsWithDetails(arr, update=False): keys = [] if (isFileAvailable(FILENAME_DETAILS)): detailsJSONStored = readJSONFromFile(FILENAME_DETAILS) for key, val in detailsJSONStored.items(): keys.append(key) # print("Keys: {}".format(keys)) total = len(arr) i = 0 for o in arr: i = i + 1 print("load progress (dashboards) {}/{}".format(i, total)) oId = str(o.getId()) # print("Check for id: {} in keys: {}".format(oId, keys)) if ((oId in keys) and not update): # print("in file") o.setDetailsJSON(detailsJSONStored[oId]) else: # print("api call") o.setDetailsJSON(getDashboardDetails(oId, update)) return arr