Skip to content

Commit 146fec5

Browse files
populate script
1 parent bb1080a commit 146fec5

File tree

14 files changed

+1155
-0
lines changed

14 files changed

+1155
-0
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ dbs: ## Start databases
2121
-p 27017:27017 \
2222
--network dev \
2323
mongo:5.0 --wiredTigerCacheSizeGB 1.5 --quiet || true
24+
25+
stop-dbs: ## Stop databases
26+
@docker stop $(APPNAME)-redis || true
27+
@docker stop $(APPNAME)-mongodb || true
28+
29+
buildseeds: ## Build populate image
30+
docker build -t seeding -f ./populate/Dockerfile ./populate
31+
32+
populate:
33+
docker run --rm --network dev -v ./populate:/app seeding populate
34+
35+

populate/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.8
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container at /app
8+
COPY ./ /app
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --trusted-host pypi.python.org requests python-dotenv motor
12+
13+
# Run populate_data.py when the container launches
14+
CMD ["python", "populate_data.py"]

populate/populate_data.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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())

populate/seeds/categories.json

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[
2+
{
3+
"_id": "64ff007d22c2c4ed0bd12424",
4+
"name": "Electronics",
5+
"description": "Category for electronic products",
6+
"order": 1,
7+
"selectable": true,
8+
"disabled": false,
9+
"icon": "mdi-laptop",
10+
"color": "#0077FF"
11+
},
12+
{
13+
"_id": "64ff007d22c2c4ed0bd12425",
14+
"name": "Clothing",
15+
"description": "Category for clothing and apparel",
16+
"order": 2,
17+
"selectable": true,
18+
"disabled": false,
19+
"icon": "mdi-hanger",
20+
"color": "#FF5733"
21+
},
22+
{
23+
"_id": "64ff007d22c2c4ed0bd12426",
24+
"name": "Books",
25+
"description": "Category for books and literature",
26+
"order": 3,
27+
"selectable": true,
28+
"disabled": false,
29+
"icon": "mdi-book",
30+
"color": "#008000"
31+
},
32+
{
33+
"_id": "64ff007d22c2c4ed0bd12427",
34+
"name": "Furniture",
35+
"description": "Category for furniture",
36+
"order": 4,
37+
"selectable": true,
38+
"disabled": false,
39+
"icon": "mdi-sofa",
40+
"color": "#A0522D"
41+
},
42+
{
43+
"_id": "64ff007d22c2c4ed0bd12428",
44+
"name": "Toys",
45+
"description": "Category for children's toys",
46+
"order": 5,
47+
"selectable": true,
48+
"disabled": false,
49+
"icon": "mdi-toy-brick",
50+
"color": "#FFD700"
51+
},
52+
{
53+
"_id": "64ff007d22c2c4ed0bd12429",
54+
"name": "Sports Equipment",
55+
"description": "Category for sports equipment",
56+
"order": 6,
57+
"selectable": true,
58+
"disabled": false,
59+
"icon": "mdi-basketball",
60+
"color": "#FF4500"
61+
},
62+
{
63+
"_id": "64ff007d22c2c4ed0bd1242a",
64+
"name": "Beauty Products",
65+
"description": "Category for beauty and skincare",
66+
"order": 7,
67+
"selectable": true,
68+
"disabled": false,
69+
"icon": "mdi-makeup",
70+
"color": "#FF1493"
71+
},
72+
{
73+
"_id": "64ff007d22c2c4ed0bd1242b",
74+
"name": "Home Decor",
75+
"description": "Category for home decoration",
76+
"order": 8,
77+
"selectable": true,
78+
"disabled": false,
79+
"icon": "mdi-lamp",
80+
"color": "#800080"
81+
},
82+
{
83+
"_id": "64ff007d22c2c4ed0bd1242c",
84+
"name": "Jewelry",
85+
"description": "Category for jewelry and accessories",
86+
"order": 9,
87+
"selectable": true,
88+
"disabled": false,
89+
"icon": "mdi-diamond",
90+
"color": "#FF69B4"
91+
},
92+
{
93+
"_id": "64ff007d22c2c4ed0bd1242d",
94+
"name": "Garden Supplies",
95+
"description": "Category for gardening tools",
96+
"order": 10,
97+
"selectable": true,
98+
"disabled": false,
99+
"icon": "mdi-shovel",
100+
"color": "#008000"
101+
}
102+
]

