Exemplo n.º 1
0
def generate_graphs(timestamp):
    # observation sets
    now = datetime.strptime(timestamp, DATETIME_STRING)
    last_day = now - timedelta(days=1)
    last_week = now - timedelta(weeks=1)
    last_month = now - timedelta(weeks=4)
    observation_sets = {
        'last_day': last_day,
        'last_week': last_week,
        'last_month': last_month
    }

    # get datapoints from database
    rows = get_rows('colours')
    hex_list = [f'#{hex}' for hex in [row['hex_value'] for row in rows]]
    temps_count = {row['temperature']: 0 for row in rows}

    # 3x graphs for every reading in last day, week, month
    for string, then in observation_sets.items():
        # fetch data
        sql = 'WHERE timestamp BETWEEN datetime((?)) AND datetime((?))'
        when = (then, now)
        rows = get_rows('observations', rows_sql=sql, args=when)

        # generate bar graph
        times = [row['timestamp'] for row in rows]
        temps = [row['temperature'] for row in rows]

        bar_chart = pygal.Bar(x_label_rotation=20,
                              x_labels_major_count=6,
                              show_minor_x_labels=False,
                              show_legend=False)
        bar_chart.add(
            'Temperature',
            [{
                'value': temp,
                'color': '#' + lookup_colour(find_temp_threshold(temp))
            } for temp in temps])
        bar_chart.x_labels = times
        filename = string + '_bar.svg'
        bar_chart.render_to_file(FILEPATH + filename)

        # generate pie chart
        for temp in temps:
            temp_threshold = find_temp_threshold(temp)
            temps_count[temp_threshold] += 1
        custom_style = Style(colors=(tuple(hex_list)))
        pie_chart = pygal.Pie(inner_radius=0.6, style=custom_style)
        for temp, count in temps_count.items():
            pie_chart.add(str(temp), count)
        filename = string + '_pie.svg'
        pie_chart.render_to_file(FILEPATH + filename)

    return 'Created graphs'
Exemplo n.º 2
0
def find_temp_threshold(temp):
    # find max and min thresholds from external database
    rows = get_rows('colours')

    all_thresholds = [row['temperature'] for row in rows]
    max_threshold = max(all_thresholds)
    min_threshold = min(all_thresholds)

    temp_threshold = int((temp + 15) / 5) * 5 - 15
    if temp_threshold > max_threshold:
        temp_threshold = max_threshold
    if temp_threshold < min_threshold:
        temp_threshold = min_threshold
    return temp_threshold
Exemplo n.º 3
0
# need to import modules from files in parent directory
# https://codeolives.com/2020/01/10/python-reference-module-in-parent-directory/
from pybloom import lookup_colour, find_temp_threshold
import pygal
import db_utils
import os
import sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)

# testing graphing methods
FILEPATH = './tests/test'
rows = db_utils.get_rows('observations')
times = [row['timestamp'] for row in rows]
temps = [row['temperature'] for row in rows]

bar_chart = pygal.Bar(x_label_rotation=20,
                      x_labels_major_count=6,
                      show_minor_x_labels=False,
                      show_legend=False)
bar_chart.add('Temperature',
              [{
                  'value': temp,
                  'color': '#' + lookup_colour(find_temp_threshold(temp))
              } for temp in temps])
bar_chart.x_labels = times
filename = FILEPATH + '_bar.svg'
bar_chart.render_to_file(filename)
'''
# testing methods to access database
Exemplo n.º 4
0
def colours_table():
    rows = get_rows('colours')
    return rows
Exemplo n.º 5
0
def lookup_colour(temperature):
    # lookup table of temperatures to colours in database.sqlite3
    sql = 'WHERE temperature = (?)'
    what = (temperature, )  # tuple with single item
    results = get_rows('colours', 'hex_value', rows_sql=sql, args=what)
    return results[0][0]  # to return the hex value string only