forked from DataTalksClub/mlops-zoomcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_metrics_calculation.py
61 lines (52 loc) · 1.77 KB
/
dummy_metrics_calculation.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
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
import datetime
import time
import random
import logging
import uuid
import pytz
import pandas as pd
import io
import psycopg
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s]: %(message)s")
SEND_TIMEOUT = 10
rand = random.Random()
create_table_statement = """
drop table if exists dummy_metrics;
create table dummy_metrics(
timestamp timestamp,
value1 integer,
value2 varchar,
value3 float
)
"""
def prep_db():
with psycopg.connect("host=localhost port=5432 user=postgres password=example", autocommit=True) as conn:
res = conn.execute("SELECT 1 FROM pg_database WHERE datname='test'")
if len(res.fetchall()) == 0:
conn.execute("create database test;")
with psycopg.connect("host=localhost port=5432 dbname=test user=postgres password=example") as conn:
conn.execute(create_table_statement)
def calculate_dummy_metrics_postgresql(curr):
value1 = rand.randint(0, 1000)
value2 = str(uuid.uuid4())
value3 = rand.random()
curr.execute(
"insert into dummy_metrics(timestamp, value1, value2, value3) values (%s, %s, %s, %s)",
(datetime.datetime.now(pytz.timezone('Europe/London')), value1, value2, value3)
)
def main():
prep_db()
last_send = datetime.datetime.now() - datetime.timedelta(seconds=10)
with psycopg.connect("host=localhost port=5432 dbname=test user=postgres password=example", autocommit=True) as conn:
for i in range(0, 100):
with conn.cursor() as curr:
calculate_dummy_metrics_postgresql(curr)
new_send = datetime.datetime.now()
seconds_elapsed = (new_send - last_send).total_seconds()
if seconds_elapsed < SEND_TIMEOUT:
time.sleep(SEND_TIMEOUT - seconds_elapsed)
while last_send < new_send:
last_send = last_send + datetime.timedelta(seconds=10)
logging.info("data sent")
if __name__ == '__main__':
main()