Esempio n. 1
0
def formatData(resultData, sensor, startTs, stopTs, precision):
    
    # Check if the first timestamp we get is meaningful
    if startTs:
        if int(startTs) < int(sensor.firstTs):
            startTs = sensor.firstTs
    else :
        startTs = sensor.firstTs
    
    if not stopTs:
        stopTs = str(int(startTs) + utils.DEFAULT_TIME_INT)
        
    # Get the column steps
    colSteps = cPickle.loads(str(sensor.steps))
    
    # Get the column names.
    colNames = resultData["colNames"]
    
    ## Initializing structures for making us creating a result data
    ## structure with approximated values
    
    # Dict containing the latest timestamps at which we encounter values 
    # for each column. We initialize it with the starting timestamp of the
    # data we want to retrieve, so that we don't generate values 
    # occurring before this timestamp 
    latestTimes = {}
    
    # Structure that we will populate to compute our final list
    # of values
    resultDict = {}
    
    # Init the latest timestamps
    for cn in colNames:
        latestTimes[cn] = int(startTs) - 1
    
    # Populate the data structure   
    for row in resultData["rows"] :
        for cn in row["columns"].keys():
            # Get the latest time for the given column
            latestTime = latestTimes[cn]
            
            # Initializes the timestamp as being the one of the row
            timestamp = int(row["id"])
            
            # If the timestamp is after the stopRow, we align directly with
            # the stop timestamp
            if timestamp > int(stopTs):
                timestamp = int(utils.alignTs(stopTs, sensor.firstTs, sensor.recInt))
                
            # As long as our timestamp is below the timestamp of the
            # preceding value in the table for this column, we
            # compute values and put them into our data structure
            while(timestamp > latestTime):
                ts = str(timestamp)
                if ts not in resultDict:
                    resultDict[ts] = {"id": ts, "columns": {}}
                resultDict[ts]["columns"][cn] = computeValue(row["columns"][cn], timestamp, cn[:2])
                timestamp -= colSteps[cn[3:]]
                
            latestTimes[cn] = int(row["id"])
            
    
    # We get the values of the data structure (since it is a dict),
    # and sort them into a list in ascending order of timestamps
    resultData = {"rows": sorted(resultDict.values(), key = lambda row: int(row["id"])),
                  "colNames": colNames}

    return resultData
Esempio n. 2
0
                    # latest_model_value = lm.getFormattedValue()

                    # Add the value to the model and retrieve the new values
                    new_values = lm.add(float(index), float(row[cn]))

                    # If the maxdist is bigger than the error
                    if new_values["maxDist"] > variables.err_th:
                        # Get the latest value for this column
                        latest_value = latest_values["lm:" + cn]
                        # Put it to the values to commit if its timestamp is not ""
                        lv_ts = latest_value["timestamp"]
                        if lv_ts:
                            # Round the timestamp so it matches the recording
                            # frequency. We do this for correcting unalignments
                            # in the data.
                            lv_ts = alignTs(lv_ts, firstTs, variables.recInt)
                            if lv_ts not in values_to_commit:
                                values_to_commit[lv_ts] = {}
                            values_to_commit[lv_ts]["lm:" + cn] = latest_value["value"]
                        # Reset the model object
                        lm.reset()
                        # Add the values to the model object and retrieve the new values
                        lm.add(float(index), float(row[cn]))

                    # Update the latest value for the given column with the new values
                    latest_values["lm:" + cn]["timestamp"] = index
                    latest_values["lm:" + cn]["value"] = lm.getFormattedValues()

                    ## Update the value for the constant model
                    # Get the model corresponding to this value
                    cm = constant_models["cm:" + cn]