File: wb_admin_perfschema_instrumentation.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 (1578 lines) | stat: -rw-r--r-- 62,871 bytes parent folder | download
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
# Copyright (c) 2013, 2016, 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 grt
import mforms
from workbench.change_tracker import ChangeCounter
from workbench.graphics.cairo_utils import Context
from workbench.graphics.canvas import ImageFigure

from wb_admin_perfschema_instrumentation_be import PSConfiguration, PSTimerType, PSObject, PSActor

from workbench.log import log_info, log_error

from wb_admin_perfschema import WbAdminPSBaseTab

MYSQL_ERR_TABLE_DOESNT_EXIST = 1146


DEFAULT_INSTRUMENTS_57 = [
'wait/io/file/%',
'wait/io/table/%',
'wait/lock/table/sql/handler',
'statement/%',
'idle'
]

DEFAULT_INSTRUMENTS_56 = [
'wait/io/file/%',
'wait/io/table/%',
'statement/%',
'wait/lock/table/sql/handler',
'idle'
]

DEFAULT_CONSUMERS_57 = [
'events_statements_current', 'events_transactions_current', 'global_instrumentation', 'thread_instrumentation', 'statements_digest'
]

DEFAULT_CONSUMERS_56 = [
'events_statements_current', 'global_instrumentation', 'thread_instrumentation', 'statements_digest'
]


class BigSwitch(mforms.PyDrawBox):
    def __init__(self):
        mforms.PyDrawBox.__init__(self)
        self.set_managed()
        self.set_release_on_add()

        self.set_instance(self)

        self.state = "default"

        self.hovering_state = None
        self.mouse_pos = None, None

        self.text_image = ImageFigure(mforms.App.get().get_resource_path("ps_switcher_text.png"))
        self.legend = ImageFigure(mforms.App.get().get_resource_path("ps_switcher_legende.png"))

        self.state_images = []
        for item in ["fully", "custom", "default", "disabled"]:
            icons = []
            for state in ["off", "hoover", "on"]:
                icon = ImageFigure(mforms.App.get().get_resource_path("ps_switcher_%s_%s.png" % (item, state)))
                icons.append(icon)
            self.state_images.append((item, tuple(icons)))



    def repaint(self, cr, x, y, w, h):
        c = Context(cr)
        self.hovering_state = None
        yy = 0
        for state, (off, hoover, on) in self.state_images:
            image = off
            xx = (self.get_width() - image.width - self.text_image.width) / 2
            if state == self.state:
                image = on
            else:
                if self.mouse_pos[0] > xx and self.mouse_pos[0] < xx + image.width and self.mouse_pos[1] >= yy and self.mouse_pos[1] <= yy + image.height and state != "custom":
                    image = hoover
                    self.hovering_state = state

            image.move(xx, yy)
            image.render(c)

            yy += image.height
        self.text_image.move(xx + image.width, 0)
        self.text_image.render(c)

        self.legend.move(xx + image.width + self.text_image.width + 100, self.get_height() - self.legend.height)
        self.legend.render(c)



    def set_state(self, state):
        self.state = state
        self.set_needs_repaint()


    def mouse_move(self, x, y):
        self.mouse_pos = x, y
        self.set_needs_repaint()


    def mouse_click(self, button, x, y):
        if self.hovering_state:
            self.callback(self.hovering_state)


class EasySetupPage(mforms.Box):
    def __init__(self, owner):
        mforms.Box.__init__(self, False)
        self.set_managed()
        self.set_release_on_add()
        self.set_spacing(12)
        self.set_padding(12)
        self.owner = owner

        self.create_ui()


    def check_instrumentation_level(self):
        try:
            r = self.owner.get_select_int_result("SELECT COUNT(*) FROM performance_schema.setup_consumers WHERE enabled='NO'")
            if r == 0:
                # Exclude results from 'memory/performance_schema/%' because they can't be disabled
                r = self.owner.get_select_int_result("SELECT COUNT(*) FROM performance_schema.setup_instruments WHERE NAME NOT LIKE 'memory/performance_schema/%' AND (enabled='NO' OR timed='NO')")
                if r == 0:
                    return "fully"

            r = self.owner.get_select_int_result("SELECT COUNT(*) FROM performance_schema.setup_consumers WHERE enabled='YES'")
            if r == 0:
                # Exclude results from 'memory/performance_schema/%' because they can't be disabled
                r = self.owner.get_select_int_result("SELECT COUNT(*) FROM performance_schema.setup_instruments WHERE NAME NOT LIKE 'memory/performance_schema/%' AND (enabled='YES' OR timed='YES')")
                if r == 0:
                    return "disabled"

            if self.owner.target_version.is_supported_mysql_version_at_least(5, 7, 0):
                instruments = DEFAULT_INSTRUMENTS_57
                consumers = DEFAULT_CONSUMERS_57
            else:
                instruments = DEFAULT_INSTRUMENTS_56
                consumers = DEFAULT_CONSUMERS_56

            # this will return 0 if all consumers have the expected value
            r = self.owner.get_select_int_result("""
                SELECT sum(IF(%s, 1, 0)) + sum(IF(%s, 1, 0)) FROM performance_schema.setup_consumers
                """ % ("ENABLED='NO' AND NAME IN (%s)" % ",".join(["'%s'" % c for c in consumers]),
                       "ENABLED='YES' AND NAME NOT IN (%s)" % ",".join(["'%s'" % c for c in consumers])))
            if r == 0:
                nlikes = []
                likes = []
                ins = []
                # Exclude results from 'memory/performance_schema/%' because they can't be disabled
                nlikes.append("'memory/performance_schema/%'")
                for i in instruments:
                   if '%' in i:
                       likes.append("NAME LIKE '%s'" % i)
                       nlikes.append("NAME NOT LIKE '%s'" % i)
                   else:
                       ins.append(i)
                exp = " OR ".join(["NAME IN (%s)" % ",".join(["'%s'" % i for i in ins])] + likes)
                nexp = " AND ".join(["NAME NOT IN (%s)" % ",".join(["'%s'" % i for i in ins])] + nlikes)
                r = self.owner.get_select_int_result("""
                    SELECT sum(IF(ENABLED='NO' AND (%(cond)s), 1, 0)) +
                        sum(IF(ENABLED='YES' AND (%(notcond)s), 1, 0)) +
                        sum(IF(TIMED='NO' AND (%(cond)s), 1, 0)) +
                        sum(IF(TIMED='YES' AND (%(notcond)s), 1, 0))
                    FROM performance_schema.setup_instruments
                    """ % {"cond" : exp, "notcond" : nexp})
                if r == 0:
                    return "default"

            return "custom"

        except grt.DBError, e:
            log_error("MySQL error querying PS instrumentation/consumers: %s\n" % e)
            mforms.Utilities.show_error("Check Performance Schema Configuration State", "Error checking configuration state of Performance Schema: %s" % e, "OK", "", "")

        return None


    def generate_updates_for_reset(self, instruments, consumers):
        likes = []
        ins = []
        for i in instruments:
            if '%' in i:
                likes.append("NAME LIKE '%s'" % i)
            else:
                ins.append(i)
        isql = """
            UPDATE performance_schema.setup_instruments
                SET ENABLED = IF(%s, 'YES', 'NO'),
                    TIMED = ENABLED
        """ % " OR ".join(["NAME IN (%s)" % ",".join(["'%s'" % i for i in ins])] + likes)

        csql = """
            UPDATE performance_schema.setup_consumers
                SET ENABLED = IF(%s, 'YES', 'NO')
        """ % "NAME IN (%s)" % ",".join(["'%s'" % c for c in consumers])

        return [isql, csql]


    def change_instrumentation(self, state):
        try:
            if state == "fully":
                if mforms.Utilities.show_warning("Performance Warning",
                                                 "While enabling all performance_schema instrumentation allows collecting a lot of information from MySQL, it will also impose a significant performance and memory overhead on it.\nDo not enable this option if your server is a production server under heavy load, unless you know what you're doing.", "Leave Unchanged", "Enable Everything", "") == mforms.ResultOk:
                    return

                sql = ["UPDATE performance_schema.setup_consumers SET enabled='YES'",
                       "UPDATE performance_schema.setup_instruments SET enabled='YES', timed='YES'"]
                mforms.App.get().set_status_text("Enabling Full Performance Schema Instrumentation...")
            elif state == "disabled":
                sql = ["UPDATE performance_schema.setup_consumers SET enabled='NO'",
                       "UPDATE performance_schema.setup_instruments SET enabled='NO', timed='NO'"]
                mforms.App.get().set_status_text("Disabling Full Performance Schema Instrumentation...")
            elif state == "default":
                if self.owner.target_version.is_supported_mysql_version_at_least(5, 7, 0):
                    instruments = DEFAULT_INSTRUMENTS_57
                    consumers = DEFAULT_CONSUMERS_57
                else:
                    instruments = DEFAULT_INSTRUMENTS_56
                    consumers = DEFAULT_CONSUMERS_56
                mforms.App.get().set_status_text("Resetting Performance Schema settings to default instrumentation...")
                sql = self.generate_updates_for_reset(instruments, consumers)
                print sql
            for s in sql:
                log_info("Executing %s...\n" % s)
                self.owner.main_view.editor.executeManagementCommand(s, 0)
            if state:
                mforms.App.get().set_status_text("Changed Performance Schema Settings")
            else:
                mforms.App.get().set_status_text("Disabled Performance Schema Settings")

            self.owner.rebuild_ui(False)
        except grt.DBError, e:
            log_error("MySQL error toggling (%s) PS Instrumentation\n" % e)
            mforms.App.get().set_status_text("Error toggling Performance Schema Instrumentation: %s" % e)
            mforms.Utilities.show_error("Toggle Performance Schema Instrumentation",
                                        "MySQL Error: %s" % e,
                                        "OK", "", "")



    def create_ui(self):
        self.logo = mforms.newImageBox()
        self.logo.set_image(mforms.App.get().get_resource_path("ps_easysetup_logo.png"))
        self.add(self.logo, False, True)

        self.switch_image = BigSwitch()
        self.switch_image.callback = self.change_instrumentation
        self.switch_image.set_size(200, 152)
        self.add(self.switch_image, False, True)

        image = mforms.newImageBox()
        image.set_image(mforms.App.get().get_resource_path("separator_vertical.png"))
        self.add(image, False, True)

        label = mforms.newLabel("\n\nThe MySQL Performance Schema allows you to:\n- instrument MySQL to collect statistics and performance data\n- log collected events into tables, so they can be analyzed\n\nUse the switch above to change Performance Schema instrumentation or disable it.")
        label.set_text_align(mforms.MiddleCenter)
        self.add(label, False, True)


    def reload(self):
        state = self.check_instrumentation_level()
        print state
        self.logo.set_image(mforms.App.get().get_resource_path("ps_easysetup_logo_enabled.png" if state != "disabled" else "ps_easysetup_logo.png"))
        self.switch_image.set_state(state)


class SetupInstruments(mforms.Box):
    def __init__(self, owner, data):
        mforms.Box.__init__(self, False)
        self.set_managed()
        self.set_release_on_add()
        self.set_spacing(12)
        self.set_padding(12)
        self.owner = owner
        self._data = data

        self.create_ui()

    def create_ui(self):
        l = mforms.newLabel("Select Enabled Instrumentation Points")
        l.set_style(mforms.BoldStyle)
        self.add(l, False, True)
      
        description = \
        """An instrument refers to a specific section of the MySQL code that has been enabled for monitoring.
When an instrument is enabled and is executed on the server, it will generate events than can be used for monitoring.

The top level elements on the table below represent the "Event Type" generated by the instrument.

The enabled column indicates whether the events for the instrument are generated.
The timed column indicates whether information about the event duration is recorded.
"""
        label = mforms.newLabel(description)
        self.add(label, False, False)
        
        self._instruments = mforms.newTreeNodeView(mforms.TreeAltRowColors)
        self._instruments.add_column(mforms.IconStringColumnType, "Instrument", 400, False)
        self._instruments.add_column(mforms.TriCheckColumnType, "Enabled", 50, True)
        self._instruments.add_column(mforms.TriCheckColumnType, "Timed", 50, True)
        self._instruments.end_columns()
        self.add(self._instruments, True, True)

        self._instruments.set_cell_edited_callback(self.cell_edited)

        self.tag_models = {}

        self.insert_nodes(self._data.instruments)

    def refresh(self):
        """
        Reverts back any change on the UI that has not been committed to the database.
        """
        changes = self._data.get_changes()

        for item in changes.keys():
            tokens = item.split('/')
            node = self._instruments.root_node()
            for token in tokens:
                found = False
                index = 0
                while not found and index < node.count():
                    child = node.get_child(index)
                    if child.get_string(0) == token:
                        node = child
                        found = True
                    else:
                        index += 1

                if not found:
                    node = None

            if node:
                for change in changes[item]:
                    (att, old, new) = change
                    column = 1 if att == 'enabled' else 2
                    node.set_bool(column, old)

    def insert_nodes(self, model, parent = None):
        """
        Inserts nodes based on the model which is an instance
        of PSInstrumentGroup.
        """
        for child in model.keys():
            node = None
            tag = child
            if parent:
                node = parent.add_child()
                tag = '/'.join([parent.get_tag(), child])
            else: 
                node = self._instruments.add_node()

            # Creates the node
            node.set_int(1, model[child].enabled)
            node.set_int(2, model[child].timed)
            node.set_string(0, child)
            node.set_tag('%s' % tag)

            update_value_cb = lambda attr, value, node = node: self.model_value_set_callback(attr, value, node)
            model[child].set_value_set_notification(update_value_cb)

            # Fills the node/model directory
            self.tag_models[tag] = model[child]

            # Inserts children
            if not model[child].has_key('_data_'):
                self.insert_nodes(model[child], node)



    def model_value_set_callback(self, attr, value, node):
        """
        Callback to update the UI when an element of the
        model has been changed.
        """
        if attr == 'enabled':
            node.set_int(1, value)
        elif attr == 'timed':
            node.set_int(2, value)
        

    def cell_edited(self, node, column, value):
        """
        Callback used to updat the model when an element
        of the UI has been updated.
        """
        if column in [1,2]:
            # Gets the associated node's model
            model = self.tag_models[node.get_tag()]


            value = 1 if value == "1" else 0
            attr = "enabled" if column == 1 else "timed"

            # Performs the update at the model
            model.set_state(attr, value)

        else:
            node.set_string(column, value)


