HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
All Classes Namespaces Files Functions Variables Pages
BaseMetric.py
Go to the documentation of this file.
1 # coding: utf-8 # pylint: disable-all
2 
3 """@package algorithms
4  @file BaseMetric.py
5  @author scorp <developers.hce@gmail.com>
6  @link http://hierarchical-cluster-engine.com/
7  @copyright Copyright &copy; 2013 IOIX Ukraine
8  @license http://hierarchical-cluster-engine.com/license/
9  @package HCE project node API
10  @since 0.1
11  """
12 
13 import logging
14 
15 import app.Consts as APP_CONSTS
16 import app.Utils as Utils # pylint: disable=F0401
17 
18 # Logger initialization
19 logger = Utils.MPLogger().getLogger()
20 
21 
22 # #The BaseMetric class, class that implements common methods for all Metrics classes
23 #
24 class BaseMetric(object):
25 
26 
27  # # class constructor
28  #
29  # @param name - metric's name
30  def __init__(self, names):
31  self.names = names
32 
33 
34  # # retForMultiNames return simple value for multi-returns
35  #
36  # @param retDict - incoming return dict with 2 mandatory keys "count" and "persent"
37  # @param metricName - neededs metric name
38  # @return - return simple return element
39  def retForMultiNames(self, retDict, metricName):
40  ret = 0
41  if len(self.names) > 0 and metricName == self.names[0]:
42  ret = retDict["count"]
43  elif len(self.names) > 1 and metricName == self.names[1]:
44  ret = retDict["percent"]
45  else:
46  ret = 0
47  return ret
48 
49 
50  # # sortElementsByMetric resort incoming elements list, by precalculated metric data
51  #
52  # @param elements - incoming elements
53  # @param metricParam - name of using metric param
54  # @return - return resorted elements
55  def sortElementsByMetric(self, elements, metricName):
56  localElemsWithMetric = []
57  localElemsWithoutMetric = []
58  for element in elements:
59  if hasattr(element, "metrics") and metricName in element.metrics and metricName in self.names:
60  localElemsWithMetric.append(element)
61  else:
62  localElemsWithoutMetric.append(element)
63  if len(localElemsWithMetric) > 0:
64  localElemsWithMetric.sort(key=lambda x: x.metrics[metricName], reverse=True)
65  ret = localElemsWithMetric + localElemsWithoutMetric
66  return ret
67 
68 
69  # # selectElementsByMetric selects elements that fit for metric limit value
70  #
71  # @param elements - incoming elements
72  # @param metricParam - name of using metric param
73  # @param metricParamLimit - limit value of using metric param
74  # @return - return reselected elements
75  def selectElementsByMetric(self, elements, metricName, metricLimitMax, metricLimitMin):
76  ret = []
77  for element in elements:
78  if hasattr(element, "metrics") and metricName in element.metrics and metricName in self.names:
79  if element.metrics[metricName] >= metricLimitMin:
80  if metricLimitMax is None or element.metrics[metricName] <= metricLimitMax:
81  ret.append(element)
82  return ret
def retForMultiNames(self, retDict, metricName)
Definition: BaseMetric.py:39
def selectElementsByMetric(self, elements, metricName, metricLimitMax, metricLimitMin)
Definition: BaseMetric.py:75
def sortElementsByMetric(self, elements, metricName)
Definition: BaseMetric.py:55