def save(self,epoch,value): """Save new data received to database. :param: epoch timestamp of data to be saved :param: value float value to be saved timestamp older than 24 hours or in future date will be discarded. """ m = self.db.fetch_from_epoch('minutes',trunc_to_min(epoch)) if m is None and ( self._last_hour(epoch) or self._last_day(epoch)) : self.db.insert('minutes',data=(trunc_to_min(epoch), value))
def _insert_minutes(self,table,data): arg_epoch, arg_value = data arg_epoch = trunc_to_min(arg_epoch) sql = """INSERT INTO {}(epoch,value) VALUES (?,?)""".format(table) self.conn.execute(sql, (arg_epoch,arg_value)) self.conn.commit() return
def test_save_hour(self): epoch = trunc_to_min(time.time()) expired_data = [ (epoch + HOUR -x*MIN,x-2) for x in xrange(0,10)] for e in expired_data: self.rrd.save(e[0],e[1]) retrive_data = self.rrd.db.fetch_all('hours',wnd_1(epoch)) self.assertEqual([], retrive_data) valid_data = [(epoch -x*MIN,float(x+1)) for x in xrange(0,10)] valid_data.reverse() for e in valid_data: self.rrd.save(e[0],e[1]) min_el = valid_data.pop() el = (trunc_to_hour(min_el[0]),min_el[1]) self.assertEqual([el], self.rrd.db.fetch_all('hours',wnd_24(epoch)))
def test_save_min(self): epoch = trunc_to_min(time.time()) expired_data = [ (epoch + HOUR -x*MIN,x-2) for x in xrange(0,10)] for e in expired_data: self.rrd.save(e[0],e[1]) retrive_data = self.rrd.db.fetch_all('minutes',wnd_1(epoch)) self.assertEqual([], retrive_data) valid_data = [ (epoch -x*MIN,x+1) for x in xrange(0,10)] valid_data.reverse() for e in valid_data: self.rrd.save(e[0],e[1]) self.assertEqual(valid_data, self.rrd.db.fetch_all('minutes',wnd_1(epoch)))
def test_insert(self): epoch = trunc_to_min(time.time()) value = 10.0 self.rrd.db.insert('minutes',(epoch,value)) self.assertEqual(epoch,self.rrd.db.fetch_from_epoch('minutes', epoch)[1])
def _last_day(self,epoch): epoch = trunc_to_min(epoch) return int(self.end_d) <= int(epoch) <= int(self.start_d)
def _last_hour(self,epoch): epoch = trunc_to_min(epoch) return int(self.end_h) <= int(epoch) <= int(self.start_h)