-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_mod.py
71 lines (58 loc) · 2.61 KB
/
data_mod.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
from datetime import datetime, timedelta
# This is the largest nuber that Timestamp can be and thus is the basis for the reverse counting
# for the everincreasing key number for the DB
BIG_NUMBER = 9999999999
class Abecldata:
def __init__(self, machine, wallet=False):
self.old_data = {}
self.new_data = {}
self.changed = False
self.machine = machine
self.wallet = wallet
self.key = 0
def update(self, data):
# Deal with that weird Go datetime format later. For now, just use the current time.
current_time = datetime.utcnow()
# The reverse timestamp as the key will help to display entries in consequitive order in Deta
data["key"] = str(BIG_NUMBER - int(datetime.timestamp(current_time)))
self.key = str(BIG_NUMBER - int(datetime.timestamp(current_time)))
self.timestamp = int(datetime.timestamp(current_time))
# We assidn the data to the new_data to be able to work with it later
self.new_data = data
# Checking if the balance has changed, in which case the self.changes switch goes ON
# In other case we are switching the switch OFF
old_balance = self.old_data.get("total_balance", None)
new_balance = self.new_data.get("total_balance", None)
if old_balance != new_balance:
self.old_data = self.new_data
self.changed = True
else:
self.changed = False
# And we finally make our new data old
self.old_data = self.new_data
def machine_total(self):
return {"key": self.machine, "total_balance": self.new_data["total_balance"]}
class Cycle:
def __init__(self): # sourcery skip: aware-datetime-for-utc
self.total: int = 0
self.successes: int = 0
self.new_ping: int = 0
self.app_start_time: datetime = datetime.utcnow()
self.last_sucess_time: datetime = self.app_start_time
self.time_since_app_start: timedelta
self.time_since_last_sucess: timedelta
def update(self): # sourcery skip: aware-datetime-for-utc
self.time_now = datetime.utcnow()
# Cycle.common()
self.total += 1
self.time_since_app_start = self.time_now - self.app_start_time
self.time_since_last_sucess = self.time_now - self.last_sucess_time
def success(self): # sourcery skip: aware-datetime-for-utc
self.time_now = datetime.utcnow()
# Cycle.common()
self.successes += 1
self.last_sucess_time = self.time_now
self.ping = self.new_ping
self.new_ping = 0
def pinging(self):
self.new_ping += 1