-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_data.py
More file actions
76 lines (66 loc) · 2.13 KB
/
gen_data.py
File metadata and controls
76 lines (66 loc) · 2.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from datetime import datetime as dt
import random
import os
from faker import Faker
import logging
import pymongo
def gen_test_data():
logging.info('Generating test data')
recs = []
faker = Faker()
events = ['Birth Day', 'Marriage Anniversary']
for i in range(1, 366):
for year in [2001, 2002, 2003]:
fn = faker.first_name()
ln = faker.last_name()
m = f'dgadiraju+{fn.lower()}{i}{year}@gmail.com'
d = dt.strptime(f'{year}{i}', '%Y%j')
e = random.choice(events)
recs.append({
'fn': fn,
'ln': ln,
'm': m,
'month': d.month,
'day': d.day,
'e': e
})
return recs
def load_mongo_coll(recs):
logging.info('Populating Mongo Collection with test data')
mongo_host = os.environ.get('MONGO_HOST')
mongo_port = int(os.environ.get('MONGO_PORT'))
client = pymongo.MongoClient(mongo_host, mongo_port)
db = client['mailer']
coll = db['mails']
coll.insert_many(recs)
def validate_mongo_coll(filter_date=None):
logging.info('Validate Mongo Collection')
if not filter_date:
filter_date = dt.now().date()
month = filter_date.month
day = filter_date.day
mongo_host = os.environ.get('MONGO_HOST')
mongo_port = int(os.environ.get('MONGO_PORT'))
client = pymongo.MongoClient(mongo_host, mongo_port)
db = client['mailer']
coll = db['mails']
for doc in coll.find({'month': month, 'day': day}):
print(doc)
def cleanup_mongo_coll():
logging.info('Cleanup Mongo Collection')
mongo_host = os.environ.get('MONGO_HOST')
mongo_port = int(os.environ.get('MONGO_PORT'))
client = pymongo.MongoClient(mongo_host, mongo_port)
db = client['mailer']
coll = db['mails']
coll.delete_many({})
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s %(asctime)s %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p'
)
recs = gen_test_data()
cleanup_mongo_coll()
load_mongo_coll(recs)
validate_mongo_coll()