Beispiel #1
0
def benchmark_conditions_add(cycles, auto_commit=True):
    # Create RCDBProvider object that connects to DB and provide most of the functions
    db = RCDBProvider("sqlite:///example.db")

    # Create condition type
    runs = [db.create_run(1), db.create_run(2)]
    ct = db.create_condition_type("my_val", ConditionType.INT_FIELD,
                                  "This is my value")

    print("Start {} cycles in benchmark_conditions_add".format(cycles))

    # cycle
    for i in range(0, cycles):
        print_cycle(i, cycles, "benchmark_conditions_add")
        db.add_condition(random.choice(runs),
                         ct,
                         random.randrange(0, 1000),
                         replace=True,
                         auto_commit=auto_commit)

    # commit in the end if needed
    if not auto_commit:
        db.session.commit()

    print("Done benchmark_conditions_add")
Beispiel #2
0
def update_conditions(input_data):

    script_start_clock = time.clock()
    script_start_time = time.time()
    last_run = None    

    con_str = os.environ["RCDB_CONNECTION"] \
        if "RCDB_CONNECTION" in os.environ.keys() \
        else "mysql://[email protected]:3306/a-rcdb"

    db = RCDBProvider(con_str)

    with open('%s' % input_data, 'rb') as f:
        for line in f:
            #parse data
           run_num = (line.split(None,2)[0]).rstrip()
           condition_name = (line.split(None,2)[1]).rstrip()
           value = (line.split(None,2)[2]).rstrip()

           if not run_num.isdigit():
               log.warn(Lf("ERROR: wrong format, run should be number! Your input: '{}'", run_num))
               continue

           # check db entry and update
           run = db.get_run(run_num)
           if not run:
               log.warn(Lf("Run '{}' is not found in DB.. skip this run", run_num))
               continue
           else:
               last_run = run_num
               try:
                   cnd_type = db.get_condition_type(condition_name)
                   if not TESTMODE:
                       db.add_condition(run, condition_name, value, True)
                       db.add_log_record("",
                                         "Update condition:'{}'. time: '{}'"
                                         .format(condition_name, 
                                                 datetime.now()), run_num)
                   else:
                       print run_num, condition_name, value
               except Exception as ex:
                   log.warn("ERROR: wrong condition name. Internal exception:\n" + str(ex))

    if not TESTMODE:
        now_clock = time.clock()
        db.add_log_record("",
                          "End of update. Script proc clocks='{}', wall time: '{}', datetime: '{}'"
                          .format(condition_name, 
                                  now_clock - script_start_clock,
                                  time.time() - script_start_time,
                                  datetime.now()), last_run)
Beispiel #3
0
def update_run(run_number):
    # DB Connection
    con_str = os.environ["RCDB_CONNECTION"] \
        if "RCDB_CONNECTION" in os.environ.keys() \
        else "mysql://[email protected]:3306/a-rcdb"

    db = RCDBProvider(con_str)

    run = db.get_run(run_number)
    if not run:
        print("Run %s is not found in DB" % run_number)
        run = db.create_run(run_number)

    conditions = {}
    conditions = db_fix_helper.get_run_end_info_from_data(run_number)

    #Replace with the start from the data file.. there are some cases where we have total time < 0
    #because the run was too short... there is 30sec delay in start script
    #delay was introduced to give enough time to the end run log before a new run log appears
    #run_length = (datetime.strptime(conditions["run_end_time"], "%Y-%m-%d %H:%M:%S") - run.start_time).total_seconds()
    run_length = (
        datetime.strptime(conditions["run_end_time"], "%Y-%m-%d %H:%M:%S") -
        datetime.strptime(conditions["run_start_time"],
                          "%Y-%m-%d %H:%M:%S")).total_seconds()

    event_rate = None
    if float(run_length) > 0 and conditions["event_count"] is not None:
        event_rate = float(conditions["event_count"]) / float(run_length)
    else:
        if not float(run_length) > 0:
            print("ERROR: run_length < 0....skip the update")
            print "Reported start time:", conditions["run_start_time"]
            print "Reported end time:", conditions["run_end_time"]
            print
        if conditions["event_count"] is None:
            print("ERROR: event_count not available")

    if TESTMODE:
        print("Run start time:\t %s" % conditions["run_start_time"])
        print("Run end time:\t %s" % conditions["run_end_time"])
        print("Total Events:\t %s" % conditions["event_count"])
        print("Run length  :\t %s" % run_length)
        print("Event rate  :\t %s" % event_rate)
    else:
        print "Update coda run info", run.number
        run.start_time = conditions["run_start_time"]
        run.end_time = conditions["run_end_time"]
        db.add_condition(run, DefaultConditions.RUN_LENGTH, run_length, True)
        db.add_condition(run, DefaultConditions.IS_VALID_RUN_END,
                         conditions["has_run_end"], True)

        if conditions["event_count"] is not None:
            db.add_condition(run, DefaultConditions.EVENT_COUNT,
                             conditions["event_count"], True)
        if event_rate is not None:
            db.add_condition(run, DefaultConditions.EVENT_RATE, event_rate,
                             True)
        db.session.commit()
