-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (57 loc) · 2.33 KB
/
Copy pathapp.py
File metadata and controls
72 lines (57 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import numpy as np
import plotly.graph_objs as go
import plotly.io as pio
from datetime import datetime, timedelta
from anomaly_detection import z_score_anomaly_detection
from data_stream import generate_data_stream
import threading
import time
app = Flask(__name__)
socketio = SocketIO(app)
# Global variable to control the streaming state
streaming = False
def generate_data_stream_continuously():
global streaming
while True:
if streaming:
# Simulate new data points
data_stream = generate_data_stream(100)
anomalies = z_score_anomaly_detection(data_stream)
# Create real-time timestamps
start_time = datetime.now()
time_points = [start_time + timedelta(seconds=i) for i in range(len(data_stream))]
# Create the plot
fig = go.Figure()
fig.add_trace(go.Scatter(x=time_points, y=data_stream, mode='lines', name='Data Stream'))
# Add points for anomalies
anomaly_points = [data_stream[i] for i in range(len(data_stream)) if anomalies[i]]
anomaly_times = [time_points[i] for i in range(len(data_stream)) if anomalies[i]]
fig.add_trace(go.Scatter(x=anomaly_times, y=anomaly_points, mode='markers',
marker=dict(color='red', size=10), name='Anomalies'))
fig.update_layout(title='Real-time Data Stream',
xaxis_title='Time',
yaxis_title='Values in Data Stream')
# Convert the plot to JSON
graph_json = pio.to_json(fig)
# Emit the graph JSON to the client
socketio.emit('update_graph', {'graph_json': graph_json})
# Sleep for a short duration to simulate data streaming
time.sleep(1)
@app.route('/')
def home():
return render_template('index.html')
@socketio.on('start_stream')
def start_stream():
global streaming
streaming = True
@socketio.on('stop_stream')
def stop_stream():
global streaming
streaming = False
if __name__ == '__main__':
# Start the data stream thread
data_thread = threading.Thread(target=generate_data_stream_continuously)
data_thread.start()
socketio.run(app, debug=True)