import re from key_request_parser import patterns, keyrequests, helper from enum import Flag, auto class KROption(Flag): VALIDATE_EXISTS = auto() VALIDATE_HASDATA = auto() class KRParser: patterns=[patterns.Pattern1(), patterns.Pattern2(), patterns.Pattern3(), patterns.Pattern4()] def normalize(self,x): #tmp=x.replace("~","") tmp=x.replace("\n","") #tmp=tmp.replace("\"/","\"") #tmp=tmp.replace("\"/","") -_>was active #tmp=tmp.replace("/\"","\"") tmp=tmp.replace("/\"","") tmp=tmp.replace("\"","") tmp=tmp.replace("\t","") tmp=re.sub("([\s]*)\)", ")", tmp) tmp=re.sub("\([\s\n\r]*", "(", tmp) tmp=re.sub("\,[\s\n\r]*", ",", tmp) tmp=re.sub("\)[\s\n\r]*,", "),", tmp) tmp=re.sub("in[\s\n\r]*\(", "in(", tmp) return tmp def applyPatterns(self,subject): groups=None for p in self.patterns: groups=p.parseServicesAndMethods(subject) if len(groups) > 0: break return groups def checkKeyRequetsHasData(self,kr, tfrom, DTAPIURL, DTAPIToken): DTAPIURL = DTAPIURL + "/api/v2/entities" headers = { 'Content-Type': 'application/json', 'Authorization': 'Api-Token ' + DTAPIToken } for gid, group in enumerate(kr.matchedGroups): params={"entitySelector": group["existsQuery"], "from":tfrom["tfrom"]} response = helper.get_request(DTAPIURL, headers, params) entities = (response.json())['entities'] if len(entities) > 0: y=0 for method in kr.keyRequests: if method["groupId"] == gid: found = [x for x in entities if x[method["comparer"]] == method[method["comparer"]]] if len(found) > 0: #method["exists"]=True method["hasData"][tfrom["label"]]=True method["displayName"]=found[0]["displayName"] method["entityId"]=found[0]["entityId"] else: method["hasData"][tfrom["label"]]=False def process(self, kr): for gid, group in enumerate(kr.matchedGroups): for method in group["methods"]: if method.startswith('SERVICE_METHOD-'): tmp={"displayName": None,"comparer": "entityId", "entityId":method, "groupId":gid, "hasData":{}} #"exists":None, 'hasData_1W':None, else: tmp={"displayName":method,"comparer": "displayName", "entityId":None, "groupId":gid, "hasData":{}} #"exists":None, 'hasData_1W':None, kr.keyRequests.append(tmp) # if self.options and KROption.VALIDATE_EXISTS in self.options: # self.checkKeyRequetsExists(kr,self.DTAPIURL, self.DTAPIToken) if KROption.VALIDATE_HASDATA in self.options: self.checkKeyRequetsHasData(kr,{"label":"1W", "tfrom":"now-1w"},self.DTAPIURL, self.DTAPIToken) self.checkKeyRequetsHasData(kr,{"label":"1M", "tfrom":"now-1M"},self.DTAPIURL, self.DTAPIToken) return kr def parseBySLO(self, row): #normalize normFilter=self.normalize(row['filter']) normExpresseion=self.normalize(row['metricExpression']) tmp_KR = keyrequests.KR({"sloName":row["name"], "env":row["env"], "metricExpression": normExpresseion, "filter": normFilter, "matchedGroups": None}) #SLO with Filter if normFilter.upper().startswith("TYPE(SERVICE_METHOD),"): subject=normFilter else: subject=normExpresseion groups=self.applyPatterns(subject) for g in groups: if g["methods"] != None and len(g["methods"]) > 0: tmp_KR.matchedGroups.append(g) #self.process(tmp_KR) return self.process(tmp_KR) def __init__(self, options: KROption , DTAPIURL, DTAPIToken ): self.DTAPIURL= DTAPIURL self.DTAPIToken=DTAPIToken self.options=options