Advertisement
Guest User

Untitled

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