parameters: (self,data_set)
docstring: return average value of given data set
          :param data_set:
          :return:
docstring_summary: return average value of given data set
          :param data_set:
          :return:
docstring_tokens: return average value of given data set : param data_set : : return :
function: def get_avg(self,data_set):
          '''
          return average value of given data set
          :param data_set:
          :return:
          '''
          clean_data_list = []
          clean_data_dic = {}
          for point in data_set:
              val,save_time = point
              #print('---point:>', val)
  
              if val:
                  if 'data' not in val:#没有子dict
                      clean_data_list.append(val[self.expression_obj.service_index.key])
  
                  else: #has sub dict
                      for k,v in val['data'].items():
                          if k not in clean_data_dic:
                              clean_data_dic[k]=[]
  
                          clean_data_dic[k].append(v[self.expression_obj.service_index.key])
  
          if clean_data_list:
              clean_data_list = [float(i) for i in clean_data_list]
              #avg_res = 0 if sum(clean_data_list) == 0 else  sum(clean_data_list)/ len(clean_data_list)
              avg_res = sum(clean_data_list)/ len(clean_data_list)
              print("\033[46;1m----avg res:%s\033[0m" % avg_res)
              return [self.judge(avg_res), avg_res,None]
              #print('clean data list:', clean_data_list)
          elif clean_data_dic:
              for k,v in clean_data_dic.items():
                  clean_v_list = [float(i) for i in v]
                  avg_res = 0 if sum(clean_v_list) == 0 else sum(clean_v_list) / len(clean_v_list)
                  print("\033[46;1m-%s---avg res:%s\033[0m" % (k,avg_res))
                  if self.expression_obj.specified_index_key:#监控了特定的指标,比如有多个网卡,但这里只特定监控eth0
                      if k == self.expression_obj.specified_index_key:#就是监控这个特定指标,match上了
                          #在这里判断是否超越阈值
                          print("test res [%s] [%s] [%s]=%s") %(avg_res,
                                                              self.expression_obj.operator_type,
                                                              self.expression_obj.threshold,
                                                              self.judge(avg_res),
                                                              )
                          calc_res = self.judge(avg_res)
                          if calc_res:
                              return  [calc_res,avg_res,k] #后面的循环不用走了,反正 已经成立了一个了
                  else:#监控这个服务 的所有项, 比如一台机器的多个网卡, 任意一个超过了阈值,都 算是有问题的
                      calc_res = self.judge(avg_res)
                      if calc_res:
                          return [calc_res,avg_res,k]
                  print('specified monitor key:',self.expression_obj.specified_index_key)
                  print('clean data dic:',k,len(clean_v_list), clean_v_list)
              else: #能走到这一步,代表 上面的循环判段都未成立
                  return [False,avg_res,k]
          else:#可能是由于最近这个服务 没有数据 汇报 过来,取到的数据 为空,所以没办法 判断阈值
              return [False,None,None]
function_tokens: def get_avg ( self , data_set ) : clean_data_list = [ ] clean_data_dic = { } for point in data_set : val , save_time = point #print('---point:>', val) if val : if 'data' not in val : #没有子dict clean_data_list . append ( val [ self . expression_obj . service_index . key ] ) else : #has sub dict for k , v in val [ 'data' ] . items ( ) : if k not in clean_data_dic : clean_data_dic [ k ] = [ ] clean_data_dic [ k ] . append ( v [ self . expression_obj . service_index . key ] ) if clean_data_list : clean_data_list = [ float ( i ) for i in clean_data_list ] #avg_res = 0 if sum(clean_data_list) == 0 else  sum(clean_data_list)/ len(clean_data_list) avg_res = sum ( clean_data_list ) / len ( clean_data_list ) print ( "\033[46;1m----avg res:%s\033[0m" % avg_res ) return [ self . judge ( avg_res ) , avg_res , None ] #print('clean data list:', clean_data_list) elif clean_data_dic : for k , v in clean_data_dic . items ( ) : clean_v_list = [ float ( i ) for i in v ] avg_res = 0 if sum ( clean_v_list ) == 0 else sum ( clean_v_list ) / len ( clean_v_list ) print ( "\033[46;1m-%s---avg res:%s\033[0m" % ( k , avg_res ) ) if self . expression_obj . specified_index_key : #监控了特定的指标,比如有多个网卡,但这里只特定监控eth0 if k == self . expression_obj . specified_index_key : #就是监控这个特定指标,match上了 #在这里判断是否超越阈值 print ( "test res [%s] [%s] [%s]=%s" ) % ( avg_res , self . expression_obj . operator_type , self . expression_obj . threshold , self . judge ( avg_res ) , ) calc_res = self . judge ( avg_res ) if calc_res : return [ calc_res , avg_res , k ] #后面的循环不用走了,反正 已经成立了一个了 else : #监控这个服务 的所有项, 比如一台机器的多个网卡, 任意一个超过了阈值,都 算是有问题的 calc_res = self . judge ( avg_res ) if calc_res : return [ calc_res , avg_res , k ] print ( 'specified monitor key:' , self . expression_obj . specified_index_key ) print ( 'clean data dic:' , k , len ( clean_v_list ) , clean_v_list ) else : #能走到这一步,代表 上面的循环判段都未成立 return [ False , avg_res , k ] else : #可能是由于最近这个服务 没有数据 汇报 过来,取到的数据 为空,所以没办法 判断阈值 return [ False , None , None ]