def get_counterscore(): ret = { "ok": False, "msg": "", "data": [], } cluster = request.form.get("cluster") or "" counter = request.form.get("counter") or "" endpoints = Endpoint.search_endpoint_by_cluster(cluster) score = 100 for x in endpoints: method = "POST" handler = urllib2.HTTPHandler() opener = urllib2.build_opener(handler) url = config.QUERY_ADDR + "/graph/last" port = 0 p = [] if x.id!="None": port = x.id else: port = 3306 q = { "endpoint": x.endpoint, "counter": (counter + "%s")%port } p.append(q) request1 = urllib2.Request(url, data=json.dumps(p)) request1.add_header("Content-Type",'application/json') request1.get_method = lambda: method try: connection = opener.open(request1) except urllib2.HTTPError,e: connection = e # check. Substitute with appropriate HTTP code. if connection.code == 200: msg = connection.read() # print ("msg===>>>" + msg) jsonmsg = json.loads(msg) if jsonmsg[0]["value"]: if jsonmsg[0]["value"]["value"]: f = float(jsonmsg[0]["value"]["value"]) if f > 5: score = score -10 ret['ok'] = True else: print '{"err":1,"msg":"%s"}' % connection ret['ok'] = False
def get_endpoints_by_cluster(cluster): ret = { "ok": False, "msg": "", "data": [], } endpoints = Endpoint.search_endpoint_by_cluster(cluster) endpoints_str = [x.endpoint for x in endpoints] endpoints_str.sort() ret['data'] = endpoints_str ret['ok'] = True return json.dumps(ret)