class SetupDataCollection(mforms.Box):
    def __init__(self, owner, data, variables):
        mforms.Box.__init__(self, False)
        self.set_managed()
        self.set_release_on_add()
        self.set_spacing(12)
        self.set_padding(12)

        self.owner = owner
        self._data = data
        self._variables = variables

        self.row_labels = []
        self.row_labels.append('Current events')
        self.row_labels.append('History (%s events)')
        self.row_labels.append('Long History (%s events)')

        self.variables = []
        self.variables.append('events_%s_current')
        self.variables.append('events_%s_history')
        self.variables.append('events_%s_history_long')

        self._controls = {}
        self._child_controls = {}

        self.create_ui()

    def update_data_collection(self, name, caller):
        is_active = caller.get_active()
        self._data[name].enabled = is_active

        # Enables/disables child controls based on paren's status
        if self._child_controls.has_key(caller):
            for child in self._child_controls[caller]:
                child.set_enabled(is_active)


    def create_dc_checkbox(self, container, var_lbl, parent = None):

        # Determines if this control should be enabled or not
        enabled = True
        if parent:
            enabled = parent.get_active()

        checkbox = mforms.newCheckBox()
        checkbox.set_text(var_lbl[1])
        checkbox.set_enabled(enabled)
        checkbox.set_active(self._data[var_lbl[0]].enabled)

        checkbox.add_clicked_callback(lambda: self.update_data_collection(var_lbl[0], checkbox))
        
        self._controls[var_lbl[0]] = checkbox

        # If the control has a parent, adds the relation
        if parent:
            if not self._child_controls.has_key(parent):
                self._child_controls[parent] = []
            
            self._child_controls[parent].append(checkbox)

        # Finally adds the control on the container
        container.add(checkbox, False, False)

        return checkbox


    def create_section_row(self, container, element, name, records, enabled = True):
        checkbox = mforms.newCheckBox()
        if records:
            caption =  self.row_labels[element] % self._variables['performance_schema_' + name + '_size'].value
        else:
            caption = self.row_labels[element]

        checkbox.set_text(caption)
        checkbox.set_enabled(bool(enabled))

        checkbox.set_active(self._data[name].enabled)

        checkbox.add_clicked_callback(lambda: self.update_data_collection(name, checkbox))
        self.ui_fields[name] = checkbox

        container.add(checkbox, False, False)

        return checkbox

    def get_var_name_and_label(self, base_name, index, var_prefix_name = ""):
        # Index equal to -1 indicates label should be retrieved from the varname
        if index == -1:

            var = base_name
            if var_prefix_name:
                var = '_'.join([var_prefix_name, base_name])

            tokens = base_name.split('_')
            caption = ' '.join(tokens).capitalize()
        # Index equal to 0 indicates data for a Current consumer was requested
        elif index == 0:
            var = self.variables[index] % base_name
            caption = self.row_labels[index]
        # Finally data for one of the history consumers has been requested
        else:
            var = self.variables[index] % base_name
            caption =  self.row_labels[index] % self._variables['performance_schema_' + var + '_size'].value

        return var, caption


    def create_section(self, container, base_name, elements = None):
        panel = mforms.newPanel(mforms.TitledBoxPanel)
        if elements:
            if base_name == 'events_waits_summary': #XXX where are these shown?
                panel.set_title('Wait Events Summary')
            elif base_name == 'file_summary':
                panel.set_title('File Summary')
        else:
            panel.set_title("%s Events" % base_name[:-1].capitalize())

        vbox = mforms.newBox(False)
        vbox.set_padding(8)
        vbox.set_spacing(8)

        if elements:
            for element in elements:
                self.create_dc_checkbox(vbox, self.get_var_name_and_label(element, -1, base_name))

        else:
            current_ctrl = self.create_dc_checkbox(vbox, self.get_var_name_and_label(base_name, 0))
            self.create_dc_checkbox(vbox, self.get_var_name_and_label(base_name, 1), current_ctrl)
            self.create_dc_checkbox(vbox, self.get_var_name_and_label(base_name, 2), current_ctrl)
        
        panel.add(vbox)

        container.add(panel, True, True)

        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            panel.set_enabled(self.thread_check.get_active())

            # Adds the panel as a child of the thread check
            self._child_controls[self.thread_check].append(panel)


    def create_ui(self):
        l = mforms.newLabel("Select performance_schema Event Types to be Collected")
        l.set_style(mforms.BoldStyle)
        self.add(l, False, True)

        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            description = "Performance Schema generates execution events only if a consumer for such events is active. A consumer is considered active if it is\n"\
                          "enabled and the consumers it depends on are active based on the next hierarchy:\n\n"\
                          "- Global Instrumentation is the top level consumer.\n"\
                          "- Thread Instrumentation and Statement Digest depend on Global Instrumentation.\n"\
                          "- The Current consumers for Statement, Stage and Wait events depend on Thread Instrumentation\n"\
                          "- The History and History Long consumers depend on it's associated Current consumer\n\n"\
                          "The options you toggle determine which of the performance_schema.events_* tables are fed with data"
        else:
            description = "Performance Schema generates execution events only if a consumer for such events is active."

        desc_label = mforms.newLabel(description)

        self.add(desc_label, False, False)

        # Container will be the main box itself if global instrumentation
        # is unavailable, but will be changed to an inner box otherwise
        # This to ease enabling/disabling the rest of the controls
        # based on Global Instrumentation state
        self.__container = self

        # Global Instrumentation was available up to 5.6
        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 0):
            self.global_check = self.create_dc_checkbox(self, self.get_var_name_and_label('global_instrumentation', -1))

            # Creates an inner box for the rest of the controls
            self.__container = mforms.newBox(False)
            self.add(self.__container, False, False)
            self._child_controls[self.global_check] = [self.__container]

        # Adds the thread instrumentation checkbox 
        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            self.thread_check = self.create_dc_checkbox(self.__container, self.get_var_name_and_label('thread_instrumentation', -1))
            self._child_controls[self.thread_check] = []

        # Adds the statements, stages and waits sections
        hbox = mforms.newBox(True)
        hbox.set_padding(12)
        hbox.set_spacing(12)

        # PS monitoring for statements and stages was available up to 5.6.3
        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            self.create_section(hbox, 'statements')
            self.create_section(hbox, 'stages')

        self.create_section(hbox, 'waits')
        if self.owner.target_version.is_supported_mysql_version_at_least(5, 7, 3):
            self.create_section(hbox, 'transactions')

        self.__container.add(hbox, False, False)

        if not self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            self.create_section(hbox, 'events_waits_summary', ['by_thread_by_event_name', 'by_event_name', 'by_instance'])
            self.create_section(hbox, 'file_summary', ['by_event_name', 'by_instance'])

        # Finally adds the statements digets controls
        # Statements digest was also available until 5.6.5
        if self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 5):
            digest_description = "Digesting normalizes statements in a way that permits grouping similar statements and collecting information about how often they occur.\n"
            digest_label = mforms.newLabel(digest_description)

            self.__container.add(digest_label, False, False)

            self.create_dc_checkbox(self.__container, self.get_var_name_and_label('statements_digest', -1))

    def refresh(self):
        """
        Reverts back any change on the UI that has not been committed to the database.
        """
        changes = self._data.get_changes()

        for item in changes.keys():
            # There's only one possible change
            (att, old, new) = changes[item][0]

            # This validation is needed cuz not all the consumers
            # have a checkbox on this screen
            if self._controls.has_key(item):
                item_ctrl = self._controls[item]

                item_ctrl.set_active(bool(old))

                if self._child_controls.has_key(item_ctrl):
                    for control in self._child_controls[item_ctrl]:
                        control.set_enabled(bool(old))
    
    def add_child_control(self, parent_id, child):
        parent = None

        if parent_id == 'thread':
            parent = self.thread_check

        if parent:
            self._child_controls[parent].append(child)
                        
class ActorInfoDialog(mforms.Form):
    def __init__(self, validate_cb = None):
        mforms.Form.__init__(self, None)
        self.set_title("Monitor Events for User")
        self._validate = validate_cb

        vbox = mforms.newBox(False)
        vbox.set_padding(20)
        vbox.set_spacing(18)

        caption = 'Define the user/host for which performance schema will monitor for events.\n'\
                  'You can use the % to indicate any user and/or any hosts.'

        l = mforms.newLabel(caption)
        vbox.add(l, False, True)

        table = mforms.newTable()
        table.set_padding(1)
        table.set_row_count(3)
        table.set_column_count(2)
        table.set_column_spacing(7)
        table.set_row_spacing(8)

        self.user = mforms.newTextEntry()
        table.add(mforms.newLabel("User:", True), 0, 1, 0, 1, mforms.HFillFlag)
        table.add(self.user, 1, 2, 0, 1, mforms.HFillFlag|mforms.HExpandFlag)

        self.host = mforms.newTextEntry()
        table.add(mforms.newLabel("Host:", True), 0, 1, 1, 2, mforms.HFillFlag)
        table.add(self.host, 1, 2, 1, 2, mforms.HFillFlag|mforms.HExpandFlag)

        bbox = mforms.newBox(True)
        bbox.set_spacing(8)
        self.ok = mforms.newButton()
        self.ok.set_text("OK")

        self.cancel = mforms.newButton()
        self.cancel.set_text("Cancel")
        vbox.add(table, True, True)

        mforms.Utilities.add_end_ok_cancel_buttons(bbox, self.ok, self.cancel)

        vbox.add_end(bbox, False, True)

        self.set_content(vbox)

        self.set_size(500, 200)
        self.center()


    def run(self):
        while True:
            cancelled = True
            if self.run_modal(self.ok, self.cancel):
                cancelled = False
                user = self.user.get_string_value()
                host = self.host.get_string_value()
                error = self._validate(user, host)
                if error is None:
                    return (user, host)
                else:
                    mforms.Utilities.show_message("User/Host Definition Error", error, "Ok", "", "")
            # Only exists if the answer was valid
            # Or the user cancelled
            if cancelled or error is None:
                break
        return None


class ObjectInfoDialog(mforms.Form):
    def __init__(self, owner, validate_cb = None):
        mforms.Form.__init__(self, None)
        self.set_title("Monitor Events Database Object")
        self.owner = owner
        self._validate = validate_cb

        vbox = mforms.newBox(False)
        vbox.set_padding(20)
        vbox.set_spacing(18)

        caption = 'Define the database objects which performance schema will monitor for events.\n'\
                  'You can use the % to indicate any schema and/or any object name.'

        l = mforms.newLabel(caption)
        vbox.add(l, False, True)

        table = mforms.newTable()
        table.set_padding(1)
        table.set_row_count(5)
        table.set_column_count(4)
        table.set_column_spacing(7)
        table.set_row_spacing(8)

        self.type = mforms.newSelector()

        # Before 5.7.2 the only available value was TABLE
        if not self.owner.target_version.is_supported_mysql_version_at_least(5, 7, 2):
            self.type.add_item('TABLE')
            self.type.set_enabled(False)
        else:
            self.type.add_items(['EVENT', 'FUNCTION', 'PROCEDURE', 'TABLE', 'TRIGGER'])

        table.add(mforms.newLabel("Type:", True), 0, 1, 0, 1, mforms.HFillFlag)
        table.add(self.type, 1, 2, 0, 1, mforms.HFillFlag|mforms.HExpandFlag)

        self.schema = mforms.newTextEntry()
        table.add(mforms.newLabel("Schema:", True), 0, 1, 1, 2, mforms.HFillFlag)
        table.add(self.schema, 1, 2, 1, 2, mforms.HFillFlag|mforms.HExpandFlag)

        self.name = mforms.newTextEntry()
        table.add(mforms.newLabel("Name:", True), 0, 1, 2, 3, mforms.HFillFlag)
        table.add(self.name, 1, 2, 2, 3, mforms.HFillFlag|mforms.HExpandFlag)

        chbox = mforms.newBox(True)

        # Previous to 5.6.3 PS was enabled for all records matching an
        # object definition on the table, after 5.6.3 the field was included
        self.enabled = mforms.newCheckBox()
        self.enabled.set_text('Enabled')
        if not self.owner.target_version.is_supported_mysql_version_at_least(5, 6, 3):
            self.enabled.set_active(True)
            self.enabled.set_enabled(False)

        chbox.add(self.enabled, True, True)
        

        self.timed = mforms.newCheckBox()
        self.timed.set_text('Timed')
        chbox.add(self.timed, True, True)

        table.add(chbox, 1, 3, 3, 4, mforms.HFillFlag|mforms.HExpandFlag)

        bbox = mforms.newBox(True)
        bbox.set_spacing(8)
        self.ok = mforms.newButton()
        self.ok.set_text("OK")

        self.cancel = mforms.newButton()
        self.cancel.set_text("Cancel")
        vbox.add(table, True, True)

        mforms.Utilities.add_end_ok_cancel_buttons(bbox, self.ok, self.cancel)

        vbox.add_end(bbox, False, True)

        self.set_content(vbox)

        self.set_size(500, 260)
        self.center()


    def run(self):
        while True:
            cancelled = True
            if self.run_modal(self.ok, self.cancel):
                cancelled = False
                type = self.type.get_string_value()
                schema = self.schema.get_string_value()
                name = self.name.get_string_value()
                enabled = self.enabled.get_active()
                timed = self.timed.get_active()
                error = self._validate(type, schema, name)
                if error is None:
                    return (type, schema, name, enabled, timed)
                else:
                    mforms.Utilities.show_message("Database Object Definition Error", error, "Ok", "", "")
            # Only exists if the answer was valid
            # Or the user cancelled
            if cancelled or error is None:
                break
        return None

class SetupFiltering(mforms.Box):
    def __init__(self, owner, actors, objects):
        mforms.Box.__init__(self, False)
        self.set_spacing(12)
        self.set_padding(12)

        self.set_managed()
        self.set_release_on_add()

        self.owner = owner
        self._actors = actors
        self._objects = objects
        
        self.create_ui()

    @property
    def target_version(self):
        return self.owner.target_version

    def make_description_box(self, text, tooltip):
        box = mforms.newBox(True)
        box.set_spacing(12)
        box.add(mforms.newLabel(text), False, False)
        l = mforms.newImageBox()
        l.set_image(mforms.App.get().get_resource_path("mini_notice.png"))
        l.set_tooltip(tooltip)
        box.add(l, False, False)
        return box

    def create_ui(self):
        l = mforms.newLabel("Filter Users and Objects to be Monitored")
        l.set_style(mforms.BoldStyle)
        self.add(l, False, True)

        user_panel = mforms.newPanel(mforms.TitledBoxPanel)
        user_panel.set_title('Users')

        users_box = mforms.newBox(False)
        users_box.set_spacing(12)
        users_box.set_padding(12)

        self.users = mforms.newTreeNodeView(mforms.TreeFlatList|mforms.TreeAltRowColors)
        self.users.add_column(mforms.StringColumnType, "User", 150, False)
        self.users.add_column(mforms.StringColumnType, "Host", 150, False)
        self.users.end_columns()
        self.users.add_changed_callback(self.selected_user_changed)
        users_box.add(self.users, True, True)

        user_buttons = mforms.newBox(True)
        user_buttons.set_spacing(12)
        
        description = ('Performance Schema allows defining filters to determine the connections for which data will be collected.\n'
                       'New connections having a user@host matching an entry below will be enabled for monitoring.')
        tooltip = 'Use % to indicate either any user or any host.'
        user_buttons.add(self.make_description_box(description, tooltip), False, False)

        self.user_del_button = mforms.newButton()
        self.user_del_button.set_text('Remove')
        self.user_del_button.add_clicked_callback(self.remove_user)
        self.user_del_button.set_enabled(False)
        user_buttons.add_end(self.user_del_button, False, False)

        user_add_button = mforms.newButton()
        user_add_button.set_text('Add')
        user_add_button.add_clicked_callback(self.add_user)
        user_buttons.add_end(user_add_button, False, False)

        users_box.add(user_buttons, False, False)
        
        user_panel.add(users_box)

        self.add(user_panel, True, True)

        # Database Objects Table
        db_panel = mforms.newPanel(mforms.TitledBoxPanel)
        db_panel.set_title('Database Objects')

        db_box = mforms.newBox(False)
        db_box.set_spacing(12)
        db_box.set_padding(12)
        
        self.objects = mforms.newTreeNodeView(mforms.TreeFlatList|mforms.TreeAltRowColors)
        self.objects.add_column(mforms.StringColumnType, "Type", 100, False)
        self.objects.add_column(mforms.StringColumnType, "Schema", 200, False)
        self.objects.add_column(mforms.StringColumnType, "Object", 200, False)
        self.objects.add_column(mforms.CheckColumnType, "Enabled", 50, False)
        self.objects.add_column(mforms.CheckColumnType, "Timed", 50, False)
        self.objects.end_columns()
        self.objects.add_changed_callback(self.selected_object_changed)
        self.objects.set_cell_edited_callback(self.object_edited)
        db_box.add(self.objects, True, True)

        db_buttons = mforms.newBox(True)
        db_buttons.set_spacing(12)
        
        description = ('Performance Schema allows defining filters to determine the objects for which data will be collected.\n' 
                       'Any schema/object matching a combination defined above will be enabled for monitoring.')
        
        tooltip = ('Use % to indicate either any schema or any object.\n\n'
                   'The enabled column indicates whether events for the matching objects are instrumented.\n'
                   'The timed column indicates whether information about the events duration is recorded.')
        
        
        db_buttons.add(self.make_description_box(description, tooltip), False, False)

        self.db_del_button = mforms.newButton()
        self.db_del_button.set_text('Remove')
        self.db_del_button.add_clicked_callback(self.remove_object)
        self.db_del_button.set_enabled(False)
        db_buttons.add_end(self.db_del_button, False, False)

        db_add_button = mforms.newButton()
        db_add_button.set_text('Add')
        db_add_button.add_clicked_callback(self.add_object)
        db_buttons.add_end(db_add_button, False, False)

        db_box.add(db_buttons, False, False)

        db_panel.add(db_box)

        
        self.add(db_panel, True, True)

        self.refresh()

    def refresh(self):
        self.users.clear()

        for item in self._actors:
            node = self.users.add_node()
            node.set_string(0, item.user)
            node.set_string(1, item.host)

        self.objects.clear()

        for item in self._objects:
            node = self.objects.add_node()
            node.set_string(0, item.type)
            node.set_string(1, item.schema)
            node.set_string(2, item.name)
            node.set_bool(3, item.enabled)
            node.set_bool(4, item.timed)

    def selected_user_changed(self):
        node = self.users.get_selected_node()
        enabled = node is not None

        self.user_del_button.set_enabled(enabled)

    def remove_user(self):
        node = self.users.get_selected_node()
        if not node is None:
            user = node.get_string(0)
            host = node.get_string(1)
            self._actors.remove(PSActor(user, host))
            node.remove_from_parent()

            self.selected_user_changed()

    def add_user(self):
        dlg = ActorInfoDialog(self.validate_user)
        actor = dlg.run()

        if actor:
            node = self.users.add_node()
            node.set_string(0, actor[0])
            node.set_string(1, actor[1])

            self._actors.append(PSActor(actor[0], actor[1]))

    def validate_user(self, user, host):
        error = None

        if user == '' and host == '':
            error = 'Both user and host are required fields'
        elif self._actors.count(PSActor(user, host)) != 0:
            error = 'The specified user/host is already setup for performance schema data collection.'

        return error

    def selected_object_changed(self):
        node = self.objects.get_selected_node()
        enabled = node is not None

        self.db_del_button.set_enabled(enabled)

    def object_edited(self, node, column, value):
        """
        This method will be used to enable/disable the instruments.
        """
        if column in [3,4]:
            # Not validating the possible assertion as the edited
            # object MUST exist on the list
            index = self._objects.index(PSObject(node.get_string(0), node.get_string(1), node.get_string(2), None, None))
            
            set = True if value == "1" else False

            if column == 3:
                self._objects[index].enabled = set
            else:
                self._objects[index].timed = set

            node.set_bool(column, set)  

    def remove_object(self):
        node = self.objects.get_selected_node()
        if not node is None:
            type = node.get_string(0)
            schema = node.get_string(1)
            name = node.get_string(2)
            self._objects.remove(PSObject(type, schema, name, None, None))
            node.remove_from_parent()

            self.selected_object_changed()

    def add_object(self):
        dlg = ObjectInfoDialog(self, self.validate_object)
        object = dlg.run()

        if object:
            node = self.objects.add_node()
            node.set_string(0, object[0])
            node.set_string(1, object[1])
            node.set_string(2, object[2])
            node.set_bool(3, object[3])
            node.set_bool(4, object[4])

            self._objects.append(PSObject(object[0], object[1], object[2], object[3], object[4]))

    def validate_object(self, type, schema, name):
        error = None

        if schema == '' or name == '':
            error = 'The next fields are required: object, name.'
        elif self._objects.count(PSObject(type, schema, name, None, None)) != 0:
            error = 'The specified database object is already setup for performance schema data collection.'

        return error


class SetupOptions(mforms.Box):
    def __init__(self, owner, timers, timer_types):
        mforms.Box.__init__(self, False)

        self.set_spacing(12)
        self.set_padding(12)

        self.set_managed()
        self.set_release_on_add()

        self._timer_types = timer_types

        self.owner = owner
        self._timers = timers
        self._timer_names = self._timers.keys()
        self._timer_names.sort()

        self._controls = {}
        self._descriptions = {}
        self._descriptions['CYCLE']= 'Occurs based on the CPU speed - frequency: %s, resolution: %s, overhead: %s'
        self._descriptions['TICK']= 'It is an established frequency on each platform - frequency: %s, resolution: %s, overhead: %s.'
        self._descriptions['MILLISECOND']= 'Occurs a thousand times in a second - frequency: %s, resolution: %s, overhead: %s.'
        self._descriptions['MICROSECOND']= 'Occurs a million times in a second - frequency: %s, resolution: %s, overhead: %s.'
        self._descriptions['NANOSECOND']= 'Occurs a billion times in a second - frequency: %s, resolution: %s, overhead: %s.'

        self.create_ui()


    def update_timer(self, name, caller):
        value = caller.get_string_value().split()[0]
        value = value[:-1].upper()
        self._timers[name].timer = value

    def get_timer_type_text(self, timer):
        description = self._descriptions[timer.name] % (timer.frequency, timer.resolution, timer.overhead)
        return "%ss        (%s)" % (timer.name.lower().capitalize(), description)
    
    def create_timer_row(self, table, offset):
        table.add(mforms.newLabel("%s Events" % (self._timer_names[offset].capitalize()), True), 0, 1, offset, offset + 1, mforms.HFillFlag)

        selector = mforms.newSelector(mforms.SelectorPopup)
        selector.add_items([self.get_timer_type_text(timer) for timer in self._timer_types])

        timer_name = self._timer_names[offset]
        ps_timer = self._timers[timer_name].timer

        index = self._timer_types.index(PSTimerType(ps_timer, None, None, None))

        selector.set_selected(index)
        selector.add_changed_callback(lambda: self.update_timer(self._timer_names[offset], selector))

        self._controls[timer_name] = selector

        table.add(selector, 1, 2, offset, offset + 1, mforms.HFillFlag|mforms.HExpandFlag)

        
    def create_ui(self):
        panel = mforms.newPanel(mforms.TitledBoxPanel)
        panel.set_title("Event Timers")

        vbox = mforms.newBox(False)
        vbox.set_padding(12)

        description = "Instruments measure the duration of events, for which they can use different timers.\n"\
                      "A timer has characteristics that need to be considered when setting up the timer to be used on the\n"\
                      "different instruments:\n"\
                      "\n"\
                      "     - Frequency: Indicates the number of timer units per second.\n"\
                      "     - Resolution: Indicates the size used to increase a timer value at a time.\n"\
                      "     - Overhead: Minimal number of cycles of overhead to obtain one timing.\n"\
                      "\n"\
                      "Here you can configure which timer will be used for each instrument type.\n\n"
        vbox.add(mforms.newLabel(description), False, False)

        table = mforms.newTable()
        table.set_padding(1)
        table.set_row_count(5)
        table.set_column_count(2)
        table.set_column_spacing(7)
        table.set_row_spacing(8)

        for i in range(len(self._timer_names)):
            self.create_timer_row(table, i)

        vbox.add(table, False, False)
        
        panel.add(vbox)

        self.add(panel, False, False)

    def refresh(self):
        for name in self._controls:
            ps_timer = self._timers[name].timer
            index = self._timer_types.index(PSTimerType(ps_timer, None, None, None))
            self._controls[name].set_selected(index)


class IntroPage(mforms.ScrollPanel):
    def __init__(self, owner):
      mforms.ScrollPanel.__init__(self, 0)
      self.set_managed()
      self.set_release_on_add()

      self.box = mforms.newBox(False)
      self.box.set_spacing(12)
      self.box.set_padding(20)

      l = mforms.newLabel("Performance Schema Basics")
      l.set_style(mforms.BigBoldStyle)
      self.box.add(l, False, True)
      
      self.box.add(mforms.newLabel("""The performance schema collects data from various aspects of MySQL performance and gives
very detailed information about what exactly is happening inside your MySQL database server.
For each statement executed, the PS instruments will gather various statistics and timing information in different
levels of granularity and from different subsystems, from network to disk storage, and keep them in the 
performance_schema.events_* tables."""), False, True)

      image = mforms.newImageBox()
      image.set_image(mforms.App.get().get_resource_path("ps_overview.png"))
      self.box.add(image, False, True)

      label = mforms.newLabel("Configuring Performance Schema")
      label.set_style(mforms.BoldStyle)
      self.box.add(label, False, True)

      self.box.add(mforms.newLabel("""To control the trade-off between data collected and overhead, the performance schema gives
you a few fine grained configuration options.
 
You can configure what, when and how much will be instrumented by the Performance Schema by tweaking three option categories:

  * Actors - filters the users, hosts and DB objects to collect data for the performance_schema. This was introduced in MySQL 5.6.
    
  * Instruments - allow fine-tuning of what kind of stats are gathered for whatever is being monitored

  * Consumers - toggle which of the performance_schema.event_* tables should be filled

You can also use the simplified configuration interface for one-click setup of the performance schema for some common use cases.
"""), False, True)

      label = mforms.newLabel("Performance Schema Instrument Types")
      label.set_style(mforms.BoldStyle)
      self.box.add(label, False, True)

      self.box.add(mforms.newLabel("""The Performance Schema will monitor the following types of events that occur internally in the MySQL server, as client requests are executed.

  * Statement monitoring starts when a new request arrives into a server thread and ends once all processing is complete.
    These are logged in the event_statement_* tables.

  * Stage monitors the different stages that occur during the statement execution. Depends on Thread Instrumentation.
    These are logged in the event_stage_* tables.

  * Wait monitoring is focused on wait events that occur during the statement execution.
    These are logged in the event_wait_* tables.


For more information, refer to the MySQL manuals section on the Performance Schema.
"""), False, True)

      self.add(self.box)
        