populate/seeds/crontabs.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"_id": "60b93ae4f1e0b4e2a07b6d31",
4+
"name": "Registration 1",
5+
"description": "Description for Registration 1",
6+
"interval": "*/5 * * * *",
7+
"actions": [
8+
{
9+
"actionType": "action1",
10+
"actionData": {
11+
"param1": "value1",
12+
"param2": "value2"
13+
}
14+
},
15+
{
16+
"actionType": "action2",
17+
"actionData": {
18+
"param1": "value1"
19+
}
20+
}
21+
],
22+
"pluginId": "60b93ae4f1e0b4e2a07b6d32",
23+
"disabled": false
24+
},
25+
{
26+
"_id": "60b93ae4f1e0b4e2a07b6d33",
27+
"name": "Registration 2",
28+
"description": "Description for Registration 2",
29+
"interval": "0 0 * * *",
30+
"actions": [
31+
{
32+
"actionType": "action3",
33+
"actionData": {
34+
"param1": "value1",
35+
"param2": "value2"
36+
}
37+
}
38+
],
39+
"pluginId": "60b93ae4f1e0b4e2a07b6d34",
40+
"disabled": true
41+
}
42+
]

populate/seeds/entities.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"_id": "5f84f96c8f3ac2082347f250",
4+
"publicEmail": "entity1@example.com",
5+
"type": 2,
6+
"profile": {
7+
"commonName": "Common Name 1",
8+
"firstName": "First Name 1",
9+
"lastName": "Last Name 1",
10+
"thirdName": "Third Name 1",
11+
"avatar": "avatar1.jpg",
12+
"phones": [
13+
{
14+
"id": "5f84f96c8f3ac2082347f251",
15+
"type": 3,
16+
"phoneNumber": "0329292929",
17+
"description": "Work Phone"
18+
},
19+
{
20+
"id": "5f84f96c8f3ac2082347f252",
21+
"type": 4,
22+
"phoneNumber": "0329292928",
23+
"description": "Personal Phone"
24+
}
25+
]
26+
},
27+
"state": {
28+
"current": 1,
29+
"lastChangedAt": "2023-09-10T00:00:00.000Z"
30+
}
31+
},
32+
{
33+
"_id": "5f84f96c8f3ac2082347f253",
34+
"publicEmail": "entity2@example.com",
35+
"type": 3,
36+
"profile": {
37+
"commonName": "Common Name 2",
38+
"firstName": "First Name 2",
39+
"lastName": "Last Name 2",
40+
"avatar": "avatar2.jpg"
41+
},
42+
"state": {
43+
"current": 1,
44+
"lastChangedAt": "2023-09-11T00:00:00.000Z"
45+
}
46+
}
47+
]

populate/seeds/identities.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[
2+
{
3+
"_id": "5f84f96c8f3ac2082347f250",
4+
"entityId": "5f84f96c8f3ac2082347f250",
5+
"username": "user1",
6+
"displayName": "User 1",
7+
"email": "user1@example.com",
8+
"password": "$argon2i$v=19$m=16,t=2,p=1$SndYMG9BTmZ4TkNMOVhKNQ$1U+dahX+7ipsgBNYZddkmA",
9+
"thirdPartyAuth": "local",
10+
"state": {
11+
"current": 1,
12+
"lastChangedAt": "2023-09-10T00:00:00.000Z"
13+
},
14+
"baseURL": "/",
15+
"roles": [
16+
"role1",
17+
"role2"
18+
],
19+
"hidden": false
20+
},
21+
{
22+
"_id": "5f84f96c8f3ac2082347f252",
23+
"entityId": "5f84f96c8f3ac2082347f253",
24+
"username": "user2",
25+
"displayName": "User 2",
26+
"email": "user2@example.com",
27+
"password": "$argon2i$v=19$m=16,t=2,p=1$SndYMG9BTmZ4TkNMOVhKNQ$48B0wb2yDFE3wiCrZTekmw",
28+
"thirdPartyAuth": "local",
29+
"state": {
30+
"current": 1,
31+
"lastChangedAt": "2023-09-11T00:00:00.000Z"
32+
},
33+
"baseURL": "/",
34+
"roles": [
35+
"role2",
36+
"role3"
37+
],
38+
"hidden": false
39+
}
40+
]

0 commit comments

Comments
 (0)