def find_last_phase(): """ Return last processed ChaudiereMinute entry.dt or None """ # if no ChaudiereMinute entry exists, return None if ChaudiereMinute.last(ChaudiereMinute) == None: return None # if some ChaudiereMinute entry exists but phase is None # (first call of this script since db creation) : # return first ChaudiereMinute dt try_first = ChaudiereMinute.first(ChaudiereMinute) if try_first is not None and try_first.phase is None: logger.debug('returning first entry') return try_first.dt # else search for lat processed entry dt = ChaudiereMinute.last(ChaudiereMinute).dt phase = None while (phase is None): dt = dt - timedelta(minutes=1) entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, dt) if entry is not None: phase = ChaudiereMinute.get_by_datetime(ChaudiereMinute, dt).phase logger.debug('last phase found is at : ' + str(dt)) return dt
def find_date_end(date): """ return the datetime of an existing Chaudiere entry close to the given *date* parameter """ last_ch_minute_date = ChaudiereMinute.last(ChaudiereMinute).dt entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, date) while ((entry is None) and (date < last_ch_minute_date)): date = date + timedelta(minutes=1) entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, date) logger.debug('found ChaudiereMinute at (dt end):' + str(entry.dt)) return entry.dt
def process_phase(mode='normal', hours=None, date=None): """ Détermine la date de début (begin) et de fin (end) des minutes à traiter en fonction du `mode` """ TEMP_CHAUDIERE_FAILURE = admin_config.temp_chaudiere_failure logger.info("TEMP_CHAUDIERE_FAILURE " + str(TEMP_CHAUDIERE_FAILURE)) rework_mode_disable_alert = False if mode is 'normal': begin = find_last_phase() # begin = last processed ChaudiereMinute entry.dt if begin is None: # Si begin = None alors il n'y a aucun reord dans la table ChaudiereMinute. Aucun traitement ne peut être effectué return end = ChaudiereMinute.last(ChaudiereMinute).dt # end = last existing ChaudiereMinute entry.dt elif mode is 'rework_from_now': rework_mode_disable_alert = True end = ChaudiereMinute.last(ChaudiereMinute).dt begin = end - timedelta(hours=hours) elif mode is 'rework_from_date': rework_mode_disable_alert = True end = find_date_end(date) begin = end - timedelta(hours=hours) else: logger.error('wrong arguments') return if begin is None: logger.info('No records') return if ((begin + timedelta(minutes=1)) >= end): logger.info('Strating from '+str(begin)+' ...Waiting more records') else: logger.info('processing phase From ' + str(begin) + ' To ' + str(end)) # Progress bar Init (for console mode) bar_items = timedelta_in_minute(begin, end) bar_item = 0 # while some entries to process while ((begin + timedelta(minutes=1)) <= end): entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, begin) # entry should not be missing, test just in case and create missing entry if entry is None: logger.warning('create missing ChaudiereMinute entry (should not be the case') ChaudiereMinute.create(ChaudiereMinute, begin, None, None, None, None, None, None, None, None, None, None) entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, begin) # Progress bar Print (for console mode) bar_item += 1 # print_bar(bar_item, bar_items, prefix=str(entry.dt)) update_phase(entry) update_change(entry) process_alerts(entry, rework_mode_disable_alert) begin = begin + timedelta(minutes=1)
def temperature_variation(entry, periode): """ retourne la variation de température (+/- Float) sur la période (minutes) retourne None si pas d'information si la période est incomplete (info capteurs absente), calcule avec une valeur plus récente """ try: first_dt = entry.dt - timedelta(minutes=periode) dt = first_dt last_dt = entry.dt minute = 0 old_entry = None # retourne la plus ancienne entry existante dans la période while old_entry is None and dt < last_dt: dt = first_dt + timedelta(minutes=minute) old_entry = ChaudiereMinute.get_by_datetime(ChaudiereMinute, dt) minute += 1 old_temp = old_entry.get(TEMP_CHAUDIERE) temp = entry.get(TEMP_CHAUDIERE) return temp - old_temp except Exception as e: logger.warning("temperature variation failed ({0})".format(e)) return None