First version of working Pilgatan API (probably buggy)

Rafael
Rafael M. Martins 7 months ago
parent 8fd35bad29
commit cd853a646a
  1. 660
      py/main.py

@ -49,12 +49,13 @@ for hus in DB_NAMES:
TABLE_FAMILIES_SENSORS[hus][table_name][fam].append(sen)
def get_table_cols(fam, typ):
def get_table_cols(hus, fam, typ):
# Get the right sensors for the family
for table_name in TABLE_FAMILIES_SENSORS:
if fam in TABLE_FAMILIES_SENSORS[table_name]:
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.extend([col for col in TABLE_COLS[table_name] if re.match(f'{fam}\.(.*)\.{typ}', col)])
col_names.extend(sen_cols)
return table_name, col_names
return None
@ -76,15 +77,16 @@ def root():
@app.route("/parallel/daily")
def parallel_daily():
# Process parameters
hus = request.args.get('hus', 'Charlie')
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
day = request.args.get('day', '2023-01-01')
# Get the right sensors for the family
table_name, col_names = get_table_cols(fam, typ)
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_NAME)
conn = sqlite3.connect(DB_NAMES[hus])
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}"=?;', [day])
res_all = cur.fetchall()
cur.close()
@ -102,379 +104,389 @@ def parallel_daily():
return dict(sample=sample, myxs=myxs)
# @app.route("/parallel/weekly")
# def parallel_weekly():
# # Process parameters
# fam = request.args.get('family', 'MP1_1')
# typ = request.args.get('type', 'celsius')
# wee = int(request.args.get('week', '1'))
# yea = int(request.args.get('year', '2023'))
@app.route("/parallel/weekly")
def parallel_weekly():
# Process parameters
hus = request.args.get('hus', 'Charlie')
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
wee = int(request.args.get('week', '1'))
yea = int(request.args.get('year', '2023'))
#if not wee or not yea:
# return 'ERROR: You need to at least specify the parameters "week" and "year".'
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)}
#print(weekdays)
# 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)
conn = sqlite3.connect(DB_NAMES[hus])
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?);', list(weekdays.keys()))
res_all = cur.fetchall()
# #if not wee or not yea:
# # return 'ERROR: You need to at least specify the parameters "week" and "year".'
# 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)}
# #print(weekdays)
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
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]})'
myxs[point_id] = 'pos1'
data_point = [point_id, *data_point[2:]]
sample.append(data_point)
# conn = sqlite3.connect(DB_NAME)
# cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?);', list(weekdays.keys()))
# res_all = cur.fetchall()
return dict(sample=sample, myxs=myxs)
# sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# 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]})'
# myxs[point_id] = 'pos1'
# data_point = [point_id, *data_point[2:]]
# sample.append(data_point)
@app.route("/parallel/monthly")
def parallel_monthly():
# Process parameters
hus = request.args.get('hus', 'Charlie')
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
mon = int(request.args.get('month', '1'))
yea = int(request.args.get('year', '2023'))
# return dict(sample=sample, myxs=myxs)
#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)
days = [str(first + timedelta(days=7*x)) for x in range(4)]
# @app.route("/parallel/monthly")
# def parallel_monthly():
# # Process parameters
# fam = request.args.get('family', 'MP1_1')
# typ = request.args.get('type', 'celsius')
# mon = int(request.args.get('month', '1'))
# yea = int(request.args.get('year', '2023'))
# 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)
# #if not wee or not yea:
# # return 'ERROR: You need to at least specify the parameters "week" and "year".'
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)
res_all = cur.fetchall()
print(res_all)
# # first days of the 4 weeks
# first = date(yea, mon, 1)
# days = [str(first + timedelta(days=7*x)) for x in range(4)]
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
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]}'
myxs[point_id] = 'pos1'
data_point = [point_id, *data_point[2:]]
sample.append(data_point)
# conn = sqlite3.connect(DB_NAME)
# query = f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?);'
# print(query)
# cur = conn.execute(query, days)
# res_all = cur.fetchall()
# print(res_all)
return dict(sample=sample, myxs=myxs)
# sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# 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]}'
# myxs[point_id] = 'pos1'
# data_point = [point_id, *data_point[2:]]
# sample.append(data_point)
@app.route("/parallel/yearly")
def parallel_yearly():
# Process parameters
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'))
# return dict(sample=sample, myxs=myxs)
#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)]
# @app.route("/parallel/yearly")
# def parallel_yearly():
# # Process parameters
# fam = request.args.get('family', 'MP1_1')
# typ = request.args.get('type', 'celsius')
# yea = int(request.args.get('year', '2023'))
conn = sqlite3.connect(DB_NAMES[hus])
# #if not wee or not yea:
# # return 'ERROR: You need to at least specify the parameters "week" and "year".'
# 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)
# # first days of each of the 12 months
# days = [str(date(yea, x, 1)) for x in range(1, 13)]
cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?,?,?,?,?,?);', days)
res_all = cur.fetchall()
# conn = sqlite3.connect(DB_NAME)
sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
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
point_id = f'{month_names[month_num]}'
myxs[point_id] = 'pos1'
data_point = [point_id, *data_point[2:]]
sample.append(data_point)
# cur = conn.execute(f'SELECT {sql_col_names} FROM "{table_name}" WHERE "{date_col}" IN (?,?,?,?,?,?,?,?,?,?,?,?);', days)
# res_all = cur.fetchall()
# sample = [["pos1", *[x*10+10 for x in range(len(col_names)-2)]]]
# 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
# point_id = f'{month_names[month_num]}'
# myxs[point_id] = 'pos1'
# data_point = [point_id, *data_point[2:]]
# sample.append(data_point)
# return dict(sample=sample, myxs=myxs)
return dict(sample=sample, myxs=myxs)
# @app.route("/grid/yearly")
# 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'))
@app.route("/grid/yearly")
def grid_yearly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
sen = request.args.get('sensor', 'Temp_MP1_1_Pos1')
typ = request.args.get('type', 'celsius')
yea = int(request.args.get('year', '2023'))
# conn = sqlite3.connect(DB_NAME)
conn = sqlite3.connect(DB_NAMES[hus])
# # Get the columns for the sensor
# table_name, col_name, fam = get_table_col_fam(sen, typ)
# sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# Get the columns for the sensor
table_name, col_name, fam = get_table_col_fam(sen, typ)
sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# 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 "{date_col}" LIKE "{yea}-%"'
query += f' GROUP BY "{date_col}"'
query += ';'
# cur = conn.execute(query)
# res_all = cur.fetchall()
cur = conn.execute(query)
res_all = cur.fetchall()
# out = []
# for i in range(len(res_all[0])):
# out.append([x[i] for x in res_all])
out = []
for i in range(len(res_all[0])):
out.append([x[i] for x in res_all])
# return out
return out
# @app.route("/grid/monthly")
# 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'))
# mon = int(request.args.get('month', '1'))
@app.route("/grid/monthly")
def grid_monthly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
sen = request.args.get('sensor', 'Temp_MP1_1_Pos1')
typ = request.args.get('type', 'celsius')
yea = int(request.args.get('year', '2023'))
mon = int(request.args.get('month', '1'))
# conn = sqlite3.connect(DB_NAME)
conn = sqlite3.connect(DB_NAMES[hus])
# # Get the columns for the sensor
# table_name, col_name, fam = get_table_col_fam(sen, typ)
# sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# Get the columns for the sensor
table_name, col_name, fam = get_table_col_fam(sen, typ)
sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# 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 "{date_col}" LIKE "{yea}-{mon:02d}%"'
query += f'GROUP BY "{date_col}" '
query += ';'
# cur = conn.execute(query)
# res_all = cur.fetchall()
cur = conn.execute(query)
res_all = cur.fetchall()
# out = []
# for i in range(len(res_all[0])):
# out.append([x[i] for x in res_all])
out = []
for i in range(len(res_all[0])):
out.append([x[i] for x in res_all])
# return out
return out
# @app.route("/grid/weekly")
# def grid_weekly():
# # Process parameters
# sen = request.args.get('sensor', 'Temp_MP1_1_Pos1')
# typ = request.args.get('type', 'celsius')
# yea = int(request.args.get('year', '2023'))
# wee = int(request.args.get('week', '1'))
@app.route("/grid/weekly")
def grid_weekly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
sen = request.args.get('sensor', 'Temp_MP1_1_Pos1')
typ = request.args.get('type', 'celsius')
yea = int(request.args.get('year', '2023'))
wee = int(request.args.get('week', '1'))
# conn = sqlite3.connect(DB_NAME)
conn = sqlite3.connect(DB_NAMES[hus])
# 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)}
# #print(weekdays)
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)}
#print(weekdays)
# # Get the columns for the sensor
# table_name, col_name, fam = get_table_col_fam(sen, typ)
# sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# Get the columns for the sensor
table_name, col_name, fam = get_table_col_fam(sen, typ)
sql_col_names = f'"{date_col}","{time_col}","{col_name}"'
# query = f'SELECT {sql_col_names} FROM "{table_name}" '
# query += f'WHERE "{date_col}" IN (?,?,?,?,?,?,?) '
# query += f'GROUP BY "{date_col}" '
# query += ';'
query = f'SELECT {sql_col_names} FROM "{table_name}" '
query += f'WHERE "{date_col}" IN (?,?,?,?,?,?,?) '
query += f'GROUP BY "{date_col}" '
query += ';'
# cur = conn.execute(query, list(weekdays.keys()))
# res_all = cur.fetchall()
cur = conn.execute(query, list(weekdays.keys()))
res_all = cur.fetchall()
# out = []
# for i in range(len(res_all[0])):
# out.append([x[i] for x in res_all])
out = []
for i in range(len(res_all[0])):
out.append([x[i] for x in res_all])
# return out
return out
# @app.route("/horizon/yearly")
# def horizon_yearly():
# # Process parameters
# fam = request.args.get('family', 'MP1_1')
# typ = request.args.get('type', 'celsius')
# yea = int(request.args.get('year', '2023'))
@app.route("/horizon/yearly")
def horizon_yearly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
yea = int(request.args.get('year', '2023'))
# conn = sqlite3.connect(DB_NAME)
conn = sqlite3.connect(DB_NAMES[hus])
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
# out = {}
# out['sensor_names'] = col_names[2:]
# aux_data = {}
# for sensor_name in out['sensor_names']:
# aux_data[sensor_name] = []
# 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 += ';'
# cur = conn.execute(query)
# 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]))
# 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]
# return out
# @app.route("/horizon/monthly")
# 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'))
# mon = int(request.args.get('month', '1'))
# conn = sqlite3.connect(DB_NAME)
# 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:]
aux_data = {}
for sensor_name in out['sensor_names']:
aux_data[sensor_name] = []
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 += ';'
cur = conn.execute(query)
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]))
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]
return out
@app.route("/horizon/monthly")
def horizon_monthly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
yea = int(request.args.get('year', '2023'))
mon = int(request.args.get('month', '1'))
conn = sqlite3.connect(DB_NAMES[hus])
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
# 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}" LIKE "{yea}-%"'
# query += f'AND "{time_col}" LIKE "{hour:02d}:%"'
# query += f' GROUP BY "{date_col}"'
# query += ';'
# cur = conn.execute(query)
# 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]))
# 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]
# return out
# @app.route("/horizon/weekly")
# def horizon_weekly():
# # Process parameters
# 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_NAME)
# 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:]
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}" LIKE "{yea}-%"'
query += f'AND "{time_col}" LIKE "{hour:02d}:%"'
query += f' GROUP BY "{date_col}"'
query += ';'
cur = conn.execute(query)
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]))
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]
return out
@app.route("/horizon/weekly")
def horizon_weekly():
hus = request.args.get('hus', 'Charlie')
# Process parameters
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])
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
# 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)}
# #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 += ';'
# cur = conn.execute(query, list(weekdays.keys()))
# 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]))
# 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]
# return out
# @app.route("/horizon/daily")
# def horizon_daily():
# # Process parameters
# fam = request.args.get('family', 'MP1_1')
# typ = request.args.get('type', 'celsius')
# day = request.args.get('day', '2023-01-01')
# conn = sqlite3.connect(DB_NAME)
# 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)
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)}
#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 += ';'
cur = conn.execute(query, list(weekdays.keys()))
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]))
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]
return out
@app.route("/horizon/daily")
def horizon_daily():
hus = request.args.get('hus', 'Charlie')
# Process parameters
fam = request.args.get('family', 'MP1_1')
typ = request.args.get('type', 'celsius')
day = request.args.get('day', '2023-01-01')
conn = sqlite3.connect(DB_NAMES[hus])
# # Get the right sensors for the family
# table_name, col_names = get_table_cols(fam, typ)
# sql_col_names = ','.join(f'"{x}"' for x in col_names)
# 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 = {}
out['sensor_names'] = col_names[2:]
# query = f'SELECT {sql_col_names} FROM "{table_name}"'
# query += f'WHERE "{date_col}"=? '
# query += ';'
query = f'SELECT {sql_col_names} FROM "{table_name}"'
query += f'WHERE "{date_col}"=? '
query += ';'
# cur = conn.execute(query, [day])
# res_all = cur.fetchall()
cur = conn.execute(query, [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]
# return out
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]
return out

Loading…
Cancel
Save