Commit 261c3e1e authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Add cutomized dashboard to PM.

parent 5b2a451f
......@@ -6,19 +6,17 @@
# ---------------------------------------------------------------------
# Third-Party modules
import demjson
from jinja2 import Environment, FileSystemLoader
from mongoengine.errors import DoesNotExist
# NOC modules
from .base import BaseDashboard
from noc.config import config
from .jinja import JinjaDashboard
from noc.sa.models.managedobject import ManagedObject
from noc.inv.models.object import Object
class ContainerDashboard(BaseDashboard):
class ContainerDashboard(JinjaDashboard):
name = "container"
template = "dash_container.j2"
def resolve_object(self, object):
try:
......@@ -46,20 +44,12 @@ class ContainerDashboard(BaseDashboard):
return self.object_data
return self.object_data
def render(self):
def get_context(self):
bi_ids = []
for o in self.object:
bi_ids.append({"id": o.bi_id, "name": o.name})
context = {
return {
"bi_ids": bi_ids,
"container_path": self.object_data["container_path"],
"container_id": self.container,
}
self.logger.info("Context with data: %s" % context)
j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
tmpl = j2_env.get_template("dash_container.j2")
data = tmpl.render(context)
render = demjson.decode(data)
return render
# ---------------------------------------------------------------------
# IPSLA's dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-Party modules
import demjson
from django.db.models import Q
from jinja2 import Environment, FileSystemLoader
# NOC modules
from .base import BaseDashboard
from sla.models.slaprobe import SLAProbe
from noc.config import config
from noc.sa.models.managedobject import ManagedObject
class IPSLADashboard(BaseDashboard):
name = "ipsla"
def resolve_object(self, object):
o = ManagedObject.objects.filter(Q(id=object) | Q(bi_id=object))[:1]
if not o:
raise self.NotFound()
else:
return o[0]
def render(self):
context = {
"device": self.str_cleanup(self.object.name),
"ip": self.object.address,
"device_id": self.object.id,
"bi_id": self.object.bi_id,
"segment": self.object.segment.id,
"probes": [
{"name": self.str_cleanup(probe.name), "value": probe.target}
for probe in SLAProbe.objects.filter(managed_object=self.object.id)
],
}
self.logger.info("Context with data: %s" % context)
j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
tmpl = j2_env.get_template("dash_ipsla.j2")
data = tmpl.render(context)
render = demjson.decode(data)
return render
# ---------------------------------------------------------------------
# IPSLA's dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-Party modules
from django.db.models import Q
# NOC modules
from .jinja import JinjaDashboard
from noc.sla.models.slaprobe import SLAProbe
from noc.sa.models.managedobject import ManagedObject
class IPSLADashboard(JinjaDashboard):
name = "ipsla"
template = "dash_ipsla.j2"
def resolve_object(self, object):
o = ManagedObject.objects.filter(Q(id=object) | Q(bi_id=object))[:1]
if not o:
raise self.NotFound()
else:
return o[0]
def get_context(self):
return {
"device": self.str_cleanup(self.object.name),
"ip": self.object.address,
"device_id": self.object.id,
"bi_id": self.object.bi_id,
"segment": self.object.segment.id,
"probes": [
{"name": self.str_cleanup(probe.name), "value": probe.target}
for probe in SLAProbe.objects.filter(managed_object=self.object.id)
],
}
# ---------------------------------------------------------------------
# Jinja dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import os
# Third-party modules
from jinja2 import Environment, FileSystemLoader
import demjson
# NOC modules
from noc.config import config
from .base import BaseDashboard
class JinjaDashboard(BaseDashboard):
template = None
def get_context(self):
"""
Context for render template
:return:
"""
return {}
def render(self):
context = self.get_context()
self.logger.info("Context with data: %s" % context)
pm_template_path = []
for p in config.get_customized_paths("", prefer_custom=True):
if p:
pm_template_path += [os.path.join(p, "templates/ddash/")]
else:
pm_template_path += [config.path.pm_templates]
j2_env = Environment(loader=FileSystemLoader(pm_template_path))
tmpl = j2_env.get_template(self.template)
data = tmpl.render(context)
render = demjson.decode(data)
return render
# ---------------------------------------------------------------------
# Link's dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-Party modules
import demjson
from jinja2 import Environment, FileSystemLoader
# NOC modules
from .base import BaseDashboard
from noc.config import config
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
if mos.interfaces[0].description:
mos.interfaces[0].description = self.str_cleanup(mos.interfaces[0].description)
if mos.interfaces[1].description:
mos.interfaces[1].description = self.str_cleanup(mos.interfaces[1].description)
context = {
"device_a": mos.interfaces[0].managed_object.name.replace('"', ""),
"device_b": mos.interfaces[1].managed_object.name.replace('"', ""),
"bi_id_a": mos.interfaces[0].managed_object.bi_id,
"bi_id_b": mos.interfaces[0].managed_object.bi_id,
"interface_a": {
"name": mos.interfaces[0].name,
"descr": mos.interfaces[0].description or mos.interfaces[0].name,
},
"interface_b": {
"name": mos.interfaces[1].name,
"descr": mos.interfaces[1].description or mos.interfaces[1].name,
},
"segment": mos.managed_objects[0].segment.id,
"device_a_id": mos.managed_objects[0].id,
"device_b_id": mos.managed_objects[1].id,
"pool": mos.managed_objects[0].pool.name,
"link_id": mos.id,
}
self.logger.info("Context with data: %s" % context)
j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
tmpl = j2_env.get_template("dash_link.j2")
data = tmpl.render(context)
render = demjson.decode(data)
return render
# ---------------------------------------------------------------------
# Link's dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# NOC modules
from .jinja import JinjaDashboard
from noc.inv.models.link import Link
class LinkDashboard(JinjaDashboard):
name = "link"
template = "dash_link.j2"
def resolve_object(self, object):
try:
return Link.objects.get(id=object)
except Link.DoesNotExist:
raise self.NotFound()
def get_context(self):
mos = self.object
if mos.interfaces[0].description:
mos.interfaces[0].description = self.str_cleanup(mos.interfaces[0].description)
if mos.interfaces[1].description:
mos.interfaces[1].description = self.str_cleanup(mos.interfaces[1].description)
return {
"device_a": mos.interfaces[0].managed_object.name.replace('"', ""),
"device_b": mos.interfaces[1].managed_object.name.replace('"', ""),
"bi_id_a": mos.interfaces[0].managed_object.bi_id,
"bi_id_b": mos.interfaces[0].managed_object.bi_id,
"interface_a": {
"name": mos.interfaces[0].name,
"descr": mos.interfaces[0].description or mos.interfaces[0].name,
},
"interface_b": {
"name": mos.interfaces[1].name,
"descr": mos.interfaces[1].description or mos.interfaces[1].name,
},
"segment": mos.managed_objects[0].segment.id,
"device_a_id": mos.managed_objects[0].id,
"device_b_id": mos.managed_objects[1].id,
"pool": mos.managed_objects[0].pool.name,
"link_id": mos.id,
}
# ----------------------------------------------------------------------
# Dashboadr Loader
# ----------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import logging
import os
# NOC modules
from noc.core.loader.base import BaseLoader
from .base import BaseDashboard
logger = logging.getLogger(__name__)
BASE_PREFIX = os.path.join("services", "web", "apps", "pm", "ddash", "dashboards")
class PMDashboardLoader(BaseLoader):
name = "pmdashboard"
base_cls = BaseDashboard
base_path = ("services", "web", "apps", "pm", "ddash", "dashboards")
ignored_names = {"base", "loader", "jinja"}
# Create singleton object
loader = PMDashboardLoader()
......@@ -6,15 +6,12 @@
# ---------------------------------------------------------------------
# Third-Party modules
import demjson
from django.db.models import Q
from jinja2 import Environment, FileSystemLoader
from collections import defaultdict
import operator
# NOC modules
from .base import BaseDashboard
from noc.config import config
from .jinja import JinjaDashboard
from noc.inv.models.interface import Interface
from noc.inv.models.subinterface import SubInterface
from noc.core.text import alnum_key
......@@ -24,8 +21,9 @@ from noc.sa.models.managedobject import ManagedObject
TITLE_BAD_CHARS = '"\\\n\r'
class MODashboard(BaseDashboard):
class MODashboard(JinjaDashboard):
name = "mo"
template = "dash_mo.j2"
def resolve_object(self, object):
o = ManagedObject.objects.filter(Q(id=object) | Q(bi_id=object))[:1]
......@@ -194,9 +192,9 @@ class MODashboard(BaseDashboard):
"dom_types": sorted(dom_types, key=lambda x: alnum_key(x["name"])),
}
def render(self):
def get_context(self):
context = {
return {
"port_types": self.object_data["port_types"],
"object_metrics": self.object_data["object_metrics"],
"object_check_metrics": self.object_data["object_check_metrics"],
......@@ -219,9 +217,3 @@ class MODashboard(BaseDashboard):
"ping_interval": self.object.object_profile.ping_interval,
"discovery_interval": self.object.object_profile.periodic_discovery_interval,
}
self.logger.info("Context with data: %s" % context)
j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
tmpl = j2_env.get_template("dash_mo.j2")
data = tmpl.render(context)
render = demjson.decode(data)
return render
......@@ -6,14 +6,11 @@
# ---------------------------------------------------------------------
# Third-Party modules
import demjson
import json
from django.db.models import Q
from jinja2 import Environment, FileSystemLoader
# NOC modules
from .base import BaseDashboard
from noc.config import config
from .jinja import JinjaDashboard
from noc.inv.models.interface import Interface
from noc.inv.models.subinterface import SubInterface
from noc.sa.models.managedobject import ManagedObject
......@@ -21,8 +18,9 @@ from noc.sa.models.managedobject import ManagedObject
TITLE_BAD_CHARS = '"\\\n\r'
class MOCardDashboard(BaseDashboard):
class MOCardDashboard(JinjaDashboard):
name = "mocard"
template = "dash_mo_card.j2"
def resolve_object(self, object):
o = ManagedObject.objects.filter(Q(id=object) | Q(bi_id=object))[:1]
......@@ -112,9 +110,9 @@ class MOCardDashboard(BaseDashboard):
"radio_types": radio_types,
}
def render(self):
def get_context(self):
context = {
return {
"port_types": self.object_data["port_types"],
"object_metrics": self.object_data["object_metrics"],
"device": self.object.name.replace('"', ""),
......@@ -125,9 +123,3 @@ class MOCardDashboard(BaseDashboard):
"ping_interval": self.object.object_profile.ping_interval,
"discovery_interval": self.object.object_profile.periodic_discovery_interval,
}
self.logger.info("Context with data: %s" % context)
j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
tmpl = j2_env.get_template("dash_mo_card.j2")
data = tmpl.render(context)
render = demjson.decode(data)
return render
......@@ -8,12 +8,8 @@
# NOC modules
from noc.lib.app.extapplication import ExtApplication, view
from .dashboards.loader import loader
from .dashboards.base import BaseDashboard
from .dashboards.mo import MODashboard
from .dashboards.mocard import MOCardDashboard
from .dashboards.link import LinkDashboard
from .dashboards.ipsla import IPSLADashboard
from .dashboards.container import ContainerDashboard
from noc.core.translation import ugettext as _
......@@ -24,17 +20,13 @@ class DynamicDashboardApplication(ExtApplication):
title = _("Dynamic Dashboard")
dashboards = {
"mo": MODashboard,
"mocard": MOCardDashboard,
"link": LinkDashboard,
"ipsla": IPSLADashboard,
"container": ContainerDashboard,
}
@view(url=r"^$", method="GET", access="launch", api=True)
def api_dashboard(self, request):
dt = self.dashboards.get(request.GET.get("dashboard"))
try:
dt = loader[request.GET.get("dashboard")]
except Exception:
self.logger.error("Exception when loading dashboard: %s", request.GET.get("dashboard"))
return self.response_not_found("Dashboard not found")
if not dt:
return self.response_not_found("Dashboard not found")
oid = request.GET.get("id")
......
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