gomyck-tools 1.2.3__py3-none-any.whl → 1.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ctools/metrics.py CHANGED
@@ -1,29 +1,34 @@
1
1
  import os
2
2
  import threading
3
+ import time
3
4
  from enum import Enum
4
5
 
5
- from prometheus_client import Counter, Gauge, Summary, Histogram
6
-
7
- from ctools import call, cjson, sys_log
8
- from ctools.application import Server
6
+ from prometheus_client import Counter, Gauge, Summary, Histogram, start_http_server
7
+ from ctools import call, cjson, sys_log, work_path
9
8
 
10
9
  log = sys_log.flog
11
10
 
12
- metrics = {}
11
+ _metrics_port = 8011
12
+ _persistent_json = {}
13
13
  _metrics_initial = {}
14
- persistent_json = {}
15
- temp_metrics_json = {}
16
- is_metrics_init: bool = True
17
14
  _lock = threading.Lock()
18
15
 
19
16
 
17
+ # 认证中间件
18
+ # @app.before_request
19
+ # def check_authentication():
20
+ # auth = request.authorization
21
+ # if not auth or auth.username != USERNAME or auth.password != PASSWORD:
22
+ # return Response(
23
+ # "Unauthorized", 401, {"WWW-Authenticate": 'Basic realm="Login Required"'}
24
+ # )
25
+
20
26
  class MetricType(Enum):
21
27
  COUNTER = 'counter'
22
28
  GAUGE = 'gauge'
23
29
  SUMMARY = 'summary'
24
30
  HISTOGRAM = 'histogram'
25
31
 
26
-
27
32
  class Metric:
28
33
  def __init__(self, metric_type: MetricType, metric_key: str, metric_labels: [],
29
34
  persistent: bool = False, buckets: [] = None, reset: bool = False, desc: str = ""):
@@ -47,60 +52,51 @@ class Metric:
47
52
  raise Exception('metric type not found')
48
53
  _metrics_initial[metric_key] = self
49
54
 
50
-
51
55
  @call.once
52
- def init():
53
- global is_metrics_init
54
- global temp_metrics_json
55
- persistent_path = os.path.join(Server.indicatorsPath, 'persistent.json')
56
- if os.path.exists(persistent_path):
56
+ def init(reset_persistent: bool = False):
57
+ persistent_path = os.path.join(work_path.get_current_path(), 'persistent.json')
58
+ if os.path.exists(persistent_path) and not reset_persistent:
57
59
  with open(persistent_path, 'r') as persistent_file:
58
- global persistent_json
60
+ global _persistent_json
59
61
  try:
60
62
  content = persistent_file.readline()
61
- # log.info("persistent初始化: %s" % content)
62
- persistent_json = cjson.loads(content)
63
+ log.info("persistent初始化: %s" % content)
64
+ _persistent_json = cjson.loads(content)
63
65
  except Exception:
64
66
  log.error('persistent.json is not valid json!!!!!')
65
- persistent_json = {}
67
+ _persistent_json = {}
66
68
  _init_all_metrics()
67
- for key, item in persistent_json.items():
69
+ for key, item in _persistent_json.items():
68
70
  metrics_key = key.split("-")[0]
69
71
  if '_labels' in key or metrics_key not in _metrics_initial: continue
70
- opt(metrics_key, persistent_json[key + '_labels'], persistent_json[key])
72
+ opt(metrics_key, _persistent_json[key + '_labels'], _persistent_json[key])
71
73
  persistent_metrics()
72
- is_metrics_init = False
74
+ start_http_server(port=_metrics_port)
73
75
 
74
-
75
- @call.schd(60, start_by_call=True)
76
+ @call.schd(5, start_by_call=True)
76
77
  def persistent_metrics():
77
- if persistent_json and not _lock.locked():
78
- with open(os.path.join(Server.indicatorsPath, 'persistent.json'), 'w') as persistent_file:
79
- persistent_file.write(cjson.dumps(persistent_json))
78
+ if _persistent_json and not _lock.locked():
79
+ log.info('begin persistent metrics to file...')
80
+ with open(os.path.join(work_path.get_current_path(), 'persistent.json'), 'w') as persistent_file:
81
+ persistent_file.write(cjson.dumps(_persistent_json))
80
82
  persistent_file.flush()
81
83
 
82
-
83
84
  def opt(metric_key: str, label_values: [], metric_value: int):
84
85
  _lock.acquire(timeout=5)
85
86
  try:
86
87
  persistent_key = "%s-%s" % (metric_key, "_".join(map(str, label_values)))
87
- metric_entity: Metric = metrics.get(persistent_key)
88
- if not metric_entity:
89
- metric_entity = metrics[persistent_key] = _metrics_initial[metric_key]
90
-
88
+ metric_entity: Metric = _metrics_initial[metric_key]
91
89
  if metric_entity.persistent:
92
- if not is_metrics_init and not metric_entity.reset and persistent_key in persistent_json:
93
- persistent_json[persistent_key] += metric_value
90
+ if not metric_entity.reset and persistent_key in _persistent_json:
91
+ _persistent_json[persistent_key] += metric_value
94
92
  else:
95
- persistent_json[persistent_key] = metric_value
96
- persistent_json[persistent_key + '_labels'] = label_values
93
+ _persistent_json[persistent_key] = metric_value
94
+ _persistent_json[persistent_key + '_labels'] = label_values
97
95
 
