瀏覽代碼

bugfix: search counters by tags in screen

laiwei 9 年之前
父節點
當前提交
d25adaf5a7
共有 2 個文件被更改,包括 48 次插入0 次删除
  1. 1 0
      rrd/__init__.py
  2. 47 0
      rrd/utils/graph_urls.py

+ 1 - 0
rrd/__init__.py

@@ -8,6 +8,7 @@ app.config.from_object("rrd.config")
 
 @app.errorhandler(Exception)
 def all_exception_handler(error):
+    print "exception: %s" %error
     return u'dashboard 暂时无法访问,请联系管理员', 500
 
 from view import api, chart, screen, index

+ 47 - 0
rrd/utils/graph_urls.py

@@ -2,9 +2,12 @@
 import requests
 import json
 import copy
+import re
 from rrd import config
 from rrd.consts import ENDPOINT_DELIMITER
 from rrd.model.graph import TmpGraph
+from rrd.model.endpoint import Endpoint
+from rrd.model.endpoint_counter import EndpointCounter
 
 def generate_graph_urls(graph, start, end):
     counters = graph.counters or []
@@ -15,6 +18,50 @@ def generate_graph_urls(graph, start, end):
     if not endpoint_list:
         return []
 
+    endpoint_objs = Endpoint.gets_by_endpoint(endpoint_list)
+    if not endpoint_objs:
+        return []
+    endpoint_ids = [x.id for x in endpoint_objs]
+
+    counters = []
+    for c in graph.counters:
+        if c.find("metric=") == -1:
+            counters.append(c)
+        else:
+            metric=""
+            tags = []
+            qs = []
+            c = c.strip()
+            for q in c.split():
+                q = q.strip()
+                if q.startswith("metric="):
+                    metric = q.replace("metric=", "", 1)
+                    qs.append(metric)
+                else:
+                    qs.append(q)
+                    tags.append(q)
+
+            counter_objs = EndpointCounter.search_in_endpoint_ids(qs, endpoint_ids[:], limit=100)
+            if not counter_objs:
+                continue
+            for co in counter_objs:
+                if not re.search('^%s(/|$)' %metric, co.counter):
+                    continue
+
+                matched = True
+                for tag in tags:
+                    if not re.search('(/|,)%s(,|$)' %tag, co.counter):
+                        matched = False
+                        break
+                if not matched:
+                    continue
+
+                counters.append(co.counter)
+
+    if not counters:
+        return []
+    counters = sorted(list(set(counters)))
+
     return _generate_graph_urls(graph, counters, endpoint_list, start, end)
 
 def _generate_graph_urls(graph, counters, endpoint_list, start, end):