def field(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} req_data = request.GET my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: res = BackendRequest.search({ "query": req_data["query"], "token": es_check["t"], "operator": es_check["u"], "owner_name": es_check["u"], "owner_id": es_check["i"], "time_range": "-1d,now" if req_data["time_range"] == "," else req_data["time_range"], "order": req_data.get("order", "desc"), "size": req_data.get("size", 20), "page": int(req_data.get("page", 1)) - 1, "filter_field": req_data.get("filters", ""), "with_trend": "false", "category": "fields", "field": req_data.get("field", ""), "source_group": req_data.get("sourcegroup", "all") }) if res["result"]: data = {"status": "1", "list": []} for i in res["buckets"]: if not i["key"] in data["list"]: data["list"].append(i["key"]) else: data = {"status": "0", "msg": "search error"} dummy_data = data else: dummy_data["status"] = "0" dummy_data["msg"] = "auth check error!" bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) result = bundle resp = self.create_response(request, result) return resp
def new_field(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} req_data = request.GET my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: res = BackendRequest.search({ "query": req_data["query"] + " | top 100 " + req_data.get("field", ""), "token": es_check["t"], "operator": es_check["u"], "owner_name": es_check["u"], "owner_id": es_check["i"], "time_range": "-1d,now" if req_data["time_range"] == "," else req_data["time_range"], "order": req_data.get("order", "desc"), "size": req_data.get("size", 20), "page": int(req_data.get("page", 1)) - 1, "filter_field": req_data.get("filters", ""), "field": req_data.get("field", ""), # "source_group": "0811test" "source_group": req_data.get("sourcegroup", "all") }) if res["rc"] == 0: data = { "status": "1", "index": req_data.get("index", 0), "list": [] } for item in res["result"]["sheets"].get("rows", []): data["list"].append(item[req_data.get("field", "")]) else: data = {"status": "0", "msg": "search error"} dummy_data = data else: dummy_data["status"] = "0" dummy_data["msg"] = "auth check error!" bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) result = bundle resp = self.create_response(request, result) return resp
def search(self, request, **kwargs): self.method_check(request, allowed=['get']) dummy_data = {} req_data = request.GET my_auth = MyBasicAuthentication() es_check = my_auth.is_authenticated(request, **kwargs) if es_check: group_id = req_data.get("group_id", "") if group_id: param = { "jid": group_id, "category": "events", "token": es_check["t"], "operator": es_check["u"], "owner_name": es_check["u"], "owner_id": es_check["i"], "sid": req_data.get("sid", ""), "size": req_data["size"], "usetable": "true", "page": int(req_data["page"]) - 1 } else: param = { "query": req_data["query"], "token": es_check["t"], "operator": es_check["u"], "owner_name": es_check["u"], "owner_id": es_check["i"], "time_range": "-1d,now" if req_data["time_range"] == "," else req_data["time_range"], "order": req_data.get("order", "desc"), "size": req_data.get("size", 20), "page": int(req_data.get("page", 1)) - 1, "filter_field": req_data.get("filters", ""), "field": req_data.get("field", ""), # "source_group": "0811test" "source_group": req_data.get("sourcegroup", "all") } res = BackendRequest.search(param) if res["rc"] == 0: _type, rst, hits = self._build_events_new(res) data = { "status": 1, "total": res["result"]["sheets"].get("total", 0), "page": int(req_data.get("page", 1)) - 1, "size": req_data.get("size", 20), "type": _type, "sid": req_data.get("sid", ""), "group_id": res.get("groups_id", ""), "search_timerange": str(res["result"].get("starttime", "")) + "," + str(res["result"].get("endtime", "")), "table": { "head": res["result"]["sheets"].get("_field_infos_", []), "body": rst, "total": res["result"]["sheets"].get("total", 0) } } if req_data.get("needParamRtn", "no") == "yes": data["search_param"] = req_data dummy_data = data dummy_data["status"] = "1" else: dummy_data = err_data.build_error_new(error_code=res.get( "rc", "1"), msg=res.get("error", "")) else: data = err_data.build_error({}, "auth error!") data["location"] = "/auth/login/" dummy_data = data bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request) response_data = bundle resp = self.create_response(request, response_data) return resp