def expand_view(request): "View for expanding a pattern into matching metric paths" local_only = int(request.REQUEST.get("local", 0)) group_by_expr = int(request.REQUEST.get("groupByExpr", 0)) leaves_only = int(request.REQUEST.get("leavesOnly", 0)) results = {} for query in request.REQUEST.getlist("query"): results[query] = set() for node in OPENTSDB_STORE.find(query, local=local_only): if node.is_leaf or not leaves_only: results[query].add(node.path) # Convert our results to sorted lists because sets aren't json-friendly if group_by_expr: for query, matches in results.items(): results[query] = sorted(matches) else: results = sorted(reduce(set.union, results.values(), set())) result = {"results": results} response = json_response_for(request, result) response["Pragma"] = "no-cache" response["Cache-Control"] = "no-cache" return response
def find_view(request): """View for finding metrics matching a given pattern.""" profile = getProfile(request) format = request.REQUEST.get("format", "treejson") local_only = int(request.REQUEST.get("local", 0)) wildcards = int(request.REQUEST.get("wildcards", 0)) fromTime = int(request.REQUEST.get("from", -1)) untilTime = int(request.REQUEST.get("until", -1)) jsonp = request.REQUEST.get("jsonp", False) if fromTime == -1: fromTime = None if untilTime == -1: untilTime = None automatic_variants = int(request.REQUEST.get("automatic_variants", 0)) try: query = str(request.REQUEST["query"]) except: return HttpResponseBadRequest(content="Missing required parameter 'query'", content_type="text/plain") if "." in query: base_path = query.rsplit(".", 1)[0] + "." else: base_path = "" if format == "completer": query = query.replace("..", "*.") if not query.endswith("*"): query += "*" if automatic_variants: query_parts = query.split(".") for i, part in enumerate(query_parts): if "," in part and "{" not in part: query_parts[i] = "{%s}" % part query = ".".join(query_parts) try: matches = list(OPENTSDB_STORE.find(query, fromTime, untilTime, local=local_only)) log.info("MATCHES: " + str(matches)) except: log.exception() raise log.info("find_view query=%s local_only=%s matches=%d" % (query, local_only, len(matches))) matches.sort(key=lambda node: node.name) log.info( ("received remote find request: pattern=%s from=%s until=%s " "local_only=%s format=%s matches=%d") % (query, fromTime, untilTime, local_only, format, len(matches)) ) if format == "treejson": content = tree_json(matches, base_path, wildcards=profile.advancedUI or wildcards) response = json_response_for(request, content) elif format == "pickle": content = pickle_nodes(matches) response = HttpResponse(content, content_type="application/pickle") elif format == "completer": results = [] for node in matches: node_info = dict(path=node.path, name=node.name, is_leaf=str(int(node.is_leaf))) if not node.is_leaf: node_info["path"] += "." results.append(node_info) if len(results) > 1 and wildcards: wildcardNode = {"name": "*"} results.append(wildcardNode) response = json_response_for(request, {"metrics": results}, jsonp=jsonp) else: return HttpResponseBadRequest(content="Invalid value for 'format' parameter", content_type="text/plain") response["Pragma"] = "no-cache" response["Cache-Control"] = "no-cache" return response