Skip to content

Commit 4396749

Browse files
committed
Cleanup
1 parent 2bd34a0 commit 4396749

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

aquaPi/machineroom/hist_nodes.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,46 @@ def __init__(self, duration):
6969
def add_field(self, name):
7070
super().add_field(name)
7171
if name not in TimeDbMemory._store:
72-
log.debug('TimeDbMemory: new history for %s', name)
7372
TimeDbMemory._store[name] = deque(maxlen=self.duration * 60 * 60) # 1/sec
7473

7574
def feed(self, name, value):
7675
with TimeDbMemory._store_lock:
76+
if name not in TimeDbMemory._store:
77+
log.error('TimeDbMemory: unknown history for %s, adding implicitly', name)
78+
TimeDbMemory._store[name] = deque(maxlen=self.duration * 60 * 60) # 1/sec
79+
7780
now = int(time())
7881
series = TimeDbMemory._store[name]
79-
if not series or (series[-1][0] != now):
82+
if (len(series) == 0 or series[-1][0] != now):
8083
series.append((now, value))
8184
else:
85+
# multiple values for same second, build average
8286
series[-1] = (now, (series[-1][1] + value) / 2)
8387

8488
# purge expired data
8589
while series[0][0] < now - self.duration * 60 * 60:
8690
series.popleft()
91+
8792
log.debug('TimeDbMemory: append %s: %r @ %d, %d ent., %d Byte'
8893
, name, value, now
8994
, len(TimeDbMemory._store[name])
9095
, sys.getsizeof(TimeDbMemory._store[name]))
9196

92-
#TODO: once transistion is finished, TimeDbMemory._store can change to new structure
9397
#TODO: add downsampling of returned data if step>1
9498
#TODO: add permanent downsampling after some period, e.g. 1h, to reduce mem consumption
9599
def query(self, node_names, start=0, step=0):
96100
with TimeDbMemory._store_lock:
97-
# breakpoint()
98-
#store_cpy = TimeDbMemory._store.copy() # freeze the source, could lock it instead
99101
result = {}
100-
## step=60 #FIXME ATM start==0 implies old format for Quest, bur nit for Mem
101102

103+
qry_begin = time()
102104
if not start and not step:
105+
log.warning('TimeDbMemory OLD API used for %r', name_names)
103106
# just for reference, was never used with this API!
104107
# previous struct:
105108
# {ser1: [(ts1, val1.1), (ts2, val1.2), ...],
106109
# ser2: [(ts1, val2.1), (ts2, val2.2),....],
107110
# ... }
108111
for name in node_names:
109-
#result[name] = [(v[0], v[1]) for v in store_cpy[name]]
110112
result[name] = [(v[0], v[1]) for v in TimeDbMemory._store[name]]
111113
else:
112114
# new structure, about 0.7 * space:
@@ -120,7 +122,6 @@ def query(self, node_names, start=0, step=0):
120122
result[start] = [None] * len(node_names)
121123
idx = 0
122124
for name in node_names:
123-
#for measurement in store_cpy[name]:
124125
for measurement in TimeDbMemory._store[name]:
125126
(ts, val) = measurement
126127
if ts <= start:
@@ -134,7 +135,8 @@ def query(self, node_names, start=0, step=0):
134135
result[ts][idx] = val
135136
idx += 1
136137

137-
#log.debug('%r', result)
138+
log.debug(' done, overall %fs, %d data points', time() - qry_begin, len(result))
139+
log.debug('TimeDbMemory.query start %r step %r: %r', start, step, result)
138140
return result
139141

