<s> from pymongo import Connection <EOL> from openpds . core . models import Profile <EOL> from openpds . backends . base import InternalDataStore <EOL> from openpds import settings <EOL> import pdb <EOL> connection = Connection ( <EOL> host = getattr ( settings , "<STR_LIT>" , None ) , <EOL> port = getattr ( settings , "<STR_LIT>" , None ) <EOL> ) <EOL> def getInternalDataStore ( profile , app_id , lab_id , token ) : <EOL> return MongoInternalDataStore ( profile , app_id , lab_id , token ) <EOL> class MongoInternalDataStore ( InternalDataStore ) : <EOL> def __init__ ( self , profile , app_id , lab_id , token ) : <EOL> """<STR_LIT>""" <EOL> super ( MongoInternalDataStore , self ) . __init__ ( profile , app_id , lab_id , token ) <EOL> self . profile = profile <EOL> self . db = connection [ profile . getDBName ( ) ] <EOL> def saveAnswer ( self , key , data ) : <EOL> """<STR_LIT>""" <EOL> collection = self . db [ "<STR_LIT>" ] if isinstance ( data , list ) else self . db [ "<STR_LIT>" ] <EOL> answer = collection . find ( { "<STR_LIT:key>" : key } ) <EOL> if answer . count ( ) == <NUM_LIT:0> : <EOL> answer = { "<STR_LIT:key>" : key } <EOL> else : <EOL> answer = answer [ <NUM_LIT:0> ] <EOL> answer [ "<STR_LIT:value>" ] = data <EOL> collection . save ( answer ) <EOL> def getAnswer ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . db [ "<STR_LIT>" ] . find ( { "<STR_LIT:key>" : key } ) if key is not None else self . db [ "<STR_LIT>" ] . find ( ) <EOL> def getAnswerList ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . db [ "<STR_LIT>" ] . find ( { "<STR_LIT:key>" : key } ) if key is not None else self . db [ "<STR_LIT>" ] . find ( ) <EOL> def getData ( self , key , startTime , endTime ) : <EOL> """<STR_LIT>""" <EOL> dataFilter = { "<STR_LIT:key>" : { "<STR_LIT>" : key + "<STR_LIT:$>" } } <EOL> if startTime is not None or endTime is not None : <EOL> timeFilter = { } <EOL> if startTime is not None : <EOL> timeFilter [ "<STR_LIT>" ] = startTime <EOL> if endTime is not None : <EOL> timeFilter [ "<STR_LIT>" ] = endTime <EOL> dataFilter [ "<STR_LIT:time>" ] = timeFilter <EOL> return self . db [ "<STR_LIT>" ] . find ( dataFilter ) <EOL> def saveData ( self , data , collectionname ) : <EOL> """<STR_LIT>""" <EOL> self . db [ collectionname ] . save ( data ) </s>
