This commit is contained in:
ferdzo
2024-10-14 12:14:49 +02:00
parent a825d27170
commit 89b06e0a60
2 changed files with 3 additions and 66 deletions

2
.gitignore vendored
View File

@@ -4,4 +4,6 @@ db.sqlite3
demo.db
demo.db-shm
demo.db-wal
/iotDashboard/demo1.db
__pycache__/
iotDashboard/db_create.py

View File

@@ -1,4 +1,5 @@
import redis
import json
from django.db import connections
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
@@ -12,10 +13,6 @@ redis_client = redis.StrictRedis(host='10.10.0.1', port=6379, db=0)
def fetch_gpt_data():
return redis_client.get("gpt").decode("utf-8").strip('b"').replace('\\"', '"').replace("\\n", "").replace("\\","").replace("\\u00b0", "°")
import json # Add this at the top of the file if not already present
def chart(request):
# Fetch devices and their related sensors
devices = Device.objects.prefetch_related('sensors__type').all() # Prefetch related sensors and their types
@@ -30,15 +27,12 @@ def chart(request):
]
try:
# Fetch GPT data from Redis and parse it
gpt_data = fetch_gpt_data()
gpt = json.loads(gpt_data)
except (redis.RedisError, json.JSONDecodeError) as e:
# Handle errors if fetching GPT data or JSON parsing fails
gpt = {"summary": "Error fetching data", "recommendations": {}}
print(f"Error fetching or parsing GPT data: {e}")
# Serialize devices_json to JSON string for use in the template
context = {
'devices_json': json.dumps(devices_json), # Convert to a JSON string
'gpt': gpt
@@ -46,65 +40,6 @@ def chart(request):
return render(request, 'chart.html', context)
# Fetch sensor data (AJAX)
# def fetch_device_data(request):
# device_name = request.GET.get('device', 'Livingroom')
# start_date = request.GET.get('start_date')
# end_date = request.GET.get('end_date')
#
# # Log the parameters to ensure they are correct
# print("Device Name:", device_name)
# print("Start Date:", start_date)
# print("End Date:", end_date)
#
# # Get the specific device by name
# device = get_object_or_404(Device, name=device_name)
#
# # Initialize the results dictionary to store sensor data
# results = {}
#
# # Prepare SQL query and parameters for the specific sensor type
# query = """
# SELECT time, metric, value
# FROM sensor_readings
# WHERE device_name = %s
# """
# params = [device.name]
#
# # Add time filtering to the query
# if start_date:
# query += " AND time >= %s::timestamptz"
# params.append(start_date)
#
# if end_date:
# query += " AND time <= %s::timestamptz"
# params.append(end_date)
#
# # Log the final query and params
# print("Final Query:", query)
# print("Params Before Execution:", params)
#
# # Fetch data from the database
# with connections["data"].cursor() as cursor:
# cursor.execute(query, params)
# rows = cursor.fetchall()
#
# # Process the results and group them by sensor type (metric)
# for row in rows:
# time, metric, value = row
# formatted_time = time.strftime('%Y-%m-%d %H:%M:%S')
#
# if metric not in results:
# results[metric] = {
# 'times': [],
# 'values': []
# }
# results[metric]['times'].append(formatted_time)
# results[metric]['values'].append(value)
#
# return JsonResponse(results)
def fetch_device_data(request):
device_name = request.GET.get('device', 'Livingroom')
sensor_name = request.GET.get('sensor') # This will be the actual sensor name