示例#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)
示例#2
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)
示例#3
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)