<s> __all__ = ( '<STR_LIT>' , ) <EOL> from dabble import ResultStorage <EOL> from dabble . util import * <EOL> from datetime import datetime <EOL> from random import randrange <EOL> from bson . son import SON <EOL> from pymongo import ASCENDING , DESCENDING <EOL> from pymongo . database import Database <EOL> from pymongo . errors import DuplicateKeyError <EOL> class MongoResultStorage ( ResultStorage ) : <EOL> def __init__ ( self , database , namespace = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( database , Database ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . namespace = namespace <EOL> self . tests = database [ '<STR_LIT>' % namespace ] <EOL> self . results = database [ '<STR_LIT>' % namespace ] <EOL> self . results . ensure_index ( [ ( '<STR_LIT:t>' , ASCENDING ) , ( '<STR_LIT:i>' , ASCENDING ) ] ) <EOL> def save_test ( self , test_name , alternatives , steps ) : <EOL> test = self . tests . find_one ( { '<STR_LIT>' : test_name } ) <EOL> if test and test [ '<STR_LIT:a>' ] != alternatives : <EOL> raise Exception ( '<STR_LIT>' % test_name ) <EOL> elif not test : <EOL> self . tests . save ( { <EOL> '<STR_LIT>' : test_name , <EOL> '<STR_LIT:a>' : alternatives , <EOL> '<STR_LIT:s>' : steps , <EOL> } , safe = True ) <EOL> def record ( self , identity , test_name , alternative , action ) : <EOL> self . results . update ( { <EOL> '<STR_LIT:i>' : identity , <EOL> '<STR_LIT:t>' : test_name , <EOL> '<STR_LIT:n>' : alternative } , <EOL> { '<STR_LIT>' : { '<STR_LIT:s>' : action } } , <EOL> upsert = True ) <EOL> def has_action ( self , identity , test_name , alternative , action ) : <EOL> return self . results . find_one ( { '<STR_LIT:i>' : identity , '<STR_LIT:t>' : test_name , '<STR_LIT:n>' : alternative , '<STR_LIT:s>' : action } ) is not None <EOL> def set_alternative ( self , identity , test_name , alternative ) : <EOL> result = self . results . find_one ( { '<STR_LIT:i>' : identity , '<STR_LIT:t>' : test_name } ) <EOL> if not result : <EOL> self . results . save ( { '<STR_LIT:i>' : identity , '<STR_LIT:t>' : test_name , '<STR_LIT:n>' : alternative , '<STR_LIT:s>' : [ ] } ) <EOL> elif result and result [ '<STR_LIT:n>' ] != alternative : <EOL> raise Exception ( '<STR_LIT>' % identity ) <EOL> def get_alternative ( self , identity , test_name ) : <EOL> result = self . results . find_one ( { '<STR_LIT:i>' : identity , '<STR_LIT:t>' : test_name } ) or { } <EOL> return result . get ( '<STR_LIT:n>' ) <EOL> def report ( self , test_name ) : <EOL> test = self . tests . find_one ( { '<STR_LIT>' : test_name } ) <EOL> if test is None : <EOL> raise Exception ( '<STR_LIT>' % test_name ) <EOL> report = { <EOL> '<STR_LIT>' : test_name , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> trials = sparsearray ( int ) <EOL> for result in self . results . find ( { '<STR_LIT:t>' : test_name } ) : <EOL> if result [ '<STR_LIT:s>' ] != test [ '<STR_LIT:s>' ] [ : len ( result [ '<STR_LIT:s>' ] ) ] : <EOL> continue <EOL> for i in xrange ( len ( result [ '<STR_LIT:s>' ] ) ) : <EOL> trials [ result [ '<STR_LIT:n>' ] ] [ i ] += <NUM_LIT:1> <EOL> for i , alternative in enumerate ( test [ '<STR_LIT:a>' ] ) : <EOL> funnel = [ ] <EOL> alt = { '<STR_LIT>' : alternative , '<STR_LIT>' : funnel } <EOL> report [ '<STR_LIT>' ] . append ( alt ) <EOL> for s , stepspair in enumerate ( pairwise ( test [ '<STR_LIT:s>' ] ) ) : <EOL> att = trials [ i ] [ s ] <EOL> con = trials [ i ] [ s + <NUM_LIT:1> ] <EOL> funnel . append ( { <EOL> '<STR_LIT>' : stepspair , <EOL> '<STR_LIT>' : att , <EOL> '<STR_LIT>' : con , <EOL> } ) <EOL> return report <EOL> def list_tests ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ t [ '<STR_LIT>' ] for t in self . tests . find ( fields = [ '<STR_LIT>' ] ) ] </s>
