Example #1
0
def record_minute(begin):
    """
    Recupere dans la base Logs l'ensemble des objets dont la date est comprise entre 
    les dates begin et (begin + 1 minute)
    Calcule des moyennes et enregistre dans une entry ChaudiereMinute
    """
    logger.debug('Minute '+ str(begin))    
    end = begin + timedelta(minutes=1)
    temp0 = 0.0
    temp1 = 0.0
    watt1 = 0
    watt2 = 0
    watt3 = 0
    # get logs
    logs = Chaudiere.get_between_date(Chaudiere, begin, end)
    nb_logs = len(logs)
    logger.info('minute ' + str(begin)+' logs :'+str(nb_logs))
    if nb_logs > 0:
        for log in logs:
            if (log.temp0 is not None): temp0 += float(log.temp0)
            if (log.temp1 is not None): temp1 += float(log.temp1)
            if (log.watt1 is not None): watt1 += int(log.watt1)
            if (log.watt2 is not None): watt2 += int(log.watt2)
            if (log.watt3 is not None): watt3 += int(log.watt3)
        # calcule des moyennes sur les series
        temp0 = temp0 / nb_logs
        temp1 = temp1 / nb_logs
        watt1 = watt1 / nb_logs
        watt2 = watt2 / nb_logs
        watt3 = watt3 / nb_logs
        # Save to db
        ChaudiereMinute.create(ChaudiereMinute, begin, temp0, temp1, None, None, watt1, watt2, watt3, None, None, None)
    # else if no log to process, we still create an entry with None fields
    else:
        ChaudiereMinute.create(ChaudiereMinute, begin, None, None, None, None, None, None, None, None, None, None)
Example #2
0
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
Example #3
0
def insert_test_data_every_minute():
    dt = datetime.now().replace(second=0, microsecond=0)
    temp = dt.minute
    watt = 1
    # Save to db
    ChaudiereMinute.create(ChaudiereMinute, dt, temp, temp, temp, watt, watt,
                           watt, watt, None, None, None)
Example #4
0
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
Example #5
0
def process_archive_minute(mode='normal', hours=None, date=None): 
    """ Defini date de debut et de fin 
    begin = (date du dernier record de la base Archive) + 1 minute
    end = (maintenant) - 1 minute
    Appelle la fonction record_minute() autant de fois que nécessaire
    """
    if mode is 'normal':
        # if ChaudiereMinute is empty then we start with the first Chaudiere record (oldest)
        if ChaudiereMinute.last(ChaudiereMinute) == None:
            logger.debug('ChaudiereMinute.last(ChaudiereMinute) == None:')
            begin = Chaudiere.first(Chaudiere).dt

        # else ChaudiereMinute is NOT empty then we start with the last ChaudiereMinute record (newest)
        else:
            logger.debug('ChaudiereMinute.last(ChaudiereMinute) != None:')
            begin = (ChaudiereMinute.last(ChaudiereMinute).dt + timedelta(minutes=1))

        # we finish with the last Chaudiere record (replace second to zero to avoid incomplete current minute)
        end = Chaudiere.last(Chaudiere).dt
    elif mode is 'rework_from_now':
        #begin is last existing Chaudiere - N hours
        dt = Chaudiere.last(Chaudiere).dt 
        begin = dt - timedelta(hours=hours)
        # we finish with the last Chaudiere record (replace second to zero to avoid incomplete current minute)
        end = Chaudiere.last(Chaudiere).dt
    elif mode is 'rework_from_date':
        end = find_datetime_end(date)
        begin = end - timedelta(hours=hours)
    else:
        logger.error('wrong arguments')
        return
    if begin is None:
        logger.info('No records')
        return
    
    begin = begin.replace(second=0, microsecond=0)
    end = end.replace(second=0, microsecond=0)
    
    if ((begin + timedelta(minutes=1)) > end):
        logger.info('Starting From '+ str(begin) + ' ...Waiting more records')
    else:
        logger.info('Archiving From '+ str(begin) +' To ' + str(end))

    # while some old Logs to Archive, call function record_minute()
    while ((begin + timedelta(minutes=1)) <= end):
        record_minute(begin)
        begin = begin + timedelta(minutes=1)
Example #6
0
def chaudiereminute(minutes):
    """Creates fake ChaudiereMinute entries every minute from now to a past date defined by a number of minutes from now (given by argument)    
    """
    now = datetime.now()
    for x in range(0, int(minutes)):
        dt = now - timedelta(minutes=x)
        rec = ChaudiereMinute(dt, 70, 0, 0, 0, 0, 0, 0, None, None, None)
        db.session.add(rec)
        print(rec)
    db.session.commit()
Example #7
0
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)
Example #8
0
def test():
    entries = ChaudiereMinute.all(ChaudiereMinute)
    out = []
    total = len(entries)
    percent = float(0)
    percent_old = float(0)
    iter = float(0)
    for entry in entries:
        iter += 1
        percent = 100 * (iter / total)
        #print ('iter : ' + str(iter/total))
        percent = int(percent)
        if percent != percent_old:
            percent_old = percent
            sys.stdout.write("\033[F")
            print(str(percent) + ' %')
        if entry.event is not None:
            out.append(entry)
    for entry in out:
        print(str(entry.dt) + ' ' + str(entry.event))
    print('end')
Example #9
0
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
Example #10
0
def test_alerts():
    logger.debug('test_alerts()')
    entry = ChaudiereMinute.last(ChaudiereMinute)
    send_email_sms.Send_Mail_Chaudiere_Alert(entry.dt)
    send_email_sms.Send_SMS_Chaudiere_Alert(entry.dt)
Example #11
0
def print_last_entry():
    print(ChaudiereMinute.last(ChaudiereMinute))