def test_parse_Normal_Single_Request_Message(self): """ suppose you want to recommend an item. You only have one item to recommend which unfortunatly is the one the user already sees. Recommending this is better then nothing """ fP = FullContestMessageParser() message = "{\"msg\":\"impression\",\"id\":1300277,\"client\":{\"id\":1},\"domain\":{\"id\":\"418\"},\"item\":{\"id\":\"1234\",\"title\":\"Inter\u00adna\u00adtional Emmy-Awards\",\"url\":\"http:\/\/www.ksta.de\/html\/fotolines\/1321650273296\/rahmen.shtml?1\",\"created\":1321957664,\"text\":\"Talkshow-Mode\u00adra\u00adtorin Wendy Williams bei den 39. inter\u00adna\u00adtio\u00adnalen Emmy-Awards. (Bild: AFP)\",\"img\":null,\"recommendable\":true},\"config\":{\"timeout\":null,\"recommend\":true,\"limit\":1,\"team\":{\"id\":\"22\"}},\"version\":\"1.0\"}" fP.parse(message) print fP.flattenedJson self.assertEqual(fP.message_id, 1300277, "message id not correctly parsed") self.assertEqual(fP.user_id, 1, "user id not correctly parsed") self.assertEqual(fP.domain_id, 418, "domain id not correctly parsed") self.assertEqual(fP.message_type, 'impression', "domain id not correctly parsed")
exchange_name = '' def __init__(self): ''' get the message, parse it and then save ''' def save(self, message, async=False, api='contest', backends=(), constraints={}): """ """ if not async: # save the data instantly if api == 'contest': fullParsedDataModel = FullContestMessageParser() fullParsedDataModel.parse(message) fullParsedDataModel.save() item_id = fullParsedDataModel.item_id if config_global.SAVE_RAW_JSON in backends: raw = rawJsonModel(message, mode='redis') raw.save() if config_global.SAVE_RANDOM_RECOMMENDER in backends: fb = Random_Recommender() domain_id = fullParsedDataModel.domain_id ## todo the recommender has to decide on its own what to save and therefore save constraints, even though the constrain management should be centralized #constraints = {'domainid': domain_id} fb.set_recommendables(item_id, constraints)
''' from contest.packages.message_parsers.fullParser import FullContestMessageParser from contest.packages.recommenders.GeneralRecommender import GeneralRecommender from contest.packages.recommenders.Random_Recommender import Random_Recommender class OnsiteRecommender(GeneralRecommender): resultSet = None def __init__(self, json_string, async=False, api='contest', backends=[] ): ''' Constructor ''' self.backends = backends fullParsedDataModel = FullContestMessageParser() fullParsedDataModel.parse(json_string) domain_id = fullParsedDataModel.domain_id user_id = fullParsedDataModel.user_id # now compile the constraints, which are important for an onsite Recommender: do not recommend item from another domain constraints = {'domainid': domain_id} random_recommender = Random_Recommender() N = 4 # TODO initialize a new training session if necessary random_recommender.train(user_id, constraints) self.resultSet = random_recommender.get_recommendation(user_id, constraints, N=N, remove=True)
#print timestamp_sec if ( not debug2 ): """ this step should simulate the normal process of the contest """ http_conn = http_connector('localhost:5001') http_conn.send('/contest/incoming_message', json_string) else: """ this step is for debugging """ #print json_string #rjm = rawJsonModel( json_string ) #rjm.save() parser = FullContestMessageParser() flattenedJson = parser.parse(json_string) print flattenedJson """ lets save the data, each field a column """ if ( flattenedJson ): #iJson = interpretedJsonModel(the_id, flattenedJson) #iJson.save() if (SAVE_ITEM_BY_USER): try: timeStamp = timestamp_sec iBU = ItemsByUser(user_id = flattenedJson['client_id'], timeStamp = timeStamp ) iBU.save( item_id = flattenedJson['item_id'] )