140142
if QUEST_DB:
@@ -172,14 +174,13 @@ def __init__(self):
172174
timestamp(ts) PARTITION BY HOUR;
173175
""")
174176
except pg.OperationalError as ex:
175-
log.warning('TimeQuestDB - %s', str(ex))
177+
log.exception('TimeDbQuest')
176178
raise ModuleNotFoundError() from ex
177179

178180
@staticmethod
179181
def _get_local_tz():
180182
# time is a bad concept, troublesome everywhere!
181-
#FIXME: this sets QuestDB to local timezone. Sensible for aquaPi host and the DB, which means debugging and logs. Conversion to and from user's TZ must be done in frontend!
182-
183+
#FIXME: this sets QuestDB to host's local timezone. Ok for debugging and logs. Conversion to and from user's TZ must be done in frontend!
183184
# To make things interesting, there's no simple way to get the 'Olson TZ name' (e.g. 'Europe/Belin'), most systems prefer the 3-4 letter names, e.g. CEST. Reading link /etc/localtime has several chances to break, but seems to work on Raspi (and Manjaro).
184185
tzfile = os.readlink('/etc/localtime')
185186
match = regex.search('/zoneinfo/(.*)$', tzfile)
@@ -201,7 +202,7 @@ def add_field(self, name):
201202
qry = sql.SQL("INSERT INTO node VALUES (%s, true)")
202203
conn.execute(qry, [name])
203204
except pg.OperationalError as ex:
204-
log.warning('TimQuestDB.add_field - %s', str(ex))
205+
log.exception('TimeDbQuest.add_field')
205206

206207
def feed(self, name, value):
207208
try:
@@ -210,7 +211,7 @@ def feed(self, name, value):
210211
qry = sql.SQL("INSERT INTO value VALUES (now(), %s, %s)")
211212
conn.execute(qry, [name, value])
212213
except pg.OperationalError as ex:
213-
log.warning('TimeQuestDB.feed - %s', str(ex))
214+
log.exception('TimeDbQuest.feed')
214215

215216
def _query(self, node_names, start, step):
216217
try:
@@ -239,9 +240,10 @@ def _query(self, node_names, start, step):
239240
SELECT ts, node_id id, avg(value) value
240241
FROM value -- JOIN node ON (node_id)
241242
WHERE ts >= to_utc({start} * 1000000L, {timezone})
243+
AND node_id IN ({nodes})
242244
SAMPLE BY 1s FILL (PREV)
243245
)
244-
WHERE id IN ({nodes})
246+
--WHERE id IN ({nodes})
245247
SAMPLE BY {step}s FILL (PREV) ALIGN TO CALENDAR
246248
GROUP BY ts,id ORDER BY span,id;
247249
""").format(
@@ -250,29 +252,26 @@ def _query(self, node_names, start, step):
250252
step=sql.Literal(step),
251253
nodes=sql.SQL(',').join(node_names)
252254
)
253-
log.debug(qry.as_string(conn))
255+
#log.debug(qry.as_string(conn))
254256
curs.execute(qry)
255257
recs = curs.fetchall()
256258

257259
return recs
258260
except pg.OperationalError as ex:
259-
log.warning('TimeQuestDB.query - %s', str(ex))
261+
log.exception('TimeDbQuest.query')
260262
return {}
261263

262264
def query(self, node_names, start=0, step=0):
263265
result = {}
264-
## step=60 #FIXME
265266

266267
qry_begin = time()
267268
log.debug('TimeDbQuest query: %s / %d / %d', node_names, start, step)
268269
recs = self._query(node_names, start, step)
269270
log.debug(' qry time %fs', time() - qry_begin)
270271

271272
if recs:
272-
#for row in recs:
273-
# log.debug(row)
274-
275273
if start <= 0:
274+
log.warning('TimeDbQuest OLD API used for %r', name_names)
276275
# old structure (start=0), each series is an array of data point tupels:
277276
# { "ser1": [(ts1, val1.1), (ts2: val1.2), ... ],
278277
# "ser2": [(ts1, val2.1), (ts3, val2.3), ... ],
@@ -320,8 +319,8 @@ def query(self, node_names, start=0, step=0):
320319
prev[n_idx] = result[ts][n_idx]
321320
result = {ts: result[ts] for ts in result if result[ts] != [None] * len(node_names)}
322321

323-
log.debug(' done, overall %fs', time() - qry_begin)
324-
#log.debug('%r', result)
322+
log.debug(' done, overall %fs, %d data points', time() - qry_begin, len(result))
323+
#log.debug('TimeDbQuest.query start %r step %r: %r', start, step, result)
325324
return result
326325
# end: if QUEST_DB
327326

@@ -372,7 +371,7 @@ def listen(self, msg):
372371
self.db.feed(msg.sender, msg.data)
373372
if time() >= self._nextrefresh:
374373
self.post(MsgData(self.id, 0))
375-
self._nextrefresh = int(time()) + 60
374+
self._nextrefresh = int(time()) + 10
376375

377376
def get_history(self, start, step):
378377
return self.db.query(self._inputs.sender, start, step)

init

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ echo "====="
5353

5454
if [ $(getconf LONG_BIT) == 64 ]
5555
then
56-
if [ -z "$(ls -d ${MyHome}/questdb-${QuestRel}/ 2>/dev/null)" ]
56+
if [ -z "$(ls -d "${MyHome}/questdb-${QuestRel}/" 2>/dev/null)" ]
5757
then
5858
if ( grep -i arm /proc/cpuinfo >/dev/null )
5959
then
@@ -69,7 +69,8 @@ then
6969
fi
7070
pip3 install 'psycopg[binary]>=3.1.9'
7171
echo "===== ... and start QuestDB"
72-
${MyHome}/questdb-${QuestRel}/bin/questdb.sh start
72+
"${MyHome}/questdb-${QuestRel}/bin/questdb.sh" start
73+
echo "You can interact with Quest DB in your browser at localhost:9000"
7374
echo "====="
7475
fi
7576

0 commit comments

Comments
 (0)