|
|
|
@ -11,16 +11,18 @@ import glob |
|
|
|
|
|
|
|
|
|
DB_NAMES = { |
|
|
|
|
'Charlie': 'Charlie.db', |
|
|
|
|
'Pilgatan': 'Pilgatan.db', |
|
|
|
|
#'Pilgatan': 'Pilgatan.db', |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
table_names = { |
|
|
|
|
'Charlie': ['Hus_Charlie_card_1', 'Hus_Charlie_card_4'], |
|
|
|
|
'Pilgatan': ['Hus_Pilgatan_card_v1', 'Hus_Pilgatan_card_v6'], |
|
|
|
|
#'Pilgatan': ['Hus_Pilgatan_card_v1', 'Hus_Pilgatan_card_v6'], |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
date_col = "Date" |
|
|
|
|
time_col = "Time" |
|
|
|
|
#date_col = "Date" |
|
|
|
|
date_cols = ['Year', 'Month', 'Day'] |
|
|
|
|
#time_col = "Time" |
|
|
|
|
time_cols = ['Hour', 'Minute', 'Second'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Startup |
|
|
|
@ -44,7 +46,7 @@ for hus in DB_NAMES: |
|
|
|
|
TABLE_FAMILIES_SENSORS[hus] = {} |
|
|
|
|
for table_name, col_names in TABLE_COLS[hus].items(): |
|
|
|
|
TABLE_FAMILIES_SENSORS[hus][table_name] = {} |
|
|
|
|
for col_name in col_names[2:]: |
|
|
|
|
for col_name in col_names[6:]: |
|
|
|
|
match = re.search(r'(.*)\.(.*)\..*', col_name) |
|
|
|
|
fam, sen = match[1], match[2] |
|
|
|
|
if fam not in TABLE_FAMILIES_SENSORS[hus][table_name]: |
|
|
|
@ -57,7 +59,7 @@ def get_table_cols(hus, fam, typ): |
|
|
|
|
for table_name in TABLE_FAMILIES_SENSORS[hus]: |
|
|
|
|
if fam in TABLE_FAMILIES_SENSORS[hus][table_name]: |
|
|
|
|
sen_cols = [col for col in TABLE_COLS[hus][table_name] if re.match(f'{fam}\.(.*)\.{typ}', col)] |
|
|
|
|
col_names = [date_col, time_col] |
|
|
|
|
col_names = [*date_cols, *time_cols] |
|
|
|
|
col_names.extend(sen_cols) |
|
|
|
|
return table_name, col_names |
|
|
|
|
return None |
|
|
|
@ -66,7 +68,7 @@ def get_table_cols(hus, fam, typ): |
|
|
|
|
def get_table_col_fam(hus, sen, typ): |
|
|
|
|
for table_name in TABLE_FAMILIES_SENSORS[hus]: |
|
|
|
|
for col in TABLE_COLS[hus][table_name]: |
|
|
|
|
print(table_name, col) |
|
|
|
|
#print(table_name, col) |
|
|
|
|
match = re.match(f'(.*)\.{sen}\.{typ}', col) |
|
|
|
|
if match: |
|
|
|
|
return table_name, col, match[1] |
|
|
|
@ -88,21 +90,28 @@ def parallel_daily(): |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
# Split date |
|
|
|
|
yea, mon, day = day.split('-') |
|
|
|
|
|
|
|
|
|
query = (f'SELECT {sql_col_names} FROM {table_name}' |
|
|
|
|
f' WHERE Year=? AND Month=? AND Day=?' |
|
|
|
|
f' GROUP BY Hour;') |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}"=?;', [day]) |
|
|
|
|
cur = conn.execute(query, [yea, mon, day]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
cur.close() |
|
|
|
|
|
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]] |
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-6)]]] |
|
|
|
|
|
|
|
|
|
myxs = {} |
|
|
|
|
for k, g in itertools.groupby(res_all, lambda x: x[1][:2]): |
|
|
|
|
data_point = next(g) |
|
|
|
|
point_id = f'{data_point[1]}' |
|
|
|
|
|
|
|
|
|
for data_point in res_all: |
|
|
|
|
point_id = ':'.join(data_point[3:6]) |
|
|
|
|
myxs[point_id] = 'pos1' |
|
|
|
|
data_point = [point_id, *data_point[2:]] |
|
|
|
|
data_point = [point_id, *data_point[6:]] |
|
|
|
|
sample.append(data_point) |
|
|
|
|
|
|
|
|
|
return dict(sample=sample, myxs=myxs) |
|
|
|
@ -116,10 +125,15 @@ def parallel_weekly(): |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
wee = int(request.args.get('week', '1')) |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
# Make sure format is right |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
|
|
|
|
|
#if not wee or not yea: |
|
|
|
|
# return 'ERROR: You need to at least specify the parameters "week" and "year".' |
|
|
|
|
|
|
|
|
|
# Find monday from the given week |
|
|
|
|
monday = date.fromisocalendar(yea, wee, 1) |
|
|
|
|
weekday_names = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] |
|
|
|
|
weekdays = {str(monday + timedelta(days=x)):weekday_names[x] for x in range(7)} |
|
|
|
@ -129,19 +143,29 @@ def parallel_weekly(): |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?);', list(weekdays.keys())) |
|
|
|
|
# Complex query to get each day of the week |
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name} WHERE (' |
|
|
|
|
query += 'OR'.join(['(Year=? AND Month=? AND Day=?)'] * len(weekdays)) |
|
|
|
|
query += ') AND Hour=?' |
|
|
|
|
query += ' GROUP BY Day;' |
|
|
|
|
|
|
|
|
|
# Break each date into three components then merge them all (in a flat list) |
|
|
|
|
params = [y for d in list(weekdays.keys()) for y in d.split('-')] |
|
|
|
|
params.append(hou) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, params) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]] |
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-6)]]] |
|
|
|
|
|
|
|
|
|
myxs = {} |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for k, g in itertools.groupby(res_all, lambda x: x[0]): |
|
|
|
|
data_point = next(g) |
|
|
|
|
point_id = f'{weekdays[data_point[0]]} ({data_point[0]})' |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for i, data_point in enumerate(res_all): |
|
|
|
|
wd = list(weekdays.items())[i] |
|
|
|
|
point_id = f'{wd[1]} ({wd[0]})' |
|
|
|
|
myxs[point_id] = 'pos1' |
|
|
|
|
data_point = [point_id, *data_point[2:]] |
|
|
|
|
data_point = [point_id, *data_point[6:]] |
|
|
|
|
sample.append(data_point) |
|
|
|
|
|
|
|
|
|
return dict(sample=sample, myxs=myxs) |
|
|
|
@ -155,34 +179,45 @@ def parallel_monthly(): |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
mon = int(request.args.get('month', '1')) |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
wda = int(request.args.get('weekday', 1)) |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
# Make sure format is right |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
|
|
|
|
|
#if not wee or not yea: |
|
|
|
|
# return 'ERROR: You need to at least specify the parameters "week" and "year".' |
|
|
|
|
|
|
|
|
|
# first days of the 4 weeks |
|
|
|
|
first = date(yea, mon, 1) |
|
|
|
|
first = date(yea, mon, wda) |
|
|
|
|
days = [str(first + timedelta(days=7*x)) for x in range(4)] |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
# Complex query to get each day separately |
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name} WHERE (' |
|
|
|
|
query += 'OR'.join(['(Year=? AND Month=? AND Day=?)'] * len(days)) |
|
|
|
|
query += ') AND Hour=?' |
|
|
|
|
query += ' GROUP BY Day;' |
|
|
|
|
|
|
|
|
|
# Break each date into three components then merge them all (in a flat list) |
|
|
|
|
params = [y for d in days for y in d.split('-')] |
|
|
|
|
params.append(hou) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?);' |
|
|
|
|
print(query) |
|
|
|
|
cur = conn.execute(query, days) |
|
|
|
|
cur = conn.execute(query, params) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
print(res_all) |
|
|
|
|
|
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]] |
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-6)]]] |
|
|
|
|
|
|
|
|
|
myxs = {} |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for k, g in itertools.groupby(res_all, lambda x: x[0]): |
|
|
|
|
data_point = next(g) |
|
|
|
|
point_id = f'{data_point[0]}' |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for data_point in res_all: |
|
|
|
|
point_id = '-'.join(data_point[:3]) |
|
|
|
|
myxs[point_id] = 'pos1' |
|
|
|
|
data_point = [point_id, *data_point[2:]] |
|
|
|
|
data_point = [point_id, *data_point[6:]] |
|
|
|
|
sample.append(data_point) |
|
|
|
|
|
|
|
|
|
return dict(sample=sample, myxs=myxs) |
|
|
|
@ -194,34 +229,36 @@ def parallel_yearly(): |
|
|
|
|
hus = request.args.get('hus', 'Charlie') |
|
|
|
|
fam = request.args.get('family', 'MP1_1') |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
yea = request.args.get('year', '2023') |
|
|
|
|
day = int(request.args.get('day', '1')) |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
# Make sure format is right |
|
|
|
|
day = f'{day:02d}' |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
|
|
|
|
|
#if not wee or not yea: |
|
|
|
|
# return 'ERROR: You need to at least specify the parameters "week" and "year".' |
|
|
|
|
|
|
|
|
|
# first days of each of the 12 months |
|
|
|
|
days = [str(date(yea, x, 1)) for x in range(1, 13)] |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?,?,?,?,?,?);', days) |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(f'SELECT {sql_col_names} FROM {table_name}' |
|
|
|
|
' WHERE Year=? AND Day=? AND Hour=? GROUP BY Month;', [yea, day, hou]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]] |
|
|
|
|
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-6)]]] |
|
|
|
|
|
|
|
|
|
myxs = {} |
|
|
|
|
month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for k, g in itertools.groupby(res_all, lambda x: x[0]): |
|
|
|
|
data_point = next(g) |
|
|
|
|
month_num = int(data_point[0][5:7])-1 |
|
|
|
|
# group by day; take first of each group |
|
|
|
|
for data_point in res_all: |
|
|
|
|
month_num = int(data_point[1])-1 |
|
|
|
|
point_id = f'{month_names[month_num]}' |
|
|
|
|
myxs[point_id] = 'pos1' |
|
|
|
|
data_point = [point_id, *data_point[2:]] |
|
|
|
|
data_point = [point_id, *data_point[6:]] |
|
|
|
|
sample.append(data_point) |
|
|
|
|
|
|
|
|
|
return dict(sample=sample, myxs=myxs) |
|
|
|
@ -233,26 +270,30 @@ def grid_yearly(): |
|
|
|
|
# Process parameters |
|
|
|
|
sen = request.args.get('sensor', 'Temp_MP1_1_Pos1') |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
yea = request.args.get('year', '2023') |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
# Make sure format is right |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
|
|
|
|
|
# Get the columns for the sensor |
|
|
|
|
table_name, col_name, fam = get_table_col_fam(hus, sen, typ) |
|
|
|
|
sql_col_names = f'"{date_col}","{time_col}","{col_name}"' |
|
|
|
|
col_names = [*date_cols, *time_cols, col_name] |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE "{date_col}" LIKE "{yea}-%"' |
|
|
|
|
query += f' GROUP BY "{date_col}"' |
|
|
|
|
query += ';' |
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name}' |
|
|
|
|
query += f' WHERE Year=? AND Hour=?' |
|
|
|
|
query += f' GROUP BY Year,Month,Day;' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query) |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, [yea, hou]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
out = [] |
|
|
|
|
for i in range(len(res_all[0])): |
|
|
|
|
out.append([x[i] for x in res_all]) |
|
|
|
|
|
|
|
|
|
out.append(['-'.join(x[:3]) for x in res_all]) |
|
|
|
|
out.append([':'.join(x[3:6]) for x in res_all]) |
|
|
|
|
out.append([x[6] for x in res_all]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -262,26 +303,31 @@ def grid_monthly(): |
|
|
|
|
# Process parameters |
|
|
|
|
sen = request.args.get('sensor', 'Temp_MP1_1_Pos1') |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
yea = request.args.get('year', '2023') |
|
|
|
|
mon = int(request.args.get('month', '1')) |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
# Make sure format is right |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
mon = f'{mon:02d}' |
|
|
|
|
|
|
|
|
|
# Get the columns for the sensor |
|
|
|
|
table_name, col_name, fam = get_table_col_fam(hus, sen, typ) |
|
|
|
|
sql_col_names = f'"{date_col}","{time_col}","{col_name}"' |
|
|
|
|
col_names = [*date_cols, *time_cols, col_name] |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}" ' |
|
|
|
|
query += f'WHERE "{date_col}" LIKE "{yea}-{mon:02d}%"' |
|
|
|
|
query += f'GROUP BY "{date_col}" ' |
|
|
|
|
query += ';' |
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name}' |
|
|
|
|
query += f' WHERE Year=? AND Month=? AND Hour=?' |
|
|
|
|
query += f' GROUP BY Year,Month,Day;' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query) |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, [yea, mon, hou]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
out = [] |
|
|
|
|
for i in range(len(res_all[0])): |
|
|
|
|
out.append([x[i] for x in res_all]) |
|
|
|
|
out.append(['-'.join(x[:3]) for x in res_all]) |
|
|
|
|
out.append([':'.join(x[3:6]) for x in res_all]) |
|
|
|
|
out.append([x[6] for x in res_all]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
@ -294,8 +340,10 @@ def grid_weekly(): |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
wee = int(request.args.get('week', '1')) |
|
|
|
|
hou = int(request.args.get('hour', '0')) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
# Make sure format is right |
|
|
|
|
hou = f'{hou:02d}' |
|
|
|
|
|
|
|
|
|
monday = date.fromisocalendar(yea, wee, 1) |
|
|
|
|
weekday_names = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] |
|
|
|
@ -304,19 +352,27 @@ def grid_weekly(): |
|
|
|
|
|
|
|
|
|
# Get the columns for the sensor |
|
|
|
|
table_name, col_name, fam = get_table_col_fam(hus, sen, typ) |
|
|
|
|
sql_col_names = f'"{date_col}","{time_col}","{col_name}"' |
|
|
|
|
col_names = [*date_cols, *time_cols, col_name] |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
# Complex query to get each day separately |
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name} WHERE (' |
|
|
|
|
query += 'OR'.join(['(Year=? AND Month=? AND Day=?)'] * len(weekdays)) |
|
|
|
|
query += ') AND Hour=?' |
|
|
|
|
query += f' GROUP BY Year,Month,Day;' |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}" ' |
|
|
|
|
query += f'WHERE "{date_col}" IN (?,?,?,?,?,?,?) ' |
|
|
|
|
query += f'GROUP BY "{date_col}" ' |
|
|
|
|
query += ';' |
|
|
|
|
# Break each date into three components then merge them all (in a flat list) |
|
|
|
|
params = [y for d in list(weekdays.keys()) for y in d.split('-')] |
|
|
|
|
params.append(hou) |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query, list(weekdays.keys())) |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, params) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
out = [] |
|
|
|
|
for i in range(len(res_all[0])): |
|
|
|
|
out.append([x[i] for x in res_all]) |
|
|
|
|
out.append(['-'.join(x[:3]) for x in res_all]) |
|
|
|
|
out.append([':'.join(x[3:6]) for x in res_all]) |
|
|
|
|
out.append([x[6] for x in res_all]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
@ -335,18 +391,20 @@ def horizon_yearly(): |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
# Important: this code is outdated when compared to the other horizon endpoints |
|
|
|
|
|
|
|
|
|
out = {} |
|
|
|
|
out['sensor_names'] = col_names[2:] |
|
|
|
|
out['sensor_names'] = col_names[6:] |
|
|
|
|
|
|
|
|
|
aux_data = {} |
|
|
|
|
for sensor_name in out['sensor_names']: |
|
|
|
|
aux_data[sensor_name] = [] |
|
|
|
|
|
|
|
|
|
for hour in ['00','06','12','18']: |
|
|
|
|
for hour in ['00','06','12','18']: |
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE "{date_col}" LIKE "{yea}-%"' |
|
|
|
|
query += f'AND "{time_col}" LIKE "{hour}:%"' |
|
|
|
|
query += f' GROUP BY "{date_col}"' |
|
|
|
|
query += f' WHERE "Year" = "{yea}"' |
|
|
|
|
query += f' AND "Hour" = "{hour}"' |
|
|
|
|
query += f' GROUP BY "Month", "Day"' |
|
|
|
|
query += ';' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query) |
|
|
|
@ -354,14 +412,14 @@ def horizon_yearly(): |
|
|
|
|
|
|
|
|
|
for row in res_all: |
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
aux_data[sensor_name].append((row[0], row[1], row[i+2])) |
|
|
|
|
aux_data[sensor_name].append((*row[:6], row[i+6])) |
|
|
|
|
|
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
sensor_data_sorted = sorted(aux_data[sensor_name]) |
|
|
|
|
sensor_data_sorted = sorted(aux_data[sensor_name]) |
|
|
|
|
if i == 0: |
|
|
|
|
out['days'] = [x[0] for x in sensor_data_sorted] |
|
|
|
|
out['times'] = [x[1] for x in sensor_data_sorted] |
|
|
|
|
out[sensor_name] = [x[2] for x in sensor_data_sorted] |
|
|
|
|
out['days'] = ['-'.join(x[:3]) for x in sensor_data_sorted] |
|
|
|
|
out['times'] = [':'.join(x[3:6]) for x in sensor_data_sorted] |
|
|
|
|
out[sensor_name] = [x[6] for x in sensor_data_sorted] |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
@ -372,42 +430,39 @@ def horizon_monthly(): |
|
|
|
|
# Process parameters |
|
|
|
|
fam = request.args.get('family', 'MP1_1') |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
yea = request.args.get('year', '2023') |
|
|
|
|
mon = int(request.args.get('month', '1')) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
# Make sure the format is right |
|
|
|
|
mon = f'{mon:02d}' |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
#print(sql_col_names) |
|
|
|
|
|
|
|
|
|
out = {} |
|
|
|
|
out['sensor_names'] = col_names[2:] |
|
|
|
|
|
|
|
|
|
aux_data = {} |
|
|
|
|
for sensor_name in out['sensor_names']: |
|
|
|
|
aux_data[sensor_name] = [] |
|
|
|
|
out['sensor_names'] = col_names[6:] |
|
|
|
|
out['days'] = [] |
|
|
|
|
out['times'] = [] |
|
|
|
|
for s in out['sensor_names']: |
|
|
|
|
out[s] = [] |
|
|
|
|
|
|
|
|
|
for hour in range(24): |
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE "{date_col}" LIKE "{yea}-{mon:02d}-%"' |
|
|
|
|
query += f'AND "{time_col}" LIKE "{hour:02d}:%"' |
|
|
|
|
query += f' GROUP BY "{date_col}"' |
|
|
|
|
query += ';' |
|
|
|
|
query = (f'SELECT {sql_col_names} FROM {table_name}' |
|
|
|
|
f' WHERE Year=? AND Month=?' |
|
|
|
|
f' GROUP BY Day, Hour') |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, [yea, mon]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
for row in res_all: |
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
aux_data[sensor_name].append((row[0], row[1], row[i+2])) |
|
|
|
|
# Important: this only works assuming that res_all is correctly sorted |
|
|
|
|
|
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
sensor_data_sorted = sorted(aux_data[sensor_name]) |
|
|
|
|
if i == 0: |
|
|
|
|
out['days'] = [x[0] for x in sensor_data_sorted] |
|
|
|
|
out['times'] = [x[1] for x in sensor_data_sorted] |
|
|
|
|
out[sensor_name] = [x[2] for x in sensor_data_sorted] |
|
|
|
|
for row in res_all: |
|
|
|
|
out['days'].append('-'.join(row[:3])) |
|
|
|
|
out['times'].append(':'.join(row[3:6])) |
|
|
|
|
for i in range(6, len(row)): |
|
|
|
|
out[out['sensor_names'][i-6]].append(row[i]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
@ -418,9 +473,7 @@ def horizon_weekly(): |
|
|
|
|
fam = request.args.get('family', 'MP1_1') |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
yea = int(request.args.get('year', '2023')) |
|
|
|
|
wee = int(request.args.get('week', '1')) |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
wee = int(request.args.get('week', '1')) |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
@ -432,32 +485,30 @@ def horizon_weekly(): |
|
|
|
|
#print(weekdays) |
|
|
|
|
|
|
|
|
|
out = {} |
|
|
|
|
out['sensor_names'] = col_names[2:] |
|
|
|
|
|
|
|
|
|
aux_data = {} |
|
|
|
|
for sensor_name in out['sensor_names']: |
|
|
|
|
aux_data[sensor_name] = [] |
|
|
|
|
|
|
|
|
|
for hour in range(24): |
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE "{date_col}" IN (?,?,?,?,?,?,?) ' |
|
|
|
|
query += f'AND "{time_col}" LIKE "{hour:02d}:%"' |
|
|
|
|
query += f' GROUP BY "{date_col}"' |
|
|
|
|
query += ';' |
|
|
|
|
out['sensor_names'] = col_names[6:] |
|
|
|
|
out['days'] = [] |
|
|
|
|
out['times'] = [] |
|
|
|
|
for s in out['sensor_names']: |
|
|
|
|
out[s] = [] |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM {table_name} WHERE ' |
|
|
|
|
query += 'OR'.join(['(Year=? AND Month=? AND Day=?)'] * len(weekdays)) |
|
|
|
|
query += ' GROUP BY Year, Month, Day, Hour' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query, list(weekdays.keys())) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
# Break each date into three components then merge them all (in a flat list) |
|
|
|
|
params = [y for d in list(weekdays.keys()) for y in d.split('-')] |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, params) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
for row in res_all: |
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
aux_data[sensor_name].append((row[0], row[1], row[i+2])) |
|
|
|
|
# Important: this only works assuming that res_all is correctly sorted |
|
|
|
|
|
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
sensor_data_sorted = sorted(aux_data[sensor_name]) |
|
|
|
|
if i == 0: |
|
|
|
|
out['days'] = [x[0] for x in sensor_data_sorted] |
|
|
|
|
out['times'] = [x[1] for x in sensor_data_sorted] |
|
|
|
|
out[sensor_name] = [x[2] for x in sensor_data_sorted] |
|
|
|
|
for row in res_all: |
|
|
|
|
out['days'].append('-'.join(row[:3])) |
|
|
|
|
out['times'].append(':'.join(row[3:6])) |
|
|
|
|
for i in range(6, len(row)): |
|
|
|
|
out[out['sensor_names'][i-6]].append(row[i]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
@ -470,31 +521,35 @@ def horizon_daily(): |
|
|
|
|
typ = request.args.get('type', 'celsius') |
|
|
|
|
day = request.args.get('day', '2023-01-01') |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
yea, mon, day = day.split('-') |
|
|
|
|
|
|
|
|
|
# Get the right sensors for the family |
|
|
|
|
table_name, col_names = get_table_cols(hus, fam, typ) |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE "{date_col}"=? ' |
|
|
|
|
query += ';' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query, [day]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
sql_col_names = ','.join(f'"{x}"' for x in col_names) |
|
|
|
|
|
|
|
|
|
out = {} |
|
|
|
|
out['sensor_names'] = col_names[2:] |
|
|
|
|
out['sensor_names'] = col_names[6:] |
|
|
|
|
out['days'] = [] |
|
|
|
|
out['times'] = [] |
|
|
|
|
for s in out['sensor_names']: |
|
|
|
|
out[s] = [] |
|
|
|
|
|
|
|
|
|
query = f'SELECT {sql_col_names} FROM "{table_name}"' |
|
|
|
|
query += f'WHERE Year=? AND Month=? AND Day=?;' |
|
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, [yea, mon, day]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Important: this only works assuming that res_all is correctly sorted |
|
|
|
|
|
|
|
|
|
for row in res_all: |
|
|
|
|
out['days'].append(row[0]) |
|
|
|
|
out['times'].append(row[1]) |
|
|
|
|
for i, s in enumerate(out['sensor_names']): |
|
|
|
|
out[s].append(row[i+2]) |
|
|
|
|
out['days'].append('-'.join(row[:3])) |
|
|
|
|
out['times'].append(':'.join(row[3:6])) |
|
|
|
|
for i in range(6, len(row)): |
|
|
|
|
out[out['sensor_names'][i-6]].append(row[i]) |
|
|
|
|
|
|
|
|
|
return out |
|
|
|
|
|
|
|
|
|