def process_archive_minute(): # defini date de debut et de fin # begin = (date du dernier record de la base Archive) + 1 minute # end = (maintenant) - 1 minute # if TeleinfoMinute is empty then we start with the first Teleinfo record (oldest) if TeleinfoMinute.last(TeleinfoMinute) == None: #TODO : should be replaced by Teleinfo.first() #begin = db.session.query(Teleinfo).filter().order_by(Teleinfo.id.asc()).first().timestamp.replace(second=0, microsecond=0) begin = Teleinfo.first(Teleinfo).timestamp.replace(second=0, microsecond=0) # else TeleinfoMinute is NOT empty then we start with the last TeleinfoMinute record (newest) else: begin = (TeleinfoMinute.last(TeleinfoMinute).timestamp + timedelta(minutes=1)).replace(second=0, microsecond=0) # we finish with the last Teleinfo record (replace second to zero to avoid incomplete current minute) end = (Teleinfo.last(Teleinfo).timestamp).replace(second=0, microsecond=0) logger.info('Minute to archive ' + str(begin) + ' -> ' + str(end)) if ((begin + timedelta(minutes=1)) > end): logger.info('\tWaiting more records') # while some old Logs to Archive while ((begin + timedelta(minutes=1)) <= end): record_minute(begin) begin = begin + timedelta(minutes=1)
def record_minute(begin): logger.debug('\t\trecord minute') #fake begin/end # begin = datetime.now() - timedelta(minutes=3) end = begin + timedelta(minutes=1) logger.info('Archiving minute ' + str(begin) + ' -> ' + str(end)) #print('\t\tminute ' + str(begin) + ' -> ' + str(end)) base = 0 papp = 0 iinst1 = 0.0 iinst2 = 0.0 iinst3 = 0.0 # get logs logs = Teleinfo.get_between_date(Teleinfo, begin, end) nb_logs = len(logs) logger.debug('\t\t\tnb_logs=' + str(nb_logs)) if nb_logs>0: for log in logs: if log.base > base: base = log.base # valeur la plus elevee de la serie papp += log.papp iinst1 += float(log.iinst1) iinst2 += float(log.iinst2) iinst3 += float(log.iinst3) timestamp = begin # calcule des moyennes sur les serie papp = papp / nb_logs iinst1 = iinst1 / nb_logs iinst2 = iinst2 / nb_logs iinst3 = iinst3 / nb_logs # Save to db ret = TeleinfoMinute.create(TeleinfoMinute, begin, base, papp, iinst1, iinst2, iinst3) logger.debug('\t\t\tret = ' + str(ret))
def createTeleinfo(timestamp, base, papp, iinst1, iinst2, iinst3): ret = 'NOK' try: entry = Teleinfo(timestamp, base, papp, iinst1, iinst2, iinst3) db.session.add(entry) db.session.commit() print entry except RuntimeError, e: print e.message
def fakedataall(): entries = Teleinfo.alltolist(Teleinfo) last = Teleinfo.query.order_by(Teleinfo.id.desc()).limit(1) ts = datetime.now() - timedelta(minutes=len(entries)) for entry in entries: ts = ts + timedelta(minutes=1) entry[0] = time.mktime(ts.timetuple()) * 5000 return jsonify(entries), 200
def createti(timestamp, base, papp, iinst1, iinst2, iinst3): ret = 'NOK' teleinfo = Teleinfo() teleinfo.timestamp = timestamp teleinfo.base = base teleinfo.papp = papp teleinfo.iinst1 = iinst1 teleinfo.iinst2 = iinst2 teleinfo.iinst3 = iinst3 try: db.session.add(teleinfo) db.session.commit() ret = True except exc.SQLAlchemyError as e: ret = False return ret #print createti(datetime.now(), 3, 2, 2, 2, 3)
def create(self, timestamp, base, papp, iinst1, iinst2, iinst3): objet = Teleinfo() return (TICommon().create(Teleinfo, objet, timestamp, base, papp, iinst1, iinst2, iinst3))
def create_data(base): """Creates a data Entry.""" me = Teleinfo(datetime.now(), base=base) db.session.add(me) db.session.commit() print me
"error_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "ERROR", "formatter": "simple", "filename": os.path.join(logfile_base, 'tiarchive_error.log'), "maxBytes": 100000, "backupCount": 3, "encoding": "utf8" } }, "loggers": { "my_module": { "level": "ERROR", "handlers": ["console"], "propagate": "no" } }, "root": { "level": "INFO", "handlers": ["console", "info_file_handler", "error_file_handler"] } }) logger.info('START (last Teleinfo recrod = ' + str(Teleinfo.last(Teleinfo).timestamp)) if args.archive_minute: process_archive_minute() if args.archive_hour: process_archive_minute() process_archive_hour()