|
| 1 | +from django.core.exceptions import ImproperlyConfigured |
1 | 2 | from django.utils.translation import gettext_lazy as _ |
2 | 3 | from openwisp_monitoring.db import chart_query |
3 | 4 |
|
|
140 | 141 | } |
141 | 142 |
|
142 | 143 |
|
| 144 | +def _validate_chart_configuration(chart_config): |
| 145 | + assert 'type' in chart_config |
| 146 | + assert 'title' in chart_config |
| 147 | + assert 'description' in chart_config |
| 148 | + assert 'order' in chart_config |
| 149 | + assert 'query' in chart_config |
| 150 | + if chart_config['query'] is None: |
| 151 | + assert 'unit' in chart_config |
| 152 | + if 'colorscale' in chart_config: |
| 153 | + assert 'max' in chart_config['colorscale'] |
| 154 | + assert 'min' in chart_config['colorscale'] |
| 155 | + assert 'label' in chart_config['colorscale'] |
| 156 | + assert 'scale' in chart_config['colorscale'] |
| 157 | + |
| 158 | + |
143 | 159 | def get_chart_configuration(): |
144 | 160 | charts = deep_merge_dicts(DEFAULT_CHARTS, app_settings.ADDITIONAL_CHARTS) |
145 | 161 | # ensure configuration is not broken |
146 | 162 | for key, options in charts.items(): |
147 | | - assert 'type' in options |
148 | | - assert 'title' in options |
149 | | - assert 'description' in options |
150 | | - assert 'order' in options |
151 | | - assert 'query' in options |
152 | | - if options['query'] is None: |
153 | | - assert 'unit' in options |
154 | | - if 'colorscale' in options: |
155 | | - assert 'max' in options['colorscale'] |
156 | | - assert 'min' in options['colorscale'] |
157 | | - assert 'label' in options['colorscale'] |
158 | | - assert 'scale' in options['colorscale'] |
| 163 | + _validate_chart_configuration(options) |
159 | 164 | return charts |
160 | 165 |
|
161 | 166 |
|
| 167 | +def register_chart(chart_name, chart_config): |
| 168 | + """ |
| 169 | + Registers a new chart configuration. |
| 170 | + """ |
| 171 | + if not isinstance(chart_name, str): |
| 172 | + raise ImproperlyConfigured('Chart name should be type "str".') |
| 173 | + if not isinstance(chart_config, dict): |
| 174 | + raise ImproperlyConfigured('Chart configuration should be type "dict".') |
| 175 | + if chart_name in DEFAULT_CHARTS: |
| 176 | + raise ImproperlyConfigured( |
| 177 | + f'{chart_name} is an already registered Chart Configuration.' |
| 178 | + ) |
| 179 | + |
| 180 | + _validate_chart_configuration(chart_config) |
| 181 | + DEFAULT_CHARTS.update({chart_name: chart_config}) |
| 182 | + _register_chart_configuration_choice(chart_name, chart_config) |
| 183 | + |
| 184 | + |
| 185 | +def unregister_chart(chart_name): |
| 186 | + if not isinstance(chart_name, str): |
| 187 | + raise ImproperlyConfigured('Chart configuration name should be type "str"') |
| 188 | + if chart_name not in DEFAULT_CHARTS: |
| 189 | + raise ImproperlyConfigured(f'No such Chart configuation "{chart_name}"') |
| 190 | + DEFAULT_CHARTS.pop(chart_name) |
| 191 | + _unregister_chart_configuration_choice(chart_name) |
| 192 | + |
| 193 | + |
| 194 | +def _register_chart_configuration_choice(chart_name, chart_config): |
| 195 | + name = chart_config.get('verbose_name', chart_name) |
| 196 | + CHART_CONFIGURATION_CHOICES.append((chart_name, name)) |
| 197 | + |
| 198 | + |
| 199 | +def _unregister_chart_configuration_choice(chart_name): |
| 200 | + for index, (key, name) in enumerate(CHART_CONFIGURATION_CHOICES): |
| 201 | + if key == chart_name: |
| 202 | + CHART_CONFIGURATION_CHOICES.pop(index) |
| 203 | + return |
| 204 | + |
| 205 | + |
162 | 206 | def get_chart_configuration_choices(): |
163 | 207 | charts = get_chart_configuration() |
164 | 208 | choices = [] |
165 | 209 | for key in sorted(charts.keys()): |
166 | 210 | label = charts[key].get('label', charts[key]['title']) |
167 | 211 | choices.append((key, label)) |
168 | 212 | return choices |
| 213 | + |
| 214 | + |
| 215 | +CHART_CONFIGURATION_CHOICES = get_chart_configuration_choices() |
0 commit comments