-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM1.py.save
More file actions
131 lines (105 loc) · 4.36 KB
/
M1.py.save
File metadata and controls
131 lines (105 loc) · 4.36 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import matplotlib.pyplot as plt
from random import shuffle, randint
from time import sleep
from collections import deque
from helper import METRICS_SOCKETS
import socket
import pickle
# bind each promotion (promo_[0,A,B]) to each bar
# bind each count to the appropriate bar height
BAR_WIDTH = 0.25
BARS_COLOR = ["#64B5F6", "#ef9a9a", "#00796B"]
# we use the term window for grouped bar to contextualize
# this "window" is different from the one provided by pyplot
# number of windows
N_GROUPED_BARS = 4
def autolabel(rects):
# attach a text label above each bar in rects, displaying its height
for rect in rects:
height = rect.get_height()
plt.annotate(
'{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom'
)
# set position of bar on x-axis
xbar1 = np.arange(N_GROUPED_BARS)
xbar2 = [x + BAR_WIDTH for x in xbar1]
xbar3 = [x + BAR_WIDTH for x in xbar2]
def animate():
isset_legend = False
isset_bars_label = False
# BEGIN SOCKET_INIT
sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
# fully qualified domain name
fqdn = socket.getfqdn()
ip_address = socket.gethostbyname(hostname)
port = METRICS_SOCKETS["metric1"]["port"]
print(f"server working on {hostname} {fqdn} with {ip_address}")
server_sckt = (ip_address, port)
print(f"starting up on {server_sckt[0]} port {server_sckt[1]}")
sckt.bind(server_sckt)
sckt.listen(1)
print("waiting for a connection")
# END SOCKET_INIT
# we initialize the dataset that we gonna plot
promotions_count_list = deque(
[
[
["hh:mm:ss-ħħ:µµ:ßß", "promo_0", 0],
["hh:mm:ss-ħħ:µµ:ßß", "promo_A", 0],
["hh:mm:ss-ħħ:µµ:ßß", "promo_B", 0]
]
]*N_GROUPED_BARS,
maxlen=N_GROUPED_BARS
)
try:
# show who connected to us
connection, client_address = sckt.accept()
print("connection from", client_address)
while True:
# listening to incomming data through socket connection
# and integrate them to the graph in realtime
data = connection.recv(1024)
if data:
bar1_heights = [promotions_count_list[nth_win][0][-1] for nth_win in range(N_GROUPED_BARS)]
bar2_heights = [promotions_count_list[nth_win][1][-1] for nth_win in range(N_GROUPED_BARS)]
bar3_heights = [promotions_count_list[nth_win][2][-1] for nth_win in range(N_GROUPED_BARS)]
win_labels = [promotions_count_list[nth_win][0][0] for nth_win in range(N_GROUPED_BARS)]
if isset_bars_label == False:
bar_labels = [promotions_count_list[nth_win][0][0] for nth_win in range(N_GROUPED_BARS)]
unserialised_data = pickle.loads(data)
print("data:", unserialised_data)
promotions_count_list.append(unserialised_data)
rects1 = plt.bar(xbar1, bar1_heights, color=BARS_COLOR[0], width=BAR_WIDTH, edgecolor='white', label='var1')
rects2 = plt.bar(xbar2, bar2_heights, color=BARS_COLOR[1], width=BAR_WIDTH, edgecolor='white', label='var2')
rects3 = plt.bar(xbar3, bar3_heights, color=BARS_COLOR[2], width=BAR_WIDTH, edgecolor='white', label='var3')
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
plt.xlabel("Fenêtres de Traitements", fontweight="bold")
plt.ylabel("Nombres d'Achats", fontweight="bold")
if isset_legend == False:
plt.legend()
isset_legend = True
plt.xticks([r + BAR_WIDTH for r in range(N_GROUPED_BARS)], win_labels)
fig.canvas.draw()
sleep(3)
plt.cla()
else:
# no more data -- quit the loop
print ("no more data.")
# break
finally:
# clean up the connection
# connection.close()
pass
# make the plot
fig = plt.figure()
win = fig.canvas.manager.window
win.after(100, animate)
plt.show()