Browse Source

Merge "Refactor Panel class"

Jenkins 3 years ago
parent
commit
d1135dea79

+ 0
- 140
grafana_dashboards/schema/panel.py View File

@@ -1,140 +0,0 @@
1
-# Copyright 2015 Red Hat, Inc.
2
-#
3
-# Licensed under the Apache License, Version 2.0 (the "License");
4
-# you may not use this file except in compliance with the License.
5
-# You may obtain a copy of the License at
6
-#
7
-# http://www.apache.org/licenses/LICENSE-2.0
8
-#
9
-# Unless required by applicable law or agreed to in writing, software
10
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
-# License for the specific language governing permissions and limitations
13
-# under the License.
14
-
15
-import voluptuous as v
16
-
17
-
18
-class Panel(object):
19
-
20
-    def __init__(self):
21
-        self.base = {
22
-            v.Required('editable', default=True): v.All(bool),
23
-            v.Required('error', default=False): v.All(bool),
24
-            v.Required('span', default=12): v.All(int, v.Range(min=0, max=12)),
25
-            v.Required('title'): v.All(str, v.Length(min=1)),
26
-            v.Required('type'): v.Any(
27
-                'dashlist', 'graph', 'singlestat', 'text'),
28
-            v.Optional('id'): int,
29
-        }
30
-
31
-        self.dashlist = {
32
-            v.Required('limit', default=10): v.All(int),
33
-            v.Required('mode', default='starred'): v.Any('search', 'starred'),
34
-            v.Required('tag', default=''): v.All(str),
35
-            v.Required('query', default=''): v.All(str),
36
-        }
37
-        self.dashlist.update(self.base)
38
-
39
-        self.graph = {
40
-            v.Required('bars', default=False): v.All(bool),
41
-            v.Required('fill', default=1): v.All(int),
42
-            v.Required('lines', default=True): v.All(bool),
43
-            v.Required('linewidth', default=2): v.All(int),
44
-            v.Required('percentage', default=False): v.All(bool),
45
-            v.Required('pointradius', default=5): v.All(int),
46
-            v.Required('points', default=False): v.All(bool),
47
-            v.Required('stack', default=False): v.All(bool),
48
-            v.Required('steppedLine', default=False): v.All(bool),
49
-            v.Required('targets', default=[]): v.All(list),
50
-            v.Required('x-axis', default=True): v.All(bool),
51
-            v.Required('y-axis', default=True): v.All(bool),
52
-        }
53
-        self.graph.update(self.base)
54
-
55
-        # TODO(pabelanger): This is pretty ugly, there much be a better way to
56
-        # set default values.
57
-        sparkline_defaults = {
58
-            'fillColor': 'rgba(31, 118, 189, 0.18)',
59
-            'full': False,
60
-            'lineColor': 'rgb(31, 120, 193)',
61
-            'show': False,
62
-        }
63
-        sparkline = {
64
-            v.Required(
65
-                'fillColor', default=sparkline_defaults['fillColor']
66
-            ): v.All(str),
67
-            v.Required('full', default=False): v.All(bool),
68
-            v.Required(
69
-                'lineColor', default=sparkline_defaults['lineColor']
70
-            ): v.All(str),
71
-            v.Required('show', default=False): v.All(bool),
72
-        }
73
-
74
-        self.singlestat = {
75
-            v.Required('colorBackground', default=False): v.All(bool),
76
-            v.Required('colorValue', default=False): v.All(bool),
77
-            v.Required('maxDataPoints', default=100): v.All(int),
78
-            v.Required('postfix', default=''): v.All(str),
79
-            # Support 0% to 200% by 10
80
-            v.Required(
81
-                'postfixFontSize', default='50%'): v.All(
82
-                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
83
-            v.Required('prefix', default=''): v.All(str),
84
-            # Support 0% to 200% by 10
85
-            v.Required(
86
-                'prefixFontSize', default='50%'): v.All(
87
-                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
88
-            v.Required('sparkline', default=sparkline_defaults): sparkline,
89
-            v.Required('targets', default=[]): v.All(list),
90
-            v.Required('thresholds', default=''): v.All(str),
91
-            # Support 0% to 200% by 10
92
-            v.Required(
93
-                'valueFontSize', default='80%'): v.All(
94
-                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
95
-            v.Required('valueName', default='avg'): v.All(
96
-                'avg', 'current', 'max', 'min', 'total'),
97
-            v.Optional('decimals'): v.All(int, v.Range(min=0, max=12)),
98
-        }
99
-        self.singlestat.update(self.base)
100
-
101
-        self.text = {
102
-            v.Required('content'): v.All(str),
103
-            v.Required('mode', default='markdown'): v.Any(
104
-                'html', 'markdown', 'text'),
105
-            v.Optional('style'): dict(),
106
-        }
107
-        self.text.update(self.base)
108
-
109
-    def _validate(self):
110
-
111
-        def f(data):
112
-            res = []
113
-            if not isinstance(data, list):
114
-                raise v.Invalid('Should be a list')
115
-
116
-            for panel in data:
117
-                validate = v.Schema(self.base, extra=True)
118
-                validate(panel)
119
-
120
-                if panel['type'] == 'dashlist':
121
-                    schema = v.Schema(self.dashlist)
122
-                elif panel['type'] == 'graph':
123
-                    schema = v.Schema(self.graph)
124
-                elif panel['type'] == 'singlestat':
125
-                    schema = v.Schema(self.singlestat)
126
-                elif panel['type'] == 'text':
127
-                    schema = v.Schema(self.text)
128
-
129
-                res.append(schema(panel))
130
-
131
-            return res
132
-
133
-        return f
134
-
135
-    def get_schema(self):
136
-        schema = v.Schema({
137
-            v.Required('panels', default=[]): v.All(self._validate()),
138
-        })
139
-
140
-        return schema

+ 57
- 0
grafana_dashboards/schema/panel/__init__.py View File

@@ -0,0 +1,57 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+from grafana_dashboards.schema.panel.base import Base
18
+from grafana_dashboards.schema.panel.dashlist import Dashlist
19
+from grafana_dashboards.schema.panel.graph import Graph
20
+from grafana_dashboards.schema.panel.singlestat import Singlestat
21
+from grafana_dashboards.schema.panel.text import Text
22
+
23
+
24
+class Panel(object):
25
+
26
+    def _validate(self):
27
+
28
+        def f(data):
29
+            res = []
30
+            if not isinstance(data, list):
31
+                raise v.Invalid('Should be a list')
32
+
33
+            for panel in data:
34
+                validate = Base().get_schema()
35
+                validate(panel)
36
+
37
+                if panel['type'] == 'dashlist':
38
+                    schema = Dashlist().get_schema()
39
+                elif panel['type'] == 'graph':
40
+                    schema = Graph().get_schema()
41
+                elif panel['type'] == 'singlestat':
42
+                    schema = Singlestat().get_schema()
43
+                elif panel['type'] == 'text':
44
+                    schema = Text().get_schema()
45
+
46
+                res.append(schema(panel))
47
+
48
+            return res
49
+
50
+        return f
51
+
52
+    def get_schema(self):
53
+        schema = v.Schema({
54
+            v.Required('panels', default=[]): v.All(self._validate()),
55
+        })
56
+
57
+        return schema

+ 32
- 0
grafana_dashboards/schema/panel/base.py View File

@@ -0,0 +1,32 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+
18
+class Base(object):
19
+
20
+    def __init__(self):
21
+        self.base = {
22
+            v.Required('editable', default=True): v.All(bool),
23
+            v.Required('error', default=False): v.All(bool),
24
+            v.Required('span', default=12): v.All(int, v.Range(min=0, max=12)),
25
+            v.Required('title'): v.All(str, v.Length(min=1)),
26
+            v.Required('type'): v.Any(
27
+                'dashlist', 'graph', 'singlestat', 'text'),
28
+            v.Optional('id'): int,
29
+        }
30
+
31
+    def get_schema(self):
32
+        return v.Schema(self.base, extra=True)

+ 30
- 0
grafana_dashboards/schema/panel/dashlist.py View File

@@ -0,0 +1,30 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+from grafana_dashboards.schema.panel.base import Base
18
+
19
+
20
+class Dashlist(Base):
21
+
22
+    def get_schema(self):
23
+        dashlist = {
24
+            v.Required('limit', default=10): v.All(int),
25
+            v.Required('mode', default='starred'): v.Any('search', 'starred'),
26
+            v.Required('tag', default=''): v.All(str),
27
+            v.Required('query', default=''): v.All(str),
28
+        }
29
+        dashlist.update(self.base)
30
+        return v.Schema(dashlist)

+ 38
- 0
grafana_dashboards/schema/panel/graph.py View File

@@ -0,0 +1,38 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+from grafana_dashboards.schema.panel.base import Base
18
+
19
+
20
+class Graph(Base):
21
+
22
+    def get_schema(self):
23
+        graph = {
24
+            v.Required('bars', default=False): v.All(bool),
25
+            v.Required('fill', default=1): v.All(int),
26
+            v.Required('lines', default=True): v.All(bool),
27
+            v.Required('linewidth', default=2): v.All(int),
28
+            v.Required('percentage', default=False): v.All(bool),
29
+            v.Required('pointradius', default=5): v.All(int),
30
+            v.Required('points', default=False): v.All(bool),
31
+            v.Required('stack', default=False): v.All(bool),
32
+            v.Required('steppedLine', default=False): v.All(bool),
33
+            v.Required('targets', default=[]): v.All(list),
34
+            v.Required('x-axis', default=True): v.All(bool),
35
+            v.Required('y-axis', default=True): v.All(bool),
36
+        }
37
+        graph.update(self.base)
38
+        return v.Schema(graph)

+ 68
- 0
grafana_dashboards/schema/panel/singlestat.py View File

@@ -0,0 +1,68 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+from grafana_dashboards.schema.panel.base import Base
18
+
19
+
20
+class Singlestat(Base):
21
+
22
+    def get_schema(self):
23
+        # TODO(pabelanger): This is pretty ugly, there much be a better way to
24
+        # set default values.
25
+        sparkline_defaults = {
26
+            'fillColor': 'rgba(31, 118, 189, 0.18)',
27
+            'full': False,
28
+            'lineColor': 'rgb(31, 120, 193)',
29
+            'show': False,
30
+        }
31
+        sparkline = {
32
+            v.Required(
33
+                'fillColor', default=sparkline_defaults['fillColor']
34
+            ): v.All(str),
35
+            v.Required('full', default=False): v.All(bool),
36
+            v.Required(
37
+                'lineColor', default=sparkline_defaults['lineColor']
38
+            ): v.All(str),
39
+            v.Required('show', default=False): v.All(bool),
40
+        }
41
+
42
+        singlestat = {
43
+            v.Required('colorBackground', default=False): v.All(bool),
44
+            v.Required('colorValue', default=False): v.All(bool),
45
+            v.Required('maxDataPoints', default=100): v.All(int),
46
+            v.Required('postfix', default=''): v.All(str),
47
+            # Support 0% to 200% by 10
48
+            v.Required(
49
+                'postfixFontSize', default='50%'): v.All(
50
+                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
51
+            v.Required('prefix', default=''): v.All(str),
52
+            # Support 0% to 200% by 10
53
+            v.Required(
54
+                'prefixFontSize', default='50%'): v.All(
55
+                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
56
+            v.Required('sparkline', default=sparkline_defaults): sparkline,
57
+            v.Required('targets', default=[]): v.All(list),
58
+            v.Required('thresholds', default=''): v.All(str),
59
+            # Support 0% to 200% by 10
60
+            v.Required(
61
+                'valueFontSize', default='80%'): v.All(
62
+                    v.Match(r'^[1-9]?[0]{1}%$|^1[0-9]?[0]{1}%$|^200%$')),
63
+            v.Required('valueName', default='avg'): v.All(
64
+                'avg', 'current', 'max', 'min', 'total'),
65
+            v.Optional('decimals'): v.All(int, v.Range(min=0, max=12)),
66
+        }
67
+        singlestat.update(self.base)
68
+        return v.Schema(singlestat)

+ 30
- 0
grafana_dashboards/schema/panel/text.py View File

@@ -0,0 +1,30 @@
1
+# Copyright 2015 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import voluptuous as v
16
+
17
+from grafana_dashboards.schema.panel.base import Base
18
+
19
+
20
+class Text(Base):
21
+
22
+    def get_schema(self):
23
+        text = {
24
+            v.Required('content'): v.All(str),
25
+            v.Required('mode', default='markdown'): v.Any(
26
+                'html', 'markdown', 'text'),
27
+            v.Optional('style'): dict(),
28
+        }
29
+        text.update(self.base)
30
+        return v.Schema(text)

Loading…
Cancel
Save