<s> from celery import task <EOL> from openpds . core . models import Profile , Notification , Device <EOL> from bson import ObjectId <EOL> from pymongo import Connection <EOL> from django . conf import settings <EOL> import time <EOL> from datetime import date , timedelta , datetime <EOL> import json <EOL> import pdb <EOL> import math <EOL> import cluster <EOL> from gcm import GCM <EOL> from openpds . core . models import Profile <EOL> connection = Connection ( <EOL> host = getattr ( settings , "<STR_LIT>" , None ) , <EOL> port = getattr ( settings , "<STR_LIT>" , None ) <EOL> ) <EOL> @ task ( ) <EOL> def findRecentIncidents ( ) : <EOL> currentTime = time . time ( ) <EOL> today = date . fromtimestamp ( currentTime ) <EOL> startTime = currentTime - ( <NUM_LIT> * <NUM_LIT> ) <EOL> profiles = Profile . objects . all ( ) <EOL> answer = { "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT:value>" : [ ] } <EOL> print "<STR_LIT>" <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> collection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> for incident in collection . find ( { "<STR_LIT:date>" : { "<STR_LIT>" : startTime } } , limit = <NUM_LIT:50> ) : <EOL> answer [ "<STR_LIT:value>" ] . append ( incident ) <EOL> print "<STR_LIT>" % ( len ( answer [ "<STR_LIT:value>" ] ) , profiles . count ( ) ) <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> answerlistCollection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> answerlistCollection . remove ( { "<STR_LIT:key>" : "<STR_LIT>" } ) <EOL> answerlistCollection . save ( answer ) <EOL> print "<STR_LIT>" <EOL> @ task ( ) <EOL> def ensureIncidentIndexes ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> collection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> collection . ensure_index ( [ ( "<STR_LIT:date>" , - <NUM_LIT:1> ) , ( "<STR_LIT:type>" , <NUM_LIT:1> ) ] , cache_for = <NUM_LIT> , background = True ) </s>
