-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_queries.py
130 lines (107 loc) · 3.99 KB
/
database_queries.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from database import Database
from datetime import datetime, timezone
from decorators import log_error
@log_error
def insert_job(
timestamp_id: str,
makefile_path: str,
jobscript_path: str,
status: str,
program_id: int,
queue_time: str = None,
start_time: str = None,
finish_time: str = None,
):
"""Insert a job into the database.
Parameters
----------
timestamp_id : str
The timestamp identifier of the job.
makefile_path : str
The path to the makefile.
jobscript_path : str
The path to the jobscript.
status : str
The status of the job.
program_id : int
The program identifier.
queue_time : str, optional
The queue time, by default None
start_time : str, optional
The start time of the job, by default None
finish_time : str, optional
The finish time of the job, by default None
"""
query = f"INSERT INTO jobs (timestamp_id, makefile_path, jobscript_path, status, program_id, queue_time, start_time, finish_time) VALUES ('{timestamp_id}','{makefile_path}','{jobscript_path}','{status}','{program_id}',NULL,NULL,NULL) RETURNING id"
with Database() as db:
result = db.query(query).fetchone()
print(result["id"], end="\n")
@log_error
def insert_program(name: str, makefile_path: str, program_path: str, directory: str):
"""Inserts a program in the program table.
Args:
name (str): Name of the program.
makefile_path (str): Path to the makefile.
program_path (str): Path to the program.
directory (str): Directory of the program.
"""
query = f"INSERT INTO programs (name, makefile_path, program_path, directory) VALUES ('{name}', '{makefile_path}', '{program_path}', '{directory}') RETURNING id"
with Database() as db:
result = db.query(query).fetchone()
print(result["id"], end="\n")
@log_error
def query_programs():
"""Select all programs."""
query = f"SELECT * FROM programs;"
with Database() as db:
programs = db.query(query).fetchall()
print([dict(**program) for program in programs], end="\n")
@log_error
def query_jobs_by_status(status: str):
"""Get all jobs with a specified status.
Args:
status (str): Status the program should have.
"""
query = f"SELECT * FROM jobs WHERE status = '{status}';"
with Database() as db:
jobs = db.query(query).fetchall()
print([dict(**job) for job in jobs], end="\n")
@log_error
def query_jobs_by_tool(toolname: str):
"""Query all jobs using a certain tool.
Args:
toolname (str): Name of the tool to be used.
"""
query = f"SELECT * FROM jobs JOIN programs ON jobs.program_id = programs.id WHERE programs.toolname = '{toolname}';"
with Database() as db:
jobs = db.query(query).fetchall()
print([dict(**job) for job in jobs], end="\n")
@log_error
def query_program_by_name(program_name: str):
"""Checks if a program is already existing in the program table.
Args:
program_name (str): Name of the program to check.
"""
query = f"SELECT * FROM programs WHERE programs.name = '{program_name}';"
with Database() as db:
program = db.query(query).fetchone()
if program:
print(dict(**program), end="\n")
else:
print("", end="\n")
@log_error
def query_job_and_program_by_timestamp(timestamp: str):
"""Gets a join of a job and a program by timestamp.
Args:
timestamp (str): The timestamp to search for.
"""
query = f"SELECT * from 'jobs' JOIN programs ON jobs.program_id = programs.id WHERE jobs.timestamp_id = '{timestamp}';"
with Database() as db:
jobs = db.query(query).fetchall()
print([dict(**job) for job in jobs], end="\n")
@log_error
def query_job(job_id: str):
query = f"SELECT * from 'jobs' WHERE slurm_job_id = {job_id};"
with Database() as db:
jobs = db.query(query).fetchall()
print([dict(**job) for job in jobs], end="\n")