Skip to content

Commit

Permalink
Fixed wrong order of imports:
Browse files Browse the repository at this point in the history
When the functions are declared on a different file and included on
the file that declares the class, the import must be done after the
class declaration. Otherwise, the help information won't be correctly
parsed.
  • Loading branch information
miguelaraujo committed Oct 20, 2020
1 parent 4a2ee4b commit 1752a20
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 99 deletions.
13 changes: 6 additions & 7 deletions check/init.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@

from mysqlsh.plugin_manager import plugin, plugin_function

from check import trx
from check import queries
from check import locks
from check import schema
from check import other

@plugin
class check:
"""
Expand All @@ -15,3 +8,9 @@ class check:
A collection of tools and utilities to perform checks on your
MySQL Database Server.
"""

from check import trx
from check import queries
from check import locks
from check import schema
from check import other
8 changes: 4 additions & 4 deletions collations/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
from mysqlsh.plugin_manager import plugin, plugin_function

def _check_if_collation_exists(session, collation):
query = """SELECT COLLATION_NAME FROM INFORMATION_SCHEMA.COLLATIONS
query = """SELECT COLLATION_NAME FROM INFORMATION_SCHEMA.COLLATIONS
where COLLATION_NAME = '%s'""" % collation
result = session.run_sql(query)
if len(result.fetch_all()) == 1:
return True
return False

@plugin_function("collations.nonUnique")
def non_unique(table, column, collation, schema=None, session=None):
"""
Find non-unique values in a column for a given collation.
This function will list all the non-unique values in a column
This function will list all the non-unique values in a column
for a given collation.
If no session is given, the current session of the MySQL Shell will be used.
Expand All @@ -38,7 +38,7 @@ def non_unique(table, column, collation, schema=None, session=None):
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""
"""
# Get hold of the global shell object
import mysqlsh
shell = mysqlsh.globals.shell
Expand Down
5 changes: 3 additions & 2 deletions collations/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Initializes the collations plugins.

from mysqlsh.plugin_manager import plugin, plugin_function
from collations import check
from collations import outoforder

@plugin
class collations:
Expand All @@ -13,3 +11,6 @@ class collations:
A collection of collation utilites.
"""

from collations import check
from collations import outoforder
8 changes: 4 additions & 4 deletions demo/init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# init.py
# -------
from mysqlsh.plugin_manager import plugin, plugin_function
from demo import oracle8ball


@plugin
class demo:
Expand All @@ -12,11 +10,13 @@ class demo:
This plugin is used only for demo purpose.
"""

from demo import oracle8ball

@plugin_function("demo.helloWorld")
def hello_world():
"""
Simple function that prints "Hello world!"
Just say Hello World
Returns:
Expand All @@ -33,7 +33,7 @@ def show_schemas(session=None):
with the global session of the MySQL Shell.
Args:
session (object): The optional session object used to query the
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
Returns:
Expand Down
12 changes: 6 additions & 6 deletions innodb/autoinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def get_autoinc_fill(percentage=50, schema=None, session=None):
Prints information about auto_increment fill up.
Args:
percentage (integer): Only shows the tables where auto increments
percentage (integer): Only shows the tables where auto increments
values are filled to at least % (default: 50).
schema (string): The name of the schema to use. This is optional.
schema (string): The name of the schema to use. This is optional.
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""
"""
where_filter = ""
having_filter = ""

Expand Down Expand Up @@ -86,9 +86,9 @@ def get_autoinc_fill(percentage=50, schema=None, session=None):
TABLE_SCHEMA NOT IN ('mysql', 'INFORMATION_SCHEMA', 'performance_schema')
AND EXTRA='auto_increment' {}
{}
ORDER BY CAST(AUTO_INCREMENT_RATIO AS SIGNED INTEGER)
ORDER BY CAST(AUTO_INCREMENT_RATIO AS SIGNED INTEGER)
""".format(where_filter, having_filter)

run_and_show(stmt)

