Commit 318627a1 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Add Link dashboard to PM

--HG--
branch : feature/microservices
parent 165980c5
......@@ -7,6 +7,8 @@
##----------------------------------------------------------------------
## Python modules
import os
import ujson
import logging
......@@ -19,6 +21,8 @@ class BaseDashboard(object):
def __init__(self, object):
self.object = self.resolve_object(object)
self.logger = logging.getLogger("dashboard.%s" % self.name)
self.templates_path = ""
self.templates = self.load_templates()
def resolve_object(self, object):
"""
......@@ -31,3 +35,40 @@ class BaseDashboard(object):
Render dashboard and return grafana's dashboard JSON
"""
return None
def load_templates(self):
"""
Load dashboard templates from path and return template dict, key is folder.filename without .json
"""
if self.templates_path:
t_path = self.templates_path
else:
t_path = os.path.join("services", "web", "apps", "pm", "ddash", "dashboards", "templates")
t = {}
if not os.path.exists(t_path):
self.logger.warn("Templates path %s is not exist" % t_path)
return {}
for f in os.listdir(t_path):
path = os.path.join(t_path, f)
if os.path.isfile(path):
if ".json" not in f:
self.logger.info("Extension file %s is not .json" % fl)
continue
with open(os.path.join(t_path, f)) as data_file:
try:
t[f.split(".")[0]] = ujson.load(data_file)
except ValueError:
self.logger.error("Dashboard template file %s not contains valid JSON" % fl)
continue
continue
for fl in os.listdir(path):
if ".json" not in fl:
self.logger.info("Extension file %s is not .json" % fl)
continue
with open(os.path.join(t_path, f, fl)) as data_file:
try:
t[".".join((f, fl.split(".")[0]))] = ujson.load(data_file)
except ValueError:
self.logger.error("Dashboard template file %s not contains valid JSON" % fl)
continue
return t
# -*- coding: utf-8 -*-
"""
##----------------------------------------------------------------------
## Link's dynamic dashboard
##----------------------------------------------------------------------
## Copyright (C) 2007-2016 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------
"""
import string
# NOC modules
from base import BaseDashboard
from noc.inv.models.link import Link
class LinkDashboard(BaseDashboard):
name = "link"
def resolve_object(self, object):
try:
return Link.objects.get(id=object)
except Link.DoesNotExist:
raise self.NotFound()
def render(self):
mos = self.object.managed_objects
ifaces = self.object.interfaces
self.logger.info("Link ID is: %s, MO: %s, Ifaces: %s" % (self.object.id, mos, ifaces))
list = []
refId = string.lowercase[:14]
fish = {
"current": {},
"datasource": None,
"hide": 2,
"includeAll": False,
"multi": False,
"name": "device_a",
"options": [],
"query": "",
"refresh": 0,
"type": "custom"
}
for o in mos:
v = {
"text": o.name,
"value": o.name
}
c = {
"name": "device_%s" % refId[mos.index(o)],
"query": o.name,
"options": v,
"current": v
}
fish.update(c)
list += [fish.copy()]
query = ",".join([i.name for i in ifaces])
options = [{"text": i.name, "value": i.name} for i in ifaces]
for i in ifaces:
v = {
"text": i.name,
"value": i.name
}
c = {
"name": "interface_%s" % refId[ifaces.index(i)],
"label": u"Интерфейс %s" % refId[ifaces.index(i)].upper(),
"query": query,
"options": options,
"current": v
}
fish.update(c)
list += [fish.copy()]
config = [{"template": "link_def1", "templating": {"list": list}}]
r = self.generator(config)
return r
def generator(self, config):
"""Create dashboard from config and template"""
t = self.templates
r = []
for c in config:
# templ_name = c["template"]
templ_name = c.pop("template")
# r1 = t[c["template"]]
if "panel" in templ_name:
m2 = []
for tg in c.pop("targets"):
t[tg.pop("template")].update(tg)
m2.append(t["panel.targets_basic"].copy())
c["targets"] = m2[:]
# t[templ_name]["targets"] = t["targets"].update(c.pop("targets"))
t[templ_name].update(c)
r["rows"][-1]["panels"] += [t[templ_name].copy()]
elif "row" in templ_name:
t[templ_name].update(c)
r["rows"] += [t[templ_name].copy()]
elif "dashboard" in templ_name or "." not in templ_name:
t[templ_name].update(c)
r = t[templ_name].copy()
else:
t[templ_name].update(c)
r += [t[templ_name].copy()]
return r
......@@ -10,6 +10,7 @@
from noc.lib.app.extapplication import ExtApplication, view
from dashboards.base import BaseDashboard
from dashboards.managedobject import ManagedObjectDashboard
from dashboards.link import LinkDashboard
from noc.core.translation import ugettext as _
......@@ -20,7 +21,8 @@ class DynamicDashboardApplication(ExtApplication):
title = _("Dynamic Dashboard")
dashboards = {
"managedobject": ManagedObjectDashboard
"managedobject": ManagedObjectDashboard,
"link": LinkDashboard
}
@view(
......
......@@ -122,7 +122,7 @@ class RunSnippetApplication(Application):
)
return task.id
@view(url=r"^$", url_name="index", menu="Tasks | Run Snippet",
@view(url=r"^$", url_name="index", menu=_("Tasks") + " | " + _("Run Snippet"),
access="launch")
def view_index(self, request):
""" Display all available snippets"""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment