|
|
|
@ -367,6 +367,8 @@ 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[6:] |
|
|
|
|
|
|
|
|
@ -404,42 +406,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 |
|
|
|
|
|
|
|
|
@ -450,9 +449,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) |
|
|
|
@ -464,32 +461,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] = [] |
|
|
|
|
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' |
|
|
|
|
|
|
|
|
|
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 += ';' |
|
|
|
|
# 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() |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query, list(weekdays.keys())) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
# Important: this only works assuming that res_all is correctly sorted |
|
|
|
|
|
|
|
|
|
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])) |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
@ -502,28 +497,33 @@ 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) |
|
|
|
|
|
|
|
|
|
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 "{date_col}"=? ' |
|
|
|
|
query += ';' |
|
|
|
|
query += f'WHERE Year=? AND Month=? AND Day=?;' |
|
|
|
|
|
|
|
|
|
cur = conn.execute(query, [day]) |
|
|
|
|
conn = sqlite3.connect(DB_NAMES[hus]) |
|
|
|
|
cur = conn.execute(query, [yea, mon, day]) |
|
|
|
|
res_all = cur.fetchall() |
|
|
|
|
|
|
|
|
|
for i, sensor_name in enumerate(out['sensor_names']): |
|
|
|
|
sensor_data_sorted = sorted(res_all) |
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
# Important: this only works assuming that res_all is correctly 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 |
|
|
|
|
|
|
|
|
|