return
42 changes: 21 additions & 21 deletions innodb/bufferpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def get_tables_in_bp(session=None):
Args:
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""
"""
# Get hold of the global shell object
import mysqlsh
shell = mysqlsh.globals.shell
Expand All @@ -27,37 +27,37 @@ def get_tables_in_bp(session=None):
return
print("Processing, this can take a while (don't forget to run ANALYZE TABLE for accurate results)...")

stmt="""select variable_name, format_bytes(variable_value/1)
from performance_schema.global_variables
stmt="""select variable_name, format_bytes(variable_value/1)
from performance_schema.global_variables
where variable_name = 'innodb_buffer_pool_size'"""
bp_size = session.run_sql(stmt).fetch_one()[1]
stmt="""select variable_name, variable_value
from performance_schema.global_variables
stmt="""select variable_name, variable_value
from performance_schema.global_variables
where variable_name = 'innodb_buffer_pool_instances'"""
bp_instances = session.run_sql(stmt).fetch_one()[1]

if int(bp_instances) > 1:
bp_instances_str = "%s instances" % bp_instances
else:
bp_instances_str = "%s instance" % bp_instances

print("InnoDB Buffer Pool Size = %s (%s)" % (bp_size, bp_instances_str))

stmt="""SELECT t1.TABLE_NAME 'Table Name', COUNT(*) AS Pages,
format_bytes(SUM(IF(COMPRESSED_SIZE = 0, 16384, COMPRESSED_SIZE)))
AS 'Total Data in BP',
format_bytes(any_value(data_length)+any_value(index_length))
stmt="""SELECT t1.TABLE_NAME 'Table Name', COUNT(*) AS Pages,
format_bytes(SUM(IF(COMPRESSED_SIZE = 0, 16384, COMPRESSED_SIZE)))
AS 'Total Data in BP',
format_bytes(any_value(data_length)+any_value(index_length))
'Total Table Size',
lpad(concat(round(SUM(IF(COMPRESSED_SIZE = 0, 16384, COMPRESSED_SIZE))
/(any_value(data_length)+any_value(index_length)) * 100,2),'%'),"6"," ")
as 'in BP'
FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE t1
JOIN INFORMATION_SCHEMA.TABLES t2
ON concat('`',t2.TABLE_SCHEMA,'`.`',t2.TABLE_NAME,'`') = t1.TABLE_NAME
WHERE t2.TABLE_SCHEMA NOT IN ('mysql', 'sys')
GROUP BY t1.TABLE_NAME
ORDER BY SUM(IF(COMPRESSED_SIZE = 0, 16384, COMPRESSED_SIZE)) desc,
(any_value(data_length)+any_value(index_length)) desc"""
/(any_value(data_length)+any_value(index_length)) * 100,2),'%'),"6"," ")
as 'in BP'
FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE t1
JOIN INFORMATION_SCHEMA.TABLES t2
ON concat('`',t2.TABLE_SCHEMA,'`.`',t2.TABLE_NAME,'`') = t1.TABLE_NAME
WHERE t2.TABLE_SCHEMA NOT IN ('mysql', 'sys')
GROUP BY t1.TABLE_NAME
ORDER BY SUM(IF(COMPRESSED_SIZE = 0, 16384, COMPRESSED_SIZE)) desc,
(any_value(data_length)+any_value(index_length)) desc"""
result = session.run_sql(stmt)
shell.dump_rows(result)
return
return
40 changes: 20 additions & 20 deletions innodb/fragmented.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def get_fragmented_tables(percentage=10, session=None):
"""
Prints InnoDB fragmented tables.
This function prints the list of InnoDB Tables having free blocks.
Args:
Expand Down Expand Up @@ -39,19 +39,19 @@ def get_fragmented_tables(percentage=10, session=None):
print("Changing information_schema_stats_expiry to 0 for this session only")
stmt = """SET information_schema_stats_expiry=0"""
result = session.run_sql(stmt)
stmt = """SELECT CONCAT(table_schema, '.', table_name) as 'TABLE',
ENGINE, CONCAT(ROUND(table_rows / 1000000, 2), 'M') `ROWS`,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') IDX,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') 'TOTAL SIZE',
ROUND(index_length / data_length, 2) IDXFRAC,
stmt = """SELECT CONCAT(table_schema, '.', table_name) as 'TABLE',
ENGINE, CONCAT(ROUND(table_rows / 1000000, 2), 'M') `ROWS`,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') IDX,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') 'TOTAL SIZE',
ROUND(index_length / data_length, 2) IDXFRAC,
CONCAT(ROUND(( data_free / 1024 / 1024),2), 'MB') AS data_free,
CONCAT('(',
IF(data_free< ( data_length + index_length ),
IF(data_free< ( data_length + index_length ),
CONCAT(round(data_free/(data_length+index_length)*100,2),'%'),
'100%'),')') AS data_free_pct
FROM information_schema.TABLES WHERE (data_free/(data_length+index_length)*100) > {limit}
AND table_schema <> 'mysql';""".format(limit=percentage)
AND table_schema <> 'mysql';""".format(limit=percentage)
result = session.run_sql(stmt)
shell.dump_rows(result)
print ("Don't forget to run 'ANALYZE TABLE ...' for a more accurate result.")
Expand All @@ -60,13 +60,13 @@ def get_fragmented_tables(percentage=10, session=None):
@plugin_function("innodb.getFragmentedTablesDisk")
def get_fragmented_tables_disk(percentage=10, session=None):
"""
Prints InnoDB fragmented tables with disk info.
Prints InnoDB fragmented tables with disk info.
Args:
percentage (integer): The amount of free space to be considered as fragmented (default: 10).
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""

import mysqlsh
Expand Down Expand Up @@ -94,17 +94,17 @@ def get_fragmented_tables_disk(percentage=10, session=None):
else:
print("Changing information_schema_stats_expiry to 0 for this session only")
stmt = """SET information_schema_stats_expiry=0"""
result = session.run_sql(stmt)
stmt = """SELECT name NAME ,table_rows `ROWS`, format_bytes(data_length) DATA_SIZE,
format_bytes(index_length) INDEX_SIZE,
format_bytes(data_length+index_length) TOTAL_SIZE,
format_bytes(data_free) DATA_FREE,
format_bytes(FILE_SIZE) FILE_SIZE,
result = session.run_sql(stmt)
stmt = """SELECT name NAME ,table_rows `ROWS`, format_bytes(data_length) DATA_SIZE,
format_bytes(index_length) INDEX_SIZE,
format_bytes(data_length+index_length) TOTAL_SIZE,
format_bytes(data_free) DATA_FREE,
format_bytes(FILE_SIZE) FILE_SIZE,
format_bytes(FILE_SIZE - (data_length + index_length)) WASTED_SIZE,
CONCAT(round(((FILE_SIZE/100 - (data_length/100 + index_length/100))/(FILE_SIZE/100))*100,2),'%') 'FREE'
FROM information_schema.TABLES as t
JOIN information_schema.INNODB_TABLESPACES as it
ON it.name = concat(table_schema,"/",table_name)
FROM information_schema.TABLES as t
JOIN information_schema.INNODB_TABLESPACES as it
ON it.name = concat(table_schema,"/",table_name)
WHERE data_length > 20000
AND ((FILE_SIZE/100 - (data_length/100 + index_length/100))/(FILE_SIZE/100))*100 > {limit}
ORDER BY (data_length + index_length) desc""".format(limit=percentage)
Expand Down
14 changes: 7 additions & 7 deletions innodb/init.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# init.py
# -------


from mysqlsh.plugin_manager import plugin, plugin_function
from innodb import fragmented
from innodb import progress
from innodb import bufferpool
from innodb import autoinc

@plugin
class innodb:
"""
InnoDB management and utilities.
A collection of InnoDB management tools and related
utilities that work on InnoDB Engine.
"""
utilities that work on InnoDB Engine.
"""

from innodb import fragmented
from innodb import progress
from innodb import bufferpool
from innodb import autoinc
28 changes: 14 additions & 14 deletions innodb/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from mysqlsh.plugin_manager import plugin, plugin_function

def _is_consumer_enabled(session, shell):

stmt = """select sys.ps_is_consumer_enabled("events_stages_current")"""
result = session.run_sql(stmt)
consumer = result.fetch_one()[0]
ok = False
if consumer == "NO":
answer = shell.prompt("""The consumer for 'events_stages_current' is not enabled,
answer = shell.prompt("""The consumer for 'events_stages_current' is not enabled,
do you want to enabled it now ? (y/N) """,
{'defaultValue':'n'})
if answer.lower() == 'y':
Expand All @@ -22,10 +22,10 @@ def _is_consumer_enabled(session, shell):
ok = True
else:
ok = True

return ok

def _are_instruments_enabled(session, shell):
def _are_instruments_enabled(session, shell):

stmt = """SELECT NAME, ENABLED
FROM performance_schema.setup_instruments
Expand All @@ -39,7 +39,7 @@ def _are_instruments_enabled(session, shell):
for instrument in instruments:
instruments_str += "%s, " % instrument[0]

answer = shell.prompt("""Some instruments are not enabled: %s
answer = shell.prompt("""Some instruments are not enabled: %s
Do you want to enabled them now ? (y/N) """
% instruments_str, {'defaultValue':'n'})
if answer.lower() == 'y':
Expand All @@ -51,7 +51,7 @@ def _are_instruments_enabled(session, shell):
ok = True
else:
ok = True

return ok

@plugin_function("innodb.getAlterProgress")
Expand All @@ -63,7 +63,7 @@ def get_alter_progress(format='table', session=None):
format (string): The output format to be used (default: table).
session (object): The optional session object used to query the
database. If omitted the MySQL Shell's current session will be used.
"""

# Get hold of the global shell object
Expand All @@ -86,15 +86,15 @@ def get_alter_progress(format='table', session=None):

stmt="""SELECT stmt.THREAD_ID, stmt.SQL_TEXT, stage.EVENT_NAME AS State,
stage.WORK_COMPLETED, stage.WORK_ESTIMATED,
lpad(CONCAT(ROUND(100*stage.WORK_COMPLETED/stage.WORK_ESTIMATED, 2),"%"),12," ")
lpad(CONCAT(ROUND(100*stage.WORK_COMPLETED/stage.WORK_ESTIMATED, 2),"%"),12," ")
AS CompletedPct, lpad(format_pico_time(stmt.TIMER_WAIT), 10, " ") AS StartedAgo,
current_allocated Memory
FROM performance_schema.events_statements_current stmt
INNER JOIN sys.memory_by_thread_by_current_bytes mt
ON mt.thread_id = stmt.thread_id
INNER JOIN performance_schema.events_stages_current stage
current_allocated Memory
FROM performance_schema.events_statements_current stmt
INNER JOIN sys.memory_by_thread_by_current_bytes mt
ON mt.thread_id = stmt.thread_id
INNER JOIN performance_schema.events_stages_current stage
ON stage.THREAD_ID = stmt.THREAD_ID"""

result = session.run_sql(stmt)
shell.dump_rows(result, format)
return
Loading

0 comments on commit 1752a20

Please sign in to comment.