예제 #1
0
def run_test_versions(f, n_query_tests, versions, selectivity=0):
    query, selection = gen_query(n_tests=n_query_tests,
                                 selectivity=selectivity)
    jit_ops = 0

    for version in versions:
        res, new_jit_ops = run_test(query, version)
        jit_ops = max(jit_ops, new_jit_ops)
        f.write("{},{},{}\n".format(jit_ops, res, selection))
def run_queries():
    print "Running all queries"

    # Set up Astral
    city_name = model.Setting.query.filter_by(name='city').first().value
    city = ast[city_name]
    now = qtime(datetime.now() + time_offset)
    if debug:
        print "Current time: {}".format(now)
    sun = city.sun(date=now.time, local=True)
    sunrise = sun['sunrise'].replace(tzinfo=None)
    sunset = sun['sunset'].replace(tzinfo=None)

    # Evaluate groups and lights in top-down order since parents need to be
    # evaluated first in case their state is referenced by their children.
    root = model.Group.query.filter_by(parent_id=None).first()
    eval_order = traverse_tree(root)
    for e in eval_order:

        # Generate query
        if e.querydata == None or e.querydata == "":
            query = "parent"
        else:
            query = gen_query(e.querydata)

        # Don't evaluate the query if the light is under manual control
        if query == "manual":
            print "'{}' is under manual control; skipping".format(e.name)
            continue # Don't do anything if the light is under manual control

        # Set variables for query evaluation, such as "time", "sunset", etc.
        inputs = {
            "qtime": qtime, # Pass the qtime class for evaluation
            "time": now,
            "date": now.time.date(),
            "dow": now.time.weekday(),
            "year": now.time.year,
            "month": now.time.month,
            "day": now.time.day,
            "sunrise": sunrise,
            "sunset": sunset
        }

        # Also set the parent variable unless this is the root group, which has no parent
        if e.parent != None:
            inputs["parent"] = e.parent.status
        else:
            inputs["parent"] = False

        # Get the result of the previous query
        previous_rule_state = e.rulestatus

        # Evaluate query using no global vars and with local vars from above
        try:
            current_rule_state = eval(query, {}, inputs)
        except StandardError as exception:
            print "'{}' has a bad query ({}: {}); skipping".format(e.name, exception.__class__.__name__, exception)
            continue # Don't do anything if the light has a bad query

        # Print query info for debugging
        if debug >= 2:
            if isinstance(e, model.Light):
                print "Light '{}'".format(e.name)
            else:
                print "Group '{}'".format(e.name)
            print "Query:\t{}".format(query)
            print "Previous rule:\t{}".format(previous_rule_state)
            print "Current rule:\t{}".format(current_rule_state)
            print "Current state:\t{}".format(bool(e.status))

        # For an explanation of this logic, see here:
        # https://github.com/rettigs/cs-senior-capstone/issues/27#issuecomment-194592403
        if bool(e.status) == previous_rule_state and bool(e.status) != current_rule_state:
            if debug >= 2:
                print "New state:\t{}".format(current_rule_state)

            # Send update to light, or update the group in the database
            if isinstance(e, model.Light):
                send_command(e, current_rule_state)
            else:
                e.status = current_rule_state
        else:
            if debug >= 2:
                print "Not changing state"

        # Update the current rule state in the database so it can be used as
        # the previous rule state the next time this function runs
        e.rulestatus = current_rule_state

        model.db.session.commit()
def advanced_getquery():
    d = request.json
    querydata = json.dumps(d['querydata'])
    query = gen_query(querydata)
    return json.dumps(query), 200, {'ContentType': 'application/json'}
예제 #4
0
def run_queries():
    print "Running all queries"

    # Set up Astral
    city_name = model.Setting.query.filter_by(name='city').first().value
    city = ast[city_name]
    now = qtime(datetime.now() + time_offset)
    if debug:
        print "Current time: {}".format(now)
    sun = city.sun(date=now.time, local=True)
    sunrise = sun['sunrise'].replace(tzinfo=None)
    sunset = sun['sunset'].replace(tzinfo=None)

    # Evaluate groups and lights in top-down order since parents need to be
    # evaluated first in case their state is referenced by their children.
    root = model.Group.query.filter_by(parent_id=None).first()
    eval_order = traverse_tree(root)
    for e in eval_order:

        # Generate query
        if e.querydata == None or e.querydata == "":
            query = "parent"
        else:
            query = gen_query(e.querydata)

        # Don't evaluate the query if the light is under manual control
        if query == "manual":
            print "'{}' is under manual control; skipping".format(e.name)
            continue  # Don't do anything if the light is under manual control

        # Set variables for query evaluation, such as "time", "sunset", etc.
        inputs = {
            "qtime": qtime,  # Pass the qtime class for evaluation
            "time": now,
            "date": now.time.date(),
            "dow": now.time.weekday(),
            "year": now.time.year,
            "month": now.time.month,
            "day": now.time.day,
            "sunrise": sunrise,
            "sunset": sunset
        }

        # Also set the parent variable unless this is the root group, which has no parent
        if e.parent != None:
            inputs["parent"] = e.parent.status
        else:
            inputs["parent"] = False

        # Get the result of the previous query
        previous_rule_state = e.rulestatus

        # Evaluate query using no global vars and with local vars from above
        try:
            current_rule_state = eval(query, {}, inputs)
        except StandardError as exception:
            print "'{}' has a bad query ({}: {}); skipping".format(
                e.name, exception.__class__.__name__, exception)
            continue  # Don't do anything if the light has a bad query

        # Print query info for debugging
        if debug >= 2:
            if isinstance(e, model.Light):
                print "Light '{}'".format(e.name)
            else:
                print "Group '{}'".format(e.name)
            print "Query:\t{}".format(query)
            print "Previous rule:\t{}".format(previous_rule_state)
            print "Current rule:\t{}".format(current_rule_state)
            print "Current state:\t{}".format(bool(e.status))

        # For an explanation of this logic, see here:
        # https://github.com/rettigs/cs-senior-capstone/issues/27#issuecomment-194592403
        if bool(e.status) == previous_rule_state and bool(
                e.status) != current_rule_state:
            if debug >= 2:
                print "New state:\t{}".format(current_rule_state)

            # Send update to light, or update the group in the database
            if isinstance(e, model.Light):
                send_command(e, current_rule_state)
            else:
                e.status = current_rule_state
        else:
            if debug >= 2:
                print "Not changing state"

        # Update the current rule state in the database so it can be used as
        # the previous rule state the next time this function runs
        e.rulestatus = current_rule_state

        model.db.session.commit()
예제 #5
0
def advanced_getquery():
    d = request.json
    querydata = json.dumps(d['querydata'])
    query = gen_query(querydata)
    return json.dumps(query), 200, {'ContentType': 'application/json'}
#!/usr/bin/env python

from gen_query import gen_query

data = file("test_query.json").read()

query = gen_query(data)

print query