File: wb_admin_performance_dashboard.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (627 lines) | stat: -rw-r--r-- 22,794 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA

import mforms

from workbench.graphics.charting import DBTimeLineGraph, DBSimpleCounter, DBRoundMeter, DBLevelMeter, DBImage, DBText
from workbench.graphics.canvas import Canvas, TextFigure
from workbench.graphics.cairo_utils import Context

from wb_admin_utils import WbAdminBaseTab
import re
from workbench.log import log_error


class MyDict:
    def __init__(self, d):
        #        dict.__init__(self)
    
        self.d = d


    def __contains__(self, k):
        print "contains", k
        return self.d.__contains__(k)


    def __getitem__(self, k):
        print "getit", k
        return self.d.__getitem__(k)



class RenderBox(mforms.PyDrawBox):
    def __init__(self, parent):
        mforms.PyDrawBox.__init__(self)
        self.set_managed()
        self.set_release_on_add()

        self.parent = parent
        self.canvas = None

        self.set_instance(self)
        self.drag_offset = None
        self.drag_object = None
        self.tooltip = None

        self.offset = (0, 0)
        self.current_pos = (0, 0)

        self.variable_values = None
    
        self.layouting_mode = False


    def __del__(self):
        if self.tooltip:
            self.tooltip.close()
            self.tooltip = None


    def mouse_down(self, b, x, y):
        x -= self.offset[0]
        y -= self.offset[1]
        if b == 0:
            if self.layouting_mode:
                self.drag_object = self.canvas.figure_at(x, y)
                if self.drag_object:
                    self.drag_offset = x - self.drag_object.x, y - self.drag_object.y

    def mouse_up(self, b, x, y):
        x -= self.offset[0]
        y -= self.offset[1]
        if b == 0:
            if self.drag_object:
                xx = x - self.drag_offset[0]
                yy = y - self.drag_offset[1]
                xx -= xx % 2
                yy -= yy % 2
                self.drag_object.move(xx, yy)
                print self.drag_object.pos

            self.drag_offset = None
            self.drag_object = None


    def mouse_move(self, x, y):
        x -= self.offset[0]
        y -= self.offset[1]

        if self.drag_object:
            xx = x - self.drag_offset[0]
            yy = y - self.drag_offset[1]
            xx -= xx % 2
            yy -= yy % 2
            self.drag_object.move(xx, yy)

        self.current_pos = x, y
        self.canvas.mouse_move(x, y)


    def repaint(self, cr, x, y, w, h):
        xoffs, yoffs = self.parent.relayout()
        self.offset = xoffs, yoffs

        c = Context(cr)
        try:
            self.canvas.repaint(c, xoffs, yoffs, w, h)
        except Exception:
            import traceback
            log_error("Exception rendering dashboard: %s\n" % traceback.format_exc())


    def add(self, figure):
        self.canvas.add(figure)
        figure.on_hover_in = self.handle_hover_in
        figure.on_hover_out = self.handle_hover_out


    def make_tooltip_text(self, figure, template):
        text = template % self.variable_values

        # find and evaluate all embedded ${expressions}
        for m in re.findall("(\${[^}]*})", text):
            value = eval(m[2:-1] % self.variable_values)
            text = text.replace(m, str(value))

        return text


    def close_tooltip(self):
        if self.tooltip:
            self.tooltip.close()
            self.tooltip = None


    def handle_hover_out(self, fig, x, y):
        self.close_tooltip()


    def handle_hover_in(self, fig, x, y):
        if self.tooltip:
            self.tooltip.close()
            self.tooltip = None

        if not mforms.Form.main_form().is_active():
            return

        if fig and getattr(fig, 'hover_text_template', None):
            text = self.make_tooltip_text(fig, fig.hover_text_template)
            if text:
                self.tooltip = mforms.newPopover(mforms.PopoverStyleTooltip)

                fx = fig.x + self.offset[0] + 2 * fig.width / 3
                fy = fig.y + self.offset[1] + 2 * fig.height / 3

                xx, yy = self.client_to_screen(fx, fy)
                box = mforms.newBox(False)
                box.set_spacing(0)
                t = ""
                for line in text.split("\n"):
                    if line.startswith("*"):
                        if t:
                            if t.endswith("\n"):
                                t = t[:-1]
                            label = mforms.newLabel(t)
                            label.set_style(mforms.SmallStyle)
                            box.add(label, False, False)
                            t = ""
                        label = mforms.newLabel(line[1:].rstrip("\n"))
                        label.set_style(mforms.SmallBoldStyle)
                        box.add(label, False, False)
                    else:
                        t += line+"\n"
                if t:
                    label = mforms.newLabel(t.rstrip("\n"))
                    label.set_style(mforms.SmallStyle)
                    box.add(label, False, False)
            
                self.tooltip.set_size(max(box.get_preferred_width(), 100), max(box.get_preferred_height(), 50))
            
                self.tooltip.set_content(box)
                self.tooltip.add_close_callback(self.close_tooltip)
                self.tooltip.show_and_track(self, xx, yy, mforms.Right)



class CDifferencePerSecond(object):
    def __init__(self, expr):
        self.expr = expr

        self.old_value = None
        self.old_value_timestamp = None


    def handle(self, values, timestamp):
        if not self.expr:
            return None
        value = eval(self.expr % values)

        if self.old_value and timestamp > self.old_value_timestamp:
            if type(value) is tuple:
                dif = []
                for i in range(len(value)):
                    dif.append(float(value[i] - self.old_value[i]) / (timestamp - self.old_value_timestamp))
                dif = tuple(dif)
            else:
                dif = float(value - self.old_value) / (timestamp - self.old_value_timestamp)
        else:
            dif = None

        self.old_value = value
        self.old_value_timestamp = timestamp

        return dif


class CRawValue(object):
    def __init__(self, expr):
        self.expr = expr
        
    
    def handle(self, values, timestamp):
        if not self.expr:
            return None
        value = eval(self.expr % values)

        return value


class CMakeTuple(object):
    def __init__(self, *items):
        self.items = items


    def handle(self, values, timestamp):
        l = []
        for i in self.items:
            l.append(i.handle(values, timestamp))
        return tuple(l)


READ_COLOR = (60/255.0, 178/255.0, 191/255.0)
WRITE_COLOR = (253/255.0, 138/255.0, 39/255.0)

GLOBAL_DASHBOARD_WIDGETS = \
[
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_header_network.png"),), None, (None, None),
  (0, 0, 0), (85, 5),
  ""),
 (None, DBText, ("Statistics for network traffic sent and received\nby the MySQL Server over client connections.",), None, (None, None),
  (0.4, 0.4, 0.4), (65, 72),
  ""),
 
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_arrow_in_static.png"),), None, (None, None),
  (0, 0, 0), (228, 196),
  ""),

 (None, DBSimpleCounter, ("receiving\n%.2f %sB/s", True), None, (CDifferencePerSecond, "%(Bytes_received)s"),
  READ_COLOR, (200, 240),
  """Bytes Received
Number of bytes received by the MySQL server at the network level.

Total bytes received: %(Bytes_received)s"""),

 ("Incoming Network Traffic (Bytes/Second)", DBTimeLineGraph, ("%.1f %sB", True), None, (CDifferencePerSecond, "%(Bytes_received)s"),
  READ_COLOR, (30, 160),
  None),

 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_arrow_out_static.png"),), None, (None, None),
  (0, 0, 0), (228, 368),
  ""),

 (None, DBSimpleCounter, ("sending\n%.2f %sB/s", True), None, (CDifferencePerSecond, "%(Bytes_sent)s"),
  WRITE_COLOR, (200, 410),
  """Bytes Sent
Number of bytes sent by the MySQL server at the network level.
      
Total bytes sent: %(Bytes_sent)s"""),
 
 ("Outgoing Network Traffic (Bytes/Second)", DBTimeLineGraph, ("%.1f %sB", True), None, (CDifferencePerSecond, "%(Bytes_sent)s"),
  WRITE_COLOR, (30, 330),
  None),

 ("Client Connections (Total)", DBTimeLineGraph, ("%.1f %s", 1, True), None, (CRawValue, "%(Threads_connected)s"),
  (124/255.0, 193/255.0, 80/255.0), (30, 500),
  None),
 
 (None, DBLevelMeter, tuple(), (CRawValue, "%(max_connections)s"), (CRawValue, "%(Threads_connected)s"),
  (124/255.0, 193/255.0, 80/255.0), (200, 520),
  """Connections
Client connections/threads to the MySQL server.

Threads connected: %(Threads_connected)s
Threads running: %(Threads_running)s

Total connection attempts: %(Connections)s
Connection errors (accept): %(Connection_errors_accept)s
Connection errors (internal): %(Connection_errors_internal)s
Connection errors (max connections reached): %(Connection_errors_max_connections)s
Connection errors (peer address): %(Connection_errors_peer_address)s
Connection errors (select): %(Connection_errors_select)s
Connection errors (tcpwrap): %(Connection_errors_tcpwrap)s"""),

 
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_separator.png"),), None, (None, None),
  (0, 0, 0), (310, 120),
  ""),
 
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_header_mysql.png"),), None, (None, None),
  (0, 0, 0), (380, 5),
  ""),
 (None, DBText, ("Primary MySQL Server activity\nand performance statistics.",), None, (None, None),
  (0.4, 0.4, 0.4), (376, 72),
  ""),
 
 ("Table Open Cache", DBRoundMeter, ("Efficiency",), None, (CRawValue, "%(Table_open_cache_hits)s/(%(Table_open_cache_hits)s+%(Table_open_cache_misses)s+0.0)"),
  (124/255.0, 193/255.0, 80/255.0), (380, 150),
  """Table Open Cache
Cache for minimizing number of times MySQL 
will open database tables when accessed.

Table open cache hits: %(Table_open_cache_hits)s
Table open cache misses: %(Table_open_cache_misses)s"""),
 
 ("SQL Statements Executed (#)", DBTimeLineGraph, ("%.1f %s", 3, True), None, (CDifferencePerSecond, "(%(Com_select)s,%(Com_insert)s+%(Com_update)s+%(Com_delete)s,%(Com_create_db)s+%(Com_create_event)s+%(Com_create_function)s+%(Com_create_index)s+%(Com_create_procedure)s+%(Com_create_server)s+%(Com_create_table)s+%(Com_create_trigger)s+%(Com_create_udf)s+%(Com_create_user)s+%(Com_create_view)s+%(Com_alter_db)s+%(Com_alter_db_upgrade)s+%(Com_alter_event)s+%(Com_alter_function)s+%(Com_alter_procedure)s+%(Com_alter_server)s+%(Com_alter_table)s+%(Com_alter_tablespace)s+%(Com_alter_user)s+%(Com_drop_db)s+%(Com_drop_event)s+%(Com_drop_function)s+%(Com_drop_index)s+%(Com_drop_procedure)s+%(Com_drop_server)s+%(Com_drop_table)s+%(Com_drop_trigger)s+%(Com_drop_user)s+%(Com_drop_view)s)"),
  [(255/255.0, 201/255.0, 2/255.0), (126/255.0, 142/255.0, 207/255.0), (194/255.0, 123/255.0, 206/255.0)], (350, 330),
    None),

 (None, DBSimpleCounter, ("SELECT\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_select)s"),
  (255/255.0, 201/255.0, 2/255.0), (350, 470),
  """SELECT Statements Executed
    
Total since start: %(Com_select)s"""),

 (None, DBSimpleCounter, ("INSERT\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_insert)s"),
  (126/255.0, 142/255.0, 207/255.0), (350, 520),
  """INSERT Statements Executed
      
Total since start: %(Com_insert)s"""),
 (None, DBSimpleCounter, ("UPDATE\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_update)s"),
  (126/255.0, 142/255.0, 207/255.0), (350, 560),
  """UPDATE Statements Executed
      
Total since start: %(Com_update)s"""),
 (None, DBSimpleCounter, ("DELETE\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_delete)s"),
  (126/255.0, 142/255.0, 207/255.0), (350, 600),
  """DELETE Statements Executed
      
Total since start: %(Com_delete)s"""),

 (None, DBSimpleCounter, ("CREATE\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_create_db)s+%(Com_create_event)s+%(Com_create_function)s+%(Com_create_index)s+%(Com_create_procedure)s+%(Com_create_server)s+%(Com_create_table)s+%(Com_create_trigger)s+%(Com_create_udf)s+%(Com_create_user)s+%(Com_create_view)s"),
  (194/255.0, 123/255.0, 206/255.0), (445, 520),
"""CREATE Statements Executed
Number of CREATE statements executed by the server (since server was started).

Create DB: %(Com_create_db)s
Create Event: %(Com_create_event)s
Create Function: %(Com_create_function)s
Create Index: %(Com_create_index)s
Create Procedure: %(Com_create_procedure)s
Create Server: %(Com_create_server)s
Create Table: %(Com_create_table)s
Create Trigger: %(Com_create_trigger)s
Create UDF: %(Com_create_udf)s
Create User: %(Com_create_user)s
Create View: %(Com_create_view)s"""),
 (None, DBSimpleCounter, ("ALTER\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_alter_db)s+%(Com_alter_db_upgrade)s+%(Com_alter_event)s+%(Com_alter_function)s+%(Com_alter_procedure)s+%(Com_alter_server)s+%(Com_alter_table)s+%(Com_alter_tablespace)s+%(Com_alter_user)s"),
  (194/255.0, 123/255.0, 206/255.0), (445, 560),
"""ALTER Statements Executed
Number of ALTER statements executed by the server (since server was started).
    
Alter DB: %(Com_alter_db)s
Alter DB Upgrade: %(Com_alter_db_upgrade)s
Alter Event: %(Com_alter_event)s
Alter Function: %(Com_alter_function)s
Alter Procedure: %(Com_alter_procedure)s
Alter Server: %(Com_alter_server)s
Alter Table: %(Com_alter_table)s
Alter Tablespace: %(Com_alter_tablespace)s
Alter User: %(Com_alter_user)s"""),
 
 (None, DBSimpleCounter, ("DROP\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Com_drop_db)s+%(Com_drop_event)s+%(Com_drop_function)s+%(Com_drop_index)s+%(Com_drop_procedure)s+%(Com_drop_server)s+%(Com_drop_table)s+%(Com_drop_trigger)s+%(Com_drop_user)s+%(Com_drop_view)s"),
  (194/255.0, 123/255.0, 206/255.0), (445, 600),
  """DROP Statements Executed
Number of DROP statements executed by the server (since server was started).
      
Drop DB: %(Com_drop_db)s
Drop Event: %(Com_drop_event)s
Drop Function: %(Com_drop_function)s
Drop Index: %(Com_drop_index)s
Drop Procedure: %(Com_drop_procedure)s
Drop Server: %(Com_drop_server)s
Drop Table: %(Com_drop_table)s
Drop Trigger: %(Com_drop_trigger)s
Drop User: %(Com_drop_user)s
Drop View: %(Com_drop_view)s"""),
 
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_separator.png"),), None, (None, None),
  (0, 0, 0), (570, 120),
  ""),

 # InnoDB
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_header_innodb.png"),), None, (None, None),
  (0, 0, 0), (710, 5),
  ""),
 (None, DBText, ("Overview of the InnoDB Buffer Pool and disk activity\ngenerated by the InnoDB storage engine.",), None, (None, None),
  (0.4, 0.4, 0.4), (655, 72),
  ""),

 # Buffer Pool
 (None, DBSimpleCounter, ("read reqs.\n%.0f %s\npages/s", True), None, (CDifferencePerSecond, "%(Innodb_buffer_pool_read_requests)s"),
  READ_COLOR, (610, 160),
  """InnoDB Buffer Pool Read Requests
The number of logical read requests InnoDB has done to the buffer pool.

Total: %(Innodb_buffer_pool_read_requests)s"""),
 (None, DBSimpleCounter, ("write reqs.\n%.0f %s\npages/s", True), None, (CDifferencePerSecond, "%(Innodb_buffer_pool_write_requests)s"),
  WRITE_COLOR, (610, 220),
  """InnoDB Buffer Pool Write Requests
The number of logical write requests InnoDB has done to the buffer pool.

Total: %(Innodb_buffer_pool_write_requests)s"""),

 ("InnoDB Buffer Pool", DBRoundMeter, ("Usage",), None, (CRawValue, "(%(Innodb_buffer_pool_pages_data)s+%(Innodb_buffer_pool_pages_misc)s)/(%(Innodb_buffer_pool_pages_total)s+0.0)"),
  (124/255.0, 193/255.0, 80/255.0), (720, 150),
"""InnoDB Buffer Pool Usage Rate
How much of the InnoDB buffer pool is in use, from the amount allocated to it.

Usage Rate: ${(%(Innodb_buffer_pool_pages_data)s+%(Innodb_buffer_pool_pages_misc)s)/(%(Innodb_buffer_pool_pages_total)s+0.0)}

Total Pages Available: %(Innodb_buffer_pool_pages_total)s
Pages Used for Data: %(Innodb_buffer_pool_pages_data)s
Pages Used Internally by InnoDB: %(Innodb_buffer_pool_pages_misc)s
Pages Free: %(Innodb_buffer_pool_pages_free)s"""),

 (None, DBSimpleCounter, ("disk reads\n%.0f %s\n#/s", True), None, (CDifferencePerSecond, "%(Innodb_buffer_pool_reads)s"),
  READ_COLOR, (890, 180),
"""InnoDB Buffer Pool Reads
The number of logical reads that InnoDB could not satisfy from the buffer pool, and had to read directly from the disk.
    
Total: %(Innodb_buffer_pool_reads)s"""),

 ("Redo Log", DBSimpleCounter, ("data written\n%.0f %sB/s", True), None, (CDifferencePerSecond, "%(Innodb_os_log_written)s"),
  WRITE_COLOR, (606, 330),
"""Bytes Written to InnoDB Redo Log
The number of bytes written to the InnoDB redo log files.
    
Total: %(Innodb_os_log_written)s"""),

 (None, DBSimpleCounter, ("writes\n%.0f %s#/s", True), None, (CDifferencePerSecond, "%(Innodb_log_writes)s"),
  WRITE_COLOR, (606, 380),
"""Writes to InnoDB Redo Log
The number of physical writes to the InnoDB redo log file.

Total: %(Innodb_log_writes)s"""),

 ("Doublewrite Buffer", DBSimpleCounter, ("writes\n%.0f %s/s", True), None, (CDifferencePerSecond, "%(Innodb_dblwr_writes)s"),
  WRITE_COLOR, (606, 480),
"""Write Operations to InnoDB Doublewrite Buffer
The number of doublewrite operations that have been performed.

Total: %(Innodb_dblwr_writes)s"""),
 
 # Storage
 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_arrow_out_static.png"),), None, (None, None),
  (0, 0, 0), (934, 360),
  ""),

 ("InnoDB Disk Writes", DBTimeLineGraph, ("%.2f %sB", True), None, (CDifferencePerSecond, "%(Innodb_data_written)s"),
  WRITE_COLOR, (738, 330),
  None),
 (None, DBSimpleCounter, ("writing\n%.2f %sB/s", True), None, (CDifferencePerSecond, "%(Innodb_data_written)s"),
  WRITE_COLOR, (916, 410),
  """InnoDB Data Written
Total amount of data in bytes written in file operations by the InnoDB storage engine.

Total: %(Innodb_data_written)s"""),

 (None, DBImage, (mforms.App.get().get_resource_path("dashboard_arrow_in_static.png"),), None, (None, None),
  (0, 0, 0), (934, 535),
  ""),

 ("InnoDB Disk Reads", DBTimeLineGraph, ("%.2f %sB", True), None, (CDifferencePerSecond, "%(Innodb_data_read)s"),
  READ_COLOR, (738, 500),
  None),
 (None, DBSimpleCounter, ("reading\n%.2f %sB/s", True), None, (CDifferencePerSecond, "%(Innodb_data_read)s"),
  READ_COLOR, (916, 580),
  """InnoDB Data Read
Total amount of data in bytes read in file operations by the InnoDB storage engine.

Total: %(Innodb_data_read)s"""),
]


class WbAdminDashboard(WbAdminBaseTab):
    min_server_version = (5,6,6)
    
    _refresh_tm = None
    drawbox = None
    _form_deactivated_conn = None
    
    @classmethod
    def wba_register(cls, admin_context):
        admin_context.register_page(cls, "wba_performance", "Dashboard", False)
    
    @classmethod
    def identifier(cls):
        return "admin_dashboard"

    def set_needs_repaint(self, x, y, w, h):
        self.drawbox.set_needs_repaint()

    def create_ui(self):
        #self.create_basic_ui("title_dashboard.png", "Dashboard")


        self._form_deactivated_conn = mforms.Form.main_form().add_deactivated_callback(self.form_deactivated)


        self.content = mforms.newScrollPanel(0)

        self.drawbox = RenderBox(self)
        self.canvas = Canvas(self.set_needs_repaint)
        self.drawbox.canvas = self.canvas
        self.drawbox.set_size(1024, 700)
        self.content.add(self.drawbox)

        self.add(self.content, True, True)

        self.widgets = []
        
        self.last_refresh_time = None
        
        #
        self.drawbox.variable_values = self.ctrl_be.server_variables

        # create all widgets
        for caption, wclass, args, init, (calc, calc_expr), color, pos, hover_text in GLOBAL_DASHBOARD_WIDGETS:
            if caption:
                fig = TextFigure(caption)
                fig.set_text_color(0.5, 0.5, 0.5)
                fig.set_font_size(11)
                fig.set_font_bold(True)
                self.drawbox.add(fig)
                fig.move(pos[0], pos[1] - 20)
        
            w = wclass(calc(calc_expr) if calc else None, *args)
            self.drawbox.add(w)
            w.set_main_color(color)
            w.move(*pos)
            if hover_text:
                w.hover_text_template = hover_text
            
            if init:
                init_calc, init_expr = init
                w.init(init_calc(init_expr).handle(self.ctrl_be.server_variables, None))

            self.widgets.append(w)


        self.refresh()
        self._refresh_tm = mforms.Utilities.add_timeout(self.ctrl_be.status_variable_poll_interval, self.refresh)
            


    def repaint(self):
        self.drawbox.set_needs_repaint()
        return True


    def shutdown(self):
        if self._form_deactivated_conn:
            self._form_deactivated_conn.disconnect()
            self._form_deactivated_conn = None

        if self._refresh_tm:
            mforms.Utilities.cancel_timeout(self._refresh_tm)
            self._refresh_tm = None


    def refresh(self):
        status_variables, timestamp = self.ctrl_be.status_variables, self.ctrl_be.status_variables_time
        if self.last_refresh_time != timestamp:
            for w in self.widgets:
                if hasattr(w, 'process'):
                    w.process(status_variables, timestamp)

            self.drawbox.variable_values.update(status_variables)

            self.drawbox.set_needs_repaint()

        return True


    def form_deactivated(self):
        if self.drawbox:
            self.drawbox.close_tooltip()


    def page_deactivated(self):
        if self.drawbox:
            self.drawbox.close_tooltip()

    def relayout(self):
        full_width = max(1024, self.content.get_width())
        full_height = max(700, self.content.get_height())

        if self.drawbox.get_width() != full_width or self.drawbox.get_height() != full_height:
            self.drawbox.set_size(full_width, full_height)

        # return offset
        return (full_width - 1024) / 2, (full_height - 700) / 2