98 lines
3.7 KiB
Python
98 lines
3.7 KiB
Python
"""
|
|
Script to fix InfluxDB schema collision by recreating measurements with consistent data types
|
|
"""
|
|
|
|
from influxdb_client import InfluxDBClient, Point, WritePrecision
|
|
from datetime import datetime, timedelta
|
|
import time
|
|
|
|
def fix_influxdb_schema():
|
|
# InfluxDB configuration
|
|
url = "http://localhost:8086"
|
|
token = "kNFfXEpPQoWrk5Tteowda21Dzv6xD3jY7QHSHHQHb5oYW6VH6mkAgX9ZMjQJkaHHa8FwzmyVFqDG7qqzxN09uQ=="
|
|
org = "smart-intersection-org"
|
|
bucket = "traffic_monitoring"
|
|
|
|
try:
|
|
print("Connecting to InfluxDB...")
|
|
client = InfluxDBClient(url=url, token=token, org=org)
|
|
write_api = client.write_api()
|
|
|
|
print("Clearing potentially problematic measurements...")
|
|
|
|
# Delete recent traffic_light_status and violation_events data to fix schema collision
|
|
delete_api = client.delete_api()
|
|
|
|
# Delete traffic light data from last 24 hours
|
|
start = datetime.utcnow() - timedelta(days=1)
|
|
stop = datetime.utcnow()
|
|
|
|
try:
|
|
delete_api.delete(start, stop, '_measurement="traffic_light_status"', bucket=bucket, org=org)
|
|
print("✅ Cleared traffic_light_status measurement")
|
|
except Exception as e:
|
|
print(f"Note: {e}")
|
|
|
|
try:
|
|
delete_api.delete(start, stop, '_measurement="violation_events"', bucket=bucket, org=org)
|
|
print("✅ Cleared violation_events measurement")
|
|
except Exception as e:
|
|
print(f"Note: {e}")
|
|
|
|
try:
|
|
delete_api.delete(start, stop, '_measurement="device_info"', bucket=bucket, org=org)
|
|
print("✅ Cleared device_info measurement")
|
|
except Exception as e:
|
|
print(f"Note: {e}")
|
|
|
|
print("\nWaiting 3 seconds for deletions to process...")
|
|
time.sleep(3)
|
|
|
|
print("Creating new measurements with consistent data types...")
|
|
|
|
# Device info with consistent types
|
|
device_point = Point("device_info") \
|
|
.tag("device_id", "smart_intersection_001") \
|
|
.tag("camera_id", "camera_001") \
|
|
.field("status", "online") \
|
|
.field("location", "Main Intersection") \
|
|
.field("version", "1.0.0") \
|
|
.field("uptime_seconds", 3600.0) \
|
|
.time(datetime.utcnow())
|
|
|
|
# Traffic light status with consistent types
|
|
traffic_light_point = Point("traffic_light_status") \
|
|
.tag("device_id", "smart_intersection_001") \
|
|
.tag("camera_id", "camera_001") \
|
|
.tag("color", "red") \
|
|
.field("color_numeric", 1) \
|
|
.field("confidence", 0.85) \
|
|
.time(datetime.utcnow())
|
|
|
|
# Violation event with consistent types
|
|
violation_point = Point("violation_events") \
|
|
.tag("device_id", "smart_intersection_001") \
|
|
.tag("camera_id", "camera_001") \
|
|
.tag("violation_type", "red_light_violation") \
|
|
.field("vehicle_id", "test_vehicle_001") \
|
|
.field("count", 1) \
|
|
.field("confidence", 0.95) \
|
|
.time(datetime.utcnow())
|
|
|
|
# Write all points
|
|
points = [device_point, traffic_light_point, violation_point]
|
|
write_api.write(bucket=bucket, org=org, record=points)
|
|
|
|
print("✅ New measurements created with consistent data types")
|
|
print("✅ Schema collision should now be resolved")
|
|
print("\nPlease check your Grafana dashboard now!")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error fixing schema: {e}")
|
|
finally:
|
|
if 'client' in locals():
|
|
client.close()
|
|
|
|
if __name__ == "__main__":
|
|
fix_influxdb_schema()
|