Skip to content

Commit

Permalink
asdfa
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingface0 committed Sep 6, 2023
1 parent 54c33a1 commit ba0f909
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
24 changes: 14 additions & 10 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### DQM^2 Mirror DB === >
import os
import sys
import logging
import psycopg2
import sqlalchemy
from sqlalchemy_utils import database_exists
Expand Down Expand Up @@ -62,29 +63,29 @@ class DQM2MirrorDB:
TB_DESCRIPTION_META_SHORT = "( name, data )"

def __str__(self):
return f"{self.__class__.__name__}: {self.db_str}"
return f"{self.__class__.__name__}: {self.db_uri}"

def __init__(self, log, db=None, server=False):
def __init__(self, log: logging.Logger, db_uri: str = None, server: bool = False):
"""
The server flag will determine if table creation will take place or not, upon
initialization.
"""
self.log = log
self.log.info("\n\n DQM2MirrorDB ===== init ")
self.db_str = db
self.db_uri = db_uri

if not self.db_str:
self.db_str = ":memory:"
if not self.db_uri:
self.db_uri = ":memory:"

self.engine = sqlalchemy.create_engine(
self.db_str,
url=self.db_uri,
poolclass=sqlalchemy.pool.QueuePool,
pool_size=20,
max_overflow=0,
)
if not database_exists(self.engine.url):
raise DatabaseNotFoundError(
f"Database name was not found when connecting to '{self.db_str}'"
f"Database name was not found when connecting to '{self.db_uri}'"
)

self.Session = sessionmaker(bind=self.engine)
Expand Down Expand Up @@ -449,7 +450,8 @@ def format_run_data(self, data):
]
return answer

def make_table_entry(self, data: dict):
def format_table_entry(self, data: dict):
""" """
answer = []
(
_,
Expand All @@ -468,7 +470,7 @@ def make_table_entry(self, data: dict):
timestamp,
_,
) = data

print("!! format_table_entry:\t\t", timestamp)
client = self.get_short_client_name(client)
# Hide part of the hostname for safety reasons
var = hostname.split("-")
Expand Down Expand Up @@ -497,6 +499,8 @@ def make_table_entry(self, data: dict):
),
(cmssw_run, runkey, cmssw_v),
]
print("!! format_table_entry iso:\t", timestamp.isoformat())

return answer

def filter_clients(self, name):
Expand Down Expand Up @@ -526,7 +530,7 @@ def get_timeline_data(
with_ls_only: bool = False,
) -> dict:
runs = self.get(run_start, run_end, bad_only, with_ls_only)
runs_out = [self.make_table_entry(run) for run in runs]
runs_out = [self.format_table_entry(run) for run in runs]

dic = defaultdict(dict)
for run in runs_out:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def testing_database() -> DQM2MirrorDB:
create_database(db_uri)
db = DQM2MirrorDB(
log=dummy_log(),
db=db_uri,
db_uri=db_uri,
server=False,
)

Expand All @@ -62,7 +62,6 @@ def testing_database() -> DQM2MirrorDB:
with db.engine.connect() as cur:
session = db.Session(bind=cur)
for run in runs:
print("!!!", run[-4])
try:
session.execute(
f"""INSERT into runs ({str(db.TB_DESCRIPTION_RUNS_SHORT).replace("[", "").replace("]", "").replace("'", "")}) VALUES ({format_entry_to_db_entry(run, [13])})"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_or_create_db(db_uri: str):
create_database(db_uri)
return DQM2MirrorDB(
log=dummy_log(),
db=db_uri,
db_uri=db_uri,
server=False,
)

Expand Down

0 comments on commit ba0f909

Please sign in to comment.