diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py index 9497e58..01a6700 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/utils.py @@ -12,6 +12,9 @@ from pgadmin.browser.collection import CollectionNodeModule from pgadmin.browser.utils import PGChildNodeView from flask import render_template +from pgadmin.utils.ajax import internal_server_error +import json + class SchemaChildModule(CollectionNodeModule): """ @@ -139,3 +142,154 @@ class DataTypeReader: return False, str(e) return True, res + + +class VacuumSettings: + """ + VacuumSettings Class. + + This class includes common utilities to fetch and parse + vacuum defaults settings. + + Methods: + ------- + * get_vacuum_table_settings(conn): + - Returns vacuum table defaults settings. + + * get_vacuum_toast_settings(conn): + - Returns vacuum toast defaults settings. + + * parse_vacuum_data(conn, result, type): + - Returns result of an associated array + of fields name, label, value and column_type. + It adds name, label, column_type properties of table/toast + vacuum into the array and returns it. + args: + * conn - It is db connection object + * result - Resultset of vacuum data + * type - table/toast vacuum type + + """ + def get_vacuum_table_settings(self, conn): + """ + Fetch the default values for autovacuum + fields, return an array of + - label + - name + - setting + values + """ + + # returns an array of name & label values + vacuum_fields = render_template("/".join( + [self.template_path, 'vacuum_fields.json'] + )) + + vacuum_fields = json.loads(vacuum_fields) + + # returns an array of setting & name values + vacuum_fields_keys = "'"+"','".join( + vacuum_fields['table'].keys())+"'" + SQL = render_template("/".join( + [self.template_path, 'sql/vacuum_defaults.sql']), + columns=vacuum_fields_keys) + status, res = conn.execute_dict(SQL) + + if not status: + return internal_server_error(errormsg=res) + + for row in res['rows']: + row_name = row['name'] + row['name'] = vacuum_fields['table'][row_name][0] + row['label'] = vacuum_fields['table'][row_name][1] + row['column_type'] = vacuum_fields['table'][row_name][2] + + return res + + def get_vacuum_toast_settings(self, conn): + """ + Fetch the default values for autovacuum + fields, return an array of + - label + - name + - setting + values + """ + + # returns an array of name & label values + vacuum_fields = render_template("/".join( + [self.template_path, 'vacuum_fields.json'] + )) + + vacuum_fields = json.loads(vacuum_fields) + + # returns an array of setting & name values + vacuum_fields_keys = "'"+"','".join( + vacuum_fields['toast'].keys())+"'" + SQL = render_template("/".join( + [self.template_path, 'sql/vacuum_defaults.sql']), + columns=vacuum_fields_keys) + status, res = conn.execute_dict(SQL) + + if not status: + return internal_server_error(errormsg=res) + + for row in res['rows']: + row_name = row['name'] + row['name'] = vacuum_fields['toast'][row_name][0] + row['label'] = vacuum_fields['toast'][row_name][1] + row['column_type'] = vacuum_fields['table'][row_name][2] + + return res + + def parse_vacuum_data(self, conn, result, type): + """ + This function returns result of an associated array + of fields name, label, value and column_type. + It adds name, label, column_type properties of table/toast + vacuum into the array and returns it. + args: + * conn - It is db connection object + * result - Resultset of vacuum data + * type - table/toast vacuum type + """ + + # returns an array of name & label values + vacuum_fields = render_template("/".join( + [self.template_path, 'vacuum_fields.json'] + )) + + vacuum_fields = json.loads(vacuum_fields) + + # returns an array of setting & name values + vacuum_fields_keys = "'"+"','".join( + vacuum_fields[type].keys()) + "'" + SQL = render_template("/".join( + [self.template_path, 'sql/vacuum_defaults.sql']), + columns=vacuum_fields_keys) + status, res = conn.execute_dict(SQL) + + if not status: + return internal_server_error(errormsg=res) + + if type is 'table': + for row in res['rows']: + row_name = row['name'] + row['name'] = vacuum_fields[type][row_name][0] + row['label'] = vacuum_fields[type][row_name][1] + row['column_type'] = vacuum_fields[type][row_name][2] + if result[row['name']] is not None: + row['value'] = row['setting'] = float(result[row_name]) + + elif type is 'toast': + for row in res['rows']: + row_old_name = row['name'] + row_name = 'toast_{0}'.format( + vacuum_fields[type][row_old_name][0]) + row['name'] = vacuum_fields[type][row_old_name][0] + row['label'] = vacuum_fields[type][row_old_name][1] + row['column_type'] = vacuum_fields[type][row_old_name][2] + if result[row_name] and result[row_name] is not None: + row['value'] = row['setting'] = float(result[row_name]) + + return res['rows']