-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (70 loc) · 2.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from flask import Flask, Response, request
import prometheus_client
import flask
from prometheus_client import Counter, generate_latest, Gauge
from os import environ
from threading import Thread
from time import sleep
import requests
app = Flask(__name__)
graphs = {}
# graphs["login"] = Gauge("lumenswap_login", "click count on login button")
COINCAPURL = environ['COINCAPURL']
INTERVAL= environ['INTERVAL']
CRYPTOS= environ['CRYPTOS']
APP_PORT=environ['APP_PORT']
CRYPTO_LIST = CRYPTOS.split(",")
graphs["cc_exporter_priceUsd"] = Gauge("cc_exporter_priceUsd",'priceUsd',["name"])
graphs["cc_exporter_rank"] = Gauge("cc_exporter_rank", 'rank',["name"])
graphs["cc_exporter_supply"] = Gauge("cc_exporter_supply", 'supply',["name"])
graphs["cc_exporter_maxSupply"] = Gauge("cc_exporter_maxSupply", 'maxSupply',["name"])
graphs["cc_exporter_marketCapUsd"] = Gauge("cc_exporter_marketCapUsd", 'marketCapUsd',["name"])
graphs["cc_exporter_volumeUsd24Hr"] = Gauge("cc_exporter_volumeUsd24Hr", 'volumeUsd24Hr',["name"])
graphs["cc_exporter_changePercent24Hr"] = Gauge("cc_exporter_changePercent24Hr", 'changePercent24Hr',["name"])
graphs["cc_exporter_vwap24Hr"] = Gauge("cc_exporter_vwap24Hr", 'vwap24Hr',["name"])
@app.route("/",methods = ['GET'])
def main():
return "OK"
@app.route("/metrics")
def requests_count():
res = []
for k,v in graphs.items():
res.append(prometheus_client.generate_latest(v))
return Response(res, mimetype="text/plain")
def flaskThread():
app.run(port=int(APP_PORT), host='0.0.0.0')
Thread(target = flaskThread).start()
def getLatestData():
return requests.get(COINCAPURL).json()
def roundDec(floatNum):
return round(floatNum,7)
def makeMetrics(singleCryptoRes):
c_name = singleCryptoRes["id"]
# print(singleCryptoRes)
graphs["cc_exporter_priceUsd"].labels(c_name).set(singleCryptoRes["priceUsd"])
graphs["cc_exporter_rank"].labels(c_name).set(singleCryptoRes["rank"])
graphs["cc_exporter_supply"].labels(c_name).set(singleCryptoRes["supply"])
try:
graphs["cc_exporter_maxSupply"].labels(c_name).set(singleCryptoRes["maxSupply"])
except:
pass
# graphs[c_name+"_maxSupply"].labels(c_name).set(0)
graphs["cc_exporter_marketCapUsd"].labels(c_name).set(singleCryptoRes["marketCapUsd"])
graphs["cc_exporter_volumeUsd24Hr"].labels(c_name).set(singleCryptoRes["volumeUsd24Hr"])
graphs["cc_exporter_changePercent24Hr"].labels(c_name).set(singleCryptoRes["changePercent24Hr"])
try:
graphs["cc_exporter_vwap24Hr"].labels(c_name).set(singleCryptoRes["vwap24Hr"])
except:
pass
# graphs[c_name+"_vwap24Hr"].labels(c_name).set(0)
def StartFetchingData():
while True:
result = getLatestData()
# print(result["data"][1])
for userCrypto in CRYPTO_LIST:
for resutlCrypto in result["data"]:
if userCrypto==resutlCrypto["id"]:
makeMetrics(resutlCrypto)
print("Interval time")
sleep(int(INTERVAL))
StartFetchingData()