-
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy path__init__.py
More file actions
68 lines (61 loc) · 2.83 KB
/
Copy path__init__.py
File metadata and controls
68 lines (61 loc) · 2.83 KB
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
import logging
from importlib import import_module
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import DatabaseError
logger = logging.getLogger(__name__)
TIMESERIES_DB = getattr(settings, 'TIMESERIES_DATABASE', None)
if not TIMESERIES_DB:
TIMESERIES_DB = {
'BACKEND': 'openwisp_monitoring.db.backends.influxdb',
'USER': getattr(settings, 'INFLUXDB_USER', 'openwisp'),
'PASSWORD': getattr(settings, 'INFLUXDB_PASSWORD', 'openwisp'),
'NAME': getattr(settings, 'INFLUXDB_DATABASE', 'openwisp2'),
'HOST': getattr(settings, 'INFLUXDB_HOST', 'localhost'),
'PORT': getattr(settings, 'INFLUXDB_PORT', '8086'),
}
logger.warning(
'The previous method to define Timeseries Database has been deprecated. Please refer to the docs:\n'
'https://github.com/openwisp/openwisp-monitoring#setup-integrate-in-an-existing-django-project'
)
def load_backend_module(backend_name=TIMESERIES_DB['BACKEND'], module=None):
"""
Returns database backend module given a fully qualified database backend name,
or raise an error if it doesn't exist or backend is not well defined.
"""
try:
assert 'BACKEND' in TIMESERIES_DB, 'BACKEND'
if 'BACKEND' in TIMESERIES_DB and '2' in TIMESERIES_DB['BACKEND']:
# InfluxDB 2.x specific checks
assert 'TOKEN' in TIMESERIES_DB, 'TOKEN'
assert 'ORG' in TIMESERIES_DB, 'ORG'
assert 'BUCKET' in TIMESERIES_DB, 'BUCKET'
else:
# InfluxDB 1.x specific checks
assert 'USER' in TIMESERIES_DB, 'USER'
assert 'PASSWORD' in TIMESERIES_DB, 'PASSWORD'
assert 'NAME' in TIMESERIES_DB, 'NAME'
if module:
return import_module(f'{backend_name}.{module}')
else:
return import_module(backend_name)
except AttributeError as e:
raise DatabaseError('No TIMESERIES_DATABASE specified in settings') from e
except AssertionError as e:
raise ImproperlyConfigured(
f'"{e}" field is not declared in TIMESERIES_DATABASE'
) from e
except ImportError as e:
# The database backend wasn't found. Display a helpful error message
# listing all built-in database backends.
builtin_backends = ['influxdb', 'influxdb2']
if backend_name not in [
f'openwisp_monitoring.db.backends.{b}' for b in builtin_backends
]:
raise ImproperlyConfigured(
f"{backend_name} isn't an available database backend.\n"
"Try using 'openwisp_monitoring.db.backends.XXX', where XXX is one of:\n"
f"{builtin_backends}"
) from e
timeseries_db = load_backend_module(module='client').DatabaseClient()
timeseries_db.queries = load_backend_module(module='queries')