Commit 151c623a authored by Carl Schaffer's avatar Carl Schaffer
Browse files

adding interface to GDBS

parent 39b0ce4e
import logging
import sqlalchemy as sa
_logger = logging.getLogger(__name__)
_gdbs_url = 'postgresql://readuser:logit@dockertest:4321/gregor'
try:
gdbs_engine = sa.create_engine(_gdbs_url)
gdbs_engine.connect()
except:
_logger.warning(
f'Could not connect to GREGOR database through {_gdbs_url}, are you on the KIS network and is the ssh tunnel running?')
import logging
from datetime import datetime, timedelta
import sqlalchemy as sa
_logger = logging.getLogger(__name__)
_gdbs_url = 'postgresql://readuser:logit@dockertest:4321/gregor'
try:
gdbs_engine = sa.create_engine(_gdbs_url)
gdbs_engine.connect()
except:
_logger.warning(
f'Could not connect to GREGOR database through {_gdbs_url}, are you on the KIS network and is the ssh tunnel running?')
raise IOError('GDBS is not online')
def query_gdbs(table, datetime, engine=gdbs_engine):
query = f"""
SELECT value, timestamp, ttl from {table} where timestamp < '{str(datetime)}' order by timestamp desc limit 1
"""
res = engine.execute(query)
value, timestamp, ttl = res.fetchall()[0]
age = datetime - timestamp
is_valid = age < timedelta(seconds=ttl)
return value, is_valid, age
if __name__ == '__main__':
engine = gdbs_engine
print(query_gdbs('xcen', datetime.now(), engine=engine))
print(query_gdbs('ycen', datetime.now(), engine=engine))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment