Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- import json
- from Tkinter import *
- import time
- import mysql.connector
- import math
- class Monitor:
- def __init__(self, master):
- self.path = 'C://Python35//DatBot2//CoolScripts//'
- self.dd_base = self.get_data2()
- self.tk = master
- self.tk.title("Ugdha's DD monitor")
- self.frame = Frame(self.tk, height=410, width=900, bg='#DDDDDD')
- self.frame.pack()
- self.canvas = Canvas(self.frame, height=410, width=700, bg='#FFFFFF')
- self.canvas.place(x=200, y=self.frame.winfo_height())
- self.bbox = [0, 0, 0.1, 0.1]
- self.offset = 30, 205
- self.scaling_factor = 1, 1
- self.ordinates = [0, 0, 0]
- self.x_axis = self.canvas.create_line(self.offset[0], self.offset[1], 700, self.offset[1])
- self.y_axis = self.canvas.create_line(self.offset[0], 0, self.offset[0], 410)
- self.graphs = []
- self.data_sets = []
- self.metrics = ['Select metric', 'Overall efficiency', 'Filling %', 'Pregnant female %', 'Reproductive %', 'Fatigue %']
- self.plotted_metrics = []
- self.dropdown_frame = Frame(self.tk)
- self.dropdown_frame.place(x=100, y=200, anchor=CENTER)
- self.dropdown_buttons_frame = Frame(self.dropdown_frame)
- self.dropdown_buttons_frame.pack()
- self.add_dropdown = Button(self.dropdown_buttons_frame, text='Add', command=self.create_dropdown)
- self.add_dropdown.pack(side=LEFT)
- self.rm_dropdown = Button(self.dropdown_buttons_frame, text='Del', command=self.remove_dropdown)
- self.rm_dropdown.pack(side=RIGHT)
- self.dropdown = []
- self.dropdown_widgets = []
- self.create_dropdown()
- self.print_efficiency()
- def get_data(self):
- print 'Fetching data...'
- start = time.time()
- conn = mysql.connector.connect(host="154.49.211.32", user="wz3xj6_spec", password="specspec", database="wz3xj6_spec")
- cursor = conn.cursor()
- cursor.execute("""SELECT json FROM DD""")
- rows = cursor.fetchall()
- data = []
- for row in rows:
- data.append(json.loads(row[0]))
- conn.close()
- print 'Done in ' + str(time.time()-start) + ' seconds'
- return data
- def get_data2(self):
- with open('DDbase.json', 'r') as f:
- data = json.load(f)
- f.close()
- return data
- def get_plot_bbox(self):
- # Extends the bbox to fit all values
- for data_set in self.data_sets:
- if self.bbox[0] > min([x for x, y in data_set]):
- self.bbox[0] = min([x for x, y in data_set])
- if self.bbox[1] > min([y for x, y in data_set]):
- self.bbox[1] = min([y for x, y in data_set])
- if self.bbox[2] < max([x for x, y in data_set]):
- self.bbox[2] = max([x for x, y in data_set])
- if self.bbox[3] < max([y for x, y in data_set]):
- 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])))
- def get_plot_offset(self):
- if self.bbox[1] >= 0 and self.bbox[3] > 0:
- new_x = 405
- elif self.bbox[1] < 0 and self.bbox[3] <= 0:
- new_x = 5
- elif self.bbox[1] <= 0 <= self.bbox[3]:
- new_x = abs(self.bbox[3])/(abs(self.bbox[1])+abs(self.bbox[3]))*400+5
- else:
- raise Exception('Ymin has to be smaller than Ymax')
- self.offset = 30, new_x
- def get_plot_scaling_factor(self):
- self.scaling_factor = 700 / abs(self.bbox[0] - self.bbox[2]), -400 / abs(self.bbox[1] - self.bbox[3])
- def delete_graphs(self, full_reset=False):
- for graph in self.graphs:
- while graph:
- self.canvas.delete(graph[0])
- del graph[0]
- if full_reset:
- self.bbox = [0, -1, 1, 1]
- self.offset = 30, 205
- self.scaling_factor = 1, 1
- def plot(self, data, width=2, bbox=None):
- self.data_sets.append(data)
- self.actual_plot(width, bbox)
- def actual_plot(self, width, bbox=None):
- self.get_plot_bbox()
- self.get_plot_offset()
- self.get_plot_scaling_factor()
- bbox = self.bbox if bbox is None else bbox
- print '[DEBUG] bbox : ' + str(bbox)
- print '[DEBUG] offset : ' + str(self.offset)
- print '[DEBUG] scaling factor : ' + str(self.scaling_factor)
- self.canvas.coords(self.x_axis, (self.offset[0], self.offset[1], 700+self.offset[0], self.offset[1]))
- while self.ordinates:
- self.canvas.delete(self.ordinates[0])
- del self.ordinates[0]
- self.ordinates = [
- self.canvas.create_text((self.offset[0]/2, 8), text=str(bbox[3])),
- self.canvas.create_text((self.offset[0]/2, 402), text=str(bbox[1]))
- ]
- if self.offset[1] != 5 and self.offset[1] != 405:
- self.ordinates.append(self.canvas.create_text((self.offset[0]/2, self.offset[1]), text='0'))
- self.delete_graphs()
- colors = ['blue', 'red', 'orange', 'green', 'dark grey', 'pink']
- for data_set in self.data_sets:
- color = colors[self.data_sets.index(data_set)]
- graph = []
- for i in range(len(data_set)-1):
- x, y, x_, y_ = data_set[i][0], data_set[i][1], data_set[i+1][0], data_set[i+1][1]
- 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))
- time.sleep(0.005)
- self.tk.update()
- self.graphs.append(graph)
- def create_dropdown(self):
- var = StringVar(self.frame)
- var.set("Select metric")
- w = apply(OptionMenu, (self.dropdown_frame, var) + tuple(self.metrics))
- w.bind('<Configure>', self.dropdown_callback)
- w.pack(anchor=CENTER)
- self.dropdown.append(var)
- self.dropdown_widgets.append(w)
- def remove_dropdown(self):
- del self.dropdown[-1]
- self.dropdown_widgets[-1].destroy()
- del self.dropdown_widgets[-1]
- self.dropdown_callback('lel')
- def get_metric(self, metric, single_dd_base):
- if metric == 'Filling %':
- 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()])
- return round(nb_active*100/250, 1)
- elif metric == 'Reproductive %':
- 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()])
- return round(nb_repro * 100 / 250, 1)
- elif metric == 'Pregnant female %':
- 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()])
- 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()])
- return round(nb_preg * 100 / nb_female, 1)
- elif metric == 'Overall efficiency':
- filling = self.get_metric('Filling %', single_dd_base)/100
- f_preg = self.get_metric('Pregnant female %', single_dd_base)/100
- repro = self.get_metric('Reproductive %', single_dd_base)/100
- return round(100*filling*f_preg*(0.71 + repro * 0.29), 1)
- elif metric == 'Fatigue %':
- fatigues = [dd['fatigue'] for dd in single_dd_base.values() if (type(dd) is dict and dd['status'] != 'out' and dd['status'] != 'pex')]
- return round(sum(fatigues)/len(fatigues)*100 / 240, 1)
- def print_efficiency(self):
- print 'Filling : ' + str(self.get_metric('Filling %', self.dd_base[-1])) + '%'
- print 'Pregnant females : ' + str(self.get_metric('Pregnant female %', self.dd_base[-1])) + '%'
- print 'Reproductive : ' + str(self.get_metric('Reproductive %', self.dd_base[-1])) + '%'
- eff = self.get_metric('Overall efficiency', self.dd_base[-1])
- print ''
- print 'Overall efficiency : ' + str(eff) + '%'
- print 'Estimated revenue : ' + str(int(eff * 1983000 / 100)) + ' k/day, ' + str(
- int(eff * 1983000 * 30 / 100)) + ' k/month'
- def display_metric(self, metric):
- data = []
- for mesure_set in self.dd_base:
- data.append((self.dd_base.index(mesure_set), self.get_metric(metric, mesure_set)))
- self.plot(data)
- def dropdown_callback(self, event):
- print "Dropdown update detected"
- new_metrics = [dd.get() for dd in self.dropdown]
- if [metric for metric in self.plotted_metrics if metric != 'Select metric'] != [metric for metric in new_metrics if metric != 'Select metric']:
- self.plotted_metrics = new_metrics
- self.data_sets = []
- for dd in self.dropdown:
- if dd.get() != 'Select metric':
- data = []
- for mesure_set in self.dd_base:
- data.append((self.dd_base.index(mesure_set), self.get_metric(dd.get(), mesure_set)))
- if data:
- self.data_sets.append(data)
- self.delete_graphs(full_reset=True)
- self.actual_plot(width=2)
- tk = Tk()
- M = Monitor(tk)
- tk.mainloop()
- __author__ = 'Alexis'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement