Exemplo n.º 1
0
def test_metric():
    conn = Model.setup_db()
    Metric.create_table(conn)
    metric = Metric.create(conn,
            created_at = datetime.now(),
            name = "mood",
            description = "On a scale of 1 to 10, 10 being best, rate your mood."
            )
    assert metric.id > 0

    MetricData.create_table(conn)

    p1 = MetricData.create(conn,
            created_at = datetime.now(),
            value = 7,
            metric_id = metric.id
            )
    assert p1.id > 0

    p2 = MetricData.create(conn,
            created_at = datetime.now(),
            value = 8,
            metric_id = metric.id
            )
    assert p2.id > 0

    latest = metric.latest(conn)
    assert latest.value == 8
    assert latest.id == p2.id

    datetimes, values = metric.ts(conn)

    assert values == [7, 8]
Exemplo n.º 2
0
def ts_command(m=-1):  # The metric id to print data for.
    c = conn()
    if m > 0:
        metric = Metric.get(c, m)
        datetimes, values = metric.ts(c)
        for i, value in enumerate(values):
            print "%s: %s" % (datetimes[i], value)
Exemplo n.º 3
0
def due_command():
    c = ado.commands.conn()
    for survey in Survey.all_due(c):
        take_survey(survey.id)
    for metric in Metric.all_due(c):
        record_metric(metric.id)
    for recipe in Recipe.all_due(c):
        ado.commands.recipe.do_command(recipe.id)
Exemplo n.º 4
0
def record_metric(m, value="None"):
    c = ado.commands.conn()
    if m < 0:
        Metric.printall(c)
        raw_m = ado.commands.clean_input("Choose a metric number: ")
        if raw_m:
            m = int(raw_m)
        else:
            sys.stderr.write("No metric chosen.\n")
            sys.exit(1)

    if value == "None":
        metric = Metric.get(c, m)
        print "Record Metric %s) %s" % (metric.id, metric.name)
        print metric.description
        value = float(ado.commands.clean_input("> "))

    md = MetricData.create(c, metric_id=m, value=value, created_at=datetime.now())
    print md.display_line()
Exemplo n.º 5
0
def values_command(m=-1, s=-1):  # The metric id to print data for.  # The survey id to print data for.
    """
    Returns space-separated data for a metric, or longer form data for a survey.
    
    Metric output can be piped to spark. https://github.com/holman/spark
    """
    c = conn()
    if m > 0:
        metric = Metric.get(c, m)
        datetimes, values = metric.ts(c)
        print " ".join("%s" % v for v in values)
    elif s > 0:
        survey = Survey.get(c, s)
        for row in survey.data(c):
            print row["created_at"]
            print row["value"]
            print ""
Exemplo n.º 6
0
def ok_command(debug=False):
    c = ado.commands.conn()
    surveys_due = bool(Survey.all_due(c))
    metrics_due = bool(Metric.all_due(c))
    recipes_due = bool(Recipe.all_due(c))

    if debug:
        print "Are surveys due?", surveys_due
        print "Are metrics due?", metrics_due
        print "Are recipes due?", recipes_due

    ok = (not surveys_due) and (not metrics_due) and (not recipes_due)
    if ok:
        if debug:
            print "nothing is due"
        sys.exit(0)
    else:
        print "something is due"
        sys.exit(1)
Exemplo n.º 7
0
def create_metric(name, frequency, description):
    c = ado.commands.conn()
    metric = Metric.create(c, name=name, frequency=frequency, description=description, created_at=datetime.now())
    print metric.id
Exemplo n.º 8
0
from ado.model import Model
from ado.metric import Metric
from ado.commands import db_filepath

conn = Model.setup_db(db_filepath)

with open("metric-data.txt", "w") as f:
    f.write("datetime\tmetric\tvalue\n")
    for metric in Metric.all(conn):
        datetimes, values = metric.ts(conn)
        for i, dt in enumerate(datetimes):
            timestamp = dt.strftime("%Y-%m-%d %H:%M:%S")
            f.write("%s\t%s\t%s\n" % (timestamp, metric.name, values[i]))