class SetupThreads(mforms.Box):
    def __init__(self, data):
        mforms.Box.__init__(self, False)
        self.set_spacing(12)
        self.set_padding(12)

        self.set_managed()
        self.set_release_on_add()

        self._threads = data
        
        self.create_ui()

    def create_ui(self):
        l = mforms.newLabel("Threads to Instrument")
        l.set_style(mforms.BoldStyle)
        self.add(l, False, True)
        
        description = mforms.newLabel('Performance Schema allows enabling/disabling the monitoring of events that occur on specific threads in the server.\n\n'
            'Changes on the instrumentation status are only effective if Thread Instrumentation is enabled on the Consumers Tab.')

        self.add(description, False, False)

        self.threads = mforms.newTreeNodeView(mforms.TreeFlatList|mforms.TreeAltRowColors)
        self.threads.add_column(mforms.LongIntegerColumnType, "Id", 50, False)
        self.threads.add_column(mforms.StringColumnType, "Name", 250, False)
        self.threads.add_column(mforms.CheckColumnType, "Instrumented", 80, True)
        self.threads.add_column(mforms.StringColumnType, "Type", 50, False)
        self.threads.add_column(mforms.StringColumnType, "Process Id", 80, False)
        self.threads.add_column(mforms.StringColumnType, "Account", 100, False)
        self.threads.add_column(mforms.StringColumnType, "Command", 100, False)
        self.threads.add_column(mforms.StringColumnType, "Time", 80, False)
        self.threads.add_column(mforms.StringColumnType, "State", 100, False)
        self.threads.add_column(mforms.StringColumnType, "Info", 100, False)
        self.threads.add_column(mforms.StringColumnType, "DB", 100, False)
        self.threads.end_columns()
        self.threads.set_cell_edited_callback(self.thread_edited)
        self.add(self.threads, True, True)

        self.refresh()

        bbox = mforms.newBox(True)
        bbox.set_spacing(12)
        
        self._enable_all = mforms.newButton()
        self._enable_all.set_text('Instrument All')
        self._enable_all.add_clicked_callback(self.enable_all)
        bbox.add(self._enable_all, False, True)

        self._disable_all = mforms.newButton()
        self._disable_all.set_text('Instrument None')
        self._disable_all.add_clicked_callback(self.enable_none)
        bbox.add(self._disable_all, False, True)

        self._refresh_button = mforms.newButton()
        self._refresh_button.set_text('Refresh')
        self._refresh_button.add_clicked_callback(self.refresh_data)

        bbox.add_end(self._refresh_button, False, False)
        self.add(bbox, False, False)

    def refresh_data(self):
        # Refreshes the model class from the database
        self._threads.refresh()

        # Refreshes the frontend
        self.refresh()
    
    def enable_all(self):
        root = self.threads.root_node()
        for row in range(self.threads.count()):
            node = root.get_child(row)
            self._threads[node.get_long(0)].instrumented = True
            node.set_bool(2, True)

    def enable_none(self):
        root = self.threads.root_node()
        for row in range(self.threads.count()):
            node = root.get_child(row)
            self._threads[node.get_long(0)].instrumented = False
            node.set_bool(2, False)

    def refresh(self):
        """
        Refreshes the UI based on the model class
        """
        self.threads.clear()

        items = self._threads.keys()
        items.sort()
        for item in items:
            node = self.threads.add_node()
            node.set_long(0, self._threads[item].id)
            node.set_string(1, self._threads[item].name)
            node.set_bool(2, self._threads[item].instrumented)
            if self._threads[item].thread_type == 'BACKGROUND':
                node.set_string(3, "BG")
            elif self._threads[item].thread_type == 'FOREGROUND':
                node.set_string(3, "FG")
            else:
                node.set_string(3, self._threads[item].thread_type)
            node.set_string(4, self._threads[item].plist_id)
            if self._threads[item].thread_type == 'BACKGROUND':
                node.set_string(5, "<system>")
            else:
                node.set_string(5, "%s@%s" % (self._threads[item].plist_user, self._threads[item].plist_host))
            node.set_string(6, self._threads[item].plist_command)
            node.set_string(7, self._threads[item].plist_time)
            node.set_string(8, self._threads[item].plist_state)
            node.set_string(9, self._threads[item].plist_info)
            node.set_string(10, self._threads[item].plist_db)

    def thread_edited(self, node, column, value):
        """
        This method will be used to enable/disable the instruments.
        """
        if column == 2:
            value = True if value == "1" else False
            self._threads[node.get_long(0)].instrumented = value
            
            node.set_bool(2, value)  


