Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -308,22 +308,27 @@ def plot_ph():
|
|
| 308 |
@app.route('/plot_ph_week', methods=['GET'])
|
| 309 |
def plot_ph_week():
|
| 310 |
try:
|
|
|
|
| 311 |
week_number = request.args.get('week', default=1, type=int)
|
| 312 |
|
|
|
|
| 313 |
if week_number < 1:
|
| 314 |
week_number = 1
|
| 315 |
elif week_number > 30:
|
| 316 |
week_number = 30
|
| 317 |
|
|
|
|
| 318 |
conn = sqlite3.connect('system_data.db')
|
| 319 |
cursor = conn.cursor()
|
| 320 |
|
|
|
|
| 321 |
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='system_data'")
|
| 322 |
table_exists = cursor.fetchone()
|
| 323 |
|
| 324 |
if not table_exists:
|
| 325 |
return jsonify({'status': 'error', 'message': 'Таблица system_data не существует'}), 404
|
| 326 |
|
|
|
|
| 327 |
cursor.execute('''
|
| 328 |
SELECT date_time, ph, ec, dey, onA, onB, onC
|
| 329 |
FROM system_data
|
|
@@ -332,8 +337,10 @@ def plot_ph_week():
|
|
| 332 |
''', (week_number,))
|
| 333 |
rows = cursor.fetchall()
|
| 334 |
|
|
|
|
| 335 |
conn.close()
|
| 336 |
|
|
|
|
| 337 |
if not rows:
|
| 338 |
return jsonify({'status': 'error', 'message': f'Нет данных за {week_number}-ю неделю'}), 404
|
| 339 |
|
|
@@ -349,9 +356,11 @@ def plot_ph_week():
|
|
| 349 |
'days_of_week': [datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S').weekday() + 1 for row in rows] # День недели (1-7)
|
| 350 |
}
|
| 351 |
|
|
|
|
| 352 |
return render_template('plot_ph_week.html', data=data)
|
| 353 |
|
| 354 |
except Exception as e:
|
|
|
|
| 355 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 356 |
|
| 357 |
|
|
|
|
| 308 |
@app.route('/plot_ph_week', methods=['GET'])
|
| 309 |
def plot_ph_week():
|
| 310 |
try:
|
| 311 |
+
# Получаем номер недели из параметров запроса
|
| 312 |
week_number = request.args.get('week', default=1, type=int)
|
| 313 |
|
| 314 |
+
# Ограничиваем диапазон недели от 1 до 30
|
| 315 |
if week_number < 1:
|
| 316 |
week_number = 1
|
| 317 |
elif week_number > 30:
|
| 318 |
week_number = 30
|
| 319 |
|
| 320 |
+
# Подключаемся к базе данных
|
| 321 |
conn = sqlite3.connect('system_data.db')
|
| 322 |
cursor = conn.cursor()
|
| 323 |
|
| 324 |
+
# Проверяем существование таблицы
|
| 325 |
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='system_data'")
|
| 326 |
table_exists = cursor.fetchone()
|
| 327 |
|
| 328 |
if not table_exists:
|
| 329 |
return jsonify({'status': 'error', 'message': 'Таблица system_data не существует'}), 404
|
| 330 |
|
| 331 |
+
# Запрашиваем данные за выбранную неделю
|
| 332 |
cursor.execute('''
|
| 333 |
SELECT date_time, ph, ec, dey, onA, onB, onC
|
| 334 |
FROM system_data
|
|
|
|
| 337 |
''', (week_number,))
|
| 338 |
rows = cursor.fetchall()
|
| 339 |
|
| 340 |
+
# Закрываем соединение с базой
|
| 341 |
conn.close()
|
| 342 |
|
| 343 |
+
# Если данных нет, возвращаем ошибку
|
| 344 |
if not rows:
|
| 345 |
return jsonify({'status': 'error', 'message': f'Нет данных за {week_number}-ю неделю'}), 404
|
| 346 |
|
|
|
|
| 356 |
'days_of_week': [datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S').weekday() + 1 for row in rows] # День недели (1-7)
|
| 357 |
}
|
| 358 |
|
| 359 |
+
# Отправляем данные в HTML-шаблон
|
| 360 |
return render_template('plot_ph_week.html', data=data)
|
| 361 |
|
| 362 |
except Exception as e:
|
| 363 |
+
# В случае ошибки возвращаем сообщение
|
| 364 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 365 |
|
| 366 |
|