├── .gitignore
├── .travis.yml
├── LICENSE.txt
├── domain-generator-maven-plugin
├── LICENSE.txt
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── uk
│ │ └── co
│ │ └── szmg
│ │ └── jsonbuildergenerator
│ │ ├── CodeGenerator.java
│ │ ├── DomainDescription.java
│ │ ├── FieldDescription.java
│ │ └── JsonBuilderGenerator.java
│ └── resources
│ └── templates
│ ├── class.ftl
│ └── factories.ftl
├── domain
├── LICENSE.txt
├── pom.xml
└── src
│ └── main
│ ├── domainconfig
│ ├── dashboard.yaml
│ ├── graph.yaml
│ ├── link.yaml
│ ├── panels.yaml
│ └── templating.yaml
│ └── java
│ └── uk
│ └── co
│ └── szmg
│ └── grafana
│ └── domain
│ ├── BaseJsonObject.java
│ ├── Colors.java
│ ├── FromTo.java
│ └── SelectedTemplate.java
├── grafana-dashboard-generator-cli
├── LICENSE.txt
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── uk
│ │ └── co
│ │ └── szmg
│ │ └── grafana
│ │ └── cli
│ │ ├── DashboardGeneratorApplication.java
│ │ └── internal
│ │ ├── CommandLineArgs.java
│ │ ├── ExitPlease.java
│ │ ├── Interaction.java
│ │ └── UploaderConfig.java
│ └── test
│ ├── java
│ └── uk
│ │ └── co
│ │ └── szmg
│ │ └── grafana
│ │ └── cli
│ │ ├── DashboardStoreTest.java
│ │ └── example
│ │ └── OverviewDashboards.java
│ └── resources
│ └── test-endpoints.yaml
├── grafana-dashboard-generator-example
├── LICENSE.txt
├── dependency-reduced-pom.xml
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── uk
│ │ └── co
│ │ └── szmg
│ │ └── grafana
│ │ └── example
│ │ ├── Main.java
│ │ └── dashboards
│ │ ├── FirstDashboard.java
│ │ ├── ManyDashboards.java
│ │ ├── MyDashboardFactory.java
│ │ ├── Utils.java
│ │ └── complex
│ │ ├── MultiEnvExampleDashboards.java
│ │ ├── Resource.java
│ │ ├── SampleAppEnvironment.java
│ │ └── XYComponentFactory.java
│ └── resources
│ └── endpoints.yaml
├── grafana-dashboard-generator
├── LICENSE.txt
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── uk
│ │ └── co
│ │ └── szmg
│ │ └── grafana
│ │ ├── DashboardFactory.java
│ │ ├── DashboardSerializer.java
│ │ ├── DashboardUploader.java
│ │ ├── GrafanaClient.java
│ │ ├── GrafanaEndpoint.java
│ │ ├── dashboard
│ │ ├── BasicFactories.java
│ │ ├── ColorTheme.java
│ │ ├── Environment.java
│ │ ├── SimpleEnvironment.java
│ │ └── StaticFactories.java
│ │ └── stores
│ │ ├── ClasspathGrafanaEndpointStore.java
│ │ ├── DashboardFilter.java
│ │ ├── DashboardStore.java
│ │ ├── FileBasedGrafanaEndpointStore.java
│ │ ├── GrafanaEndpointParser.java
│ │ └── GrafanaEndpointStore.java
│ └── test
│ └── java
│ └── uk
│ └── co
│ └── szmg
│ └── grafana
│ └── DashboardSerializerTest.java
├── grafana-dashboard-maven-plugin
├── LICENSE.txt
├── pom.xml
└── src
│ └── main
│ └── java
│ └── uk
│ └── co
│ └── szmg
│ └── grafana
│ └── maven
│ ├── AbstractMojoWithClasspath.java
│ ├── AbstractMojoWithDashboards.java
│ ├── Generate.java
│ ├── GenerateAndUpload.java
│ └── Upload.java
├── pom.xml
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .idea
3 | *.iml
4 | output
5 |
6 | # I know...
7 | node_modules
8 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - oraclejdk8
4 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | domain-generator-maven-plugin
11 | maven-plugin
12 |
13 |
14 |
15 | org.apache.maven
16 | maven-plugin-api
17 |
18 |
19 | org.apache.maven.plugin-tools
20 | maven-plugin-annotations
21 | provided
22 |
23 |
24 | org.apache.maven
25 | maven-project
26 |
27 |
28 | org.freemarker
29 | freemarker
30 |
31 |
32 | com.google.googlejavaformat
33 | google-java-format
34 |
35 |
36 | com.fasterxml.jackson.core
37 | jackson-databind
38 |
39 |
40 | com.fasterxml.jackson.dataformat
41 | jackson-dataformat-yaml
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-plugin-plugin
50 | 3.3
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/java/uk/co/szmg/jsonbuildergenerator/CodeGenerator.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.jsonbuildergenerator;
2 |
3 | /*-
4 | * #%L
5 | * domain-generator-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.google.googlejavaformat.java.Formatter;
24 | import com.google.googlejavaformat.java.FormatterException;
25 | import freemarker.template.Configuration;
26 | import freemarker.template.Template;
27 | import freemarker.template.TemplateException;
28 | import freemarker.template.TemplateExceptionHandler;
29 | import org.apache.maven.plugin.MojoFailureException;
30 |
31 | import java.io.IOException;
32 | import java.io.StringWriter;
33 | import java.util.HashMap;
34 | import java.util.List;
35 | import java.util.Map;
36 |
37 | public class CodeGenerator {
38 |
39 | private static final String CLASS_TEMPLATE = "class.ftl";
40 | private static final String FACTORIES_TEMPLATE = "factories.ftl";
41 | private static final String TEMPLATE_PATH = "templates";
42 |
43 | private final Configuration templateCfg;
44 | private final Formatter codeFormatter;
45 |
46 | public CodeGenerator() {
47 | templateCfg = new Configuration(Configuration.VERSION_2_3_23);
48 | templateCfg.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), TEMPLATE_PATH);
49 | templateCfg.setDefaultEncoding("UTF-8");
50 | templateCfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
51 | templateCfg.setLogTemplateExceptions(false);
52 |
53 | codeFormatter = new Formatter();
54 | }
55 |
56 | public String generateJavaCodeFor(DomainDescription domain, String packageName) throws MojoFailureException {
57 | Map model = new HashMap<>();
58 | model.put("domain", domain);
59 | model.put("packageName", packageName);
60 |
61 | Template temp = getTemplate(CLASS_TEMPLATE);
62 | String code = generateCode(model, temp);
63 | return formatCode(code);
64 | }
65 |
66 | public String generateFactories(List domains, String packageName) throws MojoFailureException {
67 | Map model = new HashMap<>();
68 | model.put("domains", domains);
69 | model.put("packageName", packageName);
70 |
71 | Template temp = getTemplate(FACTORIES_TEMPLATE);
72 | String code = generateCode(model, temp);
73 | return formatCode(code);
74 | }
75 |
76 | private String formatCode(String code) throws MojoFailureException {
77 | try {
78 | return codeFormatter.formatSource(code);
79 | } catch (FormatterException e) {
80 | throw new MojoFailureException("Cannot format source code", e);
81 | }
82 | }
83 |
84 | private String generateCode(Map model, Template temp) throws MojoFailureException {
85 | StringWriter stringWriter = new StringWriter();
86 | try {
87 | temp.process(model, stringWriter);
88 | } catch (TemplateException e) {
89 | throw new MojoFailureException("Error during source generation", e);
90 | } catch (IOException e) {
91 | throw new MojoFailureException("This just cannot happen... can it?", e);
92 | }
93 | return stringWriter.toString();
94 | }
95 |
96 | private Template getTemplate(String name) throws MojoFailureException {
97 | try {
98 | return templateCfg.getTemplate(name);
99 | } catch (IOException e) {
100 | throw new MojoFailureException("Could not load template", e);
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/java/uk/co/szmg/jsonbuildergenerator/DomainDescription.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.jsonbuildergenerator;
2 |
3 | /*-
4 | * #%L
5 | * domain-generator-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.fasterxml.jackson.annotation.JsonProperty;
24 |
25 | import java.util.List;
26 | import java.util.Map;
27 |
28 | public class DomainDescription {
29 |
30 | private String name;
31 |
32 | private List fields;
33 |
34 | private Map defaultValues;
35 |
36 | @JsonProperty("abstract")
37 | private boolean isAbstract;
38 |
39 | @JsonProperty("extends")
40 | private String extendedClass;
41 |
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | public List getFields() {
47 | return fields;
48 | }
49 |
50 | public Map getDefaultValues() {
51 | return defaultValues;
52 | }
53 |
54 | public boolean isAbstract() {
55 | return isAbstract;
56 | }
57 |
58 | public String getExtendedClass() {
59 | return extendedClass;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/java/uk/co/szmg/jsonbuildergenerator/FieldDescription.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.jsonbuildergenerator;
2 |
3 | /*-
4 | * #%L
5 | * domain-generator-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | public class FieldDescription {
24 |
25 | private String name;
26 | private String type;
27 | private String description;
28 | private boolean readonly;
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public String getType() {
35 | return type;
36 | }
37 |
38 | public String getDescription() {
39 | return description;
40 | }
41 |
42 | public boolean isReadonly() {
43 | return readonly;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/java/uk/co/szmg/jsonbuildergenerator/JsonBuilderGenerator.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.jsonbuildergenerator;
2 |
3 | /*-
4 | * #%L
5 | * domain-generator-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 |
24 | import com.fasterxml.jackson.databind.MappingIterator;
25 | import com.fasterxml.jackson.databind.ObjectMapper;
26 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
27 | import org.apache.maven.model.FileSet;
28 | import org.apache.maven.plugin.AbstractMojo;
29 | import org.apache.maven.plugin.MojoExecutionException;
30 | import org.apache.maven.plugin.MojoFailureException;
31 | import org.apache.maven.plugins.annotations.LifecyclePhase;
32 | import org.apache.maven.plugins.annotations.Mojo;
33 | import org.apache.maven.plugins.annotations.Parameter;
34 | import org.apache.maven.project.MavenProject;
35 | import org.codehaus.plexus.util.DirectoryScanner;
36 | import org.codehaus.plexus.util.StringUtils;
37 |
38 | import java.io.File;
39 | import java.io.FileWriter;
40 | import java.io.IOException;
41 | import java.util.ArrayList;
42 | import java.util.List;
43 |
44 | /**
45 | * In fact it doesn't build anything like Json. It's a bad name.
46 | *
47 | * Not thread-safe.
48 | */
49 | @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
50 | public class JsonBuilderGenerator extends AbstractMojo {
51 |
52 | @Parameter(defaultValue = "${project}", required = true)
53 | protected MavenProject project;
54 |
55 | @Parameter(defaultValue = "${project.build.directory}/generated-sources/jsonbuilder", required = true)
56 | protected File outputJavaDirectory;
57 |
58 | @Parameter(required = true)
59 | protected List descriptors;
60 |
61 | @Parameter
62 | protected String packageName;
63 |
64 | private CodeGenerator codeGenerator = new CodeGenerator();
65 |
66 | private List generatedDomains = new ArrayList<>();
67 |
68 | @Override
69 | public void execute() throws MojoExecutionException, MojoFailureException {
70 | if (this.project != null) {
71 | this.project.addCompileSourceRoot(this.outputJavaDirectory.getAbsolutePath());
72 | }
73 | if (!this.outputJavaDirectory.mkdirs()) {
74 | getLog().error("Could not create source directory! (it might already exist)");
75 | } else {
76 | try {
77 | generateDomains();
78 | generateFactories();
79 | } catch (IOException e) {
80 | throw new MojoExecutionException("Could not generate Java source code!", e);
81 | }
82 | }
83 | }
84 |
85 | private void generateFactories() throws MojoFailureException {
86 | String code = codeGenerator.generateFactories(generatedDomains, packageName);
87 | File f = createPackageDirsAndGetJavaFile("DomainFactories");
88 | writeToFile(f, code);
89 | }
90 |
91 | private void generateDomains() throws IOException, MojoExecutionException, MojoFailureException {
92 |
93 | for (FileSet descriptor : descriptors) {
94 | DirectoryScanner scanner = new DirectoryScanner();
95 | scanner.setBasedir(descriptor.getDirectory());
96 | scanner.setIncludes(toArrayOrNull(descriptor.getIncludes()));
97 | scanner.setExcludes(toArrayOrNull(descriptor.getExcludes()));
98 | scanner.scan();
99 | for (String yamlPath : scanner.getIncludedFiles()) {
100 | generateJavaCodeFor(new File(scanner.getBasedir(), yamlPath));
101 | }
102 | }
103 | }
104 |
105 | private void generateJavaCodeFor(File yamlPath) throws MojoExecutionException, MojoFailureException {
106 | ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
107 | try {
108 | MappingIterator iterator = mapper.readerFor(DomainDescription.class).readValues(yamlPath);
109 | while (iterator.hasNextValue()) {
110 | generateJavaCodeFor(iterator.next());
111 | }
112 | } catch (IOException e) {
113 | getLog().error(String.format("Cannot parse descriptor [%s]", yamlPath), e);
114 | throw new MojoExecutionException(String.format("Cannot parse descriptor [%s]", yamlPath), e);
115 | }
116 | }
117 |
118 | private void generateJavaCodeFor(DomainDescription domain) throws MojoFailureException {
119 | getLog().info(String.format("Generating %s", domain.getName()));
120 | generatedDomains.add(domain); // :(
121 |
122 | String code = codeGenerator.generateJavaCodeFor(domain, packageName);
123 | File javaFile = createPackageDirsAndGetJavaFile(domain.getName());
124 | writeToFile(javaFile, code);
125 | }
126 |
127 | private void writeToFile(File file, String content) throws MojoFailureException {
128 | try (FileWriter writer = new FileWriter(file)) {
129 | writer.write(content);
130 | } catch (IOException e) {
131 | throw new MojoFailureException("Could not write source file", e);
132 | }
133 | }
134 |
135 | private File createPackageDirsAndGetJavaFile(String name) {
136 | File dir = new File(outputJavaDirectory.getAbsolutePath());
137 | if (StringUtils.isNotBlank(packageName)) {
138 | dir = new File(dir, packageName.replaceAll("\\.", File.separator));
139 | }
140 | dir.mkdirs();
141 |
142 | return new File(dir, name + ".java");
143 | }
144 |
145 | private static String[] toArrayOrNull(List list) {
146 | String[] result = null;
147 | if (list != null && !list.isEmpty()) {
148 | result = list.toArray(new String[list.size()]);
149 | }
150 | return result;
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/resources/templates/class.ftl:
--------------------------------------------------------------------------------
1 | <#--
2 | #%L
3 | domain-generator-maven-plugin
4 | %%
5 | Copyright (C) 2017 Mate Gabor Szvoboda
6 | %%
7 | Licensed under the Apache License, Version 2.0 (the "License");
8 | you may not use this file except in compliance with the License.
9 | You may obtain a copy of the License at
10 |
11 | http://www.apache.org/licenses/LICENSE-2.0
12 |
13 | Unless required by applicable law or agreed to in writing, software
14 | distributed under the License is distributed on an "AS IS" BASIS,
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | See the License for the specific language governing permissions and
17 | limitations under the License.
18 | #L%
19 | -->
20 | <#if packageName??>package ${packageName};#if>
21 |
22 | import java.util.List;
23 | import java.util.Map;
24 | import java.util.Set;
25 |
26 | <#assign className = domain.name?cap_first>
27 | <#assign withReturnType = className>
28 | <#assign parent = domain.extendedClass!"BaseJsonObject">
29 | <#if domain.abstract>
30 | <#assign withReturnType = "C">
31 | public abstract class ${className}> extends ${parent} {
32 | <#else>
33 | public class ${className} extends ${parent}<${className}> {
34 | #if>
35 |
36 |
37 | <#list domain.fields as field>
38 | protected static final String ${toConstName(field.name)} = "${field.name?j_string}";
39 | #list>
40 |
41 |
42 | public ${className}() {
43 | super();
44 | <#if domain.defaultValues??>
45 | <#list domain.defaultValues?keys as field>
46 | setValue("${field?j_string}", ${domain.defaultValues[field]});
47 | #list>
48 | #if>
49 | }
50 |
51 |
52 | <#list domain.fields as field>
53 | <#assign fieldNameConst = toConstName(field.name)>
54 | <#assign fieldNameCap = field.name?cap_first>
55 |
56 | /**
57 | * Gets ${field.name}.
58 | * ${field.description!?html}
59 | *
60 | * @return ${field.name}
61 | */
62 | public ${field.type} get${fieldNameCap}() {
63 | return getValue(${fieldNameConst});
64 | }
65 |
66 | /**
67 | * Sets ${field.name}.
68 | * ${field.description!?html}
69 | *
70 | * @param ${field.name} the ${field.name}
71 | */
72 | public void set${fieldNameCap}(${field.type} ${field.name}) {
73 | setValue(${fieldNameConst}, ${field.name});
74 | }
75 |
76 | /**
77 | * Sets ${field.name}.
78 | * ${field.description!?html}
79 | *
80 | * @param ${field.name} the ${field.name}
81 | * @return itself
82 | */
83 | public ${withReturnType} with${fieldNameCap}(${field.type} ${field.name}) {
84 | return withValue(${fieldNameConst}, ${field.name});
85 | }
86 |
87 | <#if field.type?starts_with("List<")>
88 | <#assign innerType = field.type?keep_after("<")?keep_before_last(">")>
89 | <#assign singularName = fieldNameCap?remove_ending("s")>
90 | <#assign paramName = singularName?uncap_first>
91 |
92 | /**
93 | * Adds a(n) ${field.name?remove_ending("s")}.
94 | * ${field.description!?html}
95 | *
96 | * @param ${paramName} the ${paramName}
97 | * @return itself
98 | */
99 | public ${withReturnType} add${singularName}(${innerType} ${paramName}) {
100 | ${field.type} _list = get${fieldNameCap}();
101 | if (_list == null) {
102 | _list = new java.util.ArrayList<>();
103 | }
104 | _list.add(${paramName});
105 | return with${fieldNameCap}(_list);
106 | }
107 | #if>
108 | #list>
109 |
110 | <#if domain.abstract>
111 | /** Generic implementation of ${className}. It's easier to to use sometimes. */
112 | public static class Generic extends ${className} { }
113 | #if>
114 |
115 | }
116 |
117 |
118 | <#function toConstName name>
119 | <#return "FIELD_" + name?replace("[A-Z]", "_$0", "r")?upper_case>
120 | #function>
121 |
--------------------------------------------------------------------------------
/domain-generator-maven-plugin/src/main/resources/templates/factories.ftl:
--------------------------------------------------------------------------------
1 | <#--
2 | #%L
3 | domain-generator-maven-plugin
4 | %%
5 | Copyright (C) 2017 Mate Gabor Szvoboda
6 | %%
7 | Licensed under the Apache License, Version 2.0 (the "License");
8 | you may not use this file except in compliance with the License.
9 | You may obtain a copy of the License at
10 |
11 | http://www.apache.org/licenses/LICENSE-2.0
12 |
13 | Unless required by applicable law or agreed to in writing, software
14 | distributed under the License is distributed on an "AS IS" BASIS,
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | See the License for the specific language governing permissions and
17 | limitations under the License.
18 | #L%
19 | -->
20 | <#if packageName??>package ${packageName};#if>
21 |
22 | public class DomainFactories {
23 |
24 | protected DomainFactories() { }
25 |
26 | <#list domains as domain>
27 | <#if !domain.abstract>
28 | <#assign className = domain.name?cap_first>
29 |
30 | /**
31 | * Creates a new ${domain.name}.
32 | * @return a new instance of ${className}
33 | */
34 | public static ${className} new${className}() {
35 | return new ${className}();
36 | }
37 | #if>
38 | #list>
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/domain/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | domain
11 |
12 |
13 |
14 | com.fasterxml.jackson.core
15 | jackson-databind
16 |
17 |
18 |
19 |
20 |
21 |
22 | uk.co.szmg.grafana
23 | domain-generator-maven-plugin
24 | ${parent.version}
25 |
26 |
27 |
28 | generate
29 |
30 |
31 | uk.co.szmg.grafana.domain
32 |
33 |
34 | ${project.basedir}/src/main/domainconfig
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/domain/src/main/domainconfig/dashboard.yaml:
--------------------------------------------------------------------------------
1 | name: Dashboard
2 | fields:
3 | - name: id
4 | type: Integer
5 | - name: title
6 | type: String
7 | - name: style
8 | type: String
9 | description: "Style, e.g., 'dark'"
10 | - name: timezone
11 | type: String
12 | description: "E.g., 'browser'"
13 | - name: editable
14 | type: Boolean
15 | - name: hideControls
16 | type: Boolean
17 | - name: graphTooltip
18 | type: Integer
19 | - name: rows
20 | type: List
21 | - name: time
22 | type: FromTo
23 | - name: tags
24 | type: List
25 | description: "List of tags, which can be used for e.g., link generation"
26 | - name: links
27 | type: List
28 | description: "Links to be shown on the top of the dashboard."
29 | - name: annotations
30 | type: AnnotationList
31 | - name: templating
32 | type: TemplateList
33 |
34 | ---
35 | name: Row
36 | fields:
37 | - name: collapse
38 | type: Boolean
39 | - name: editable
40 | type: Boolean
41 | - name: height
42 | type: String
43 | description: "E.g., '200px'"
44 | - name: panels
45 | type: List
46 | - name: title
47 | type: String
48 |
49 | ---
50 | name: DashboardLevelLink
51 | abstract: true
52 | fields:
53 | - name: type
54 | type: String
55 | readonly: true
56 |
57 | ---
58 | name: ExternalDashboardLevelLink
59 | extends: DashboardLevelLink
60 | defaultValues:
61 | type: '"link"'
62 | fields:
63 | - name: icon
64 | type: String
65 | - name: includeVars
66 | type: Boolean
67 | - name: keepTime
68 | type: Boolean
69 | - name: targetBlank
70 | type: Boolean
71 | - name: title
72 | type: String
73 | - name: tooltip
74 | type: String
75 | - name: url
76 | type: String
77 |
78 | ---
79 | name: LinksToDashboards
80 | extends: DashboardLevelLink
81 | defaultValues:
82 | type: '"dashboards"'
83 | tags: 'new java.util.ArrayList()'
84 | fields:
85 | - name: includeVars
86 | type: Boolean
87 | - name: keepTime
88 | type: Boolean
89 | - name: targetBlank
90 | type: Boolean
91 | - name: tags
92 | type: List
93 |
94 | ---
95 | name: DropdownToDashboards
96 | extends: DashboardLevelLink
97 | defaultValues:
98 | type: '"dashboards"'
99 | asDropdown: 'true'
100 | tags: 'new java.util.ArrayList()'
101 | fields:
102 | - name: asDropdown
103 | type: Boolean
104 | readonly: true
105 | - name: includeVars
106 | type: Boolean
107 | - name: keepTime
108 | type: Boolean
109 | - name: targetBlank
110 | type: Boolean
111 | - name: tags
112 | type: List
113 | - name: title
114 | type: String
115 |
116 | ---
117 | name: AnnotationList
118 | fields:
119 | - name: list
120 | type: List
121 |
122 | ---
123 | name: Annotation
124 | abstract: true
125 | fields:
126 | - name: datasource
127 | type: String
128 | - name: enable
129 | type: Boolean
130 | - name: iconColor
131 | type: String
132 | - name: name
133 | type: String
134 |
135 | ---
136 | name: AlertAnnotation
137 | extends: Annotation
138 | defaultValues:
139 | datasource: '"-- Grafana --"'
140 | type: '"alert"'
141 | fields:
142 | - name: limit
143 | type: Integer
144 | description: "Watch out, it can't be anything in the UI. 10, 50, 100, 200, 300, 500, 1000, 2000."
145 | - name: type
146 | type: String
147 |
148 | ---
149 | name: GraphiteAnnotation
150 | extends: Annotation
151 | fields:
152 | - name: tags
153 | type: String
154 | description: "Graphite events query"
155 | - name: target
156 | type: String
157 | description: "Graphite metrics query"
158 |
--------------------------------------------------------------------------------
/domain/src/main/domainconfig/graph.yaml:
--------------------------------------------------------------------------------
1 | name: Graph
2 | extends: Panel
3 | defaultValues:
4 | type: '"graph"'
5 | fields:
6 | - name: aliasColors
7 | type: Object
8 | description: "TODO"
9 | - name: bars
10 | type: Boolean
11 | - name: datasource
12 | type: String
13 | - name: fill
14 | type: Integer
15 | - name: legend
16 | type: Legend
17 | - name: lines
18 | type: Boolean
19 | - name: linewidth
20 | type: Integer
21 | - name: links
22 | type: List
23 | - name: nullPointMode
24 | type: String
25 | description: "Possible values: 'null', 'null as zero', 'connected'"
26 | - name: percentage
27 | type: Boolean
28 | - name: pointradius
29 | type: Integer
30 | - name: points
31 | type: Boolean
32 | - name: renderer
33 | type: String
34 | description: "TODO what is this? it's flot by default"
35 | - name: seriesOverrides
36 | type: List
37 | - name: stack
38 | type: Boolean
39 | - name: steppedLine
40 | type: Boolean
41 | description: "It's called 'Staircase' on UI (belongs to lines draw mode)"
42 | - name: targets
43 | type: List
44 | - name: thresholds
45 | type: List
46 | - name: timeFrom
47 | type: String
48 | description: "Override relative time, e.g., 1h"
49 | - name: timeShift
50 | type: String
51 | description: "Add time shift, e.g., 1h"
52 | - name: tooltip
53 | type: Tooltip
54 | - name: xaxis
55 | type: XAxis
56 | - name: yaxes
57 | type: List
58 | description: "Length should be 2: left, right. TODO"
59 |
60 | ---
61 | name: Legend
62 | fields:
63 | - name: alignAsTable
64 | type: Boolean
65 | - name: avg
66 | type: Boolean
67 | - name: current
68 | type: Boolean
69 | - name: hideEmpty
70 | type: Boolean
71 | - name: hideZero
72 | type: Boolean
73 | - name: max
74 | type: Boolean
75 | - name: min
76 | type: Boolean
77 | - name: rightSide
78 | type: Boolean
79 | - name: show
80 | type: Boolean
81 | - name: total
82 | type: Boolean
83 | - name: values
84 | type: Boolean
85 |
86 | ---
87 | name: Tooltip
88 | fields:
89 | - name: shared
90 | type: Boolean
91 | description: "True if all series is in one tooltip."
92 | - name: sort
93 | type: Integer
94 | description: "Sort order; 0=None, 1=Increasing, 2=Decreasing"
95 | - name: value_type
96 | type: String
97 | description: "Stacked value; 'individual' or 'cumulative'"
98 |
99 | ---
100 | name: XAxis
101 | fields:
102 | - name: mode
103 | type: String
104 | description: "time or series"
105 | - name: name
106 | type: String
107 | - name: show
108 | type: Boolean
109 | - name: values
110 | type: List
111 | description: "Use when mode is 'series'. Add one of 'avg', 'max', 'min', 'total', 'count', 'current'."
112 |
113 | ---
114 | name: YAxis
115 | fields:
116 | - name: format
117 | type: String
118 | description: "Format, e.g., 'short', 'ms'"
119 | - name: label
120 | type: String
121 | description: "Label on axis"
122 | - name: logBase
123 | type: Integer
124 | description: "1: linear, 2: log2, 10: log10, ..."
125 | - name: max
126 | type: String
127 | description: "Forced max value"
128 | - name: min
129 | type: String
130 | description: "Forced min value"
131 | - name: show
132 | type: Boolean
133 |
134 | ---
135 | name: Threshold
136 | fields:
137 | - name: colorMode
138 | type: String
139 | description: "'critical', 'warning', 'ok' or 'custom'"
140 | - name: op
141 | type: String
142 | description: "'gt' or 'lt'"
143 | - name: value
144 | type: Integer
145 | - name: fill
146 | type: Boolean
147 | description:
148 | - name: fillColor
149 | type: String
150 | description: "Only is colorMode is custom, e.g., rgb(222, 21, 21)"
151 | - name: line
152 | type: Boolean
153 | description:
154 | - name: lineColor
155 | type: String
156 | description: "Only is colorMode is custom, e.g., rgb(222, 21, 21)"
157 |
158 | ---
159 | name: SeriesOverride
160 | fields:
161 | - name: alias
162 | type: String
163 | description: "Alias or regex. Add fields to override with the addField method."
164 |
--------------------------------------------------------------------------------
/domain/src/main/domainconfig/link.yaml:
--------------------------------------------------------------------------------
1 | name: Link
2 | abstract: true
3 | fields:
4 | - name: type
5 | type: String
6 | readonly: true
7 | - name: title
8 | type: String
9 | - name: includeVars
10 | type: Boolean
11 | - name: keepTime
12 | type: Boolean
13 | - name: targetBlank
14 | type: Boolean
15 | - name: params
16 | type: String
17 |
18 | ---
19 | name: DashboardLink
20 | extends: Link
21 | defaultValues:
22 | type: '"dashboard"'
23 | fields:
24 | - name: dashboard
25 | type: String
26 |
27 | ---
28 | name: AbsoluteLink
29 | extends: Link
30 | defaultValues:
31 | type: '"absolute"'
32 | fields:
33 | - name: url
34 | type: String
35 |
36 |
--------------------------------------------------------------------------------
/domain/src/main/domainconfig/panels.yaml:
--------------------------------------------------------------------------------
1 | name: Panel
2 | abstract: true
3 | fields:
4 | - name: id
5 | type: Integer
6 | - name: title
7 | type: String
8 | - name: description
9 | type: String
10 | - name: span
11 | type: Integer
12 | - name: height
13 | type: String
14 | - name: transparent
15 | type: Boolean
16 | - name: minSpan
17 | type: Integer
18 | - name: type
19 | type: String
20 | readonly: true
21 |
22 |
23 | ---
24 | name: Text
25 | extends: Panel
26 | defaultValues:
27 | type: '"text"'
28 | fields:
29 | - name: mode
30 | type: String
31 | description: "Render mode, valid values are 'html', 'markdown', 'text'"
32 | - name: content
33 | type: String
34 |
35 | ---
36 | name: SingleStat
37 | extends: Panel
38 | defaultValues:
39 | type: '"singlestat"'
40 | fields:
41 | - name: cacheTimeout
42 | type: Integer
43 | - name: colorBackground
44 | type: Boolean
45 | - name: colorValue
46 | type: Boolean
47 | - name: colors
48 | type: Colors
49 | - name: datasource
50 | type: String
51 | - name: format
52 | type: String
53 | description: "E.g., 'short', 'ms'"
54 | - name: gauge
55 | type: Gauge
56 | - name: interval
57 | type: String
58 | description: "TODO what is this?"
59 | - name: links
60 | type: List
61 | - name: mappingType
62 | type: Integer
63 | description: "1 --> value to text; 2 --> range to text"
64 | - name: mappingTypes
65 | type: Object
66 | description: "TODO 'value to text' = 1; 'range to text' = 2"
67 | - name: maxDataPoints
68 | type: Integer
69 | - name: nullPointMode
70 | type: String
71 | description: "Default value: 'connected'"
72 | - name: nullText
73 | type: String
74 | - name: postfix
75 | type: String
76 | - name: postfixFontSize
77 | type: String
78 | description: "Default value: '50%'"
79 | - name: prefix
80 | type: String
81 | - name: prefixFontSize
82 | type: String
83 | description: "Default value: '50%'"
84 | - name: rangeMaps
85 | type: List
86 | description: "mappingType should be 2"
87 | - name: sparkline
88 | type: Sparkline
89 | - name: targets
90 | type: List
91 | - name: thresholds
92 | type: String
93 | - name: valueFontSize
94 | type: String
95 | description: "Default value: '80%'"
96 | - name: valueMaps
97 | type: List
98 | description: "mappingType should be 1"
99 | - name: valueName
100 | type: String
101 | description: "Default value: 'avg'. Possible names: 'min', 'max', 'avg', 'current', 'total', 'name', 'first', 'delta', 'diff', 'range'."
102 |
103 | ---
104 | name: Gauge
105 | defaultValues:
106 | show: 'true' # If a gauge is not null, user wants it to show.
107 | fields:
108 | - name: maxValue
109 | type: Integer
110 | - name: minValue
111 | type: Integer
112 | - name: show
113 | type: Boolean
114 | - name: thresholdLabels
115 | type: Boolean
116 | - name: thresholdMarkers
117 | type: Boolean
118 |
119 | ---
120 | name: Sparkline
121 | defaultValues:
122 | show: 'true' # If a gauge is not null, user wants it to show.
123 | fields:
124 | - name: fillColor
125 | type: String
126 | - name: full
127 | type: Boolean
128 | - name: show
129 | type: Boolean
130 | - name: lineColor
131 | type: String
132 |
133 | ---
134 | name: Target #TODO move to normal object and create a very nice builder (with functions maybe)
135 | fields:
136 | - name: refId
137 | type: String
138 | - name: target
139 | type: String
140 |
141 | ---
142 | name: RangeMapping
143 | fields:
144 | - name: from
145 | type: String
146 | - name: to
147 | type: String
148 | - name: text
149 | type: String
150 |
151 | ---
152 | name: ValueMapping
153 | fields:
154 | - name: value
155 | type: String
156 | - name: op
157 | type: String
158 | description: 'E.g., "="'
159 | - name: text
160 | type: String
161 |
--------------------------------------------------------------------------------
/domain/src/main/domainconfig/templating.yaml:
--------------------------------------------------------------------------------
1 | name: TemplateList
2 | fields:
3 | - name: list
4 | type: List
5 |
6 | ---
7 | name: Template
8 | defaultValues:
9 | options: 'new java.util.ArrayList()'
10 | refresh: 2
11 | fields:
12 | - name: name
13 | type: String
14 | - name: label
15 | type: String
16 | - name: hide
17 | type: int
18 | description: "Hide: 0 for none, 1 for label, 2 for variable"
19 | - name: current
20 | type: SelectedTemplate
21 | - name: type
22 | type: String
23 | description: "valid values: query, interval, datasource, custom, constant"
24 | - name: allValue
25 | type: String
26 | description: "Custom all value"
27 | - name: includeAll
28 | type: Boolean
29 | description: "Include All option"
30 | - name: multi
31 | type: Boolean
32 | description: "Enables multiple values to be selected at the same time"
33 | - name: datasource
34 | type: String
35 | - name: options
36 | type: List
37 | description: "option list"
38 | - name: query
39 | type: String
40 | description: "This is the Graphite query, the constant value or the comma separated list of custom values."
41 | - name: refresh
42 | type: int
43 | description: "0 for never, 1 for On dashboard load, 2 for On Time Range Change"
44 | - name: regex
45 | type: String
46 | description: "Optional, if you want to extract part of a series name or metric node segment."
47 | - name: sort
48 | type: int
49 | description: >
50 | 0 for none,
51 | 1 for alphabetical asc,
52 | 2 for alphabetical desc,
53 | 3 for numerical asc,
54 | 4 for numerical desc
55 | - name: tagValuesQuery
56 | type: String
57 | - name: tags
58 | type: List
59 | - name: tagsQuery
60 | type: String
61 | - name: useTags
62 | type: Boolean
63 | description: "Value groups/tags (Experimental feature)"
64 |
65 | ---
66 | name: TemplateOption
67 | fields:
68 | - name: selected
69 | type: Boolean
70 | - name: text
71 | type: String
72 | - name: value
73 | type: String
74 |
--------------------------------------------------------------------------------
/domain/src/main/java/uk/co/szmg/grafana/domain/BaseJsonObject.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.domain;
2 |
3 | /*-
4 | * #%L
5 | * domain
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.fasterxml.jackson.core.JsonGenerator;
24 | import com.fasterxml.jackson.databind.JsonSerializer;
25 | import com.fasterxml.jackson.databind.SerializerProvider;
26 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
27 |
28 | import java.io.IOException;
29 | import java.util.HashMap;
30 | import java.util.Map;
31 |
32 | /**
33 | * A naively simple Map wrapper. The goal was to be able to model JSON
34 | * objects that has known and unknown properties as well. This class
35 | * should be extended with the known fields, while one can always add
36 | * custom values using {@link #setValue(String, Object)} or {@link #withValue(String, Object)}.
37 | * (Or "change" the type of a known field.
38 | *
39 | * Note that this class is mutable.
40 | */
41 | @JsonSerialize(using = BaseJsonObject.BaseJsonObjectSerializer.class)
42 | public abstract class BaseJsonObject> {
43 |
44 | private Map fields = new HashMap<>();
45 |
46 | /**
47 | * Sets a given field.
48 | * @param fieldName field name, i.e., JSON property name when serialised
49 | * @param value value, which should be serializable by Jackson; {@code null} values will not be included
50 | */
51 | public void setValue(String fieldName, Object value) {
52 | fields.put(fieldName, value);
53 | }
54 |
55 | /**
56 | * Fluent setter of a given field.
57 | * @param fieldName field name, i.e., JSON property name when serialised
58 | * @param value value, which should be serializable by Jackson; {@code null} values will not be included
59 | * @return itself, so it is chainable
60 | */
61 | public C withValue(String fieldName, Object value) {
62 | fields.put(fieldName, value);
63 | return (C) this;
64 | }
65 |
66 | /**
67 | * Gets value of the given field, or {@code null} if not set.
68 | * @param fieldName field name, i.e., JSON property name when serialised
69 | * @param expected type of the value
70 | * @return the value if it has been set, {@code null} otherwise
71 | */
72 | public T getValue(String fieldName) {
73 | return (T) fields.get(fieldName);
74 | }
75 |
76 | public static class BaseJsonObjectSerializer extends JsonSerializer> {
77 |
78 | @Override
79 | public void serialize(BaseJsonObject> baseJsonObject, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
80 | jsonGenerator.writeStartObject();
81 | for (Map.Entry entry : baseJsonObject.fields.entrySet()) {
82 | jsonGenerator.writeObjectField(entry.getKey(), entry.getValue());
83 | }
84 | jsonGenerator.writeEndObject();
85 | }
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/domain/src/main/java/uk/co/szmg/grafana/domain/Colors.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.domain;
2 |
3 | /*-
4 | * #%L
5 | * domain
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.fasterxml.jackson.core.JsonGenerator;
24 | import com.fasterxml.jackson.core.JsonProcessingException;
25 | import com.fasterxml.jackson.databind.JsonSerializer;
26 | import com.fasterxml.jackson.databind.SerializerProvider;
27 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
28 |
29 | import java.io.IOException;
30 |
31 | /**
32 | * Grafana color triplet used in singlestat.
33 | */
34 | @JsonSerialize(using = Colors.ColorsSerialiser.class)
35 | public class Colors {
36 |
37 | private String low;
38 | private String mid;
39 | private String high;
40 |
41 | /**
42 | * Default constructor with default colors: red, yellow, green.
43 | */
44 | public Colors() {
45 | this("rgba(245, 54, 54, 0.9)", "rgba(237, 129, 40, 0.89)", "rgba(50, 172, 45, 0.97)");
46 | }
47 |
48 | /**
49 | * Convenience constructor.
50 | * @param low color used when the value is low
51 | * @param mid color used when the value is medium
52 | * @param high color used when the value is high
53 | */
54 | public Colors(String low, String mid, String high) {
55 | this.low = low;
56 | this.mid = mid;
57 | this.high = high;
58 | }
59 |
60 | /**
61 | * Gets color used when the value is low.
62 | * @return color
63 | */
64 | public String getLow() {
65 | return low;
66 | }
67 |
68 | /**
69 | * Sets color used when the value is low.
70 | * @param low color that's meaningful in HTML
71 | */
72 | public void setLow(String low) {
73 | this.low = low;
74 | }
75 |
76 | /**
77 | * Gets color used when the value is medium.
78 | * @return color
79 | */
80 | public String getMid() {
81 | return mid;
82 | }
83 |
84 | /**
85 | * Sets color used when the value is medium.
86 | * @param mid color that's meaningful in HTML
87 | */
88 | public void setMid(String mid) {
89 | this.mid = mid;
90 | }
91 |
92 | /**
93 | * Gets color used when the value is high.
94 | * @return color
95 | */
96 | public String getHigh() {
97 | return high;
98 | }
99 |
100 | /**
101 | * Sets color used when the value is high.
102 | * @param high color that's meaningful in HTML
103 | */
104 | public void setHigh(String high) {
105 | this.high = high;
106 | }
107 |
108 | public static class ColorsSerialiser extends JsonSerializer {
109 |
110 | @Override
111 | public void serialize(Colors colors, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
112 | jsonGenerator.writeStartArray();
113 | jsonGenerator.writeString(colors.low);
114 | jsonGenerator.writeString(colors.mid);
115 | jsonGenerator.writeString(colors.high);
116 | jsonGenerator.writeEndArray();
117 | }
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/domain/src/main/java/uk/co/szmg/grafana/domain/FromTo.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.domain;
2 |
3 | /*-
4 | * #%L
5 | * domain
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | /**
24 | * Time range that is used in Grafana Dashboards.
25 | * By default it's from "now-6h" to "now". Look up Grafana docs for valid values.
26 | */
27 | public class FromTo {
28 | // TODO time type + utils
29 | private String from;
30 | private String to;
31 |
32 | /**
33 | * Default constructor with default values: from "now-6h" to "now".
34 | */
35 | public FromTo() {
36 | this("now-6h", "now");
37 | }
38 |
39 | /**
40 | * Convenience constructor.
41 | * @param from start of the displayed time range
42 | * @param to end of the displayed time range
43 | */
44 | public FromTo(String from, String to) {
45 | this.from = from;
46 | this.to = to;
47 | }
48 |
49 | /**
50 | * Gets the start of the displayed time range.
51 | * @return Grafana time expression
52 | */
53 | public String getFrom() {
54 | return from;
55 | }
56 |
57 | /**
58 | * Sets the start of the displayed time range.
59 | * @param from Grafana time expression
60 | */
61 | public void setFrom(String from) {
62 | this.from = from;
63 | }
64 |
65 | /**
66 | * Gets the end of the displayed time range.
67 | * @return Grafana time expression
68 | */
69 | public String getTo() {
70 | return to;
71 | }
72 |
73 | /**
74 | * Sets the end of the displayed time range.
75 | * @param to Grafana time expression
76 | */
77 | public void setTo(String to) {
78 | this.to = to;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/domain/src/main/java/uk/co/szmg/grafana/domain/SelectedTemplate.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.domain;
2 |
3 | /*-
4 | * #%L
5 | * domain
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | public class SelectedTemplate {
24 |
25 | private String text;
26 | private String value;
27 |
28 | public SelectedTemplate() { }
29 |
30 | public SelectedTemplate(String text, String value) {
31 | this.text = text;
32 | this.value = value;
33 | }
34 |
35 | public String getText() {
36 | return text;
37 | }
38 |
39 | public void setText(String text) {
40 | this.text = text;
41 | }
42 |
43 | public String getValue() {
44 | return value;
45 | }
46 |
47 | public void setValue(String value) {
48 | this.value = value;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | grafana-dashboard-generator-cli
11 |
12 |
13 |
14 | uk.co.szmg.grafana
15 | grafana-dashboard-generator
16 |
17 |
18 | org.springframework
19 | spring-context
20 |
21 |
22 | javax.inject
23 | javax.inject
24 |
25 |
26 | de.codeshelf.consoleui
27 | consoleui
28 |
29 |
30 | com.fasterxml.jackson.core
31 | jackson-databind
32 |
33 |
34 | com.fasterxml.jackson.dataformat
35 | jackson-dataformat-yaml
36 |
37 |
38 | net.sf.jopt-simple
39 | jopt-simple
40 |
41 |
42 |
43 | junit
44 | junit
45 | test
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/DashboardGeneratorApplication.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.fusesource.jansi.AnsiConsole;
24 | import uk.co.szmg.grafana.DashboardSerializer;
25 | import uk.co.szmg.grafana.DashboardUploader;
26 | import uk.co.szmg.grafana.GrafanaClient;
27 | import uk.co.szmg.grafana.stores.ClasspathGrafanaEndpointStore;
28 | import uk.co.szmg.grafana.cli.internal.CommandLineArgs;
29 | import uk.co.szmg.grafana.stores.DashboardStore;
30 | import uk.co.szmg.grafana.cli.internal.ExitPlease;
31 | import uk.co.szmg.grafana.stores.GrafanaEndpointStore;
32 | import uk.co.szmg.grafana.cli.internal.Interaction;
33 | import uk.co.szmg.grafana.cli.internal.UploaderConfig;
34 | import uk.co.szmg.grafana.domain.Dashboard;
35 |
36 | import java.io.File;
37 | import java.io.FileOutputStream;
38 | import java.io.IOException;
39 | import java.util.Collection;
40 | import java.util.List;
41 | import java.util.Optional;
42 |
43 | import static org.fusesource.jansi.Ansi.ansi;
44 |
45 | public class DashboardGeneratorApplication {
46 |
47 | private String rootPackage;
48 |
49 | public DashboardGeneratorApplication(String rootPackage) {
50 | this.rootPackage = rootPackage;
51 | }
52 |
53 | public int main(String[] args) throws IOException {
54 | try {
55 | return safeMain(args);
56 | } catch (ExitPlease ex) {
57 | return ex.getReturnCode();
58 | }
59 | }
60 |
61 | private int safeMain(String[] args) throws IOException, ExitPlease {
62 | AnsiConsole.systemInstall();
63 |
64 | // welcome
65 | System.out.println(ansi().render("\n@|blue Welcome to Grafana Dashboard Builder|@\n"));
66 |
67 | UploaderConfig config = new UploaderConfig();
68 |
69 | CommandLineArgs commandLineArgs = new CommandLineArgs();
70 | commandLineArgs.parse(args, config);
71 | initStores(config);
72 | commandLineArgs.apply(config);
73 | loadDefaults(config);
74 |
75 | Interaction.askUserForDetails(config);
76 |
77 | if (config.getEndpoint().isPresent()) {
78 | upload(config);
79 | } else {
80 | writeToFile(config);
81 | }
82 |
83 | return 0;
84 | }
85 |
86 | private void initStores(UploaderConfig config) {
87 | // dashboard
88 | config.setDashboardStore(new DashboardStore(rootPackage));
89 | DashboardStore dashboardStore = config.getDashboardStore();
90 | dashboardStore.load();
91 |
92 | // check duplicates
93 | dashboardStore.getDuplicatesErrorMessage().ifPresent(msg -> {
94 | System.err.println(msg);
95 | throw new ExitPlease(-1);
96 | });
97 |
98 | // endpoint
99 | GrafanaEndpointStore endpointStore = config.getEndpointStore();
100 | if (endpointStore == null) {
101 | endpointStore = new ClasspathGrafanaEndpointStore("/endpoints.yaml");
102 | config.setEndpointStore(endpointStore);
103 | }
104 |
105 | try {
106 | endpointStore.load();
107 | } catch (IOException e) {
108 | System.err.println("Could not load endpoint store; " + e.getMessage());
109 | throw new ExitPlease(-2);
110 | }
111 |
112 | }
113 |
114 | private void loadDefaults(UploaderConfig config) {
115 | List dashboards = config.getDashboardStore().getDashboards();
116 | if (config.getDashboards() == null && dashboards.size() == 1) {
117 | config.setDashboards(dashboards);
118 | }
119 |
120 | if (config.getEndpointStore().getEndpoints().isEmpty()) {
121 | config.setEndpoint(Optional.empty());
122 | }
123 |
124 | if (config.getOutputDirectory() == null) {
125 | config.setOutputDirectory(new File("./output"));
126 | }
127 |
128 | if (config.isBatchMode() && config.getOverwrite() == null) {
129 | config.setOverwrite(false);
130 | }
131 | }
132 |
133 | private void upload(UploaderConfig config) {
134 | boolean overwrite = config.getOverwrite();
135 | DashboardUploader dashboardUploader = new DashboardUploader(config.getEndpoint().get());
136 | for (Dashboard dashboard : config.getDashboards()) {
137 | System.out.printf("%s: ", dashboard.getTitle());
138 |
139 | try {
140 | dashboardUploader.upload(dashboard, overwrite);
141 | System.out.println(ansi().render("@|green done|@"));
142 | } catch (RuntimeException e) {
143 | boolean alreadyExists = false;
144 | if (e.getCause() instanceof GrafanaClient.UnexpectedGrafanaResponseException) {
145 | GrafanaClient.UnexpectedGrafanaResponseException ex = (GrafanaClient.UnexpectedGrafanaResponseException) e.getCause();
146 | alreadyExists = ex.getResponseCode() == 412;
147 | }
148 |
149 | if (alreadyExists && !overwrite) {
150 | System.out.println(ansi().render("@|red already exists|@"));
151 | } else {
152 | throw e;
153 | }
154 | }
155 | }
156 | }
157 |
158 | private void writeToFile(UploaderConfig config) throws IOException {
159 | DashboardSerializer dashboardSerializer = new DashboardSerializer();
160 | File outputDirectory = config.getOutputDirectory();
161 | outputDirectory.mkdirs();
162 | for (Dashboard dashboard : config.getDashboards()) {
163 | System.out.printf("%s: ", dashboard.getTitle());
164 |
165 | File f = new File(outputDirectory, filenameFor(dashboard));
166 | if (f.exists() && !config.getOverwrite()) {
167 | System.out.println(ansi().render("@|red already exists|@"));
168 | } else {
169 | try (FileOutputStream stream = new FileOutputStream(f)) {
170 | dashboardSerializer.write(dashboard, stream);
171 | }
172 | System.out.println(ansi().render("@|green done|@"));
173 | }
174 |
175 | }
176 |
177 | }
178 |
179 | private String filenameFor(Dashboard dashboard) {
180 | return dashboard.getTitle().replaceAll("[\\s:/\\\\]+", "-") + ".json";
181 | }
182 |
183 | }
184 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/internal/CommandLineArgs.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli.internal;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import joptsimple.OptionParser;
24 | import joptsimple.OptionSet;
25 | import uk.co.szmg.grafana.GrafanaEndpoint;
26 | import uk.co.szmg.grafana.domain.Dashboard;
27 | import uk.co.szmg.grafana.stores.DashboardFilter;
28 | import uk.co.szmg.grafana.stores.FileBasedGrafanaEndpointStore;
29 |
30 | import java.io.File;
31 | import java.io.IOException;
32 | import java.util.List;
33 | import java.util.Optional;
34 | import java.util.stream.Collectors;
35 |
36 | import static java.util.Arrays.asList;
37 |
38 | public class CommandLineArgs {
39 |
40 | private String forcedEndpoint;
41 | private DashboardFilter dashboardFilter;
42 |
43 | public void parse(String[] args, UploaderConfig config) throws IOException {
44 | OptionParser parser = new OptionParser();
45 | parser.acceptsAll(asList("B", "batch"), "Non-interactive, batch mode");
46 | parser.acceptsAll(asList("f", "force"), "Overwrite existing dashboards");
47 | parser.acceptsAll(asList("d", "dashboard"), "Dashboards to generate; can have multiple value")
48 | .withRequiredArg()
49 | .describedAs("title");
50 | parser.acceptsAll(asList("i", "include"), "Dashboards to include")
51 | .withRequiredArg()
52 | .describedAs("regexp");
53 | parser.acceptsAll(asList("exclude"), "Dashboards to exclude")
54 | .withRequiredArg()
55 | .describedAs("regexp");
56 | parser.accepts("endpoints", "Endpoints YAML file")
57 | .withRequiredArg()
58 | .ofType(File.class);
59 | parser.acceptsAll(asList("e", "endpoint"), "Name of the endpoint to use")
60 | //.availableUnless("output")
61 | .withRequiredArg()
62 | .describedAs("endpoint");
63 | parser.acceptsAll(asList("o", "output"), "output directory -- will skip upload")
64 | .availableUnless("endpoint")
65 | .withRequiredArg()
66 | .ofType(File.class);
67 | parser.acceptsAll(asList("h", "?", "help"), "show help" )
68 | .forHelp();
69 |
70 | OptionSet options = parser.parse(args);
71 |
72 | if (options.has("help")) {
73 | parser.printHelpOn(System.out);
74 | throw new ExitPlease(-3);
75 | }
76 |
77 | config.setBatchMode(options.has("batch"));
78 |
79 | if (options.has("endpoints")) {
80 | File endpointsFile = (File) options.valueOf("endpoints");
81 | config.setEndpointStore(new FileBasedGrafanaEndpointStore(endpointsFile));
82 | }
83 |
84 | if (options.has("force")) {
85 | config.setOverwrite(true);
86 | }
87 |
88 | if (options.has("output")) {
89 | config.setEndpoint(Optional.empty());
90 | config.setOutputDirectory((File) options.valueOf("output"));
91 | }
92 |
93 | if (options.has("endpoint")) {
94 | forcedEndpoint = (String) options.valueOf("endpoint");
95 | }
96 |
97 | if (options.has("dashboard") || options.has("include") || options.has("exclude"))
98 | dashboardFilter = new DashboardFilter(
99 | toStringList(options.valuesOf("dashboard")),
100 | toStringList(options.valuesOf("include")),
101 | toStringList(options.valuesOf("exclude")));
102 | }
103 |
104 | public void apply(UploaderConfig config) {
105 | if (forcedEndpoint != null) {
106 | GrafanaEndpoint endpoint = config.getEndpointStore().getEndpoints().get(forcedEndpoint);
107 | if (endpoint == null) {
108 | System.err.println("Endpoint not found: " + forcedEndpoint);
109 | throw new ExitPlease(-5);
110 | }
111 | config.setEndpoint(Optional.of(endpoint));
112 | }
113 |
114 | if (dashboardFilter != null) {
115 | List dashboards;
116 | try {
117 | dashboards = dashboardFilter.filter(config.getDashboardStore().getDashboards());
118 | } catch (DashboardFilter.IncludedDashboardIsNotFound ex) {
119 | System.err.println(ex.getMessage());
120 | throw new ExitPlease(-6);
121 | }
122 | config.setDashboards(dashboards);
123 | System.out.println("Dashboards: " + dashboards.stream()
124 | .map(Dashboard::getTitle)
125 | .collect(Collectors.joining(", ")));
126 | }
127 | }
128 |
129 | private List toStringList(List> list) {
130 | return (List) list;
131 | }
132 |
133 |
134 | }
135 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/internal/ExitPlease.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli.internal;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | public class ExitPlease extends IllegalStateException {
24 |
25 | private int returnCode;
26 |
27 | public ExitPlease(int returnCode) {
28 | this.returnCode = returnCode;
29 | }
30 |
31 | public int getReturnCode() {
32 | return returnCode;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/internal/Interaction.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli.internal;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import de.codeshelf.consoleui.elements.ConfirmChoice;
24 | import de.codeshelf.consoleui.elements.PromptableElementIF;
25 | import de.codeshelf.consoleui.prompt.CheckboxResult;
26 | import de.codeshelf.consoleui.prompt.ConfirmResult;
27 | import de.codeshelf.consoleui.prompt.ConsolePrompt;
28 | import de.codeshelf.consoleui.prompt.ListResult;
29 | import de.codeshelf.consoleui.prompt.PromtResultItemIF;
30 | import de.codeshelf.consoleui.prompt.builder.CheckboxPromptBuilder;
31 | import de.codeshelf.consoleui.prompt.builder.ListPromptBuilder;
32 | import de.codeshelf.consoleui.prompt.builder.PromptBuilder;
33 | import uk.co.szmg.grafana.GrafanaEndpoint;
34 | import uk.co.szmg.grafana.domain.Dashboard;
35 |
36 | import java.io.IOException;
37 | import java.util.ArrayList;
38 | import java.util.Collections;
39 | import java.util.List;
40 | import java.util.Map;
41 | import java.util.Optional;
42 |
43 | public class Interaction {
44 |
45 | public static void askUserForDetails(UploaderConfig config) throws IOException {
46 | List dashboards = config.getDashboardStore().getDashboards();
47 | Map endpoints = config.getEndpointStore().getEndpoints();
48 |
49 | ConsolePrompt prompt = new ConsolePrompt();
50 |
51 | Map results = Collections.emptyMap();
52 | do {
53 | PromptBuilder promptBuilder = prompt.getPromptBuilder();
54 |
55 | if (config.getDashboards() == null) {
56 | CheckboxPromptBuilder dashboardPrompt = promptBuilder.createCheckboxPrompt()
57 | .name("dashboards")
58 | .message("Which dashboard(s) would you like to work with?");
59 | for (int i = 0; i < dashboards.size(); i++) {
60 | dashboardPrompt.newItem(Integer.toString(i)).text(dashboards.get(i).getTitle()).check().add();
61 | }
62 | dashboardPrompt.addPrompt();
63 | }
64 |
65 | if (config.getEndpoint() == null) {
66 | ListPromptBuilder endpointPrompt = promptBuilder.createListPrompt()
67 | .name("endpoint")
68 | .message("Which endpoint to upload to?");
69 | for (Map.Entry entry : endpoints.entrySet()) {
70 | endpointPrompt
71 | .newItem(entry.getKey())
72 | .text(String.format("%s (%s)", entry.getKey(), entry.getValue().getBaseUrl()))
73 | .add();
74 | }
75 | endpointPrompt
76 | .newItem("-")
77 | .text("Just generate the files to " + config.getOutputDirectory())
78 | .add()
79 | .addPrompt();
80 | }
81 |
82 | if (config.getOverwrite() == null) {
83 | promptBuilder.createConfirmPromp()
84 | .name("overwrite")
85 | .message("Should dashboards be overwritten if already exist?")
86 | .defaultValue(ConfirmChoice.ConfirmationValue.YES)
87 | .addPrompt();
88 | }
89 |
90 | promptBuilder.createConfirmPromp()
91 | .name("confirmed")
92 | .message("Are you ready?")
93 | .defaultValue(ConfirmChoice.ConfirmationValue.YES)
94 | .addPrompt();
95 |
96 |
97 | List questions = promptBuilder.build();
98 | if (questions.size() == 1) {
99 | // only the confirmation, we know everything
100 | break;
101 | }
102 |
103 | if (config.isBatchMode()) {
104 | System.err.println("Missing info for batch mode"); // TODO details
105 | throw new ExitPlease(-6);
106 | }
107 |
108 | results = prompt.prompt(questions);
109 | } while (!getBooleanResult(results, "confirmed"));
110 |
111 | if (results.containsKey("dashboards")) {
112 | CheckboxResult selection = (CheckboxResult) results.get("dashboards");
113 | List selectedDashboards = new ArrayList<>();
114 | for (String i : selection.getSelectedIds()) {
115 | selectedDashboards.add(dashboards.get(Integer.parseInt(i, 10)));
116 | }
117 | config.setDashboards(selectedDashboards);
118 | }
119 |
120 | if (results.containsKey("endpoint")) {
121 | ListResult result = (ListResult) results.get("endpoint");
122 | if ("-".equals(result.getSelectedId())) {
123 | config.setEndpoint(Optional.empty());
124 | } else {
125 | config.setEndpoint(Optional.of(endpoints.get(result.getSelectedId())));
126 | }
127 | }
128 |
129 | if (results.containsKey("overwrite")) {
130 | config.setOverwrite(getBooleanResult(results, "overwrite"));
131 | }
132 | }
133 |
134 | private static boolean getBooleanResult(Map results, String key) {
135 | return ((ConfirmResult) results.get(key)).getConfirmed() == ConfirmChoice.ConfirmationValue.YES;
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/main/java/uk/co/szmg/grafana/cli/internal/UploaderConfig.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli.internal;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.GrafanaEndpoint;
24 | import uk.co.szmg.grafana.domain.Dashboard;
25 | import uk.co.szmg.grafana.stores.DashboardStore;
26 | import uk.co.szmg.grafana.stores.GrafanaEndpointStore;
27 |
28 | import java.io.File;
29 | import java.util.List;
30 | import java.util.Optional;
31 |
32 | public class UploaderConfig {
33 |
34 | private boolean batchMode;
35 | private DashboardStore dashboardStore;
36 | private GrafanaEndpointStore endpointStore;
37 | private Boolean overwrite;
38 | private Optional endpoint;
39 | private List dashboards;
40 | private File outputDirectory;
41 |
42 | public boolean isBatchMode() {
43 | return batchMode;
44 | }
45 |
46 | public void setBatchMode(boolean batchMode) {
47 | this.batchMode = batchMode;
48 | }
49 |
50 | public DashboardStore getDashboardStore() {
51 | return dashboardStore;
52 | }
53 |
54 | public void setDashboardStore(DashboardStore dashboardStore) {
55 | this.dashboardStore = dashboardStore;
56 | }
57 |
58 | public GrafanaEndpointStore getEndpointStore() {
59 | return endpointStore;
60 | }
61 |
62 | public void setEndpointStore(GrafanaEndpointStore endpointStore) {
63 | this.endpointStore = endpointStore;
64 | }
65 |
66 | public Boolean getOverwrite() {
67 | return overwrite;
68 | }
69 |
70 | public void setOverwrite(Boolean overwrite) {
71 | this.overwrite = overwrite;
72 | }
73 |
74 | public Optional getEndpoint() {
75 | return endpoint;
76 | }
77 |
78 | public void setEndpoint(Optional endpoint) {
79 | this.endpoint = endpoint;
80 | }
81 |
82 | public List getDashboards() {
83 | return dashboards;
84 | }
85 |
86 | public void setDashboards(List dashboards) {
87 | this.dashboards = dashboards;
88 | }
89 |
90 | public File getOutputDirectory() {
91 | return outputDirectory;
92 | }
93 |
94 | public void setOutputDirectory(File outputDirectory) {
95 | this.outputDirectory = outputDirectory;
96 | }
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/test/java/uk/co/szmg/grafana/cli/DashboardStoreTest.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.junit.Test;
24 | import uk.co.szmg.grafana.stores.DashboardStore;
25 | import uk.co.szmg.grafana.domain.Dashboard;
26 |
27 | import java.util.List;
28 |
29 | import static org.hamcrest.CoreMatchers.is;
30 | import static org.junit.Assert.assertThat;
31 |
32 | public class DashboardStoreTest {
33 |
34 | @Test
35 | public void shouldFindDashboards() {
36 | DashboardStore store = new DashboardStore("uk.co.szmg.grafana.cli.example");
37 | store.load();
38 | List dashboards = store.getDashboards();
39 |
40 | assertThat(dashboards.size(), is(1));
41 | assertThat(dashboards.get(0).getTitle(), is("Overview dashboard"));
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/test/java/uk/co/szmg/grafana/cli/example/OverviewDashboards.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.cli.example;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.DashboardFactory;
24 | import uk.co.szmg.grafana.domain.Dashboard;
25 |
26 | import javax.inject.Named;
27 | import java.util.Collections;
28 | import java.util.List;
29 |
30 | import static uk.co.szmg.grafana.domain.DomainFactories.newDashboard;
31 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
32 | import static uk.co.szmg.grafana.domain.DomainFactories.newText;
33 |
34 | @Named
35 | public class OverviewDashboards implements DashboardFactory {
36 |
37 | @Override
38 | public List create() {
39 | Dashboard dashboard = newDashboard()
40 | .withTitle("Overview dashboard")
41 | .addRow(newRow()
42 | .addPanel(newText().withSpan(12).withTransparent(true).withContent("Hello World!")));
43 | return Collections.singletonList(dashboard);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-cli/src/test/resources/test-endpoints.yaml:
--------------------------------------------------------------------------------
1 | # This file contains different Grafana endpoints.
2 | # It's parsed to Map.
3 |
4 |
5 | # This is my local test Grafana server! Hurray!
6 | test:
7 | baseUrl: http://localhost:8080
8 | apiKey: some-api-key
9 |
10 |
11 | # This one's for production... need to find a solution for auth
12 | production:
13 | baseUrl: https://grafana.invalidcert.com
14 | apiKey: some-other-api-key
15 | skipSSLValidation: yes
16 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/dependency-reduced-pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.0.0-SNAPSHOT
7 |
8 | 4.0.0
9 | grafana-dashboard-generator-example
10 |
11 |
12 |
13 | uk.co.szmg.grafana
14 | grafana-dashboard-maven-plugin
15 | ${project.parent.version}
16 |
17 |
18 | generate-jsons
19 | prepare-package
20 |
21 | generate
22 |
23 |
24 | uk.co.szmg.grafana.example.dashboards
25 |
26 | MYAPP.*
27 | fancy
28 |
29 |
30 | Production
31 |
32 |
33 |
34 |
35 | generate-and-upload-prod
36 | deploy
37 |
38 | generateAndUpload
39 |
40 |
41 | uk.co.szmg.grafana.example.dashboards
42 |
43 | Production
44 |
45 | ${grafanaUrl}
46 | ${grafanaSession}
47 | true
48 | true
49 |
50 |
51 |
52 |
53 |
54 | maven-shade-plugin
55 | 3.0.0
56 |
57 |
58 | package
59 |
60 | shade
61 |
62 |
63 |
64 |
65 | uk.co.szmg.grafana.example.Main
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | grafana-dashboard-generator-parent
7 | uk.co.szmg.grafana
8 | 2.0.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | grafana-dashboard-generator-example
13 |
14 |
15 |
16 | uk.co.szmg.grafana
17 | grafana-dashboard-generator-cli
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | uk.co.szmg.grafana
26 | grafana-dashboard-maven-plugin
27 | ${project.parent.version}
28 |
29 |
30 | generate-jsons
31 | prepare-package
32 |
33 | generate
34 |
35 |
36 | uk.co.szmg.grafana.example.dashboards
37 |
38 | MYAPP.*
39 | fancy
40 |
41 |
42 | Production
43 |
44 |
45 |
46 |
47 |
48 | generate-and-upload-prod
49 | deploy
50 |
51 | generateAndUpload
52 |
53 |
54 | uk.co.szmg.grafana.example.dashboards
55 |
56 | Production
57 |
58 | ${grafanaUrl}
59 | ${grafanaSession}
60 | true
61 | true
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | org.apache.maven.plugins
71 | maven-shade-plugin
72 | 3.0.0
73 |
74 |
75 | package
76 |
77 | shade
78 |
79 |
80 |
81 |
82 | uk.co.szmg.grafana.example.Main
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/Main.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.cli.DashboardGeneratorApplication;
24 |
25 | import java.io.IOException;
26 |
27 | public class Main {
28 |
29 | public static void main(String[] args) throws IOException {
30 | new DashboardGeneratorApplication("uk.co.szmg.grafana.example.dashboards").main(args);
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/FirstDashboard.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Dashboard;
24 |
25 | import javax.inject.Named;
26 |
27 | import static uk.co.szmg.grafana.domain.DomainFactories.newDropdownToDashboards;
28 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
29 | import static uk.co.szmg.grafana.domain.DomainFactories.newText;
30 |
31 | @Named
32 | public class FirstDashboard extends Dashboard { // not recommended as it enables access to protected members
33 |
34 | public FirstDashboard() {
35 | withTitle("Dashboard with inheritance");
36 | addRow(newRow().addPanel(newText().withContent("This was created by extending Dashboard and").withSpan(12)));
37 | addRow(newRow().addPanel(newText().withContent("marking it as Spring bean with @Named annotation").withSpan(12)));
38 | addTag("mate");
39 | addLink(newDropdownToDashboards().withTitle("All dashboard").withTargetBlank(true));
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/ManyDashboards.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.springframework.context.annotation.Bean;
24 | import org.springframework.context.annotation.Configuration;
25 | import uk.co.szmg.grafana.domain.Dashboard;
26 |
27 | import static uk.co.szmg.grafana.example.dashboards.Utils.createSimpleDashboard;
28 |
29 | @Configuration
30 | public class ManyDashboards {
31 |
32 | @Bean
33 | public Dashboard oneDashboard() {
34 | return createSimpleDashboard("Some fancy dashboard", ManyDashboards.class);
35 | }
36 |
37 | @Bean
38 | public Dashboard otherDashboard() {
39 | return createSimpleDashboard("Some other fancy dashboard", ManyDashboards.class);
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/MyDashboardFactory.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.DashboardFactory;
24 | import uk.co.szmg.grafana.domain.Dashboard;
25 |
26 | import javax.inject.Named;
27 | import java.util.ArrayList;
28 | import java.util.List;
29 |
30 | import static uk.co.szmg.grafana.example.dashboards.Utils.createSimpleDashboard;
31 |
32 | @Named
33 | public class MyDashboardFactory implements DashboardFactory {
34 |
35 | @Override
36 | public List create() {
37 | List dashboards = new ArrayList<>();
38 | dashboards.add(createSimpleDashboard("By factory 1", MyDashboardFactory.class));
39 | dashboards.add(createSimpleDashboard("By factory 2", MyDashboardFactory.class));
40 | dashboards.add(createSimpleDashboard("By factory 3", MyDashboardFactory.class));
41 | return dashboards;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/Utils.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Dashboard;
24 | import uk.co.szmg.grafana.domain.SelectedTemplate;
25 |
26 | import static uk.co.szmg.grafana.domain.DomainFactories.newAlertAnnotation;
27 | import static uk.co.szmg.grafana.domain.DomainFactories.newAnnotationList;
28 | import static uk.co.szmg.grafana.domain.DomainFactories.newDashboard;
29 | import static uk.co.szmg.grafana.domain.DomainFactories.newGraphiteAnnotation;
30 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
31 | import static uk.co.szmg.grafana.domain.DomainFactories.newTemplate;
32 | import static uk.co.szmg.grafana.domain.DomainFactories.newTemplateList;
33 | import static uk.co.szmg.grafana.domain.DomainFactories.newTemplateOption;
34 | import static uk.co.szmg.grafana.domain.DomainFactories.newText;
35 |
36 | public class Utils {
37 |
38 | public static Dashboard createSimpleDashboard(String title, Class> clazz) {
39 | return newDashboard()
40 | .withTitle(title)
41 | .addRow(newRow()
42 | .addPanel(newText()
43 | .withSpan(12).withContent("This was created by " + clazz.getSimpleName())))
44 | .addRow(newRow()
45 | .withHeight("95px")
46 | .addPanel(newText()
47 | .withSpan(12).withContent("Selected env: $env")))
48 | .withAnnotations(newAnnotationList()
49 | .addList(newAlertAnnotation()
50 | .withName("alert")
51 | .withLimit(300)
52 | .withEnable(true))
53 | .addList(newGraphiteAnnotation()
54 | .withName("deployment")
55 | .withDatasource("graphite")
56 | .withTarget("app.deployment")
57 | .withIconColor("green")))
58 | .withTemplating(newTemplateList()
59 | .addList(newTemplate()
60 | .withName("env")
61 | .withLabel("Environment")
62 | .withType("custom")
63 | .withQuery("staging,canary,production,perf")
64 | // options have to be added... templating support should be revised...
65 | .addOption(newTemplateOption().withValue("staging").withText("staging"))
66 | .addOption(newTemplateOption().withValue("production").withText("production"))
67 | .withCurrent(new SelectedTemplate("staging", "staging"))));
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/complex/MultiEnvExampleDashboards.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards.complex;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.DashboardFactory;
24 | import uk.co.szmg.grafana.dashboard.ColorTheme;
25 | import uk.co.szmg.grafana.domain.Dashboard;
26 | import uk.co.szmg.grafana.domain.Row;
27 | import uk.co.szmg.grafana.domain.SingleStat;
28 |
29 | import javax.inject.Named;
30 | import java.util.ArrayList;
31 | import java.util.List;
32 |
33 | import static uk.co.szmg.grafana.dashboard.StaticFactories.placeholder;
34 | import static uk.co.szmg.grafana.dashboard.StaticFactories.thinRow;
35 | import static uk.co.szmg.grafana.dashboard.StaticFactories.title;
36 | import static uk.co.szmg.grafana.domain.DomainFactories.newDashboard;
37 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
38 |
39 | @Named
40 | public class MultiEnvExampleDashboards implements DashboardFactory {
41 |
42 | private SampleAppEnvironment environments[] =
43 | new SampleAppEnvironment[]{
44 | new SampleAppEnvironment("Staging", "graphite", "MYAPP.stage", 2),
45 | new SampleAppEnvironment("Production", "aws.graphite", "MYAPP.prod", 10)
46 | };
47 |
48 | private ColorTheme colorTheme = ColorTheme.RED_YELLOW_GREEN;
49 |
50 | @Override
51 | public List create() {
52 | List dashboards = new ArrayList<>();
53 | for (SampleAppEnvironment environment : environments) {
54 | dashboards.add(create(environment));
55 | }
56 | return dashboards;
57 | }
58 |
59 | private Dashboard create(SampleAppEnvironment environment) {
60 | XYComponentFactory factory = new XYComponentFactory(environment, colorTheme);
61 |
62 | // might be better off in a factory method
63 | SingleStat e500 = factory
64 | .errorCounter("sumSeries({ROOT}.instrumentedFilter.serverErrors.count)", true)
65 | .withTitle("E500 count");
66 |
67 | // row definitions
68 | Row row1 = thinRow()
69 | .addPanel(title("Overview in " + environment.getName()).withSpan(12));
70 |
71 | Row row2 = thinRow()
72 | .addPanel(e500)
73 | .addPanel(placeholder(8))
74 | .addPanel(factory.appInstances());
75 |
76 | Row row3 = newRow()
77 | .addPanel(factory.responseTime(Resource.USER_REGISTRATION_REQUEST))
78 | .addPanel(factory.responseTime(Resource.FORGOTTEN_PASSWORD));
79 |
80 | // dashboard
81 | return newDashboard()
82 | .withTitle("MYAPP :: " + environment.getName())
83 | .addTag("generated")
84 | .addTag("MyApp")
85 | .addRow(row1)
86 | .addRow(row2)
87 | .addRow(row3);
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/complex/Resource.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards.complex;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | public enum Resource {
24 | USER_REGISTRATION_REQUEST("user.registration", "User registration"),
25 | FORGOTTEN_PASSWORD("paswordhelp", "Forgatten password");
26 |
27 | Resource(String name, String metricsPath) {
28 | this.name = name;
29 | this.metricsPath = metricsPath;
30 | }
31 |
32 | private String name;
33 | private String metricsPath;
34 |
35 | public String getName() {
36 | return name;
37 | }
38 |
39 | public String getMetricsPath() {
40 | return metricsPath;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/complex/SampleAppEnvironment.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards.complex;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.dashboard.SimpleEnvironment;
24 |
25 | public class SampleAppEnvironment extends SimpleEnvironment {
26 |
27 | private int expectedNumberOfInstances;
28 |
29 | public SampleAppEnvironment(String name, String datasource, String metricsRoot, int expectedNumberOfInstances) {
30 | super(name, datasource, metricsRoot);
31 | this.expectedNumberOfInstances = expectedNumberOfInstances;
32 | }
33 |
34 | public int getExpectedNumberOfInstances() {
35 | return expectedNumberOfInstances;
36 | }
37 |
38 | public void setExpectedNumberOfInstances(int expectedNumberOfInstances) {
39 | this.expectedNumberOfInstances = expectedNumberOfInstances;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/java/uk/co/szmg/grafana/example/dashboards/complex/XYComponentFactory.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.example.dashboards.complex;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-example
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.dashboard.BasicFactories;
24 | import uk.co.szmg.grafana.dashboard.ColorTheme;
25 | import uk.co.szmg.grafana.domain.Graph;
26 | import uk.co.szmg.grafana.domain.SingleStat;
27 |
28 | public class XYComponentFactory extends BasicFactories {
29 |
30 | /**
31 | * Constructor.
32 | *
33 | * @param environment environment that this factory should work for
34 | * @param colorTheme color theme
35 | */
36 | public XYComponentFactory(SampleAppEnvironment environment, ColorTheme colorTheme) {
37 | super(environment, colorTheme);
38 | }
39 |
40 | public Graph responseTime(Resource resource) {
41 | return simpleGraph(
42 | "maxSeries({ROOT}.resources." + resource.getMetricsPath() + ".max)",
43 | "maxSeries({ROOT}.resources." + resource.getMetricsPath() + ".p99)",
44 | "maxSeries({ROOT}.resources." + resource.getMetricsPath() + ".p95)")
45 | .withTitle(resource.getName() + " response time")
46 | .withSpan(6);
47 | }
48 |
49 | public SingleStat appInstances() {
50 | int okAbove = getEnvironment().getExpectedNumberOfInstances();
51 | int warningAbove = okAbove - 1;
52 |
53 | return counter("{ROOT}.noOfInstancesAlive", true)
54 | .withThresholds(warningAbove + "," + okAbove)
55 | .withPostfix("/" + getEnvironment().getExpectedNumberOfInstances())
56 | .withTitle("Instances");
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator-example/src/main/resources/endpoints.yaml:
--------------------------------------------------------------------------------
1 | # This file contains different Grafana endpoints.
2 | # It's parsed to Map.
3 |
4 |
5 | # This is my local test Grafana server! Hurray!
6 | test:
7 | baseUrl: http://localhost:8080
8 | apiKey: some-api-key
9 |
10 |
11 | # This one's for production... need to find a solution for auth
12 | production:
13 | baseUrl: https://grafana.invalidcert.com
14 | apiKey: some-other-api-key
15 | # sessionCookie:
16 | skipSSLValidation: yes
17 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | grafana-dashboard-generator
11 |
12 |
13 |
14 | uk.co.szmg.grafana
15 | domain
16 |
17 |
18 | com.fasterxml.jackson.core
19 | jackson-databind
20 |
21 |
22 |
23 |
24 | com.fasterxml.jackson.dataformat
25 | jackson-dataformat-yaml
26 | true
27 |
28 |
29 |
30 |
31 | org.springframework
32 | spring-context
33 | true
34 |
35 |
36 |
37 | junit
38 | junit
39 | test
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/DashboardFactory.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Dashboard;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * Implementing classes can generate dashboards.
29 | */
30 | public interface DashboardFactory {
31 |
32 | /**
33 | * Creates dashboards.
34 | * @return a list of dashboards created, should not be {@code null}
35 | */
36 | List create();
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/DashboardSerializer.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * dashboard-builder-main
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.fasterxml.jackson.core.JsonProcessingException;
24 | import com.fasterxml.jackson.databind.ObjectMapper;
25 | import com.fasterxml.jackson.databind.SerializationFeature;
26 | import uk.co.szmg.grafana.domain.Dashboard;
27 |
28 | import java.io.IOException;
29 | import java.io.OutputStream;
30 | import java.io.Writer;
31 |
32 | /**
33 | * Turns {@link Dashboard}s into their JSON representation.
34 | *
35 | * It does no validation.
36 | */
37 | public class DashboardSerializer {
38 |
39 | private final ObjectMapper mapper;
40 |
41 | /**
42 | * Default constructor, which initialises Jackson ObjectMapper with pretty print option.
43 | */
44 | public DashboardSerializer() {
45 | mapper = new ObjectMapper();
46 | mapper.enable(SerializationFeature.INDENT_OUTPUT);
47 | }
48 |
49 | /**
50 | * Gets the String representation of the given dashboard.
51 | * The output is pretty printed.
52 | * @param dashboard dashboard to turn into String
53 | * @return String (JSON) representation of dashboard
54 | * @throws IllegalArgumentException when the dashboard cannot be serialised
55 | */
56 | public String toString(Dashboard dashboard) {
57 | try {
58 | return mapper.writeValueAsString(dashboard);
59 | } catch (JsonProcessingException e) {
60 | throw new IllegalArgumentException("dashboard cannot be serialized to JSON." +
61 | "It might contain non-serializable values.", e);
62 | }
63 | }
64 |
65 | /**
66 | * Writes the pretty printed JSON representation of a dashboard with the given {@link Writer}.
67 | * @param dashboard dashboard to be serialised
68 | * @param writer writer to be written with
69 | * @throws IOException when "writer" throws IOException
70 | * @throws IllegalArgumentException when the dashboard cannot be serialised
71 | */
72 | public void write(Dashboard dashboard, Writer writer) throws IOException {
73 | try {
74 | mapper.writeValue(writer, dashboard);
75 | } catch (JsonProcessingException e) {
76 | throw new IllegalArgumentException("dashboard cannot be serialized to JSON." +
77 | "It might contain non-serializable values.", e);
78 | }
79 | }
80 |
81 | /**
82 | * Writes the pretty printed JSON representation of a dashboard on the given stream.
83 | * @param dashboard dashboard to be serialised
84 | * @param stream stream to be written on
85 | * @throws IOException when "stream" throws IOException
86 | * @throws IllegalArgumentException when the dashboard cannot be serialised
87 | */
88 | public void write(Dashboard dashboard, OutputStream stream) throws IOException {
89 | try {
90 | mapper.writeValue(stream, dashboard);
91 | } catch (JsonProcessingException e) {
92 | throw new IllegalArgumentException("dashboard cannot be serialized to JSON." +
93 | "It might contain non-serializable values.", e);
94 | }
95 | }
96 |
97 | }
98 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/DashboardUploader.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * dashboard-builder-main
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Dashboard;
24 |
25 | import java.io.IOException;
26 |
27 | /**
28 | * Serialises and uploads a dashboard to a Grafana endpoint.
29 | *
30 | * This class uses {@link DashboardSerializer#toString(Dashboard)} and
31 | * {@link GrafanaClient#uploadDashboard(String, boolean)} method.
32 | */
33 | public class DashboardUploader {
34 |
35 | private GrafanaClient client;
36 | private DashboardSerializer dashboardSerializer;
37 |
38 | /**
39 | * Constructor that sets the Grafana endpoint. It also creates the client,
40 | * but no connection is opened at this point.
41 | *
42 | * @param endpoint Grafana endpoint to be used
43 | */
44 | public DashboardUploader(GrafanaEndpoint endpoint) {
45 | client = new GrafanaClient(endpoint);
46 | dashboardSerializer = new DashboardSerializer();
47 | }
48 |
49 | /**
50 | * Serializes and uploads a dashboard.
51 | *
52 | * Throws exceptions if anything is wrong.
53 | *
54 | * @param dashboard dashboard to upload
55 | * @param overwrite should anything be overwritten?
56 | */
57 | public void upload(Dashboard dashboard, boolean overwrite) {
58 | if (dashboard.getTitle() == null) {
59 | throw new IllegalArgumentException("Dashboard must have a title.");
60 | }
61 |
62 | String dashboardJson = dashboardSerializer.toString(dashboard);
63 | try {
64 | client.uploadDashboard(dashboardJson, overwrite);
65 | } catch (IOException e) {
66 | throw new RuntimeException(String.format("Error happened during uploading [%s]", dashboard.getTitle()), e);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/GrafanaClient.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * dashboard-builder-main
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import javax.net.ssl.HostnameVerifier;
24 | import javax.net.ssl.HttpsURLConnection;
25 | import javax.net.ssl.SSLContext;
26 | import javax.net.ssl.SSLSession;
27 | import javax.net.ssl.TrustManager;
28 | import javax.net.ssl.X509TrustManager;
29 | import java.io.BufferedReader;
30 | import java.io.IOException;
31 | import java.io.InputStreamReader;
32 | import java.io.OutputStream;
33 | import java.net.HttpURLConnection;
34 | import java.net.URL;
35 | import java.security.KeyManagementException;
36 | import java.security.NoSuchAlgorithmException;
37 | import java.security.cert.X509Certificate;
38 |
39 | /**
40 | * Simple and stupid Grafana client. Without any 3rd party dependencies (but java).
41 | *
42 | * It supports authentication with API key or with session cookie. If none of them
43 | * is set in {@link GrafanaEndpoint} then no authentication will happen.
44 | *
45 | * It also support turning off SSL certificate validation for the connection (so not
46 | * globally).
47 | */
48 | public class GrafanaClient {
49 |
50 | private static final String USER_AGENT = "NaiveGrafanaClient";
51 | private static final String API_DASHBOARDS_PATH = "api/dashboards/db";
52 | private GrafanaEndpoint endpoint;
53 |
54 | /**
55 | * Constructor that sets the Grafana endpoint.
56 | *
57 | * @param endpoint Grafana endpoint to be used
58 | */
59 | public GrafanaClient(GrafanaEndpoint endpoint) {
60 | this.endpoint = endpoint;
61 | }
62 |
63 | /**
64 | * Uploads a dashboard to Grafana.
65 | *
66 | * If a dashboard with the same title exists, it will either be overwritten,
67 | * when {@code overwrite} parameter is {@code true}, or an UnexpectedGrafanaResponseException
68 | * is thrown.
69 | *
70 | * It also handles authentication based on the endpoint set by the constructor.
71 | *
72 | * It's thread-safe.
73 | *
74 | * @param dashboard JSON representation of the dashboard
75 | * @param overwrite should an existing dashboard with the same name be overwritten?
76 | * @throws IOException when some error happens, e.g., {@link java.net.MalformedURLException}
77 | * @throws UnexpectedGrafanaResponseException when response code is not in the 2xx zone
78 | */
79 | public void uploadDashboard(String dashboard, boolean overwrite) throws IOException {
80 | String url = getUrl(API_DASHBOARDS_PATH);
81 |
82 | // poor man's json builder
83 | String content = "{\"overwrite\": " + overwrite + ", \"dashboard\": " + dashboard + "}";
84 | postToGrafana(url, content);
85 | }
86 |
87 | /**
88 | * Do a Grafana tailored POST with the given content and returns response code.
89 | * Throws UnexpectedGrafanaResponseException if response code is not in the 2xx
90 | * domain.
91 | *
92 | * @param url full URL, with protocol and stuff
93 | * @param content POST payload to send
94 | * @return response code if it is 2xx
95 | * @throws IOException when some error happens, e.g., {@link java.net.MalformedURLException}
96 | * @throws UnexpectedGrafanaResponseException when response code is not in the 2xx zone
97 | */
98 | protected int postToGrafana(String url, String content) throws IOException {
99 | HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
100 | if (endpoint.isSkipSSLValidation() && conn instanceof HttpsURLConnection) {
101 | disableSslCertValidation((HttpsURLConnection) conn);
102 | }
103 |
104 | conn.setRequestMethod("POST");
105 | conn.setRequestProperty("User-Agent", USER_AGENT);
106 | conn.setRequestProperty("Accept", "application/json");
107 | conn.setRequestProperty("Content-Type", "application/json");
108 | addAuth(conn);
109 | conn.setDoOutput(true);
110 | try (OutputStream stream = conn.getOutputStream()) {
111 | stream.write(content.getBytes());
112 | stream.flush();
113 | }
114 |
115 | int responseCode = conn.getResponseCode();
116 |
117 | if (responseCode / 100 == 2) {
118 | return responseCode;
119 | } else {
120 | String response;
121 | try {
122 | response = readResponse(conn);
123 | } catch (IOException ex) {
124 | response = "Cannot read response; " + ex.getMessage();
125 | }
126 | throw new UnexpectedGrafanaResponseException(
127 | String.format("Unexpected response from [%s]; responseCode: [%d]; response: [%s]", url, responseCode, response),
128 | responseCode,
129 | response
130 | );
131 | }
132 | }
133 |
134 | /**
135 | * Adds authentication headers to request if needed.
136 | * @param conn connection
137 | */
138 | protected void addAuth(HttpURLConnection conn) {
139 | if (endpoint.getApiKey() != null) {
140 | conn.setRequestProperty("Authorization", "Bearer " + endpoint.getApiKey());
141 | } else if (endpoint.getSessionCookie() != null) {
142 | conn.setRequestProperty("Cookie", "grafana_sess=" + endpoint.getSessionCookie());
143 | }
144 | }
145 |
146 | /**
147 | * Gets URL of the given path, relative to the Grafana endpoint.
148 | * @param path path, relative to the Grafana endpoint
149 | * @return absolute URL (as String) of the given path
150 | */
151 | protected String getUrl(String path) {
152 | String url = endpoint.getBaseUrl();
153 | if (!url.endsWith("/")) {
154 | url += "/";
155 | }
156 | url += path;
157 | return url;
158 | }
159 |
160 | private void disableSslCertValidation(HttpsURLConnection conn) {
161 | conn.setSSLSocketFactory(SslCertValidationSkipper.noCheckSslContext.getSocketFactory());
162 | conn.setHostnameVerifier(SslCertValidationSkipper.allValidHostname);
163 | }
164 |
165 | private String readResponse(HttpURLConnection conn) throws IOException {
166 | StringBuffer response = new StringBuffer();
167 | try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
168 | String inputLine;
169 | while ((inputLine = in.readLine()) != null) {
170 | response.append(inputLine);
171 | }
172 | }
173 | return response.toString();
174 | }
175 |
176 | /**
177 | * Exception that is thrown when the status code of a Grafana request is outside of the
178 | * 200-299 range.
179 | */
180 | public static class UnexpectedGrafanaResponseException extends IllegalStateException {
181 |
182 | private int responseCode;
183 | private String response;
184 |
185 | /**
186 | * Constuctor that sets all required fields.
187 | * @param msg the message
188 | * @param responseCode the HTTP status code of the Grafana response
189 | * @param response the HTTP status line of the Grafana response
190 | */
191 | public UnexpectedGrafanaResponseException(String msg, int responseCode, String response) {
192 | super(msg);
193 | this.responseCode = responseCode;
194 | this.response = response;
195 | }
196 |
197 | /**
198 | * Gets HTTP status code of the triggering response.
199 | * @return HTTP status code
200 | */
201 | public int getResponseCode() {
202 | return responseCode;
203 | }
204 |
205 | /**
206 | * Gets HTTP status line of the triggering response.
207 | * @return HTTP status
208 | */
209 | public String getResponse() {
210 | return response;
211 | }
212 | }
213 |
214 | private static class SslCertValidationSkipper {
215 | private static SSLContext noCheckSslContext;
216 | private static HostnameVerifier allValidHostname;
217 |
218 | static {
219 | // Create a trust manager that does not validate certificate chains
220 | TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
221 | public java.security.cert.X509Certificate[] getAcceptedIssuers() {
222 | return null;
223 | }
224 | public void checkClientTrusted(X509Certificate[] certs, String authType) {
225 | }
226 | public void checkServerTrusted(X509Certificate[] certs, String authType) {
227 | }
228 | }
229 | };
230 |
231 | // Install the all-trusting trust manager
232 | try {
233 | noCheckSslContext = SSLContext.getInstance("SSL");
234 | noCheckSslContext.init(null, trustAllCerts, new java.security.SecureRandom());
235 | } catch (NoSuchAlgorithmException e) {
236 | throw new RuntimeException("Cannot skip SSL cert validation", e);
237 | } catch (KeyManagementException e) {
238 | throw new RuntimeException("Cannot skip SSL cert validation", e);
239 | }
240 |
241 | // Create all-trusting host name verifier
242 | allValidHostname = new HostnameVerifier() {
243 | public boolean verify(String hostname, SSLSession session) {
244 | return true;
245 | }
246 | };
247 |
248 | }
249 | }
250 | }
251 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/GrafanaEndpoint.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * dashboard-builder-main
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | /**
24 | * Grafana endpoint bean with auth data.
25 | *
26 | * To use API key authentication, see {@link #setApiKey(String)}.
27 | *
28 | * To use session cookie authentication, see {@link #setSessionCookie(String)}.
29 | *
30 | * If both are set, API key authentication will be used.
31 | *
32 | * If neither apiKey nor sessionCookie is set, no authentication will happen.
33 | *
34 | * To turn off SSL certificate validation for the current connection, see
35 | * {@link #setSkipSSLValidation(boolean)}.
36 | *
37 | * The only required field is {@code baseUrl}.
38 | */
39 | public class GrafanaEndpoint {
40 |
41 | private String baseUrl;
42 | private String apiKey;
43 | private String sessionCookie;
44 | private boolean skipSSLValidation;
45 |
46 | /**
47 | * Gets Grafana base URL.
48 | * @return base URL
49 | */
50 | public String getBaseUrl() {
51 | return baseUrl;
52 | }
53 |
54 | /**
55 | * Sets Grafana base URL.
56 | * @param baseUrl Grafana base URL
57 | */
58 | public void setBaseUrl(String baseUrl) {
59 | this.baseUrl = baseUrl;
60 | }
61 |
62 | /**
63 | * Gets API key.
64 | * @return API key
65 | */
66 | public String getApiKey() {
67 | return apiKey;
68 | }
69 |
70 | /**
71 | * True if SSL cert check should be forced to pass.
72 | * @return value of skipSSLValidation
73 | */
74 | public boolean isSkipSSLValidation() {
75 | return skipSSLValidation;
76 | }
77 |
78 | /**
79 | * Sets API gey.
80 | *
81 | * http://docs.grafana.org/http_api/auth/
82 | *
83 | * You need either this or the sessionCookie for authentication.
84 | *
85 | * @param apiKey API key
86 | */
87 | public void setApiKey(String apiKey) {
88 | this.apiKey = apiKey;
89 | }
90 |
91 | /**
92 | * Gets session cookie.
93 | * @return session cookie
94 | */
95 | public String getSessionCookie() {
96 | return sessionCookie;
97 | }
98 |
99 | /**
100 | * Sets session cookie.
101 | *
102 | * To get the value log in and copy the value of the "grafana_sess" cookie.
103 | *
104 | * You need either this or the apiKey for authentication.
105 | *
106 | * @param sessionCookie session cookie
107 | */
108 | public void setSessionCookie(String sessionCookie) {
109 | this.sessionCookie = sessionCookie;
110 | }
111 |
112 | /**
113 | * Set to true if you don't want SSL cert validation.
114 | * @param skipSSLCertValidation true if SSL cert validation should be skipped
115 | */
116 | public void setSkipSSLValidation(boolean skipSSLCertValidation) {
117 | this.skipSSLValidation = skipSSLCertValidation;
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/dashboard/BasicFactories.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.dashboard;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Graph;
24 | import uk.co.szmg.grafana.domain.SingleStat;
25 | import uk.co.szmg.grafana.domain.Target;
26 |
27 | import static uk.co.szmg.grafana.domain.DomainFactories.newGraph;
28 | import static uk.co.szmg.grafana.domain.DomainFactories.newSingleStat;
29 | import static uk.co.szmg.grafana.domain.DomainFactories.newTarget;
30 |
31 | /**
32 | * An extensible dashboard component factory.
33 | *
34 | *
This provides basic builders for singlestat and graphs that set the
35 | * datasource and changes {@code {ROOT}} in the targets to {@link Environment#getMetricsRoot()}.
36 |
37 | *
It is intended to be extended, although it can be used on its own as well.
38 | * When extended, putting most of your business related generators in the child class(es)
39 | * is recommended, so the dashboard configuration can look much cleaner.
40 | *
41 | *
The main diferrence from the {@link StaticFactories} class is that here we have a context. Actually,
42 | * two context objects: a {@link ColorTheme} and an {@link Environment} implementation.
43 | * The latter is likely to be overridden, hence the type parameter.
44 | *
45 | *
See an example of this in the examples project.
46 | *
47 | * @since 1.1
48 | * @see Environment
49 | * @see SimpleEnvironment
50 | * @see ColorTheme
51 | * @param the environment type, e.g., {@link SimpleEnvironment}
52 | */
53 | public class BasicFactories {
54 |
55 | private ENV environment;
56 | private ColorTheme colorTheme;
57 |
58 | /**
59 | * Constructor.
60 | * @param environment environment that this factory should work for
61 | * @param colorTheme color theme
62 | */
63 | public BasicFactories(ENV environment, ColorTheme colorTheme) {
64 | this.environment = environment;
65 | this.colorTheme = colorTheme;
66 | }
67 |
68 | /**
69 | * A singlestat panel with span of 2.
70 | *
71 | *
Datasource and {@code {ROOT}} are set/evaluated with the environment object.
72 | * @param target the Graphite(?) query
73 | * @return new singlestat
74 | */
75 | public SingleStat neutralCounter(String target) {
76 | return newSingleStat()
77 | .withSpan(2)
78 | .addTarget(resolveTarget(target))
79 | .withDatasource(getEnvironment().getDatasource());
80 | }
81 |
82 | /**
83 | * A {@link #neutralCounter(String)} that is colored. Higher values are better.
84 | * Thresholds are still need to be set!
85 | *
86 | *
Datasource and {@code {ROOT}} are set/evaluated with the environment object.
87 | *
88 | * @param target target query
89 | * @param important if {@code true}, the background is colored, otherwise the text
90 | * @return new singlestat
91 | */
92 | public SingleStat counter(String target, boolean important) {
93 | return neutralCounter(target)
94 | .withColors(getColorTheme().colorsWhenHighIsHealthy())
95 | .withColorBackground(important)
96 | .withColorValue(!important);
97 | }
98 |
99 | /**
100 | * A {@link #neutralCounter(String)} that is colored. Lower values are better.
101 | * Thresholds are still need to be set!
102 | *
103 | *
Datasource and {@code {ROOT}} are set/evaluated with the environment object.
104 | *
105 | * @param target target query
106 | * @param important if {@code true}, the background is colored, otherwise the text
107 | * @return new singlestat
108 | */
109 | public SingleStat errorCounter(String target, boolean important) {
110 | return counter(target, important)
111 | .withColors(getColorTheme().colorsWhenLowIsHealthy());
112 | }
113 |
114 | /**
115 | * Creates a new graph with the given targets. The targets are added in
116 | * order and are referenced with uppercase letters ('A', 'B', ...)
117 | * Expect surprise when adding more than 26 targets.
118 | *
119 | *
Datasource and {@code {ROOT}} are set/evaluated with the environment object.
120 | *
121 | * @param targets targets
122 | * @return new graph with targets
123 | */
124 | public Graph simpleGraph(String... targets) {
125 | Graph graph = newGraph().withDatasource(getEnvironment().getDatasource());
126 | char ref = 'A';
127 | for (String target : targets) {
128 | graph.addTarget(resolveTarget(target).withRefId(String.valueOf(ref)));
129 | ref++;
130 | }
131 | return graph;
132 | }
133 |
134 | /**
135 | * Creates a Target object from the given query string. Also replaces
136 | * {@code {ROOT}} with {@link Environment#getMetricsRoot()}.
137 | *
138 | * @param target
139 | * @return
140 | */
141 | protected Target resolveTarget(String target) {
142 | return newTarget()
143 | .withTarget(target.replaceAll("\\{ROOT}", getEnvironment().getMetricsRoot()));
144 | }
145 |
146 | /**
147 | * Gets the current environment.
148 | * @return environment
149 | */
150 | public ENV getEnvironment() {
151 | return environment;
152 | }
153 |
154 | /**
155 | * Gets the current color theme.
156 | * @return color theme
157 | */
158 | public ColorTheme getColorTheme() {
159 | return colorTheme;
160 | }
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/dashboard/ColorTheme.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.dashboard;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Colors;
24 |
25 | /**
26 | * Most dashboards have a common color palette. This class
27 | * is to store one of that. Extend it in case you'd need
28 | * more colors in your factories.
29 | *
30 | * There are 4 types of color:
31 | *
32 | *
neutral
33 | *
color for texts that are neither good nor bad; e.g., white
34 | *
healthy
35 | *
color to mark healthy things, e.g., green
36 | *
warning
37 | *
color used for the middle part of the thresholds, e.g., yellow
38 | *
error
39 | *
color that marks an error, e.g., red
40 | *
41 | *
42 | * Colors should be defined as html/css color strings.
43 | *
44 | * @see BasicFactories
45 | * @since 1.1
46 | */
47 | public class ColorTheme {
48 |
49 | /**
50 | * A simple, red-yellow-green and white theme.
51 | */
52 | public static final ColorTheme RED_YELLOW_GREEN = new ColorTheme(
53 | "white",
54 | "rgba(50, 172, 45, 0.97)",
55 | "rgba(237, 129, 40, 0.89)",
56 | "rgba(245, 54, 54, 0.9)");
57 |
58 | private String neutral;
59 | private String healthy;
60 | private String warning;
61 | private String error;
62 |
63 | /**
64 | * Constructor.
65 | *
66 | * @param neutral neutral color for texts that are neither good nor bad; e.g., white
67 | * @param healthy color to mark healthy things, e.g., green
68 | * @param warning color used for the middle part of the thresholds, e.g., yellow
69 | * @param error color that marks an error, e.g., red
70 | */
71 | public ColorTheme(String neutral, String healthy, String warning, String error) {
72 | this.neutral = neutral;
73 | this.healthy = healthy;
74 | this.warning = warning;
75 | this.error = error;
76 | }
77 |
78 | /**
79 | * Gets neutral color.
80 | * @return neutral color
81 | */
82 | public String getNeutral() {
83 | return neutral;
84 | }
85 |
86 | /**
87 | * Gets healthy color.
88 | * @return healthy color
89 | */
90 | public String getHealthy() {
91 | return healthy;
92 | }
93 |
94 | /**
95 | * Gets warning color.
96 | * @return warning color
97 | */
98 | public String getWarning() {
99 | return warning;
100 | }
101 |
102 | /**
103 | * Gets error color.
104 | * @return error color
105 | */
106 | public String getError() {
107 | return error;
108 | }
109 |
110 | /**
111 | * Gets a color configuration used in singlestat for when low value is good,
112 | * e.g., number of errors.
113 | * @return color configuration for singlestat
114 | */
115 | public Colors colorsWhenLowIsHealthy() {
116 | return new Colors(healthy, warning, error);
117 | }
118 |
119 | /**
120 | * Gets a color configuration used in singlestat for when high value is good,
121 | * e.g., number of instances alive.
122 | * @return color configuration for singlestat
123 | */
124 | public Colors colorsWhenHighIsHealthy() {
125 | return new Colors(error, warning, healthy);
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/dashboard/Environment.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.dashboard;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | /**
24 | * An abstraction of the environment that the dashboard
25 | * is going to monitor.
26 | *
27 | * For example staging and production might have different dashboard instances,
28 | * but they should use the same "template".
29 | *
30 | * This interface is used by {@link BasicFactories} and its
31 | * descendants.
32 | *
33 | * It has a basic, Java Bean implementation in {@link SimpleEnvironment}.
34 | *
35 | * @see BasicFactories
36 | * @see SimpleEnvironment
37 | * @since 1.1
38 | */
39 | public interface Environment {
40 |
41 | /**
42 | * Gets the name of the environment, e.g., production, staging.
43 | * @return name of the environment
44 | */
45 | String getName();
46 |
47 | /**
48 | * Gets the default data source.
49 | * @return default data source
50 | */
51 | String getDatasource();
52 |
53 | /**
54 | * Gets the metrics root that {@code {ROOT}} will be replaced with in target queries.
55 | *
56 | *
Use this when your metrics have different prefixes for each environment, e.g.
57 | * {@code SUPERAPP.prod.instance1.someResource.GET.count}.
58 | *
59 | * @return metrics root
60 | */
61 | String getMetricsRoot();
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/dashboard/SimpleEnvironment.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.dashboard;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | /**
24 | * Simple Java Bean implementation of {@link Environment}.
25 | *
26 | * One can use this for simple projects or either extend this
27 | * or implement.
28 | *
29 | * @see Environment
30 | * @since 1.1
31 | */
32 | public class SimpleEnvironment implements Environment {
33 |
34 | private String name;
35 | private String datasource;
36 | private String metricsRoot;
37 |
38 | /**
39 | * Default constructor.
40 | */
41 | public SimpleEnvironment() {
42 | }
43 |
44 | /**
45 | * Convenience constructor.
46 | *
47 | * @param name name of the environment, e.g., production
48 | * @param datasource default datasource for that environment
49 | * @param metricsRoot default metrics root; {@code {ROOT}} in targets will be substituted to this value
50 | */
51 | public SimpleEnvironment(String name, String datasource, String metricsRoot) {
52 | this.name = name;
53 | this.datasource = datasource;
54 | this.metricsRoot = metricsRoot;
55 | }
56 |
57 | @Override
58 | public String getName() {
59 | return name;
60 | }
61 |
62 | /**
63 | * Sets name.
64 | *
65 | * @param name name of the environment, e.g., production
66 | */
67 | public void setName(String name) {
68 | this.name = name;
69 | }
70 |
71 | @Override
72 | public String getDatasource() {
73 | return datasource;
74 | }
75 |
76 | /**
77 | * Sets default datasource.
78 | *
79 | * @param datasource default datasource for that environment
80 | */
81 | public void setDatasource(String datasource) {
82 | this.datasource = datasource;
83 | }
84 |
85 | @Override
86 | public String getMetricsRoot() {
87 | return metricsRoot;
88 | }
89 |
90 | /**
91 | * Sets metrics root, that {@code {ROOT}} will be substituted with in targets.
92 | *
93 | * @param metricsRoot default metrics root
94 | */
95 | public void setMetricsRoot(String metricsRoot) {
96 | this.metricsRoot = metricsRoot;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/dashboard/StaticFactories.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.dashboard;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Row;
24 | import uk.co.szmg.grafana.domain.Text;
25 |
26 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
27 | import static uk.co.szmg.grafana.domain.DomainFactories.newText;
28 |
29 | /**
30 | * Simple, stupid static generator functions.
31 | *
32 | * See {@link BasicFactories} for more complex generators.
33 | *
34 | * @see BasicFactories
35 | * @since 1.1
36 | */
37 | public class StaticFactories {
38 |
39 | /**
40 | * Creates a row with height of 95px. It is usually good
41 | * for single number metrics (singlestat) and texts.
42 | *
43 | * @return new row that has a height of 95px
44 | */
45 | public static Row thinRow() {
46 | return newRow().withHeight("95px");
47 | }
48 |
49 | /**
50 | * Creates a nice title text panel, just like the one on the default home dashboard.
51 | * It does not set the width (span).
52 | *
53 | * @param title the title in HTML
54 | * @return new text panel
55 | */
56 | public static Text title(String title) {
57 | return newText()
58 | .withContent("
" + title + "
")
59 | .withMode("html")
60 | .withTransparent(true);
61 | }
62 |
63 | /**
64 | * Creates a totally empty and transparent text panel with the given width.
65 | *
66 | * @param span width
67 | * @return new text panel
68 | */
69 | public static Text placeholder(int span) {
70 | return newText()
71 | .withContent("")
72 | .withTransparent(true)
73 | .withSpan(span);
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/ClasspathGrafanaEndpointStore.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import java.io.InputStream;
24 |
25 | /**
26 | * Endpoint store that reads from the classpath.
27 | */
28 | public class ClasspathGrafanaEndpointStore extends GrafanaEndpointStore {
29 |
30 | private String path;
31 |
32 | /**
33 | * Creates a ClasspathGrafanaEndpointStore.
34 | * @param path path of the endpoint yaml file on the classpath of this class
35 | */
36 | public ClasspathGrafanaEndpointStore(String path) {
37 | this.path = path;
38 | }
39 |
40 | @Override
41 | protected InputStream getStream() {
42 | return getClass().getResourceAsStream(path);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/DashboardFilter.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.domain.Dashboard;
24 |
25 | import java.util.ArrayList;
26 | import java.util.Iterator;
27 | import java.util.LinkedHashSet;
28 | import java.util.List;
29 | import java.util.Map;
30 | import java.util.Set;
31 | import java.util.regex.Pattern;
32 |
33 | import static java.util.function.Function.identity;
34 | import static java.util.stream.Collectors.toMap;
35 |
36 | /**
37 | * Filters dashboards based on their titles.
38 | *
39 | *
It's a three step filter, starting from an empty set:
40 | *
41 | *
adds every dashboard with title matching any of {@code includeFilters}
42 | *
removes every dashboard with title matching any of {@code excludeFilters};
43 | * if {@code includeFilters} is empty, it adds all dashboards first
44 | *
adds all dashboards specified in {@code forceInclude}
45 | *
46 | *
47 | *
Note that {@code includeFilters} and {@code excludeFilters} looks for
48 | * partial match and are regular expressions, while {@code forceInclude}
49 | * uses String.equals to decide if a dashboard title matches.
50 | */
51 | public class DashboardFilter {
52 |
53 | private List forceInclude;
54 | private List includeFilters;
55 | private List excludeFilters;
56 |
57 | /**
58 | * Creates a DashboardFilter.
59 | *
60 | * @param forceInclude list of dashboard titles to include, no matter what
61 | * @param includeFilters list of regexps to include dashboards
62 | * @param excludeFilters list of regexps to exclude dashboards
63 | */
64 | public DashboardFilter(List forceInclude, List includeFilters, List excludeFilters) {
65 | this.forceInclude = forceInclude;
66 | this.includeFilters = includeFilters;
67 | this.excludeFilters = excludeFilters;
68 | }
69 |
70 | /**
71 | * Do the filtering. For the filter method, see {@link DashboardFilter}.
72 | * @param dashboards dashboards to filter
73 | * @return set of dashboards that mathced the filter
74 | * @throws IncludedDashboardIsNotFound when a dashboard in {@code forceInclude} is not found
75 | */
76 | public List filter(List dashboards) {
77 | Set result = new LinkedHashSet<>();
78 |
79 | for (String includeFilter : includeFilters) {
80 | add(result, includeFilter, dashboards);
81 | }
82 |
83 | if (includeFilters.isEmpty() && !excludeFilters.isEmpty()) {
84 | result.addAll(dashboards);
85 | }
86 |
87 | for (String excludeFilter : excludeFilters) {
88 | remove(result, excludeFilter);
89 | }
90 |
91 | if (!forceInclude.isEmpty()) {
92 | Map dashboardIndex = dashboards.stream()
93 | .collect(toMap(Dashboard::getTitle, identity()));
94 | for (String s : forceInclude) {
95 | Dashboard dashboard = dashboardIndex.get(s);
96 | if (dashboard == null) {
97 | throw new IncludedDashboardIsNotFound(s);
98 | }
99 | result.add(dashboard);
100 | }
101 | }
102 |
103 | return new ArrayList<>(result);
104 | }
105 |
106 | private void remove(Set result, String filter) {
107 | Pattern pattern = Pattern.compile(filter);
108 | Iterator it = result.iterator();
109 | while (it.hasNext()) {
110 | if (pattern.matcher(it.next().getTitle()).find()) {
111 | it.remove();
112 | }
113 | }
114 | }
115 |
116 | private void add(Set result, String filter, List dashboards) {
117 | Pattern pattern = Pattern.compile(filter);
118 | for (Dashboard dashboard : dashboards) {
119 | if (pattern.matcher(dashboard.getTitle()).find()) {
120 | result.add(dashboard);
121 | }
122 | }
123 | }
124 |
125 | /**
126 | * Exception showing that a dashboard that is explicitly included
127 | * with {@code forceInclude} is not among all dashboards.
128 | *
129 | * @see DashboardFilter
130 | */
131 | public static class IncludedDashboardIsNotFound extends IllegalArgumentException {
132 |
133 | private String dashboardTitle;
134 |
135 | /**
136 | * Creates an IncludedDashboardIsNotFound.
137 | * @param dashboardTitle title of the dashboard that could not be found
138 | */
139 | public IncludedDashboardIsNotFound(String dashboardTitle) {
140 | super("Included dashboard cannot be found: " + dashboardTitle);
141 | this.dashboardTitle = dashboardTitle;
142 | }
143 |
144 | /**
145 | * Gets dashboard title that could not be found.
146 | * @return dashboard title
147 | */
148 | public String getDashboardTitle() {
149 | return dashboardTitle;
150 | }
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/DashboardStore.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.springframework.context.ApplicationContext;
24 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25 | import uk.co.szmg.grafana.DashboardFactory;
26 | import uk.co.szmg.grafana.domain.Dashboard;
27 |
28 | import java.util.ArrayList;
29 | import java.util.Collection;
30 | import java.util.HashSet;
31 | import java.util.List;
32 | import java.util.Map;
33 | import java.util.Optional;
34 | import java.util.Set;
35 | import java.util.stream.Collectors;
36 |
37 | import static java.util.Collections.unmodifiableList;
38 |
39 | /**
40 | * Loads and provides dashboards.
41 | *
42 | *
First, an annotation based Spring context is built
43 | * in {@code rootPackage}. Then every {@link DashboardFactory}
44 | * beans are looked up and invoked to create dashboards.
45 | * Finally, all {@link Dashboard} beans are read.
46 | *
47 | *
This class can also return the duplicated dashboards.
48 | * Two dashboards are duplicated if their titles are equal.
49 | */
50 | public class DashboardStore {
51 |
52 | private String rootPackage;
53 |
54 | private List dashboards;
55 |
56 | /**
57 | * Creates a DashboardStore.
58 | * @param rootPackage root package of the annotation based spring context to be searched
59 | */
60 | public DashboardStore(String rootPackage) {
61 | this.rootPackage = rootPackage;
62 | }
63 |
64 | /**
65 | * Loads dashboards.
66 | *
67 | *
This will create the Spring context and look for
68 | * {@link DashboardFactory} and {@link Dashboard} implementations.
69 | */
70 | public void load() {
71 | List dashboards = new ArrayList<>();
72 | ApplicationContext applicationContext = new AnnotationConfigApplicationContext(rootPackage);
73 | Map factories = applicationContext.getBeansOfType(DashboardFactory.class);
74 |
75 | for (DashboardFactory dashboardFactory : factories.values()) {
76 | dashboards.addAll(dashboardFactory.create());
77 | }
78 |
79 | Map dashboardBeans = applicationContext.getBeansOfType(Dashboard.class);
80 | dashboards.addAll(dashboardBeans.values());
81 |
82 | this.dashboards = unmodifiableList(dashboards);
83 | }
84 |
85 | /**
86 | * Gets a list of dashboard titles that presents
87 | * more than once among the dashboards.
88 | *
89 | *
Grafana treats them as equal, so there is no point
90 | * uploading them twice.
91 | *
92 | * @return list of dashboard titles
93 | */
94 | public Collection getDuplicates() {
95 | Set all = new HashSet<>();
96 | Set dupes = new HashSet<>();
97 |
98 | for (Dashboard dashboard : dashboards) {
99 | String title = dashboard.getTitle();
100 | if (all.contains(title)) {
101 | dupes.add(title);
102 | } else {
103 | all.add(title);
104 | }
105 | }
106 |
107 | return dupes;
108 | }
109 |
110 | /**
111 | * Gets an error message with the list of duplicated dashboards if any.
112 | * @return an error message if there are duplicates, otherwise {@code Optional.empty()}
113 | */
114 | public Optional getDuplicatesErrorMessage() {
115 | Collection duplicates = getDuplicates();
116 | String message = null;
117 | if (!duplicates.isEmpty()) {
118 | message = String.format("%d duplicates found among dashboards:", duplicates.size());
119 |
120 | message += duplicates.stream().collect(Collectors.joining(", "));
121 | }
122 | return Optional.ofNullable(message);
123 | }
124 |
125 | /**
126 | * Gets all dashboards.
127 | * @return list of dashboards
128 | */
129 | public List getDashboards() {
130 | return dashboards;
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/FileBasedGrafanaEndpointStore.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import java.io.File;
24 | import java.io.FileInputStream;
25 | import java.io.FileNotFoundException;
26 | import java.io.InputStream;
27 |
28 | /**
29 | * Endpoint store that reads from file.
30 | */
31 | public class FileBasedGrafanaEndpointStore extends GrafanaEndpointStore {
32 |
33 | private File source;
34 |
35 | /**
36 | * Creates a FileBasedGrafanaEndpointStore.
37 | * @param source path of the endpoint config yaml file
38 | */
39 | public FileBasedGrafanaEndpointStore(File source) {
40 | this.source = source;
41 | }
42 |
43 | @Override
44 | protected InputStream getStream() throws FileNotFoundException {
45 | return new FileInputStream(source);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/GrafanaEndpointParser.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import com.fasterxml.jackson.databind.ObjectMapper;
24 | import com.fasterxml.jackson.databind.type.MapType;
25 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
26 | import uk.co.szmg.grafana.GrafanaEndpoint;
27 |
28 | import java.io.IOException;
29 | import java.io.InputStream;
30 | import java.util.LinkedHashMap;
31 | import java.util.Map;
32 |
33 | /**
34 | * Example in test/resources/test-endpoints.yaml
35 | */
36 | public class GrafanaEndpointParser {
37 |
38 | private ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
39 | private MapType mapOfStringAndEndpointType = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, GrafanaEndpoint.class);
40 |
41 | /**
42 | * Gets a sorted map of endpoint IDs and endpoints.
43 | *
44 | * @param input stream that contains the endpoint config
45 | * @return sorted map of endpoint IDs and endpoints
46 | * @throws IOException when something wents wrong (e.g., wrong format)
47 | */
48 | public Map parse(InputStream input) throws IOException {
49 | return mapper.readerFor(mapOfStringAndEndpointType).readValue(input);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/main/java/uk/co/szmg/grafana/stores/GrafanaEndpointStore.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.stores;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-generator-cli
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import uk.co.szmg.grafana.GrafanaEndpoint;
24 |
25 | import java.io.IOException;
26 | import java.io.InputStream;
27 | import java.util.Collections;
28 | import java.util.Map;
29 |
30 | /**
31 | * Loads and provides endpoint data.
32 | */
33 | public abstract class GrafanaEndpointStore {
34 |
35 | private Map endpoints;
36 |
37 | /**
38 | * Loads endpoints.
39 | * @throws IOException when cannot read the underlying stream
40 | */
41 | public void load() throws IOException {
42 | try (InputStream stream = getStream()) {
43 | if (stream != null) {
44 | endpoints = new GrafanaEndpointParser().parse(stream);
45 | } else {
46 | endpoints = Collections.emptyMap();
47 | }
48 | }
49 | }
50 |
51 | /**
52 | * Gets endpoints by their names.
53 | * @return endpoints, indexed by their names
54 | */
55 | public Map getEndpoints() {
56 | return endpoints;
57 | }
58 |
59 | /**
60 | * Gets the stream to parse for endpoints.
61 | * @return an open InputStream, or {@code null} if there are no endpoints to read
62 | * @throws IOException when something goes wrong
63 | */
64 | protected abstract InputStream getStream() throws IOException;
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/grafana-dashboard-generator/src/test/java/uk/co/szmg/grafana/DashboardSerializerTest.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana;
2 |
3 | /*-
4 | * #%L
5 | * dashboard-builder-main
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.junit.Ignore;
24 | import org.junit.Test;
25 | import uk.co.szmg.grafana.domain.Dashboard;
26 | import uk.co.szmg.grafana.domain.Row;
27 | import uk.co.szmg.grafana.domain.Target;
28 |
29 | import java.io.IOException;
30 |
31 | import static uk.co.szmg.grafana.domain.DomainFactories.newDashboard;
32 | import static uk.co.szmg.grafana.domain.DomainFactories.newGraph;
33 | import static uk.co.szmg.grafana.domain.DomainFactories.newRow;
34 | import static uk.co.szmg.grafana.domain.DomainFactories.newSingleStat;
35 | import static uk.co.szmg.grafana.domain.DomainFactories.newTarget;
36 | import static uk.co.szmg.grafana.domain.DomainFactories.newText;
37 |
38 | public class DashboardSerializerTest {
39 |
40 | private DashboardSerializer serializer = new DashboardSerializer();
41 |
42 | /**
43 | * Of course it's not really a test... yet.
44 | */
45 | @Test
46 | @Ignore
47 | public void printTestDashboard() throws IOException {
48 | //System.out.println(serializer.toString(testDashboard()));
49 | serializer.write(testDashboard(), System.out);
50 | }
51 |
52 | @Test
53 | @Ignore
54 | public void upload() {
55 | GrafanaEndpoint endpoint = new GrafanaEndpoint();
56 | endpoint.setBaseUrl(System.getProperty("grafana.url"));
57 | endpoint.setApiKey(System.getProperty("grafana.apiKey"));
58 | // endpoint.setSessionCookie("");
59 | endpoint.setSkipSSLValidation(true);
60 | DashboardUploader uploader = new DashboardUploader(endpoint);
61 | uploader.upload(DashboardSerializerTest.testDashboard(), true);
62 |
63 | }
64 |
65 | public static Dashboard testDashboard() {
66 | Target target = newTarget()
67 | .withTarget("maxSeries(humidity.peti.test.sensors)");
68 |
69 | Row row1 = newRow()
70 | .withHeight("100px")
71 | .addPanel(newSingleStat()
72 | .withTitle("Single stat test")
73 | .addTarget(target)
74 | .withSpan(2))
75 | .addPanel(newText()
76 | .withContent("
This is the test
")
77 | .withMode("html")
78 | .withTransparent(true)
79 | .withSpan(8))
80 | .addPanel(newSingleStat()
81 | .withTitle("Single stat test")
82 | .addTarget(target)
83 | .withSpan(2));
84 |
85 | Row row2 = newRow()
86 | .addPanel(newGraph()
87 | .addTarget(target)
88 | .withSpan(12));
89 |
90 | return newDashboard()
91 | .withTitle("Test dashboard")
92 | .addRow(row1)
93 | .addRow(row2);
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | grafana-dashboard-generator-parent
5 | uk.co.szmg.grafana
6 | 2.1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | grafana-dashboard-maven-plugin
11 | maven-plugin
12 |
13 |
14 |
15 | uk.co.szmg.grafana
16 | grafana-dashboard-generator
17 |
18 |
19 | com.fasterxml.jackson.dataformat
20 | jackson-dataformat-yaml
21 |
22 |
23 | org.springframework
24 | spring-context
25 |
26 |
27 |
28 | org.apache.maven
29 | maven-plugin-api
30 |
31 |
32 | org.apache.maven
33 | maven-project
34 |
35 |
36 | org.apache.maven.plugin-tools
37 | maven-plugin-annotations
38 | provided
39 |
40 |
41 |
42 |
43 |
44 |
45 | org.apache.maven.plugins
46 | maven-plugin-plugin
47 | 3.3
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/src/main/java/uk/co/szmg/grafana/maven/AbstractMojoWithClasspath.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.maven;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.apache.maven.artifact.Artifact;
24 | import org.apache.maven.model.Resource;
25 | import org.apache.maven.plugin.AbstractMojo;
26 | import org.apache.maven.plugin.MojoFailureException;
27 | import org.apache.maven.plugins.annotations.Parameter;
28 | import org.apache.maven.project.MavenProject;
29 |
30 | import java.io.File;
31 | import java.net.MalformedURLException;
32 | import java.net.URL;
33 | import java.net.URLClassLoader;
34 | import java.util.ArrayList;
35 | import java.util.List;
36 |
37 | /**
38 | * Provides the {@link #getClassLoader()} method that creates a new classloader
39 | * with runtime dependencies, build output directory and resources on classpath.
40 | */
41 | public abstract class AbstractMojoWithClasspath extends AbstractMojo {
42 |
43 | @Parameter( defaultValue = "${project}", readonly = true )
44 | protected MavenProject project;
45 |
46 | /**
47 | * Creates and new class loader that has runtime dependencies, build output
48 | * and resources on class path.
49 | * @return new class loader
50 | * @throws MojoFailureException when one of the class path elements is misconfigured, should never happen
51 | */
52 | protected ClassLoader getClassLoader() throws MojoFailureException {
53 | List classpath = null;
54 | try {
55 | classpath = getClasspathUrls();
56 | } catch (MalformedURLException e) {
57 | throw new MojoFailureException("Cannot load classpath", e);
58 | }
59 |
60 | // org.springframework.beans.factory.annotation.Autowire is loaded by this classloader already.
61 | // If the parent classloader is null, which would be preferred, then it throws ClassCastException
62 | // as is cannot case the two different class instance between each other.
63 | return new URLClassLoader(classpath.toArray(new URL[classpath.size()]), getClass().getClassLoader());
64 | }
65 |
66 | private List getClasspathUrls() throws MalformedURLException {
67 | List classpath = new ArrayList<>();
68 |
69 | for (Resource resource : project.getBuild().getResources()) {
70 | classpath.add(new File(resource.getDirectory()).toURI().toURL());
71 | }
72 |
73 | classpath.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
74 |
75 | for (Object o : project.getRuntimeArtifacts()) {
76 | Artifact artifact = (Artifact) o;
77 | classpath.add(artifact.getFile().toURI().toURL());
78 | }
79 |
80 | getLog().debug("classpath: " + classpath);
81 |
82 | return classpath;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/src/main/java/uk/co/szmg/grafana/maven/AbstractMojoWithDashboards.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.maven;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.apache.maven.plugin.MojoFailureException;
24 | import org.apache.maven.plugins.annotations.Parameter;
25 | import uk.co.szmg.grafana.domain.Dashboard;
26 | import uk.co.szmg.grafana.stores.DashboardFilter;
27 | import uk.co.szmg.grafana.stores.DashboardStore;
28 |
29 | import java.util.Collections;
30 | import java.util.List;
31 | import java.util.Optional;
32 |
33 | /**
34 | * Provides dashboard loading and filtering support for a mojo.
35 | */
36 | public abstract class AbstractMojoWithDashboards extends AbstractMojoWithClasspath {
37 |
38 | /**
39 | * Root package for the Spring context in which {@link Dashboard}s and
40 | * {@link uk.co.szmg.grafana.DashboardFactory}s should be looked up.
41 | */
42 | @Parameter(required = true)
43 | protected String rootPackage;
44 |
45 | /**
46 | * Dashboard title regexp to be included.
47 | *
48 | *
If empty and {@code excludes} is not empty, it defaults
49 | * to all dashboards.
50 | */
51 | @Parameter
52 | protected List includes;
53 |
54 | /**
55 | * Dashboard title regexp to be excluded.
56 | *
57 | *
Filtering is done after the {@code includes} filter
58 | * has been applied. If that is empty, filtering happens
59 | * on all dashboards.
60 | */
61 | @Parameter
62 | protected List excludes;
63 |
64 | /**
65 | * Exact dashboard titles to explicitly include.
66 | *
67 | *
This will add any matching dashboards after the filtering.
68 | * This means that a dashboard can be excluded, but if it is added
69 | * to {@code forceInclude}, it will be taken into account.
70 | *
71 | *
Providing not existing dashboard title will result
72 | * in BUILD FAILURE.
73 | */
74 | @Parameter
75 | protected List forceInclude;
76 |
77 | /**
78 | * Loads, filters and gets dashboards.
79 | * @return list of dashboards
80 | * @throws MojoFailureException when dashboards cannot be found
81 | * @throws MojoFailureException when there are more dashboard with the same title
82 | */
83 | protected List loadDashboards() throws MojoFailureException {
84 | final DashboardStore dashboardStore = new DashboardStore(rootPackage);
85 | Thread thread = new Thread("dashboard-loader") {
86 | @Override
87 | public void run() {
88 | dashboardStore.load();
89 | }
90 | };
91 |
92 | thread.setContextClassLoader(getClassLoader());
93 | thread.start();
94 | try {
95 | thread.join();
96 | } catch (InterruptedException e) {
97 | throw new MojoFailureException("Cannot load dashboards", e);
98 | }
99 |
100 | // check for duplicates
101 | Optional duplicationException = dashboardStore
102 | .getDuplicatesErrorMessage()
103 | .map(MojoFailureException::new);
104 | if (duplicationException.isPresent()) {
105 | throw duplicationException.get();
106 | }
107 |
108 | // filter
109 | DashboardFilter dashboardFilter = new DashboardFilter(
110 | emptyIfNull(forceInclude),
111 | emptyIfNull(includes),
112 | emptyIfNull(excludes));
113 | getLog().debug("all dashboards: " + dashboardStore.getDashboards().size());
114 | List dashboards = dashboardFilter.filter(dashboardStore.getDashboards());
115 | getLog().info("filtered dashboards: " + dashboards.size());
116 | return dashboards;
117 | }
118 |
119 | private List emptyIfNull(List list) {
120 | return list == null ? Collections.emptyList() : list;
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/src/main/java/uk/co/szmg/grafana/maven/Generate.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.maven;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.apache.maven.plugin.MojoExecutionException;
24 | import org.apache.maven.plugin.MojoFailureException;
25 | import org.apache.maven.plugins.annotations.LifecyclePhase;
26 | import org.apache.maven.plugins.annotations.Mojo;
27 | import org.apache.maven.plugins.annotations.Parameter;
28 | import org.apache.maven.plugins.annotations.ResolutionScope;
29 | import uk.co.szmg.grafana.DashboardSerializer;
30 | import uk.co.szmg.grafana.domain.Dashboard;
31 | import uk.co.szmg.grafana.stores.DashboardFilter;
32 | import uk.co.szmg.grafana.stores.DashboardStore;
33 |
34 | import java.io.File;
35 | import java.io.FileNotFoundException;
36 | import java.io.FileOutputStream;
37 | import java.io.IOException;
38 | import java.io.OutputStream;
39 | import java.util.Collections;
40 | import java.util.List;
41 | import java.util.Optional;
42 |
43 | /**
44 | * Generates JSON resources to the output directory.
45 | */
46 | @Mojo(name = "generate", defaultPhase = LifecyclePhase.PREPARE_PACKAGE, requiresDependencyResolution = ResolutionScope.RUNTIME)
47 | public class Generate extends AbstractMojoWithDashboards {
48 |
49 | /**
50 | * Output directory for the Grafana Json files.
51 | */
52 | @Parameter(defaultValue = "${project.build.directory}/grafana-dashboards", required = true)
53 | protected File outputDirectory;
54 |
55 | @Override
56 | public void execute() throws MojoExecutionException, MojoFailureException {
57 | List dashboards = loadDashboards();
58 |
59 | outputDirectory.mkdirs();
60 | DashboardSerializer dashboardSerializer = new DashboardSerializer();
61 | for (Dashboard dashboard : dashboards) {
62 | getLog().info("Generating " + dashboard.getTitle());
63 |
64 | File file = new File(outputDirectory, filenameFor(dashboard));
65 | try (OutputStream stream = new FileOutputStream(file)) {
66 | dashboardSerializer.write(dashboard, stream);
67 | } catch (FileNotFoundException e) {
68 | throw new MojoFailureException("This should not happen...", e);
69 | } catch (IOException e) {
70 | throw new MojoFailureException("Error during writing file: " + file, e);
71 | }
72 | }
73 | }
74 |
75 | private String filenameFor(Dashboard dashboard) {
76 | return dashboard.getTitle().replaceAll("[\\s:/\\\\]+", "-") + ".json";
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/src/main/java/uk/co/szmg/grafana/maven/GenerateAndUpload.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.maven;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.apache.maven.plugin.MojoExecutionException;
24 | import org.apache.maven.plugin.MojoFailureException;
25 | import org.apache.maven.plugins.annotations.LifecyclePhase;
26 | import org.apache.maven.plugins.annotations.Mojo;
27 | import org.apache.maven.plugins.annotations.Parameter;
28 | import org.apache.maven.plugins.annotations.ResolutionScope;
29 | import uk.co.szmg.grafana.DashboardUploader;
30 | import uk.co.szmg.grafana.GrafanaClient;
31 | import uk.co.szmg.grafana.GrafanaEndpoint;
32 | import uk.co.szmg.grafana.domain.Dashboard;
33 |
34 | import java.util.List;
35 |
36 | @Mojo(name = "generateAndUpload", defaultPhase = LifecyclePhase.DEPLOY, requiresDependencyResolution = ResolutionScope.RUNTIME)
37 | public class GenerateAndUpload extends AbstractMojoWithDashboards {
38 |
39 | /**
40 | * When {@code true}, existing Grafana dashboards will be overwritten.
41 | */
42 | @Parameter(property = "grafana.overwrite", defaultValue = "false", required = true)
43 | protected boolean overwrite;
44 |
45 | /**
46 | * Grafana URL.
47 | */
48 | @Parameter(property = "grafana.url", required = true)
49 | protected String url;
50 |
51 | /**
52 | * Grafana session cookie value for authentication.
53 | *
54 | *
Authentication can happen through session
55 | * cookie (this) or API key ({@code apiKey} parameter).
56 | */
57 | @Parameter(property = "grafana.sessionCookie")
58 | protected String sessionCookie;
59 |
60 | /**
61 | * Grafana API key for authentication.
62 | *
63 | *
Authentication can happen through session
64 | * cookie ({@code sessionCookie} parameter) or API key (this).
65 | */
66 | @Parameter(property = "grafana.apiKey")
67 | protected String apiKey;
68 |
69 | /**
70 | * Skips SSL certificate validation if {@code true}.
71 | */
72 | @Parameter(property = "grafana.skipSSLValidation", defaultValue = "false", required = true)
73 | protected boolean skipSSLValidation;
74 |
75 | @Override
76 | public void execute() throws MojoExecutionException, MojoFailureException {
77 |
78 | GrafanaEndpoint endpoint = new GrafanaEndpoint();
79 | endpoint.setBaseUrl(url);
80 | endpoint.setSessionCookie(sessionCookie);
81 | endpoint.setApiKey(apiKey);
82 | endpoint.setSkipSSLValidation(skipSSLValidation);
83 |
84 | List dashboards = loadDashboards();
85 |
86 | DashboardUploader uploader = new DashboardUploader(endpoint);
87 | for (Dashboard dashboard : dashboards) {
88 | String title = dashboard.getTitle();
89 | try {
90 | getLog().debug(title + " - starting upload");
91 | uploader.upload(dashboard, overwrite);
92 | getLog().info(title + " - uploaded");
93 | } catch (GrafanaClient.UnexpectedGrafanaResponseException e) {
94 | if (!overwrite && e.getResponseCode() == 412) {
95 | getLog().info(title + " - dashboard already exists, skipping");
96 | } else {
97 | throw new MojoFailureException("Unexpected Grafana response while uploading " + title, e);
98 | }
99 | } catch (RuntimeException e) {
100 | throw new MojoFailureException("Error during uploading " + title, e);
101 | }
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/grafana-dashboard-maven-plugin/src/main/java/uk/co/szmg/grafana/maven/Upload.java:
--------------------------------------------------------------------------------
1 | package uk.co.szmg.grafana.maven;
2 |
3 | /*-
4 | * #%L
5 | * grafana-dashboard-maven-plugin
6 | * %%
7 | * Copyright (C) 2017 Mate Gabor Szvoboda
8 | * %%
9 | * Licensed under the Apache License, Version 2.0 (the "License");
10 | * you may not use this file except in compliance with the License.
11 | * You may obtain a copy of the License at
12 | *
13 | * http://www.apache.org/licenses/LICENSE-2.0
14 | *
15 | * Unless required by applicable law or agreed to in writing, software
16 | * distributed under the License is distributed on an "AS IS" BASIS,
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 | * See the License for the specific language governing permissions and
19 | * limitations under the License.
20 | * #L%
21 | */
22 |
23 | import org.apache.maven.plugin.AbstractMojo;
24 | import org.apache.maven.plugin.MojoExecutionException;
25 | import org.apache.maven.plugin.MojoFailureException;
26 | import org.apache.maven.plugins.annotations.LifecyclePhase;
27 | import org.apache.maven.plugins.annotations.Mojo;
28 | import org.apache.maven.plugins.annotations.Parameter;
29 | import org.codehaus.plexus.util.FileUtils;
30 | import uk.co.szmg.grafana.GrafanaClient;
31 | import uk.co.szmg.grafana.GrafanaEndpoint;
32 |
33 | import java.io.File;
34 | import java.io.IOException;
35 |
36 | @Mojo(name = "upload", defaultPhase = LifecyclePhase.DEPLOY)
37 | public class Upload extends AbstractMojo {
38 |
39 | /**
40 | * Source directory where Grafana Json files are.
41 | */
42 | @Parameter(property = "grafana.sourceDir", defaultValue = "${project.build.directory}/grafana-dashboards", required = true)
43 | protected File inputDirectory;
44 |
45 | /**
46 | * When {@code true}, existing Grafana dashboards will be overwritten.
47 | */
48 | @Parameter(property = "grafana.overwrite", defaultValue = "false", required = true)
49 | protected boolean overwrite;
50 |
51 | /**
52 | * Grafana URL.
53 | */
54 | @Parameter(property = "grafana.url", required = true)
55 | protected String url;
56 |
57 | /**
58 | * Grafana session cookie value for authentication.
59 | *
60 | *
Authentication can happen through session
61 | * cookie (this) or API key ({@code apiKey} parameter).
62 | */
63 | @Parameter(property = "grafana.sessionCookie")
64 | protected String sessionCookie;
65 |
66 | /**
67 | * Grafana API key for authentication.
68 | *
69 | *
")
57 | .withMode("html")
58 | .withTransparent(true)
59 | .withSpan(8))
60 | .addPanel(newSingleStat()
61 | .withTitle("Single stat test")
62 | .addTarget(target)
63 | .withSpan(2));
64 |
65 | Row row2 = newRow()
66 | .addPanel(newGraph()
67 | .addTarget(target)
68 | .withSpan(12));
69 |
70 | Dashboard dashboard = newDashboard()
71 | .withTitle("Test dashboard")
72 | .addRow(row1)
73 | .addRow(row2);
74 |
75 | ```
76 |
77 | ### Higher level bits and bobs
78 |
79 | TODO document
80 |
81 | ### Write to stream (optional)
82 |
83 | Convert to string or write onto a stream:
84 |
85 | ```java
86 | DashboardSerializer serializer = new DashboardSerializer();
87 |
88 | // to String
89 | System.out.println(serializer.toString(dashboard));
90 |
91 | // write to Stream
92 | serializer.write(testDashboard(), System.out);
93 | ```
94 |
95 | ### Upload to Grafana
96 |
97 | It's easy to update your dashboards from code.
98 |
99 | ```java
100 |
101 | // Create an endpoint first...
102 | // Can use either API key or session cookie auth.
103 | GrafanaEndpoint endpoint = new GrafanaEndpoint();
104 | endpoint.setBaseUrl("https://grafana.mydomain.com");
105 |
106 | // see http://docs.grafana.org/http_api/auth/
107 | endpoint.setApiKey("some api key");
108 |
109 | // copy "grafana_sess" cookie
110 | //endpoint.setSessionCookie("123456789asd");
111 |
112 | // for insecure Grafana installations...
113 | endpoint.setSkipSSLValidation(true);
114 |
115 |
116 | // ...then the uploader...
117 | DashboardUploader uploader = new DashboardUploader(endpoint);
118 |
119 |
120 | // ...and upload, overwriting any existing dashboard with
121 | // that title. ("Test dashboard")
122 | uploader.upload(dashboard, true);
123 |
124 |
125 | ```
126 |
127 | ## Flexible domain objects
128 |
129 | If a field that you want to use is missing or its type has changed (e.g., a new type of panel), you can
130 | quickly add/override it without changing the library code itself. (Although a PR is welcomed.)
131 |
132 | So no one have to clone/modify/compile/raise PR/wait for trying out an unusual/new/forgotten field/type.
133 |
134 | Every domain object has a generic setter, getter and "with" method:
135 |
136 | ```java
137 |
138 | // setter
139 | dashboard.setField("templateInstance", "my favorite");
140 | assert dashboard.getField("templateInstance") == "my favorite";
141 |
142 | // "with" method
143 | Dashboard sameDashboard = dashboard.withField("templateInstance", "my other favorite");
144 | assert dashboard.getField("templateInstance") == "my other favorite"; // it's not immutable
145 | assert sameDashboard.getField("templateInstance") == "my other favorite";
146 |
147 | // use a new type of panel
148 | row1.addPanel(new Panel.Generic()
149 | .withValue("type", "new type")
150 | .withValue("newField", "value"));
151 | ```
152 |
153 | Technical stuff: every domain object is a `Map` wrapped into their class; every typed property reads and writes that map. By using the generic setters/getter, you can write that map, too.
154 |
155 | You might want to look at the source of e.g., `Graph` to see how it works. And look for the file `graph.yaml` to see what it is generated from.
156 |
157 | ## License
158 |
159 | Apache License Version 2.0
160 |
161 | Copyright (c) 2017 Mate Gabor Szvoboda
162 |
--------------------------------------------------------------------------------