Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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")
Ejemplo n.º 4
0
    # 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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
#   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)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
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
Ejemplo n.º 9
0
                        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")