You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import logging
  6. from openerp import api, fields, models
  7. _logger = logging.getLogger(__name__)
  8. class WizardModuleUninstallLine(models.TransientModel):
  9. _name = 'wizard.module.uninstall.line'
  10. LINE_TYPE_SELECTION = [
  11. ('model', 'Model'),
  12. ('field', 'Field'),
  13. ]
  14. DB_TYPE_SELECTION = [
  15. ('', 'Unknown Type'),
  16. ('r', 'Ordinary Table'),
  17. ('i', 'Index'),
  18. ('S', 'Sequence'),
  19. ('v', 'view'),
  20. ('c', 'Composite Type'),
  21. ('t', 'TOAST table'),
  22. ]
  23. wizard_id = fields.Many2one(
  24. comodel_name='wizard.module.uninstall', required=True)
  25. line_type = fields.Selection(
  26. selection=LINE_TYPE_SELECTION, required=True)
  27. model_id = fields.Many2one(comodel_name='ir.model', readonly=True)
  28. model_name = fields.Char(
  29. string='Model Name', related='model_id.model', readonly=True)
  30. table_size = fields.Integer(
  31. string='Table Size (KB)', compute='_compute_database',
  32. multi='database', store=True)
  33. index_size = fields.Integer(
  34. string='Indexes Size (KB)', compute='_compute_database',
  35. multi='database', store=True)
  36. db_type = fields.Selection(
  37. selection=DB_TYPE_SELECTION, compute='_compute_database',
  38. multi='database', store=True)
  39. db_size = fields.Integer(
  40. string='Total DB Size (KB)', compute='_compute_database',
  41. multi='database', store=True)
  42. model_row_qty = fields.Integer(
  43. string='Rows Quantity', compute='_compute_database', multi='database',
  44. store=True,
  45. help="The approximate value of the number of records in the database,"
  46. " based on the PostgreSQL column 'reltuples'.\n You should reindex"
  47. " your database, to have a more precise value\n\n"
  48. " 'REINDEX database your_database_name;'")
  49. field_id = fields.Many2one(comodel_name='ir.model.fields', readonly=True)
  50. field_name = fields.Char(
  51. string='Field Name', related='field_id.name', readonly=True)
  52. field_ttype = fields.Selection(
  53. string='Field Type', related='field_id.ttype', readonly=True)
  54. field_model_name = fields.Char(
  55. string='Field Model Name', related='field_id.model_id.model',
  56. readonly=True)
  57. @api.multi
  58. @api.depends('model_id', 'line_type')
  59. def _compute_database(self):
  60. table_names = []
  61. for line in self.filtered(lambda x: x.model_id):
  62. model_obj = self.env.registry.get(line.model_id.model, False)
  63. if model_obj:
  64. table_names.append(model_obj._table)
  65. else:
  66. # Try to guess table name, replacing "." by "_"
  67. table_names.append(line.model_id.model.replace('.', '_'))
  68. # Get Relation Informations
  69. req = (
  70. "SELECT"
  71. " table_name,"
  72. " row_qty,"
  73. " db_type,"
  74. " pg_table_size(table_name::regclass::oid) AS table_size,"
  75. " pg_indexes_size(table_name::regclass::oid) AS index_size,"
  76. " pg_total_relation_size(table_name::regclass::oid) AS db_size"
  77. " FROM ("
  78. " SELECT"
  79. " relname AS table_name,"
  80. " reltuples AS row_qty,"
  81. " relkind as db_type"
  82. " FROM pg_class"
  83. " WHERE relname IN %s) AS tmp;"
  84. )
  85. self.env.cr.execute(req, (tuple(table_names),))
  86. res = self.env.cr.fetchall()
  87. table_res = {x[0]: (x[1], x[2], x[3], x[4], x[5]) for x in res}
  88. for line in self:
  89. model_obj = self.env.registry.get(line.model_id.model, False)
  90. if model_obj:
  91. table_name = model_obj._table
  92. else:
  93. # Try to guess table name, replacing "." by "_"
  94. table_name = line.model_id.model.replace('.', '_')
  95. res = table_res.get(table_name, (0, '', 0, 0, 0))
  96. line.model_row_qty = res[0]
  97. line.db_type = res[1]
  98. line.table_size = res[2] / 1024
  99. line.index_size = res[3] / 1024
  100. line.db_size = res[4] / 1024