def list_instance(self, request, project_id, cluster_id): """targets列表, 按instance_id聚合 """ show_discovered = request.GET.get("show_discovered") == "1" result = prometheus.get_targets(project_id, cluster_id).get("data") or [] targets = self._filter_targets(result, None, None, show_discovered) targets_dict = {} for instance_id, t in itertools.groupby( sorted(targets, key=lambda x: x["instance_id"]), key=lambda x: x["instance_id"]): t = [i for i in t] jobs = set([i["job"] for i in t]) if jobs: jobs = "|".join(jobs) expr = f'{{cluster_id="{cluster_id}",job=~"{jobs}"}}' params = {"project_id": project_id, "expr": expr} query = parse.urlencode(params) graph_url = f"{settings.DEVOPS_HOST}/console/monitor/{request.project.project_code}/metric?{query}" else: graph_url = None targets_dict[instance_id] = { "targets": t, "graph_url": graph_url, "total_count": len(t), "health_count": len([i for i in t if i["health"] == "up"]), } return Response(targets_dict)
def list(self, request, project_id, cluster_id, namespace, name): """获取targets列表 """ result = prometheus.get_targets(project_id, cluster_id).get("data") or [] targets = self._filter_targets(result, namespace, name) return Response(targets)
def list(self, request, project_id, cluster_id, namespace=None, name=None): """获取targets列表 """ show_discovered = request.GET.get("show_discovered") == "1" result = prometheus.get_targets(project_id, cluster_id).get("data") or [] targets = self._filter_targets(result, namespace, name, show_discovered) return Response(targets)
def graph(self, request, project_id, cluster_id, namespace, name): """获取下面所有targets的job, 跳转到容器监控""" result = prometheus.get_targets(project_id, cluster_id).get("data") or [] jobs = self._filter_jobs(result, namespace, name) if jobs: jobs = "|".join(jobs) expr = f'{{cluster_id="{cluster_id}",job=~"{jobs}"}}' else: expr = f'{{cluster_id="{cluster_id}"}}' params = {"project_id": project_id, "expr": expr} query = parse.urlencode(params) redirect_url = f"{settings.DEVOPS_HOST}/console/monitor/{request.project.project_code}/metric?{query}" return HttpResponseRedirect(redirect_url)
def list(self, request, project_id, cluster_id): """ 按 instance_id 聚合的 targets 列表""" params = self.params_validate(FetchTargetsSLZ) result = get_targets(project_id, cluster_id).get('data') or [] targets = self._filter_targets(result, params['show_discovered']) targets_dict = {} for instance_id, targets in itertools.groupby( sorted(targets, key=lambda x: x['instance_id']), key=lambda y: y['instance_id'] ): targets = list(targets) jobs = {t['job'] for t in targets} graph_url = self._gen_graph_url(project_id, cluster_id, jobs) if jobs else None targets_dict[instance_id] = { 'targets': targets, 'graph_url': graph_url, 'total_count': len(targets), 'health_count': len([t for t in targets if t['health'] == 'up']), } return Response(targets_dict)