def recomputeOccupancies(sensorId): sensorObj = SensorDb.getSensorObj(sensorId) if sensorObj.getSensorStatus() == PURGING: return {"status": "NOK", "ErrorMessage": "Sensor is PURGING"} elif sensorObj.getSensorStatus() == RECOMPUTING: return {"status": "NOK", "ErrorMessage": "Sensor is RECOMPUTING"} else: SensorDb.setSensorStatus(sensorId, RECOMPUTING) return {"status": "OK", "sensors": SensorDb.getAllSensors()}
def resetNoiseFloor(sensorId, noiseFloor): if SessionLock.isAcquired(): return { "status": "NOK", "StatusMessage": "Session is locked. Try again later." } SessionLock.acquire() try: dataMessages = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) if dataMessages is None: return {"status": "OK", "StatusMessage": "No Data Found"} SessionLock.release() for jsonData in dataMessages: DbCollections.getDataMessages(sensorId).update( {"_id": jsonData["_id"]}, {"$set": jsonData}, upsert=False) finally: SessionLock.release() return {"status": "OK", "sensors": SensorDb.getAllSensors()}
def runGarbageCollector(sensorId): SessionLock.acquire() try: userCount = SessionLock.getUserSessionCount() if userCount != 0: return {"status": "NOK", "ErrorMessage": "Active user session detected"} sensorObj = SensorDb.getSensorObj(sensorId) if sensorObj is None: return {"status": "NOK", "ErrorMessage": "Sensor Not found"} if sensorObj.getSensorStatus() != DISABLED: return {"status": "NOK", "ErrorMessage": "Sensor is ENABLED -- DISABLE it first"} dataRetentionDuration = sensorObj.getSensorDataRetentionDurationMonths( ) dataRetentionTime = dataRetentionDuration * 30 * SECONDS_PER_DAY cur = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) #dataMessages = cur.sort('t', pymongo.ASCENDING) currentTime = time.time() for msg in cur: insertionTime = Message.getInsertionTime(msg) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getDataMessages(sensorId).remove(msg) msgutils.removeData(msg) else: break # Now redo our book keeping summary fields. #dataMessages = cur.sort('t', pymongo.ASCENDING) locationMessages = DbCollections.getLocationMessages().find( {SENSOR_ID: sensorId}) for locationMessage in locationMessages: insertionTime = Message.getInsertionTime(locationMessage) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getLocationMessages().remove(msg) else: LocationMessage.clean(locationMessage) DbCollections.getLocationMessages().update( {"_id": locationMessage["_id"]}, {"$set": locationMessage}, upsert=False) sensorObj.cleanSensorStats() # Update the summary statistics. cur = DbCollections.getDataMessages(sensorId).find( {SENSOR_ID: sensorId}) for jsonData in cur: freqRange = DataMessage.getFreqRange(jsonData) minPower = DataMessage.getMinPower(jsonData) maxPower = DataMessage.getMaxPower(jsonData) messageId = DataMessage.getLocationMessageId(jsonData) lastLocationPost = DbCollections.getLocationMessages().find_one({"_id": messageId}) if DataMessage.getMeasurementType(jsonData) == FFT_POWER: minOccupancy = DataMessage.getMinOccupancy(jsonData) maxOccupancy = DataMessage.getMaxOccupancy(jsonData) meanOccupancy = DataMessage.getMeanOccupancy(jsonData) sensorObj.updateMinOccupancy(freqRange, minOccupancy) sensorObj.updateMaxOccupancy(freqRange, maxOccupancy) sensorObj.updateOccupancyCount(freqRange, meanOccupancy) LocationMessage.updateMaxBandOccupancy(lastLocationPost, freqRange, maxOccupancy) LocationMessage.updateMinBandOccupancy(lastLocationPost, freqRange, minOccupancy) LocationMessage.updateOccupancySum(lastLocationPost, freqRange, meanOccupancy) else: occupancy = DataMessage.getOccupancy(jsonData) sensorObj.updateMinOccupancy(freqRange, occupancy) sensorObj.updateMaxOccupancy(freqRange, occupancy) sensorObj.updateOccupancyCount(freqRange, occupancy) LocationMessage.updateMaxBandOccupancy(lastLocationPost, freqRange, occupancy) LocationMessage.updateMinBandOccupancy(lastLocationPost, freqRange, occupancy) LocationMessage.updateOccupancySum(lastLocationPost, freqRange, occupancy) sensorObj.updateTime(freqRange, Message.getTime(jsonData)) sensorObj.updateDataMessageTimeStamp(Message.getTime(jsonData)) SensorDb.updateSensor(sensorObj.getJson(), False, False) DbCollections.getLocationMessages().update( {"_id": lastLocationPost["_id"]}, {"$set": lastLocationPost}, upsert=False) # Garbage collect the unprocessed data messages. cur = DbCollections.getUnprocessedDataMessages(sensorId).find({SENSOR_ID: sensorId}) if cur is not None: dataMessages = cur.sort('t', pymongo.ASCENDING) for msg in dataMessages: insertionTime = Message.getInsertionTime(msg) if currentTime - dataRetentionTime >= insertionTime: DbCollections.getUnprocessedDataMessages(sensorId).remove(msg) else: break DbCollections.dropDailyOccupancyCache(sensorId) return {"status": "OK", "sensors": SensorDb.getAllSensors()} finally: SessionLock.release()