def ProcessRawEvents(rawEvent): myDB = MongoDBInterface('grande', 27017) myDB.SetDB('citybeat') myDB.SetCollection('photos') event = {} photos = [] lineNumber = -1 for line in rawEvent: lineNumber = lineNumber + 1 if lineNumber == 0: event['created_time'] = line.strip() continue if lineNumber == 1: event['lat'] = line.split(',')[0].strip() event['lng'] = line.split(',')[1].strip() continue if lineNumber == 2: vals = line.split(' ') event['predicted_mu'] = vals[0].strip() event['predicted_std'] = vals[1].strip() event['within_range'] = vals[4].strip() event['actual_value'] = vals[7].strip() continue photo_id = line.split(' ')[5].strip() photo = myDB.GetItem({'id':photo_id}) photos.append(photo) event['photos'] = photos return event
class AlarmDataInterface: def __init__(self, address=dbAddress, port=27017, db='alarm_filter', collection='photos'): self.db = MongoDBInterface(address, port) self.db.SetDB(db) self.db.SetCollection(collection) def GetUnlabeledEvent(self): # Get unlabeled "event" from the alarm filter return self.db.GetItem({'label': 'unlabeled'}) def LabelEvent(self, event, label): # Label an "event" as a true event or non-event event['label'] = label self.db.UpdateItem(event) def _GetAllEventsAtLocation(self, lat, lon): return self.db.GetAllItems({'mid_lat': lat, 'mid_lng': lon}) def _MergeTwoEvents(self, oldEvent, newEvent): # merge the photos from the new event to the old event # it will remove the duplicate photos oldPhotos = oldEvent['photos'] newPhotos = newEvent['photos'] oldPhoto = oldPhotos[-1] for i in xrange(0, len(newPhotos)): if self._UnicodeToInt( newPhotos[i]['created_time']) > self._UnicodeToInt( oldPhoto['created_time']): oldPhotos = oldPhotos + newPhotos[i:] print '%d out of %d photos have been increased' % (len( newPhotos[i:]), len(newPhotos)) break # this print is just for debug print 'no photo has been increased' # end for debug oldEvent['photos'] = oldPhotos return oldEvent def _UnicodeToInt(self, unic): return string.atoi(unic.encode("utf-8")) def MergeEvent(self, event): # merge the event with older events in DB # get all events in the same location allEvents = self._GetAllEventsAtLocation(event['mid_lat'], event['mid_lng']) if allEvents is None: return False for oldEvent in allEvents: # find a proper old event to combine (we assume there is only one "proper" old event) lastPhoto = oldEvent['photos'][-1] t1 = self._UnicodeToInt(event['photos'][-1]['created_time']) t2 = self._UnicodeToInt(lastPhoto['created_time']) if t1 == t2: # no further photos for this event return True # maximal allowed time interval is 15 mins if t1 > t2 and t1 <= t2 + 60 * 15: #within 15 minutes mergedEvent = self._MergeTwoEvents(oldEvent, event) self.db.UpdateItem(mergedEvent) return True return False def GetUnlabelEvents(self, condition=None): # TODO: select events according to the conditions pass