<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 <EOL> import json <EOL> import pdb <EOL> from gcm import GCM <EOL> from SPARQLWrapper import SPARQLWrapper , JSON <EOL> from collections import Counter <EOL> import sqlite3 <EOL> import random <EOL> from openpds . questions . socialhealth_tasks import getStartTime <EOL> from openpds import getInternalDataStore <EOL> connection = Connection ( <EOL> host = getattr ( settings , "<STR_LIT>" , None ) , <EOL> port = getattr ( settings , "<STR_LIT>" , None ) <EOL> ) <EOL> @ task ( ) <EOL> def ensureFunfIndexes ( ) : <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:time>" , - <NUM_LIT:1> ) , ( "<STR_LIT:key>" , <NUM_LIT:1> ) ] , cache_for = <NUM_LIT> , background = True , unique = True , dropDups = True ) <EOL> @ task ( ) <EOL> def deleteUnusedProfiles ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> start = getStartTime ( <NUM_LIT> , False ) <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> collection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> if collection . find ( { "<STR_LIT:time>" : { "<STR_LIT>" : start } } ) . count ( ) == <NUM_LIT:0> : <EOL> connection . drop_database ( dbName ) <EOL> profile . delete ( ) <EOL> @ task ( ) <EOL> def recentProbeCounts ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> startTime = getStartTime ( <NUM_LIT:1> , False ) <EOL> for profile in profiles : <EOL> ids = getInternalDataStore ( profile , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> probes = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> answer = { } <EOL> for probe in probes : <EOL> data = ids . getData ( probe , startTime , None ) <EOL> answer [ probe ] = data . count ( ) <EOL> ids . saveAnswer ( "<STR_LIT>" , answer ) <EOL> def addNotification ( profile , notificationType , title , content , uri ) : <EOL> notification , created = Notification . objects . get_or_create ( datastore_owner = profile , type = notificationType ) <EOL> notification . title = title <EOL> notification . content = content <EOL> notification . datastore_owner = profile <EOL> if uri is not None : <EOL> notification . uri = uri <EOL> notification . save ( ) <EOL> def addNotificationAndNotify ( profile , notificationType , title , content , uri ) : <EOL> addNotification ( profile , notificationType , title , content , uri ) <EOL> if Device . objects . filter ( datastore_owner = profile ) . count ( ) > <NUM_LIT:0> : <EOL> gcm = GCM ( settings . GCM_API_KEY ) <EOL> for device in Device . objects . filter ( datastore_owner = profile ) : <EOL> try : <EOL> gcm . plaintext_request ( registration_id = device . gcm_reg_id , data = { "<STR_LIT:action>" : "<STR_LIT>" } ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % ( profile . id , profile . uuid ) <EOL> print e <EOL> def notifyAll ( ) : <EOL> for profile in Profile . objects . all ( ) : <EOL> if Device . objects . filter ( datastore_owner = profile ) . count ( ) > <NUM_LIT:0> : <EOL> gcm = GCM ( settings . GCM_API_KEY ) <EOL> for device in Device . objects . filter ( datastore_owner = profile ) : <EOL> try : <EOL> gcm . plaintext_request ( registration_id = device . gcm_reg_id , data = { "<STR_LIT:action>" : "<STR_LIT>" } ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % ( profile . id , profile . uuid ) <EOL> print e <EOL> def broadcastNotification ( notificationType , title , content , uri ) : <EOL> for profile in Profile . objects . all ( ) : <EOL> addNotificationAndNotify ( profile , notificationType , title , content , uri ) <EOL> @ task ( ) <EOL> def sendVerificationSurvey ( ) : <EOL> broadcastNotification ( <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ task ( ) <EOL> def sendPast3DaysSurvey ( ) : <EOL> broadcastNotification ( <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ task ( ) <EOL> def sendExperienceSampleSurvey ( ) : <EOL> broadcastNotification ( <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ task ( ) <EOL> def sendSleepStartSurvey ( ) : <EOL> broadcastNotification ( <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ task ( ) <EOL> def sendSleepEndSurvey ( ) : <EOL> broadcastNotification ( <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def minDiff ( elements , item ) : <EOL> return min ( [ abs ( el - item ) for el in elements ] ) <EOL> @ task ( ) <EOL> def scheduleExperienceSamplesForToday ( ) : <EOL> maxDelay = <NUM_LIT> * <NUM_LIT:8> <EOL> delays = [ random . randint ( <NUM_LIT:0> , maxDelay ) ] <EOL> while len ( delays ) < <NUM_LIT:4> : <EOL> nextDelay = random . randint ( <NUM_LIT:0> , maxDelay ) <EOL> if minDiff ( delays , nextDelay ) >= <NUM_LIT> : <EOL> delays . append ( nextDelay ) <EOL> print delays <EOL> print [ time . strftime ( "<STR_LIT>" , time . localtime ( <NUM_LIT> + d ) ) for d in delays ] <EOL> for t in delays : <EOL> print "<STR_LIT>" % str ( t ) <EOL> sendExperienceSampleSurvey . apply_async ( countdown = t ) <EOL> @ task ( ) <EOL> def findMusicGenres ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> sparql = SPARQLWrapper ( "<STR_LIT>" ) <EOL> sparql . setReturnFormat ( JSON ) <EOL> artistQuery = "<STR_LIT>" <EOL> albumQuery = "<STR_LIT>" <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> answerListCollection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> collection = connection [ dbName ] [ "<STR_LIT>" ] <EOL> songs = [ song [ "<STR_LIT:value>" ] for song in collection . find ( { "<STR_LIT:key>" : { "<STR_LIT>" : "<STR_LIT>" } } ) ] <EOL> artists = set ( [ str ( song [ "<STR_LIT>" ] ) for song in songs if str ( song [ "<STR_LIT>" ] ) != "<STR_LIT>" and '<STR_LIT:">' not in str ( song [ "<STR_LIT>" ] ) ] ) <EOL> genres = [ ] <EOL> for artist in artists : <EOL> temp = artistQuery % artist <EOL> print temp <EOL> sparql . setQuery ( temp ) <EOL> results = sparql . query ( ) . convert ( ) <EOL> genres . extend ( [ binding [ "<STR_LIT>" ] [ "<STR_LIT:value>" ] for binding in results [ "<STR_LIT>" ] [ "<STR_LIT>" ] ] ) <EOL> if len ( genres ) > <NUM_LIT:0> : <EOL> counts = Counter ( genres ) . most_common ( <NUM_LIT:10> ) <EOL> musicGenres = answerListCollection . find ( { "<STR_LIT:key>" : "<STR_LIT>" } ) <EOL> musicGenres = musicGenres [ <NUM_LIT:0> ] if musicGenres . count ( ) > <NUM_LIT:0> else { "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT:value>" : [ ] } <EOL> musicGenres [ "<STR_LIT:value>" ] = [ count [ <NUM_LIT:0> ] for count in counts ] <EOL> answerListCollection . save ( musicGenres ) <EOL> @ task ( ) <EOL> def dumpFunfData ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> outputConnection = sqlite3 . connect ( "<STR_LIT>" ) <EOL> c = outputConnection . cursor ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> startTime = getStartTime ( <NUM_LIT:3> , False ) <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> funf = connection [ dbName ] [ "<STR_LIT>" ] <EOL> user = int ( profile . id ) <EOL> c . executemany ( "<STR_LIT>" , [ ( user , d [ "<STR_LIT:key>" ] [ d [ "<STR_LIT:key>" ] . rfind ( "<STR_LIT:.>" ) + <NUM_LIT:1> : ] , d [ "<STR_LIT:time>" ] , "<STR_LIT:%s>" % d [ "<STR_LIT:value>" ] ) for d in funf . find ( { "<STR_LIT:time>" : { "<STR_LIT>" : startTime } } ) if d [ "<STR_LIT:key>" ] is not None ] ) <EOL> outputConnection . commit ( ) <EOL> outputConnection . close ( ) <EOL> @ task ( ) <EOL> def dumpSurveyData ( ) : <EOL> profiles = Profile . objects . all ( ) <EOL> outputConnection = sqlite3 . connect ( "<STR_LIT>" ) <EOL> c = outputConnection . cursor ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> for profile in profiles : <EOL> dbName = profile . getDBName ( ) <EOL> answerlist = connection [ dbName ] [ "<STR_LIT>" ] <EOL> user = int ( profile . id ) <EOL> for datum in answerlist . find ( { "<STR_LIT:key>" : { "<STR_LIT>" : "<STR_LIT>" } } ) : <EOL> for answer in datum [ "<STR_LIT:value>" ] : <EOL> c . execute ( "<STR_LIT>" , ( user , datum [ "<STR_LIT:key>" ] , answer [ "<STR_LIT:time>" ] , "<STR_LIT:%s>" % answer [ "<STR_LIT:value>" ] ) ) <EOL> for datum in answerlist . find ( { "<STR_LIT:key>" : { "<STR_LIT>" : "<STR_LIT>" } } ) : <EOL> for answer in datum [ "<STR_LIT:value>" ] : <EOL> c . execute ( "<STR_LIT>" , ( user , datum [ "<STR_LIT:key>" ] , answer [ "<STR_LIT:time>" ] , "<STR_LIT:%s>" % answer [ "<STR_LIT:value>" ] ) ) <EOL> for datum in answerlist . find ( { "<STR_LIT:key>" : { "<STR_LIT>" : "<STR_LIT>" } } ) : <EOL> for answer in datum [ "<STR_LIT:value>" ] : <EOL> c . execute ( "<STR_LIT>" , ( user , datum [ "<STR_LIT:key>" ] , answer [ "<STR_LIT:time>" ] , "<STR_LIT:%s>" % answer [ "<STR_LIT:value>" ] ) ) <EOL> outputConnection . commit ( ) <EOL> outputConnection . close ( ) </s>