def update_slug():

    # get run list to update
    runs=sys.argv[1]
    if "-" in runs:
        brun = runs.split("-")[0]
        erun = runs.split("-")[1]
    else:
        brun = runs
        erun = runs

    #slug number
    slug=sys.argv[2]

    print "Update SLUGNUMBER for run ", runs, " to SLUG = ", slug
    """
    Ask user to enter Y or N (case-insensitive).
    :return: True if the answer is Y.
    :rtype: bool
    """
    answer = ""
    while answer not in ["y", "n"]:
        answer = raw_input("OK to continue [Y/N]? ").lower()
    print answer
    if answer != "y":
        return

    if TESTMODE:
        for runno in range(int(brun), int(erun)+1):
            print runno, find_slug_number(runno)
    else:
        # DB Connection
        con_str = os.environ["RCDB_CONNECTION"] \
            if "RCDB_CONNECTION" in os.environ.keys() \
            else "mysql://[email protected]:3306/a-rcdb"

        db = RCDBProvider(con_str)

        for run_number in range(int(brun), int(erun)+1):
            run = db.get_run(run_number)
            if not run:
                print ("Run %s is not found in DB.. skip this run" % run_number)
                continue
#            slug = find_slug_number(run_number)
            db.add_condition(run, ParityConditions.SLUG, slug, True)
def benchmark_conditions_add(cycles, auto_commit=True):
    # Create RCDBProvider object that connects to DB and provide most of the functions
    db = RCDBProvider("sqlite:///example.db")

    # Create condition type
    runs = [db.create_run(1), db.create_run(2)]
    ct = db.create_condition_type("my_val", ConditionType.INT_FIELD, "This is my value")

    print ("Start {} cycles in benchmark_conditions_add".format(cycles))

    # cycle
    for i in range(0, cycles):
        print_cycle(i, cycles, "benchmark_conditions_add")
        db.add_condition(random.choice(runs), ct, random.randrange(0, 1000),
                         replace=True, auto_commit=auto_commit)

    # commit in the end if needed
    if not auto_commit:
        db.session.commit()

    print ("Done benchmark_conditions_add")
def update_avg_current():

    # get run list to update
    runs = sys.argv[1]
    if "-" in runs:
        brun = runs.split("-")[0]
        erun = runs.split("-")[1]
    else:
        brun = runs
        erun = runs

    print("Update beam_current for run {}".format(runs))

    if TESTMODE:
        for runno in range(int(brun), int(erun) + 1):
            print("{} {}".format(runno, find_avg_current(runno)))
    else:
        # DB Connection
        con_str = os.environ["RCDB_CONNECTION"] \
            if "RCDB_CONNECTION" in os.environ.keys() \
            else "mysql://[email protected]:3306/a-rcdb"

        db = RCDBProvider(con_str)

        for run_number in range(int(brun), int(erun) + 1):
            run = db.get_run(run_number)
            tmp = find_avg_current(run_number)
            if is_number(tmp):
                newCur = float(tmp)
            else:
                newCur = 0.0
            if not run:
                print("Run %s is not found in DB.. skip this run {}".format(
                    run_number))
                continue
            db.add_condition(run, ParityConditions.BEAM_CURRENT, newCur, True)
