-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_data.py
34 lines (28 loc) · 1.11 KB
/
fetch_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from pymongo import MongoClient
import json
import os
from bson import ObjectId
from datetime import datetime
# Custom JSON Encoder for handling MongoDB ObjectId and datetime objects
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
if isinstance(o, datetime):
return o.isoformat()
return json.JSONEncoder.default(self, o)
# Connect to MongoDB
client = MongoClient(os.environ['MONGODB_URI'])
db = client.FMD
# Function to fetch and save collection data
def fetch_and_save(collection_name, file_path):
data = list(db[collection_name].find({}))
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as file:
file.write(JSONEncoder().encode(data))
# Fetch and save FMD.Endpoints
fetch_and_save("Endpoints", 'fmd-telemetry/src/assets/Endpoints.json')
# Fetch and save FMD.UserSession
fetch_and_save("UserSession", 'fmd-telemetry/src/assets/UserSession.json')
# Fetch and save FMD.DatabasePruning
fetch_and_save("DatabasePruning", 'fmd-telemetry/src/assets/DatabasePruning.json')