all grid endpoints ported to index db

Rafael
Rafael M. Martins 7 months ago
parent 11c71ef8b7
commit 131800400d
  1. 49
      py/main.py

@ -287,26 +287,29 @@ 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'))
conn = sqlite3.connect(DB_NAMES[hus])
# Make sure the format is right
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=?'
query += f' GROUP BY Year,Month,Day;'
cur = conn.execute(query)
conn = sqlite3.connect(DB_NAMES[hus])
cur = conn.execute(query, [yea, mon])
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
@ -318,9 +321,7 @@ def grid_weekly():
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_NAMES[hus])
wee = int(request.args.get('week', '1'))
monday = date.fromisocalendar(yea, wee, 1)
weekday_names = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
@ -329,19 +330,25 @@ 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)
query = f'SELECT {sql_col_names} FROM "{table_name}" '
query += f'WHERE "{date_col}" IN (?,?,?,?,?,?,?) '
query += f'GROUP BY "{date_col}" '
query += ';'
# 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 += f' GROUP BY Year,Month,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('-')]
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

Loading…
Cancel
Save