def start_rcdb(self): """Setup a connection to the RCDB return: an RCDB handle""" if "RCDB_CONNECTION" in os.environ: connection_string = os.environ["RCDB_CONNECTION"] else: connection_string = "mysql://[email protected]/rcdb" # print("Using standard connection string from HallB") try: self._db = RCDBProvider(connection_string) except: print("WARNING: Cannot connect to the RCDB. Will try with data from Cache only.") self._db = None
def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.add_column( 'files', sa.Column('importance', sa.Integer(), server_default='0', nullable=False)) # ### end Alembic commands ### from alembic import context from datetime import datetime from rcdb.provider import RCDBProvider from rcdb.model import ConditionType x_args = context.get_x_argument(as_dictionary=True) # It is very important, if we run it programmatically # we have rcdb_connection. Otherwise connection_string is taken from alembic.ini # (!) don't be confused by replacement of alembic_config["sqlalchemy.url"] in env.py # in this file it reads the value from alembic.ini if "rcdb_connection" in x_args: connection_string = x_args["rcdb_connection"] else: alembic_config = context.config.get_section( context.config.config_ini_section) connection_string = alembic_config["sqlalchemy.url"] # Create RCDBProvider object that connects to DB and provide most of the functions db = RCDBProvider(connection_string, check_version=False) # Create condition type db.create_condition_type("run_start_time", ConditionType.TIME_FIELD, "Run start time") db.create_condition_type("run_end_time", ConditionType.TIME_FIELD, "Run end (or last record) time")
# import RCDB from rcdb.provider import RCDBProvider # connect to DB db = RCDBProvider("mysql://[email protected]/rcdb") # select values with query table = db.select_values(['cdc_gas_pressure'], run_min=41512, run_max=41540) for row in table: (run_number, cdc_gas_pressure) = tuple(row) print("{} {}".format(run_number, cdc_gas_pressure))
from datetime import datetime from rcdb.provider import RCDBProvider from rcdb.model import ConditionType # Create RCDBProvider object that connects to DB and provide most of the functions db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("my_val", ConditionType.INT_FIELD, "This is my value") # Add data to database db.add_condition(1, "my_val", 1000) # Replace previous value db.add_condition(1, "my_val", 2000, replace=True) # Add time information to the db.add_condition(1, "my_val", 2000, datetime(2015, 10, 10, 15, 28, 12, 111111), replace=True) # Get condition from database condition = db.get_condition(1, "my_val") print condition print "value =", condition.value print "name =", condition.name print "time =", condition.time_value
import json from rcdb.provider import RCDBProvider from rcdb.model import ConditionType # Create RCDBProvider provider object and connect it to DB db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("list_data", ConditionType.JSON_FIELD, "Data list") db.create_condition_type("dict_data", ConditionType.JSON_FIELD, "Data dict") list_to_store = [1, 2, 3] dict_to_store = {"x": 1, "y": 2, "z": 3} # Dump values to JSON and save it to DB to run 1 db.add_condition(1, "list_data", json.dumps(list_to_store)) db.add_condition(1, "dict_data", json.dumps(dict_to_store)) # Get condition from database restored_list = json.loads(db.get_condition(1, "list_data").value) restored_dict = json.loads(db.get_condition(1, "dict_data").value) print restored_list print restored_dict print restored_dict["x"] print restored_dict["y"] print restored_dict["z"]
def make_dummy_db(): """ Creates inmemory SQLite database""" # Create in-memory database db = RCDBProvider("sqlite://", check_version=False) destroy_all_create_schema(db) print("Dummy memory database created!") # create conditions types event_count_type = db.create_condition_type("event_count", ConditionType.INT_FIELD, "The event count") data_value_type = db.create_condition_type("data_value", ConditionType.FLOAT_FIELD, "Some data value") tag_type = db.create_condition_type("tag", ConditionType.STRING_FIELD, "Tag... What it means... hm...") # create runs and fill values for i in range(0, 100): run = db.create_run(i) db.add_condition(run, event_count_type, i + 950) # event_count in range 950 - 1049 db.add_condition(i, data_value_type, (i / 100.0) + 1) # data_value in 1 - 2 db.add_condition(run, tag_type, "tag" + str(i)) # Some text data print("Runs filled with data") return db
default=False) parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() print(args) test_flag = args.test log.setLevel(logging.DEBUG if args.verbose else logging.INFO) #DEBUG: print everything brun = args.run_range.split('-')[0] erun = args.run_range.split('-')[1] #connection con_str = os.environ["RCDB_CONNECTION"] \ if "RCDB_CONNECTION" in os.environ.keys() \ else "mysql://pvdb@localhost/pvdb" db = RCDBProvider(con_str) if test_flag: for run_number in range(int(brun), int(erun) + 1): print_conds(db, run_number) else: for run_number in range(int(brun), int(erun) + 1): update_run(db, run_number, "update")
import sys from rcdb.provider import RCDBProvider, destroy_all_create_schema from rcdb.model import ConditionType if len(sys.argv) > 1: # Open database using connection string from command line argument db = RCDBProvider(sys.argv[1]) else: # Create in-memory database db = RCDBProvider("sqlite://", check_version=False) destroy_all_create_schema(db) # Create condition type db.create_condition_type("my_val", ConditionType.INT_FIELD, "This is my value") # create run number 1 db.create_run(1) # Add data to database db.add_condition(1, "my_val", 1000) # Replace previous value db.add_condition(1, "my_val", 2000, replace=True) # Get condition from database condition = db.get_condition(1, "my_val") print(condition) print("value =", condition.value) print("name =", condition.name)
class Cat(object): def __init__(self, name): self.name = name self.mice_eaten = 1230 try: import jsonpickle except ImportError: print "no jsonpickle module installed. It is required for this example" exit(1) if len(sys.argv) > 1: # Open database using connection string from command line argument db = RCDBProvider(sys.argv[1]) else: # Create in-memory database db = RCDBProvider("sqlite://", check_version=False) destroy_all_create_schema(db) # Create run number 1 (in case it doesn't exist) db.create_run(1) # Create condition type db.create_condition_type("cat", ConditionType.JSON_FIELD, "The Cat lives here") # Create a cat and store in in the DB for run 1 cat = Cat('Alice') db.add_condition(1, "cat", jsonpickle.encode(cat))
from datetime import datetime from rcdb.provider import RCDBProvider from rcdb.model import ConditionType # Create RCDBProvider object that connects to DB and provide most of the functions db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("my_val", ConditionType.INT_FIELD, description="This is my value") # Add data to database db.add_condition(1, "my_val", 1000) # Replace previous value db.add_condition(1, "my_val", 2000, replace=True) # Add time information to the db.add_condition(1, "my_val", 2000, datetime(2015, 10, 10, 15, 28, 12, 111111), replace=True) # Get condition from database condition = db.get_condition(1, "my_val") print condition print "value =", condition.value print "name =", condition.name print "time =", condition.time_value # Getting type information ct = db.get_condition_type("my_val") print ct
class Cat(object): def __init__(self, name): self.name = name self.mice_eaten = 1230 try: import jsonpickle except ImportError: print "no jsonpickle modele installed. It is required for this example" exit(1) # Create RCDBProvider provider object and connect it to DB db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("cat", ConditionType.JSON_FIELD, "The Cat lives here") # Create a cat and store in in the DB for run 1 cat = Cat('Alice') db.add_condition(1, "cat", jsonpickle.encode(cat)) # Get condition from database for run 1 condition = db.get_condition(1, "cat") loaded_cat = jsonpickle.decode(condition.value) print "How cat is stored in DB:" print condition.value
import json import sys from rcdb.provider import RCDBProvider, destroy_all_create_schema from rcdb.model import ConditionType if len(sys.argv) > 1: # Open database from command line argument db = RCDBProvider(sys.argv[1]) else: # Create in-memory database db = RCDBProvider("sqlite://", check_version=False) destroy_all_create_schema(db) # Create run (just in case it is not there) db.create_run(1) # Create condition type db.create_condition_type("list_data", ConditionType.JSON_FIELD, "Data list") db.create_condition_type("dict_data", ConditionType.JSON_FIELD, "Data dict") list_to_store = [1, 2, 3] dict_to_store = {"x": 1, "y": 2, "z": 3} # Dump values to JSON and save it to DB to run 1 db.add_condition(1, "list_data", json.dumps(list_to_store)) db.add_condition(1, "dict_data", json.dumps(dict_to_store)) # Get condition from database restored_list = json.loads(db.get_condition(1, "list_data").value) restored_dict = json.loads(db.get_condition(1, "dict_data").value)
class Cat(object): def __init__(self, name): self.name = name self.mice_eaten = 1230 try: import jsonpickle except ImportError: print "no jsonpickle module installed. It is required for this example" exit(1) # Create RCDBProvider provider object and connect it to DB db = RCDBProvider("sqlite:///example.db") # Create condition type db.create_condition_type("cat", ConditionType.JSON_FIELD, "The Cat lives here") # Create a cat and store in in the DB for run 1 cat = Cat('Alice') db.add_condition(1, "cat", jsonpickle.encode(cat)) # Get condition from database for run 1 condition = db.get_condition(1, "cat") loaded_cat = jsonpickle.decode(condition.value) print "How cat is stored in DB:" print condition.value print "Deserialized cat:"
import sys import numpy as np from matplotlib import pyplot as plt from datetime import datetime, date, time, timedelta from matplotlib.backends.backend_pdf import PdfPages import argparse as AG def datetime_from_string(s): # date time format yyyy-mm-dd" f = np.array(s.split('-')).astype(int) return datetime( *f ) # get db db = RCDBProvider("mysql://rcdb@hallddb/rcdb") #--------------------------------------------------------- general parameters ------------- # default run numbers # run1_def = 10331 # spring 2016 # run2_def = 13000 # run1_def = 21968 # fall 2016 # run2_def = 22167 #run1 = 30000 # spring 2017 run1_def = 30278 # spring 2017 run2_def = 31000 start_date_def = '2017-2-1'
""" import rcdb from rcdb.provider import RCDBProvider import sys import numpy as np from matplotlib import pyplot as plt from matplotlib.legend_handler import HandlerLine2D from datetime import datetime, date, time, timedelta from matplotlib.backends.backend_pdf import PdfPages import matplotlib import argparse as AG # get db db = RCDBProvider("mysql://[email protected]/rcdb") #--------------------------------------------------------- general parameters ------------- # default run numbers #run1_def = 10391 # spring 2016 # run2_def = 13000 # run1_def = 21968 # fall 2016 # run2_def = 22167 run1_def = 30274 # spring 2017 #run1_def = 30279 # spring 2017 #run2_def = 32000
# E X A M P L E 1 # 3 Lines to get values!!! from rcdb.provider import RCDBProvider db = RCDBProvider("mysql://[email protected]/rcdb") table = db.select_values(['polarization_angle', 'polarization_direction'], runs=[30641, 30653]) # P R I N T O U T 1 # Print results print(" run_number, polarization_angle, polarization_direction") for row in table: print (row[0], row[1], row[2]) # E X A M P L E 2 # select values description # Default value | Descrition #---------------+------------------------------------ table = db. select_values(val_names=['event_count', 'beam_current'], # [] | List of conditions names to select, empty by default search_str="@is_production and event_count>1000", # "" | Search pattern. run_min=30200, # 0 | minimum run to search/select run_max=30301, # sys.maxsize | maximum run to search/select sort_desc=True, # False | if True result runs will by sorted descendant by run_number, ascendant if False insert_run_number=True, # True | If True the first column of the result will be a run number runs=None) # None | a list of runs to search from. In this case run_min and run_max are not used # Some more remarks # # 1. val_names. If val_names is left empty, run numbers will be selected (assuming that insert_run_number=True by default)
""" Select event_count condition for selected runs """ from rcdb.provider import RCDBProvider db = RCDBProvider("mysql://rcdb@hallddb/rcdb") # get runs runs = db.get_runs(10000, 20000) # All conditions of this run by name. conditions_by_name = runs[0].get_conditions_by_name() print(conditions_by_name.keys()) for run in runs: # Remember that get_condition() function returns Condition object. Call .value to get the value event_count_cnd = run.get_condition('event_count') # get_condition returns None if no such condition is written for tun if event_count_cnd: print(event_count_cnd.value) else: print("no event_count for run", run.number)
# E X A M P L E 1 # 3 Lines to get values!!! from rcdb.provider import RCDBProvider db = RCDBProvider("mysql://[email protected]/rcdb") table = db.select_values(['polarization_angle', 'polarization_direction'], runs=[30641, 30653]) # # # P R I N T O U T 1 # # Print results # print(" run_number, polarization_angle, polarization_direction") for row in table: print row[0], row[1], row[2] # # # # E X A M P L E 2 # # select values description # # # Default value | Descrition # #---------------+------------------------------------ # table = db. select_values(val_names=['event_count', 'beam_current'], # [] | List of conditions names to select, empty by default # search_str="@is_production and event_count>1000", # "" | Search pattern. # run_min=30200, # 0 | minimum run to search/select # run_max=30301, # sys.maxsize | maximum run to search/select # sort_desc=True, # False | if True result runs will by sorted descendant by run_number, ascendant if False # insert_run_number=True, # True | If True the first column of the result will be a run number # runs=None) # None | a list of runs to search from. In this case run_min and run_max are not used # # Some more remarks # # # # 1. val_names. If val_names is left empty, run numbers will be selected (assuming that insert_run_number=True by default)
# Getting connection string connection_string = args.connection if not connection_string: if "RCDB_CONNECTION" in environ.keys(): connection_string = environ["RCDB_CONNECTION"] log.debug("Connection string '{}' taken from RCDB_CONNECTION environment".format(connection_string)) else: print("ERROR. Connection string is not set. " "To connect to database set RCDB_CONNECTION environment variable or set -c or --connection keys") exit(1) else: log.debug("Connection string '{}' taken from arguments".format(connection_string)) # Connect log.debug("Opening database") db = RCDBProvider(connection_string) log.debug("Opened! (But still no data has been transferred)") # CHOOSE ACTION # List names only if args.list_names: log.debug("Listing names only") list_all_condition_types(db) exit(0) # List all condition types with additional info if args.list: log.debug("Listing conditions ") list_all_condition_types(db, True) exit(0)
""" rplot_rcdb3_WB.py python script to plot information in the rcdb, especially integrated triggers vs run number, or time based on Elton Smith's and Mark Daltons scripts. """ import rcdb from rcdb.provider import RCDBProvider import sys import numpy as np import matplotlib as mpl mpl.use('Agg') #this backend is needed for interactive mode from matplotlib import pyplot as plt from datetime import datetime, date, time, timedelta import argparse as AG # get db db = RCDBProvider("mysql://[email protected]/rcdb") #--------------------------------------------------------- general parameters ------------- # default run numbers # run1_def = 10331 # spring 2016 # run2_def = 13000 # run1_def = 21968 # fall 2016 # run2_def = 22167 #run1 = 30000 # spring 2017 #run1_def = 30278 # spring 2017 #run2_def = 31000 #run1 = 40000 # spring 2018