1+ import os
2+ import json
3+ import asyncio
4+ import motor .motor_asyncio
5+ from dotenv import load_dotenv
6+ import logging
7+ import requests
8+
9+ # Configure logging
10+ logging .basicConfig (level = logging .INFO )
11+ logger = logging .getLogger (__name__ )
12+ # Load environment variables from .env file
13+ load_dotenv ()
14+
15+ # Get the MongoDB URL from the environment variables
16+ mongo_url = os .getenv ("MONGODB_URL" )
17+ collections = [
18+ {
19+ "name" : "identities" ,
20+ "file" : "identities.json" ,
21+ "endpoint" : "core/identities" ,
22+ },
23+ {
24+ "name" : "entities" ,
25+ "file" : "entities.json" ,
26+ "endpoint" : "core/entities" ,
27+ },
28+ {
29+ "name" : "categories" ,
30+ "file" : "categories.json" ,
31+ "endpoint" : "core/categories" ,
32+ },
33+ {
34+ "name" : "sla" ,
35+ "file" : "sla.json" ,
36+ "endpoint" : "tickets/sla" ,
37+ },
38+ {
39+ "name" : "source-requests" ,
40+ "file" : "source-requests.json" ,
41+ "endpoint" : "tickets/source-request" ,
42+ },
43+ {
44+ "name" : "states" ,
45+ "file" : "states.json" ,
46+ "endpoint" : "tickets/state" ,
47+ },
48+ {
49+ "name" : "crontabs" ,
50+ "file" : "crontabs.json" ,
51+ "endpoint" : "core/crontabs" ,
52+ },
53+ {
54+ "name" : "projects" ,
55+ "file" : "projects.json" ,
56+ "endpoint" : "core/project" ,
57+ },
58+ {
59+ "name" : "triggers" ,
60+ "file" : "triggers.json" ,
61+ "endpoint" : "core/triggers" ,
62+ },
63+ {
64+ "name" : "tickets" ,
65+ "file" : "tickets.json" ,
66+ "endpoint" : "tickets/ticket" ,
67+ },
68+ {
69+ "name" : "threads" ,
70+ "file" : "threads.json" ,
71+ "endpoint" : "tickets/thread" ,
72+ },
73+ ]
74+
75+ # async def populate_collection(collection_name, db):
76+ # print(f"Populating {collection_name}...")
77+ # file_path = os.path.join(os.path.dirname(__file__), 'seeds', f"{collection_name}.json")
78+ # with open(file_path) as f:
79+ # data = json.load(f)
80+ # for d in data:
81+ # d["_id"] = ObjectId(d["_id"])
82+ # try:
83+ # result = await db[collection_name].insert_many(data)
84+ # logger.info(f"{len(result.inserted_ids)} {collection_name} inserted")
85+ # except Exception as e:
86+ # logger.warning(f"Duplicate key error: {e}")
87+ # logger.warning(f"Skipping {collection_name}...")
88+
89+ api_endpoint = os .getenv ("API_BASE_URL" )
90+
91+ async def populate_collection (collection ):
92+ logger .info (f"Populating { collection .get ('name' )} ..." )
93+ file_path = os .path .join (os .path .dirname (__file__ ), 'seeds' , collection .get ('file' ))
94+ with open (file_path ) as f :
95+ datas = json .load (f )
96+ for data in datas :
97+ try :
98+ response = requests .post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , json = data )
99+ response .raise_for_status ()
100+ logger .info (f"{ collection .get ('name' )} inserted" )
101+ except Exception as e :
102+ logger .warning (f"Failed to insert { collection .get ('name' )} : { e } " )
103+
104+ async def main ():
105+ collection_tasks = [populate_collection (col ) for col in collections ]
106+ await asyncio .gather (* collection_tasks )
107+
108+ print ("DB populated" )
109+
110+ if __name__ == "__main__" :
111+ asyncio .run (main ())
0 commit comments