Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. from __future__ import division
  2. import json
  3. from Tkinter import *
  4. import time
  5. import mysql.connector
  6. import math
  7.  
  8.  
  9. class Monitor:
  10. def __init__(self, master):
  11. self.path = 'C://Python35//DatBot2//CoolScripts//'
  12. self.dd_base = self.get_data2()
  13.  
  14. self.tk = master
  15. self.tk.title("Ugdha's DD monitor")
  16. self.frame = Frame(self.tk, height=410, width=900, bg='#DDDDDD')
  17. self.frame.pack()
  18. self.canvas = Canvas(self.frame, height=410, width=700, bg='#FFFFFF')
  19. self.canvas.place(x=200, y=self.frame.winfo_height())
  20.  
  21. self.bbox = [0, 0, 0.1, 0.1]
  22. self.offset = 30, 205
  23. self.scaling_factor = 1, 1
  24. self.ordinates = [0, 0, 0]
  25.  
  26. self.x_axis = self.canvas.create_line(self.offset[0], self.offset[1], 700, self.offset[1])
  27. self.y_axis = self.canvas.create_line(self.offset[0], 0, self.offset[0], 410)
  28.  
  29. self.graphs = []
  30. self.data_sets = []
  31.  
  32. self.metrics = ['Select metric', 'Overall efficiency', 'Filling %', 'Pregnant female %', 'Reproductive %', 'Fatigue %']
  33. self.plotted_metrics = []
  34. self.dropdown_frame = Frame(self.tk)
  35. self.dropdown_frame.place(x=100, y=200, anchor=CENTER)
  36. self.dropdown_buttons_frame = Frame(self.dropdown_frame)
  37. self.dropdown_buttons_frame.pack()
  38. self.add_dropdown = Button(self.dropdown_buttons_frame, text='Add', command=self.create_dropdown)
  39. self.add_dropdown.pack(side=LEFT)
  40. self.rm_dropdown = Button(self.dropdown_buttons_frame, text='Del', command=self.remove_dropdown)
  41. self.rm_dropdown.pack(side=RIGHT)
  42. self.dropdown = []
  43. self.dropdown_widgets = []
  44. self.create_dropdown()
  45.  
  46. self.print_efficiency()
  47.  
  48. def get_data(self):
  49. print 'Fetching data...'
  50. start = time.time()
  51. conn = mysql.connector.connect(host="154.49.211.32", user="wz3xj6_spec", password="specspec", database="wz3xj6_spec")
  52. cursor = conn.cursor()
  53. cursor.execute("""SELECT json FROM DD""")
  54. rows = cursor.fetchall()
  55. data = []
  56. for row in rows:
  57. data.append(json.loads(row[0]))
  58. conn.close()
  59.  
  60. print 'Done in ' + str(time.time()-start) + ' seconds'
  61. return data
  62.  
  63. def get_data2(self):
  64. with open('DDbase.json', 'r') as f:
  65. data = json.load(f)
  66. f.close()
  67. return data
  68.  
  69. def get_plot_bbox(self):
  70. # Extends the bbox to fit all values
  71. for data_set in self.data_sets:
  72. if self.bbox[0] > min([x for x, y in data_set]):
  73. self.bbox[0] = min([x for x, y in data_set])
  74. if self.bbox[1] > min([y for x, y in data_set]):
  75. self.bbox[1] = min([y for x, y in data_set])
  76. if self.bbox[2] < max([x for x, y in data_set]):
  77. self.bbox[2] = max([x for x, y in data_set])
  78. if self.bbox[3] < max([y for x, y in data_set]):
  79. self.bbox[3] = math.ceil(max([y for x, y in data_set]) / (10 ** int(math.log10(max([y for x, y in data_set]))))) * 10 ** int(math.log10(max([y for x, y in data_set])))
  80.  
  81. def get_plot_offset(self):
  82. if self.bbox[1] >= 0 and self.bbox[3] > 0:
  83. new_x = 405
  84. elif self.bbox[1] < 0 and self.bbox[3] <= 0:
  85. new_x = 5
  86. elif self.bbox[1] <= 0 <= self.bbox[3]:
  87. new_x = abs(self.bbox[3])/(abs(self.bbox[1])+abs(self.bbox[3]))*400+5
  88. else:
  89. raise Exception('Ymin has to be smaller than Ymax')
  90.  
  91. self.offset = 30, new_x
  92.  
  93. def get_plot_scaling_factor(self):
  94. self.scaling_factor = 700 / abs(self.bbox[0] - self.bbox[2]), -400 / abs(self.bbox[1] - self.bbox[3])
  95.  
  96. def delete_graphs(self, full_reset=False):
  97. for graph in self.graphs:
  98. while graph:
  99. self.canvas.delete(graph[0])
  100. del graph[0]
  101. if full_reset:
  102. self.bbox = [0, -1, 1, 1]
  103. self.offset = 30, 205
  104. self.scaling_factor = 1, 1
  105.  
  106. def plot(self, data, width=2, bbox=None):
  107. self.data_sets.append(data)
  108. self.actual_plot(width, bbox)
  109.  
  110. def actual_plot(self, width, bbox=None):
  111. self.get_plot_bbox()
  112. self.get_plot_offset()
  113. self.get_plot_scaling_factor()
  114.  
  115. bbox = self.bbox if bbox is None else bbox
  116.  
  117. print '[DEBUG] bbox : ' + str(bbox)
  118. print '[DEBUG] offset : ' + str(self.offset)
  119. print '[DEBUG] scaling factor : ' + str(self.scaling_factor)
  120.  
  121. self.canvas.coords(self.x_axis, (self.offset[0], self.offset[1], 700+self.offset[0], self.offset[1]))
  122.  
  123. while self.ordinates:
  124. self.canvas.delete(self.ordinates[0])
  125. del self.ordinates[0]
  126. self.ordinates = [
  127. self.canvas.create_text((self.offset[0]/2, 8), text=str(bbox[3])),
  128. self.canvas.create_text((self.offset[0]/2, 402), text=str(bbox[1]))
  129. ]
  130. if self.offset[1] != 5 and self.offset[1] != 405:
  131. self.ordinates.append(self.canvas.create_text((self.offset[0]/2, self.offset[1]), text='0'))
  132.  
  133. self.delete_graphs()
  134. colors = ['blue', 'red', 'orange', 'green', 'dark grey', 'pink']
  135. for data_set in self.data_sets:
  136. color = colors[self.data_sets.index(data_set)]
  137. graph = []
  138. for i in range(len(data_set)-1):
  139. x, y, x_, y_ = data_set[i][0], data_set[i][1], data_set[i+1][0], data_set[i+1][1]
  140. graph.append(self.canvas.create_line(self.scaling_factor[0]*x+self.offset[0], self.scaling_factor[1]*y+self.offset[1], self.scaling_factor[0]*x_+self.offset[0], self.scaling_factor[1]*y_+self.offset[1], width=width, fill=color))
  141. time.sleep(0.005)
  142. self.tk.update()
  143. self.graphs.append(graph)
  144.  
  145. def create_dropdown(self):
  146. var = StringVar(self.frame)
  147. var.set("Select metric")
  148. w = apply(OptionMenu, (self.dropdown_frame, var) + tuple(self.metrics))
  149. w.bind('<Configure>', self.dropdown_callback)
  150. w.pack(anchor=CENTER)
  151. self.dropdown.append(var)
  152. self.dropdown_widgets.append(w)
  153.  
  154. def remove_dropdown(self):
  155. del self.dropdown[-1]
  156. self.dropdown_widgets[-1].destroy()
  157. del self.dropdown_widgets[-1]
  158. self.dropdown_callback('lel')
  159.  
  160. def get_metric(self, metric, single_dd_base):
  161. if metric == 'Filling %':
  162. nb_active = sum([1 if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex') else 0 for dd in single_dd_base.values()])
  163. return round(nb_active*100/250, 1)
  164. elif metric == 'Reproductive %':
  165. nb_repro = sum([1 if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex' and dd['repro']) else 0 for dd in single_dd_base.values()])
  166. return round(nb_repro * 100 / 250, 1)
  167. elif metric == 'Pregnant female %':
  168. nb_female = sum([1 if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex' and dd['sex'] == 'female') else 0 for dd in single_dd_base.values()])
  169. nb_preg = sum([1 if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex' and dd['pregnant']) else 0 for dd in single_dd_base.values()])
  170. return round(nb_preg * 100 / nb_female, 1)
  171. elif metric == 'Overall efficiency':
  172. filling = self.get_metric('Filling %', single_dd_base)/100
  173. f_preg = self.get_metric('Pregnant female %', single_dd_base)/100
  174. repro = self.get_metric('Reproductive %', single_dd_base)/100
  175. return round(100*filling*f_preg*(0.71 + repro * 0.29), 1)
  176. elif metric == 'Fatigue %':
  177. fatigues = [dd['fatigue'] for dd in single_dd_base.values() if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex')]
  178. return round(sum(fatigues)/len(fatigues)*100 / 240, 1)
  179.  
  180. def print_efficiency(self):
  181. print 'Filling : ' + str(self.get_metric('Filling %', self.dd_base[-1])) + '%'
  182. print 'Pregnant females : ' + str(self.get_metric('Pregnant female %', self.dd_base[-1])) + '%'
  183. print 'Reproductive : ' + str(self.get_metric('Reproductive %', self.dd_base[-1])) + '%'
  184. eff = self.get_metric('Overall efficiency', self.dd_base[-1])
  185. print ''
  186. print 'Overall efficiency : ' + str(eff) + '%'
  187. print 'Estimated revenue : ' + str(int(eff * 1983000 / 100)) + ' k/day, ' + str(
  188. int(eff * 1983000 * 30 / 100)) + ' k/month'
  189.  
  190. def display_metric(self, metric):
  191. data = []
  192. for mesure_set in self.dd_base:
  193. data.append((self.dd_base.index(mesure_set), self.get_metric(metric, mesure_set)))
  194. self.plot(data)
  195.  
  196. def dropdown_callback(self, event):
  197. print "Dropdown update detected"
  198. new_metrics = [dd.get() for dd in self.dropdown]
  199. if [metric for metric in self.plotted_metrics if metric != 'Select metric'] != [metric for metric in new_metrics if metric != 'Select metric']:
  200. self.plotted_metrics = new_metrics
  201. self.data_sets = []
  202. for dd in self.dropdown:
  203. if dd.get() != 'Select metric':
  204. data = []
  205. for mesure_set in self.dd_base:
  206. data.append((self.dd_base.index(mesure_set), self.get_metric(dd.get(), mesure_set)))
  207.  
  208. if data:
  209. self.data_sets.append(data)
  210. self.delete_graphs(full_reset=True)
  211. self.actual_plot(width=2)
  212.  
  213. tk = Tk()
  214. M = Monitor(tk)
  215. tk.mainloop()
  216.  
  217. __author__ = 'Alexis'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement