def test_01(self): Chatroom.chatrooms2upsert([ChatroomKakaotalk.chatroom()]) sender_name = "iris" channel_user_codename = ChannelUserKakaotalk.sender_name2codename( sender_name) ChannelUser.channel_users2upsert( [ChannelUserKakaotalk.sender_name2channel_user(sender_name)]) # collection = ChannelUserCollection.collection() # collection.delete_one({ChannelUser.Field.CODENAME: channel_user_codename}) packet = { KhalaPacket.Field.TEXT: "?price 사탕무 리스본 120ㅅ", KhalaPacket.Field.CHATROOM: KakaotalkUWOChatroom.codename(), KhalaPacket.Field.CHANNEL_USER: channel_user_codename, KhalaPacket.Field.SENDER_NAME: sender_name, } PriceSkill.packet2response(packet) collection = ChannelUserCollection.collection() channel_user_doc = MongoDBTool.bson2json( collection.find_one( {ChannelUser.Field.CODENAME: channel_user_codename})) hyp = MongoDBTool.doc2id_excluded(channel_user_doc, ) ref = { 'channel': 'kakaotalk_uwo', 'codename': 'kakaotalk_uwo.iris', 'alias': 'iris' } # pprint(hyp) self.assertEqual(hyp, ref)
def upsert_channel_user(): channel_user_list_raw = lfilter(is_not_none, map(ig(1), pair_list)) logger.debug({"len(channel_user_list_raw)": len(channel_user_list_raw)}) # raise Exception({"channel_user_list_raw[0]": channel_user_list_raw[0]}) channel_user_list = luniq(channel_user_list_raw, idfun=ChannelUser.channel_user2codename) collection = ChannelUserCollection.collection().with_options(write_concern=write_concern) def doc2pair(channel_user): cond = {ChannelUser.Field.CODENAME: ChannelUser.channel_user2codename(channel_user)} return cond, channel_user j_pair_list = lmap(doc2pair, channel_user_list) MongoDBTool.j_pair_list2upsert(collection, j_pair_list)
def j_iter2mongodb(cls, j_iter, chunk_size=100000): logger = HenriqueLogger.func_level2logger(cls.j_iter2mongodb, logging.DEBUG) j_list = list(j_iter) n = len(j_list) logger.debug({"n": n}) write_concern = WriteConcern(w=3, wtimeout=chunk_size) collection = MarkettrendCollection.collection().with_options( write_concern=write_concern) for i, j_list_chunk in enumerate( ChunkTool.chunk_size2chunks(j_list, chunk_size)): logger.debug({"i/n": "{}/{}".format(i * chunk_size, n)}) j_pair_list = [(j, j) for j in j_list_chunk] MongoDBTool.j_pair_list2upsert(collection, j_pair_list)
def docs2upsert(cls, docs): logger = KhalaLogger.func_level2logger(cls.docs2upsert, logging.DEBUG) def doc2pair(doc): doc_filter = DictTool.keys2filtered(doc, [ChannelUser.Field.CODENAME]) return doc_filter, doc pair_list = lmap(doc2pair, docs) collection = ChannelUserCollection.collection() mongo_result = MongoDBTool.j_pair_list2upsert(collection, pair_list) logger.debug({ "mongo_result.bulk_api_result": mongo_result.bulk_api_result, }) # raise Exception({"docs": docs, # "pair_list": pair_list , # "mongo_result.bulk_api_result":mongo_result.bulk_api_result, # }) cls._docs2cache(docs) return mongo_result # class KakaotalkUWOChannelUserDoc: # @classmethod # def username2doc(cls, username): # WARMER.warmup()
def _dict_id2codename(cls, ): doc_list = cls.doc_list_all() h = merge_dicts([{ MongoDBTool.doc2id(doc): cls.doc2key(doc) } for doc in doc_list], vwrite=vwrite_no_duplicate_key) return h
def docs2upsert(cls, docs): def doc2pair(doc): doc_filter = DictTool.keys2filtered(doc, [Chatroom.Field.CODENAME]) return (doc_filter, doc) pair_list = lmap(doc2pair, docs) collection = ChatroomCollection.collection() mongo_result = MongoDBTool.j_pair_list2upsert(collection, pair_list) return mongo_result
def test_04(self): logger = FoxylibLogger.func_level2logger(self.test_04, logging.DEBUG) c = MongodbToolCollection.collection_default() c.delete_many({}) client = MongoClient(host=FoxylibMongodb.uri()) db = MongoDBTool.name2db(client, FoxylibMongodb.Constant.DBNAME) collection = MongodbToolCollection.db2collection(db) self.assertNotEqual(collection, c)
def docs2upsert(cls, docs): logger = HenriqueLogger.func_level2logger(cls.docs2upsert, logging.DEBUG) def doc2pair(doc): doc_filter = DictTool.keys2filtered(doc, [cls.Field.CODENAME]) return doc_filter, doc pair_list = lmap(doc2pair, docs) value_args_kwargs_list = [(doc, [[cls.doc2codename(doc)]], {}) for doc in docs] with CacheManager.update_cache_each(cls.codenames2docs, value_args_kwargs_list): collection = ServerCollection.collection() mongo_result = MongoDBTool.j_pair_list2upsert( collection, pair_list) # raise Exception({"cls.codenames2docs(lmap(cls.doc2codename, docs))": cls.codenames2docs(lmap(cls.doc2codename, docs)), # "lmap(ig(1), pair_list)": lmap(ig(1), pair_list), # }) return mongo_result
def tuple2j_doc(t, i): j_postgres = t[PortTradegoodStateTable.index_json()] sender_name = j_postgres.get("sender_name") j_doc = { MarkettrendDoc.Field.SERVER: jdown(j_postgres, ["server", "name"]), MarkettrendDoc.Field.CREATED_AT: datetime.fromisoformat(j_postgres["created_at"]), MarkettrendDoc.Field.PORT_ID: MongoDBTool.doc2id(j_port_list[i]), MarkettrendDoc.Field.TRADEGOOD_ID: tradegood_id_list[i], MarkettrendDoc.Field.RATE: rate_list[i], MarkettrendDoc.Field.TREND: trend_list[i], } if sender_name: j_doc[MarkettrendDoc.Field.SENDER_NAME] = sender_name return j_doc
def upsert_marketprice(): marketprice_list = lmap(ig(0), pair_list) collection = MarketpriceCollection.collection().with_options(write_concern=write_concern) j_pair_list = [(x,x) for x in marketprice_list] MongoDBTool.j_pair_list2upsert(collection, j_pair_list)
def db(cls): client = cls.client() return MongoDBTool.name2db(client, cls.Constant.DBNAME)