class WbAdminPerformanceSchemaInstrumentation(WbAdminPSBaseTab, ChangeCounter):
    min_server_version = (5,6,6)

    @classmethod
    def wba_register(cls, admin_context):
        admin_context.register_page(cls, "wba_performance", "Performance Schema Setup", False)
    
    @classmethod
    def identifier(cls):
        return "admin_instrumentation_setup"

    def __init__(self, ctrl_be, instance_info, main_view):
        WbAdminPSBaseTab.__init__(self, ctrl_be, instance_info, main_view)
        ChangeCounter.__init__(self)

        self.ctrl_be = ctrl_be
        self.content = None


    @property
    def target_version(self):
        return self.ctrl_be.target_version


    def count_change(self, change, attr, value):
        ChangeCounter.count_change(self, change, attr, value)
        
        # Enables/Disables the buttons accordingly
        self.apply_button.set_enabled(self.change_count != 0)
        #self.cancel_button.set_enabled(self.change_count)

    def create_ui(self):
        self.advanced_button = mforms.newButton()
        self.advanced_button.add_clicked_callback(self.switch_advanced)
        self.advanced_button.set_text("Show Advanced")
        self.create_basic_ui("title_instrumentation_setup.png", "Performance Schema - Setup", self.advanced_button)

        self.content = mforms.newBox(False)
        self.config_tab = mforms.TabView(mforms.TabViewSystemStandard)

        self.easy_setup = EasySetupPage(self)
        self.config_tab.add_page(self.easy_setup, "Easy Setup")
        self.content.add(self.config_tab, True, True)

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

        self._showing_advanced = True
        self.switch_advanced()


    def switch_config_tabs(self, flag):
        if flag:
            self.config_tab.add_page(self.intro, "Introduction")
            self.config_tab.add_page(self.instruments, "Instruments")
            self.config_tab.add_page(self.data_collection, "Consumers")
            if self.filtering:
                self.config_tab.add_page(self.filtering, "Actors & Objects")
            if self.threads:
                self.config_tab.add_page(self.threads, "Threads")
            self.config_tab.add_page(self.options, "Options")
        else:
            self.config_tab.remove_page(self.intro)
            self.config_tab.remove_page(self.instruments)
            self.config_tab.remove_page(self.data_collection)
            if self.filtering:
              self.config_tab.remove_page(self.filtering)
            if self.threads:
              self.config_tab.remove_page(self.threads)
            self.config_tab.remove_page(self.options)


    def switch_advanced(self):
        self._showing_advanced = not self._showing_advanced

        if self._showing_advanced:
            self.switch_config_tabs(True)
            self.apply_button.show(True)
            self.cancel_button.show(True)
        else:
            self.switch_config_tabs(False)
            self.apply_button.show(False)
            self.cancel_button.show(False)

        self.advanced_button.set_text("Hide Advanced" if self._showing_advanced else "Show Advanced")


    def rebuild_ui(self, from_scratch):
        if not from_scratch:
            self.switch_config_tabs(False)

        self.easy_setup.reload()

        # The configuration will be needed to create the UI
        self.config_data = PSConfiguration(self.ctrl_be)
        self.config_data.load()
        self.count_changes_on(self.config_data)

        self.intro = IntroPage(self)

        self.instruments = SetupInstruments(self, self.config_data.sections['instruments'])

        self.data_collection = SetupDataCollection(self, self.config_data.sections['consumers'], self.config_data.variables)


        # setup_actors and setup_objects was available up to 5.6
        if self.ctrl_be.target_version.is_supported_mysql_version_at_least(5, 6):
            self.filtering = SetupFiltering(self, self.config_data.sections['actors'], self.config_data.sections['objects'])
        else:
            self.filtering = None

        # setup_actors and setup_objects was available up to 5.6
        if self.ctrl_be.target_version.is_supported_mysql_version_at_least(5, 6):
            self.threads = SetupThreads(self.config_data.sections['threads'])
            
            self.data_collection.add_child_control('thread', self.threads)
        else:
            self.threads = None

        self.options = SetupOptions(self, self.config_data.sections['timers'], self.config_data.timer_types)


        if from_scratch:
            self.buttons = mforms.newBox(True)
            self.buttons.set_padding(6)
            self.buttons.set_spacing(12)

            self.reset_button = mforms.newButton()
            self.reset_button.set_tooltip("Resets/truncates all performance_schema event tables, deleting all performance schema data collected so far.")
            self.reset_button.set_text('Clear Event Tables')
            self.reset_button.add_clicked_callback(self.on_reset_button_clicked)
            self.buttons.add(self.reset_button, False, True)

            self.apply_button = mforms.newButton()
            self.apply_button.set_text('Apply')
            self.apply_button.set_tooltip("Apply changes to the performance_schema configurations to the MySQL server.")
            self.apply_button.add_clicked_callback(self.on_apply_button_clicked)
            self.apply_button.set_enabled(False)

            self.cancel_button = mforms.newButton()
            self.cancel_button.set_text('Revert')
            self.cancel_button.set_tooltip("Revert to the configuration currently saved in the MySQL server.")
            self.cancel_button.add_clicked_callback(self.on_revert_button_clicked)
            #self.cancel_button.set_enabled(False)

            self.buttons.add_end(self.apply_button, False, False)
            self.buttons.add_end(self.cancel_button, False, False)

            if self.ctrl_be.target_version.is_supported_mysql_version_at_least(5, 6):
                self.reset_to_factory_button = mforms.newButton()
                self.reset_to_factory_button.set_tooltip("Reset all performance_schema settings (instruments, threads, actors, objects etc) to factory defaults.")
                self.reset_to_factory_button.set_text('Full Reset to Factory Defaults')
                self.reset_to_factory_button.add_clicked_callback(self.on_reset_to_factory_button_clicked)
                self.buttons.add_end(self.reset_to_factory_button, False, True)

            self.content.add(self.buttons, False, False)

        if not from_scratch and self._showing_advanced:
            self.switch_config_tabs(True)


    def on_revert_button_clicked(self):
        # revert changes to DB state by rebuilding the UI
        tab = self.config_tab.get_active_tab()
        self.rebuild_ui(False)
        self.config_tab.set_active_tab(tab)


    def on_cancel_button_clicked(self):
        ## XXX unused for now, this only changes the UI state, when we need to reload the state from DB and
        # display, which is not currently supported

        # Reverts the changes at UI level
        self.refresh()
        self.instruments.refresh()
        self.data_collection.refresh()

        # Reverts the changes in the model
        self.config_data.revert_changes()

        # Reverts the changes on actors and objects
        # setup_actors and setup_objects was available up to 5.6
        if self.ctrl_be.target_version.is_supported_mysql_version_at_least(5, 6, 0):
            self.filtering.refresh()

        # Reverts the changes on options
        self.options.refresh()

        self.threads.refresh()

    def on_apply_button_clicked(self):
        # Commits the changes to the database
        self.config_data.commit_changes()

    def on_reset_button_clicked(self):
        if mforms.Utilities.show_message("Clear Event Tables",
                                         "performance_schema event summary and history tables will be cleared (truncated).\nPlease confirm.",
                                         "Clear Tables", "Cancel", "") == mforms.ResultOk:
            try:
                self.main_view.editor.executeManagementCommand('call sys.ps_truncate_all_tables(0)', 1)
            except Exception, e:
                log_error("Error calling sys.ps_truncate_all_tables(0): %s\n" % e)
                mforms.Utilities.show_error("Clear Event Tables", "An error occurred truncating performance_schema event tables.\n%s"%e, "OK", "", "")

    def on_reset_to_factory_button_clicked(self):
        if mforms.Utilities.show_message("Reset Performance Schema Settings",
                                         "All performance_schema instrumentation settings will be reset to factory configurations.\nPlease confirm.",
                                         "Reset", "Cancel", "") == mforms.ResultOk:
            try:
                self.main_view.editor.executeManagementCommand('call sys.ps_setup_reset_to_default(0)', 1)
            except Exception, e:
                log_error("Error calling sys.ps_reset_to_default(0): %s\n" % e)
                mforms.Utilities.show_error("Reset Performance Schema Settings", "An error occurred resetting performance_schema instrumentation settings.\n%s"%e, "OK", "", "")
                return
            self.on_revert_button_clicked()

    def refresh(self):
        pass