-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-api-log-table.py
More file actions
49 lines (42 loc) · 1.32 KB
/
create-api-log-table.py
File metadata and controls
49 lines (42 loc) · 1.32 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
import psycopg2
import os
credentials = [os.getenv("DB_HOST"), os.getenv("DB_PORT"), os.getenv("DB_USER"), os.getenv("DB_PASSWORD"), os.getenv("DB_NAME")]
def conn(query, vars=()):
try:
conn = psycopg2.connect(
host=credentials[0],
port=credentials[1],
user=credentials[2],
password=credentials[3],
database=credentials[4],
sslmode='require'
)
cursor = conn.cursor()
if len(vars) > 0:
cursor.execute(query,vars)
else:
cursor.execute(query)
conn.commit()
except psycopg2.Error as e:
print('Error:', e)
finally:
if conn:
cursor.close()
conn.close()
create_api_logging_query = """
CREATE TABLE IF NOT EXISTS "[vapi].API_LOGGING_STATS" (
"ID" SERIAL PRIMARY KEY,
"ENDPOINT" VARCHAR(100) NOT NULL,
"METHOD" VARCHAR(100) NOT NULL,
"QUERY_PARAMS" JSONB,
"STATUS_CODE" INTEGER,
"CLIENT_IP" TEXT,
"USER_AGENT" TEXT,
"REQUEST_START" TIMESTAMP NOT NULL,
"REQUEST_END" TIMESTAMP,
"RESPONSE_TIME_MS" DOUBLE PRECISION
);
"""
delete_api_logging_query = 'DROP TABLE IF EXISTS "[vapi].API_LOGGING_STATS" CASCADE'
conn(delete_api_logging_query)
conn(create_api_logging_query)