Skip to content

Commit 780eb3e

Browse files
authored
Merge pull request #316 from em230418/14.0-database_limit_size
[14.0] database_limit_size port
2 parents 0fe13c4 + 219ed56 commit 780eb3e

File tree

8 files changed

+154
-0
lines changed

8 files changed

+154
-0
lines changed

database_limit_size/README.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. image:: https://itpp.dev/images/infinity-readme.png
2+
:alt: Tested and maintained by IT Projects Labs
3+
:target: https://itpp.dev
4+
5+
.. image:: https://img.shields.io/badge/license-MIT-blue.svg
6+
:target: https://opensource.org/licenses/MIT
7+
:alt: License: MIT
8+
9+
=====================
10+
Database Limit Size
11+
=====================
12+
13+
This module allows blocking backend access when database limit is exceeded
14+
15+
On loading backend page, module fetches size of database (including filestore) and compares it with value, that
16+
is defined in "System Parameters" as ``database_limit_size``. Value is expected to be in bytes.
17+
18+
If ``database_limit_size`` is not given or zero, there is no limit.
19+
20+
Roadmap
21+
=======
22+
23+
* Customize percentage of the limit which, if exceeded, would indicate a warning. As for now it is hardcoded to 90%
24+
25+
Questions?
26+
==========
27+
28+
To get an assistance on this module contact us by email :arrow_right: help@itpp.dev
29+
30+
Contributors
31+
============
32+
* `Eugene Molotov <https://it-projects.info/team/em230418>`__:
33+
34+
35+
Further information
36+
===================
37+
38+
Odoo Apps Store: https://apps.odoo.com/apps/modules/14.0/database_limit_size/
39+
40+
41+
Notifications on updates: `via Atom <https://github.com/itpp-labs/access-addons/commits/14.0/database_limit_size.atom>`_, `by Email <https://blogtrottr.com/?subscribe=https://github.com/itpp-labs/access-addons/commits/14.0/database_limit_size.atom>`_
42+
43+
Tested on `Odoo 14.0 <https://github.com/odoo/odoo/commit/8ca3ea063050f2ab2d19cce8a68116489872a734>`_

database_limit_size/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# License MIT (https://opensource.org/licenses/MIT).
2+
3+
from . import models
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418>
2+
# License MIT (https://opensource.org/licenses/MIT).
3+
4+
{
5+
"name": """Database Limit Size""",
6+
"summary": """This module allows blocking backend access when database limit is exceeded""",
7+
"category": "Extra Tools",
8+
"images": [],
9+
"version": "14.0.1.0.0",
10+
"application": False,
11+
"author": "IT-Projects LLC, Eugene Molotov",
12+
"support": "help@itpp.dev",
13+
"website": "https://twitter.com/OdooFree",
14+
"license": "Other OSI approved licence", # MIT
15+
"depends": ["database_block"],
16+
"external_dependencies": {"python": [], "bin": []},
17+
"data": [],
18+
"demo": [],
19+
"qweb": [],
20+
"post_load": None,
21+
"pre_init_hook": None,
22+
"post_init_hook": None,
23+
"uninstall_hook": None,
24+
"auto_install": False,
25+
"installable": True,
26+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`1.0.0`
2+
-------
3+
4+
- **Init version**

database_limit_size/doc/index.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=====================
2+
Database Limit Size
3+
=====================
4+
5+
Installation
6+
============
7+
8+
* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way
9+
10+
Configuration
11+
=============
12+
13+
* `Log in as admin
14+
* `Activate Developer Mode <https://odoo-development.readthedocs.io/en/latest/odoo/usage/debug-mode.html>`__
15+
* Open menu ``[[ Settings ]] >> Technical >> System Parameter``
16+
* Edit existing record by key ``database_limit_size`` or create new one
17+
* Set integer value, click "Save" and reload web page
18+
* If you set small nonzero value (for example "1" without quotes), "Database size exceed" will appear and will disable navigating
19+
* If you set value that is less than actual database size, but greater than 90% of actual database size and ``web_responsive`` is installed, you will see warning message "Database size is about to be exceed"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# License MIT (https://opensource.org/licenses/MIT).
2+
3+
from . import ir_http
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418>
2+
# License MIT (https://opensource.org/licenses/MIT).
3+
4+
import os
5+
6+
from odoo import models
7+
from odoo.tools import human_size
8+
9+
10+
# https://stackoverflow.com/a/1392549
11+
def get_directory_size(start_path):
12+
total_size = 0
13+
for dirpath, _dirnames, filenames in os.walk(start_path):
14+
for f in filenames:
15+
fp = os.path.join(dirpath, f)
16+
total_size += os.path.getsize(fp)
17+
return total_size
18+
19+
20+
class IrHttp(models.AbstractModel):
21+
22+
_inherit = "ir.http"
23+
24+
def session_info(self):
25+
res = super(IrHttp, self).session_info()
26+
27+
Config = self.env["ir.config_parameter"].sudo()
28+
try:
29+
database_limit_size = int(Config.get_param("database_limit_size", 0))
30+
except ValueError:
31+
return res
32+
33+
if not database_limit_size:
34+
return res
35+
36+
self.env.cr.execute("select pg_database_size(%s)", [self.env.cr.dbname])
37+
database_size = self.env.cr.fetchone()[0]
38+
39+
filestore_size = get_directory_size(self.env["ir.attachment"]._filestore())
40+
41+
total_size = database_size + filestore_size
42+
if total_size > database_limit_size:
43+
res["database_block_message"] = "Database size exceed ({} / {})".format(
44+
human_size(total_size),
45+
human_size(database_limit_size),
46+
)
47+
elif total_size > database_limit_size * 0.9:
48+
res[
49+
"database_block_message"
50+
] = "Database size is about to be exceed (%s / %s)" % (
51+
human_size(total_size),
52+
human_size(database_limit_size),
53+
)
54+
res["database_block_is_warning"] = True
55+
56+
return res
6.87 KB
Loading

0 commit comments

Comments
 (0)