Beispiel #7
0
def update_arm_flag():

    # get run list to update
    runs = sys.argv[1]
    if "-" in runs:
        brun = runs.split("-")[0]
        erun = runs.split("-")[1]
    else:
        brun = runs
        erun = runs

    #arm_flag number
    arm_flag = sys.argv[2]
    print "Update ARM_FLAG for run ", runs

    if TESTMODE:
        print "Would change arm flag to be ", arm_flag, " for runs ", brun, "-", erun
        for runno in range(int(brun), int(erun) + 1):
            print runno, arm_flag
    else:
        # DB Connection
        con_str = os.environ["RCDB_CONNECTION"] \
            if "RCDB_CONNECTION" in os.environ.keys() \
            else "mysql://[email protected]:3306/a-rcdb"

        db = RCDBProvider(con_str)

        for run_number in range(int(brun), int(erun) + 1):
            run = db.get_run(run_number)
            if not run:
                print("Run %s is not found in DB.. skip this run" % run_number)
                continue


#            arm_flag = find_arm_flag_number(run_number)
            db.add_condition(run, ParityConditions.ARM_FLAG, arm_flag, True)
Beispiel #8
0
def update_run(run_number):
    # DB Connection
    con_str = os.environ["RCDB_CONNECTION"] \
        if "RCDB_CONNECTION" in os.environ.keys() \
        else "mysql://[email protected]:3306/a-rcdb"

    db = RCDBProvider(con_str)

    run = db.get_run(run_number)
    if not run:
        print("Run %s is not found in DB" % run_number)
        sys.exit(1)

    conditions = {}
    conditions = db_fix_helper.get_run_end_info_from_data(run_number)

    run_length = (
        datetime.strptime(conditions["run_end_time"], "%Y-%m-%d %H:%M:%S") -
        run.start_time).total_seconds()
    event_rate = None
    if float(run_length) > 0 and conditions["event_count"] is not None:
        event_rate = float(conditions["event_count"]) / float(run_length)
    else:
        if not float(run_length) > 0:
            print("ERROR: run_length < 0....skip the update\n")
        if conditions["event_count"] is None:
            print("ERROR: event_count not available")

    if TESTMODE:
        print("Run end time:\t %s" % conditions["run_end_time"])
        print("Total Events:\t %s" % conditions["event_count"])
        print("Run length  :\t %s" % run_length)
        print("Event rate  :\t %s" % event_rate)
    else:
        run.end_time = conditions["run_end_time"]
        db.add_condition(run, DefaultConditions.RUN_LENGTH, run_length, True)
        db.add_condition(run, DefaultConditions.IS_VALID_RUN_END,
                         conditions["has_run_end"], True)

        if conditions["event_count"] is not None:
            db.add_condition(run, DefaultConditions.EVENT_COUNT,
                             conditions["event_count"], True)
        if event_rate is not None:
            db.add_condition(run, DefaultConditions.EVENT_RATE, event_rate,
                             True)
        db.session.commit()
Beispiel #9
0
"""
import argparse
import sys
from rcdb import RCDBProvider
from rcdb.model import ConditionType, Run
from rcdb import DefaultConditions

if __name__ == "__main__":
    print sys.argv
    # Get connection string from arguments
    parser = argparse.ArgumentParser(description="This example shows select runs and put them by dates")
    parser.add_argument("connection_string", nargs='?', default="mysql://[email protected]/rcdb")
    args = parser.parse_args()

    # Open DB connection
    db = RCDBProvider(args.connection_string)

    # add two conditions type. If they are already exist this function just does nothing
    db.create_condition_type(DefaultConditions.RUN_START_TIME, ConditionType.TIME_FIELD, "Run start time by DAQ")
    db.create_condition_type(DefaultConditions.RUN_END_TIME, ConditionType.TIME_FIELD, "Run end time by DAQ")

    # get all runs
    runs = db.get_runs(0, sys.maxint)
    for run in runs:
        print (run.number)
        if run.start_time:
            db.add_condition(run, DefaultConditions.RUN_START_TIME, run.start_time)
        if run.end_time:
            db.add_condition(run, DefaultConditions.RUN_END_TIME, run.end_time)