Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.1 kB
3
Indexable
Never
from pymongo import MongoClient
from datetime import datetime
from pandas import DataFrame, ExcelWriter, concat
import argparse
from multiprocessing.pool import ThreadPool
from tqdm import tqdm


envs = {
    "articprod": {"con_str": "mongodb://afgarticprodcosmosaccount:D4lfqlqPdBqrUOEknWwSu8j4USD43lnA7wfSRxOrStfd4YqCuCzVDdAgKNWM1Etz5DfYzqTuiOWOsSeTtHjOnw==@afgarticprodcosmosaccount.documents.azure.com:10255/?ssl=true&retrywrites=false",
                  "db_name": "afgarticprodcosmosdb"}
}


class FetchFromDb():
    def __init__(self, env_name):
        self.env_name = env_name
        self.env = envs[env_name]
        self.client = MongoClient(self.env["con_str"])
        self.db = self.client[self.env["db_name"]]
        self.ord_col = self.db["artic_orders"]
        self.final_list = []

    # def parse_to_list_df(self, i):
    #     print('here')
    #     parsed_list = self.parse_db_value(i)
    #     self.final_list = self.final_list + parsed_list

    # def parse_db_value(self, i):
    #     if 'lineItems' in i:
    #         parsed_dict_list = []
    #         for item in i['lineItems']:
    #             parsed_dict = {}
    #             parsed_dict['productId'] = item['productId']
    #             parsed_dict['productName'] = item['productName'] if 'productName' in item else ""
    #             parsed_dict['quantity'] = item['quantity']
    #             parsed_dict_list.append(parsed_dict)
    #         return parsed_dict_list

    def fetch_custom(self, start_date, end_date):
        start_time_obj = datetime.strptime(start_date, '%d-%m-%Y')
        end_time_obj = datetime.strptime(end_date, '%d-%m-%Y')
        coll_order_list = list(self.ord_col.find(
            {"orderDate": {"$gte": start_time_obj,
                           "$lte": end_time_obj
                           }, "status": {"$nin": ["Receipt Closed", "Return Created", "ORDER_CANCELED"]
                                         }, "siteId": "mns-food-UAE"},
            {"_id": 1, "orderNo": 1, "customerName": 1,
             "customerEmail": 1, "customerNo": 1, "orderDate": 1}))

        df_order_single_prod_list = DataFrame(coll_order_list)
        grouped = df_order_single_prod_list.groupby("customerEmail")
        customerEmail = []
        customerName = []
        count = []
        orderNo = []
        customerNames = []
        print(grouped.head())
        for count_items, group in grouped:
            # print("Count > ", count_items)
            # print("Group > ", group)

            customerEmail.append(count_items)

            customerName.append(group['customerName'].values)
            for i in customerName:
                custNames = list(set(i))
            #  list(dict.fromkeys(customerName))
            # print("\n\n\ncount_items\n\n", custNames)
            count.append(len(group))
            orderNo.append(group['orderNo'].values)
            customerNames.append(custNames[0])
        final_df = DataFrame({
            "customerEmail": customerEmail,
            "customerName": customerNames,
            "count": count,
            "orderNo": orderNo,
        })
        print(final_df)
        # final_df1 =final_df.sort_values("count", ascending=False)

        # dto = datetime.now()
        # final_df1.to_excel(self.env_name + str(dto.year)+str(dto.month)+str(dto.day)+str(dto.hour)+str(dto.minute)+str(dto.second) + '_custom.xlsx',
        #                                    sheet_name="custom", index=False)


if __name__ == "__main__":
    fetch_from_db_articprod = FetchFromDb(env_name="articprod")

    parser = argparse.ArgumentParser()

    parser.add_argument('db', help='Enter which db to fetch. "uat" or "prod"')
    parser.add_argument(
        'fromdate', help='Enter the from date. Ex: "dd-mm-yyyy"')
    parser.add_argument('todate', help='Enter the to date. Ex: "dd-mm-yyyy"')

    args = parser.parse_args()
    fetch_db = args.db
    start_date = args.fromdate
    end_date = args.todate

    if fetch_db == "articprod":
        fetch_from_db_articprod.fetch_custom(start_date, end_date)