def logNewsClickForUser(user_id, news_id, isLikeToggleOn, isDisLikeToggleOn): message = { 'userId': user_id, 'newsId': news_id, 'isLikeToggleOn': isLikeToggleOn, 'isDisLikeToggleOn': isDisLikeToggleOn, 'timestamp': datetime.utcnow() } db = mongodb_client.get_db() db[CLICK_LOGS_TABLE_NAME].insert(message) app_log.info(message) print message slice_news = db[NEWS_TABLE_NAME].find({'digest': news_id}) for news in slice_news: news['isLkeToggleOn'] = isLikeToggleOn news['isDisLikeToggleOn'] = isDisLikeToggleOn db['news'].replace_one({'digest': news['digest']}, news, upsert=True) app_log.info('%s is inserted successfully into mongodb news table', news) # Send log task to machine learning service for prediction message = { 'userId': user_id, 'newsId': news_id, 'isLikeToggleOn': isLikeToggleOn, 'isDisLikeToggleOn': isDisLikeToggleOn, 'timestamp': str(datetime.utcnow()) } cloudAMQP_client.sendMessage(message)
def getMessage(self): method_frame, header_frame, body = self.channel.basic_get( self.queue_name) if method_frame is not None: app_log.info("[O] Received message from %s: %s" % (self.queue_name, body)) self.channel.basic_ack(method_frame.delivery_tag) return json.loads(body) else: app_log.info("No message returned") return None
def getNewsSummariesForUser(user_id, page_num): page_num = int(page_num) begin_index = (page_num - 1) * NEWS_LIST_BATCH_SIZE end_index = page_num * NEWS_LIST_BATCH_SIZE # The final list of news to be returned. sliced_news = [] print user_id if redis_client.get(user_id) is not None: app_log.info('redis is not None') news_digests = pickle.loads(redis_client.get(user_id)) # If begin_index is out of range, this will return empty list; # If end_index is out of range (begin_index is within the range), this # will return all remaining news ids. sliced_news_digests = news_digests[begin_index:end_index] app_log.info(sliced_news_digests) db = mongodb_client.get_db() sliced_news = list(db[NEWS_TABLE_NAME].find( {'digest': { '$in': sliced_news_digests }})) else: db = mongodb_client.get_db() total_news = list(db[NEWS_TABLE_NAME].find().sort([ ('publishedAt', -1) ]).limit(NEWS_LIMIT)) total_news_digests = map(lambda x: x['digest'], total_news) redis_client.set(user_id, pickle.dumps(total_news_digests)) redis_client.expire(user_id, USER_NEWS_TIME_OUT_IN_SECONDS) sliced_news = total_news[begin_index:end_index] # Get preference for the user preference = news_recommendation_service_client.getPreferenceForUser( user_id) topPreference = None if preference is not None and len(preference) > 0: topPreference = preference[0] for news in sliced_news: # Remove text field to save bandwidth. del news['text'] if news['class'] == topPreference: news['reason'] = 'Recommend' if news['publishedAt'].date() == datetime.today().date(): news['time'] = 'today' return json.loads(dumps(sliced_news))
def sendMessage(self, message): self.channel.basic_publish(exchange='', routing_key=self.queue_name, body=json.dumps(message)) app_log.info("[X] Sent message to %s: %s" % (self.queue_name, message)) return
def getNewsSummariesForUser(self, user_id, page_num): app_log.info("rpc service: getNewsSummariesForUser") return operations.getNewsSummariesForUser(user_id, page_num)
import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) from applogconfig import app_log app_log.info("Preference list: %s" % str({'key': 'hello', 'demo': 'go'}))
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'config')) import mongodb_client import news_topic_modeling_service_client from applogconfig import app_log if __name__ == '__main__': db = mongodb_client.get_db() cursor = db['news'].find({}) count = 0 for news in cursor: count += 1 print count if 'class' not in news: app_log.info('Populating classes...') title = news['title'] topic = news_topic_modeling_service_client.classify(title) news['class'] = topic db['news'].replace_one({'digest': news['digest']}, news, upsert=True) if 'isLikeToggleOn' in news: app_log.info('Populating isLikeToggleOn...') news['isLikeToggleOn'] = False db['news'].replace_one({'digest': news['digest']}, news, upsert=True) if 'isDisLikeToggleOn' in news: app_log.info('Populating isLikeToggleOn...') news['isDisLikeToggleOn'] = False
def getPreferenceForUser(userId): preference = client.call('getPreferenceForUser', userId) app_log.info("Preference list: %s" % str(preference)) return preference