Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Fix bugs in creating dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Jun 8, 2020
1 parent 26d50b9 commit 2f8cb9e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
5 changes: 3 additions & 2 deletions monitor/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from common.logger import Logger

LOG_PREFIX = "System Monitor"
logger = Logger(actor="Searcher", log_name_prefix=LOG_PREFIX)
logger = Logger(actor="Data loader", log_name_prefix=LOG_PREFIX)


class DataLoader:
Expand Down Expand Up @@ -64,7 +64,8 @@ def __init__(self, es_host, es_port, api_host, api_port, log_file, cloud_id=None
def start(self):
"""Loads data to be visualized in Kibana"""

self.delete_index("logs")
if self.index_client.exists("logs"):
self.delete_index("logs")

# Pull the watchtower logs into Elasticsearch.
self.create_index("logs")
Expand Down
45 changes: 37 additions & 8 deletions monitor/visualizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import requests

from monitor.searcher import LOG_PREFIX
from monitor.data_loader import LOG_PREFIX
from common.logger import Logger

from monitor.visualizations import index_pattern, visualizations, dashboard
Expand All @@ -23,17 +23,22 @@ def __init__(self, kibana_host, kibana_port, auth_user, auth_pw, max_users):
self.max_users = max_users

def create_dashboard(self):
# Create index pattern to pull Elasticsearch data into Kibana.
if not self.exists("index-pattern", "title", index_pattern.get("attributes").get("title")):
resp = self.create_saved_object("index-pattern", index_pattern.get("attributes"), [])
index_id = None

# Find index pattern id if it exists. If it does not, create one to pull Elasticsearch data into Kibana.
index_pattern_json = self.find("index-pattern", "title", index_pattern.get("attributes").get("title"))

index_id = resp.get("id")
if index_pattern_json.get("total") == 0:
resp = self.create_saved_object("index-pattern", index_pattern.get("attributes"), [])
index_id = resp.get("id")
else:
index_id = index_pattern_json.get("saved_objects")[0].get("id")

visuals = []
panelCount = 0

for key, value in visualizations.items():
if not self.exists("visualization", "title", value.get("attributes").get("title")):
for key, value in visualizations.items():
if not self.exists("visualization", "title", value.get("attributes").get("title")):
if key == "available_user_slots_visual":
visState_json = json.loads(value["attributes"]["visState"])
visState_json["params"]["gauge"]["colorsRange"][0]["to"] = self.max_users
Expand All @@ -53,8 +58,32 @@ def create_dashboard(self):
panelCount += 1

if not self.exists("dashboard", "title", dashboard.get("attributes").get("title")):
panels_JSON = json.loads(dashboard["attributes"]["panelsJSON"])

for i, panel in enumerate(panels_JSON):
visual_id = visuals[i].get("id")
panel["gridData"]["i"] = visual_id
panel["panelIndex"] = visual_id

dashboard["attributes"]["panelsJSON"] = json.dumps(panels_JSON)

self.create_saved_object("dashboard", dashboard.get("attributes"), visuals)

def find(self, obj_type, search_field, search):
endpoint = "{}{}".format(self.saved_obj_endpoint, "_find")

data = {
"type": obj_type,
"search_fields": search_field,
"search": search,
"default_search_operator": "AND"
}

response = requests.get(endpoint, params=data, headers=self.headers, auth=self.auth)

return response.json()


def exists(self, obj_type, search_field, search):
endpoint = "{}{}".format(self.saved_obj_endpoint, "_find")

Expand Down Expand Up @@ -90,6 +119,6 @@ def create_saved_object(self, obj_type, attributes, references):
response = requests.post(endpoint, data=data, headers=self.headers, auth=self.auth)

# log when an item is created.
logger.info("New Kibana saved object was created", response.text)
logger.info("New Kibana saved object was created")

return response.json()

0 comments on commit 2f8cb9e

Please sign in to comment.