-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
53 lines (41 loc) · 1.83 KB
/
db.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
import psycopg2
import os
from datetime import date
def insert(ids, rep, wrkr, qwt, bttm, dt, tm, st):
#establishing the connection
conn = psycopg2.connect(
database="worker_usage", user='postgres', password=os.environ['DB_PASS'], host=os.environ['DB_IP'], port=os.environ['DB_PORT']
)
print("[LOG]: Connected to remote postgres db")
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
ids, rep = str(ids), str(rep)
wrkr, qwt = str(wrkr), str(qwt)
bttm, dt = str(bttm), str(dt)
tm, st = str(tm), str(st)
if st == 'passed':
query = "INSERT INTO lxd_usage_details values(" + ids + ", '" + rep + "', '" + wrkr + "'," + qwt + "," + bttm + ", '" + dt + "', '" + dt + "', '" + tm + "', '" + st + "');"
else:
query = "INSERT INTO lxd_usage_details(job_id, repo, queue_wait_time_m, logged_after, logged_before, job_state) values(" + \
ids + ", '" + rep + "', " + qwt + ", '" + dt + "', '" + dt + "', '" + st + "');"
print(query)
cursor.execute(query)
conn.commit()
conn.close()
print("[LOG]: DB Connection closed")
def parse_log_input(jid, worker_name, job_bootup, job_started_at, queue_wait_time, job_state):
today = date.today()
repo = 'anup-kodlekere/travis-sample-job'
if job_state == 'queued':
insert(jid, repo, 'NULL', queue_wait_time, 'NULL', today, 'NULL', job_state)
else:
worker_bootup = 0
minute_j1 = job_bootup.find('m')
if minute_j1 != -1:
m = int(job_bootup[1:minute_j1])
s = round(float(job_bootup[minute_j1+1:-1]))
worker_bootup = m * 60 + s
else:
j1s = round(float(job_bootup[1:-1]))
worker_bootup = j1s
insert(jid, repo, worker_name, queue_wait_time, worker_bootup, today, job_started_at, job_state)