98
- if persistent_json[persistent_key] < 0:
99
- persistent_json[persistent_key] = 0
96
+ if _persistent_json[persistent_key] < 0:
97
+ _persistent_json[persistent_key] = 0
100
98
  metric_value = 0
101
99
 
102
- temp_metrics_json[persistent_key] = metric_value
103
-
104
100
  if metric_entity.metric_type == MetricType.COUNTER or metric_entity.metric_type == MetricType.GAUGE:
105
101
  if label_values is None or len(label_values) == 0:
106
102
  if metric_entity.metric_type == MetricType.COUNTER and metric_entity.reset:
@@ -123,4 +119,13 @@ def opt(metric_key: str, label_values: [], metric_value: int):
123
119
  _lock.release()
124
120
 
125
121
  def _init_all_metrics():
126
- Metric(MetricType.COUNTER, 'demo123123', ['asdasd', 'sdfsdf'], persistent=True)
122
+ Metric(MetricType.GAUGE, 'gomyck', ['g_label1', 'g_label2'], persistent=True, reset=True)
123
+
124
+ # if __name__ == '__main__':
125
+ # init()
126
+ # import random
127
+ # while True:
128
+ # opt('data_reported_time', ['123', '123'], random.randint(1, 10))
129
+ # opt('data_received_time', ['123', '123'], random.randint(1, 10))
130
+ # opt('data_insert_time', ['123', '123'], random.randint(1, 10))
131
+ # time.sleep(1)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gomyck-tools
3
- Version: 1.2.3
3
+ Version: 1.2.5
4
4
  Summary: A ctools for python development by hao474798383
5
5
  Home-page: https://blog.gomyck.com
6
6
  Author: gomyck
@@ -27,5 +27,6 @@ Requires-Dist: paho-mqtt ~=2.1.0
27
27
  Requires-Dist: fuzzywuzzy ~=0.18.0
28
28
  Requires-Dist: pymysql ~=1.1.1
29
29
  Requires-Dist: pyzipper ==0.3.6
30
+ Requires-Dist: prometheus-client ==0.21.1
30
31
 
31
32
  this package is for python development
@@ -26,7 +26,7 @@ ctools/html_soup.py,sha256=rnr8M3gn3gQGo-wNaNFXDjdzp8AAkv9o4yqfIIfO-zw,1567
26
26
  ctools/http_utils.py,sha256=dG26aci1_YxAyKwYqMKFw4wZAryLkDyvnQ3hURjB6Lk,768
27
27
  ctools/images_tools.py,sha256=TapXYCPqC7GesgrALecxxa_ApuT_dxUG5fqQIJF2bNY,670
28
28
  ctools/imgDialog.py,sha256=zFeyPmpnEn9Ih7-yuJJrePqW8Myj3jC9UYMTDk-umTs,1385
29
- ctools/metrics.py,sha256=vq9Fnq2fhmhS4yoHS4gO7ArKS033Eou8vpA779Uue0I,4414
29
+ ctools/metrics.py,sha256=5wC3luUmZKhDSN-0XMJzlfhk792u_tw0IBR6g3YHbHI,4893
30
30
  ctools/mqtt_utils.py,sha256=ZWSZiiNJLLlkHF95S6LmRmkYt-iIL4K73VdN3b1HaHw,10702
31
31
  ctools/obj.py,sha256=GYS1B8NyjtUIh0HlK9r8avC2eGbK2SJac4C1CGnfGhI,479
32
32
  ctools/pacth.py,sha256=MJ9Du-J9Gv62y4cZKls1jKbl5a5kL2y9bD-gzYUCveQ,2604
@@ -51,7 +51,7 @@ ctools/wordFill.py,sha256=dB1OLt6GLmWdkDV8H20VWbJmY4ggNNI8iHD1ocae2iM,875
51
51
  ctools/word_fill.py,sha256=xeo-P4DOjQUqd-o9XL3g66wQrE2diUPGwFywm8TdVyw,18210
52
52
  ctools/word_fill_entity.py,sha256=eX3G0Gy16hfGpavQSEkCIoKDdTnNgRRJrFvKliETZK8,985
53
53
  ctools/work_path.py,sha256=i4MTUobqNW2WMrT3mwEC_XYQ0_IhFmKoNpTX2W6A8Tc,1680
54
- gomyck_tools-1.2.3.dist-info/METADATA,sha256=Zc516X4DTe8baLkQjf8ivEsGohdI9UgSeWH709wl3y4,1004
55
- gomyck_tools-1.2.3.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
56
- gomyck_tools-1.2.3.dist-info/top_level.txt,sha256=-MiIH9FYRVKp1i5_SVRkaI-71WmF1sZSRrNWFU9ls3s,7
57
- gomyck_tools-1.2.3.dist-info/RECORD,,
54
+ gomyck_tools-1.2.5.dist-info/METADATA,sha256=EkR8czXmSe8KiXedT-SxkqIKQAXelkBo4YOjoJuFkIg,1046
55
+ gomyck_tools-1.2.5.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
56
+ gomyck_tools-1.2.5.dist-info/top_level.txt,sha256=-MiIH9FYRVKp1i5_SVRkaI-71WmF1sZSRrNWFU9ls3s,7
57
+ gomyck_tools-1.2.5.dist-info/RECORD,,