uk.co.szmg.grafana:domain

Java based Grafana dashboard generator and uploader.

License

License

Categories

Categories

Doma Data ORM
GroupId

GroupId

uk.co.szmg.grafana
ArtifactId

ArtifactId

domain
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Java based Grafana dashboard generator and uploader.
Source Code Management

Source Code Management

https://github.com/szmg/grafana-dashboard-generator-java/tree/master/domain

Download domain

How to add to project

<!-- https://jarcasting.com/artifacts/uk.co.szmg.grafana/domain/ -->
<dependency>
    <groupId>uk.co.szmg.grafana</groupId>
    <artifactId>domain</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/uk.co.szmg.grafana/domain/
implementation 'uk.co.szmg.grafana:domain:2.0.0'
// https://jarcasting.com/artifacts/uk.co.szmg.grafana/domain/
implementation ("uk.co.szmg.grafana:domain:2.0.0")
'uk.co.szmg.grafana:domain:jar:2.0.0'
<dependency org="uk.co.szmg.grafana" name="domain" rev="2.0.0">
  <artifact name="domain" type="jar" />
</dependency>
@Grapes(
@Grab(group='uk.co.szmg.grafana', module='domain', version='2.0.0')
)
libraryDependencies += "uk.co.szmg.grafana" % "domain" % "2.0.0"
[uk.co.szmg.grafana/domain "2.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.8.8

Project Modules

There are no modules declared in this project.

Grafana dashboard builder in Java

Build Status Maven Central

This is Work in progress

Code your Grafana dashboards in Java, more or less type-safe, with auto-complete. Then upload them to Grafana. As part of your build process if you fancy.

Features

  • create dashboards in Java
  • create and reuse your own frequently used components
  • upload your dashboards to Grafana
  • ApiKey or session cookie authentication
  • upload as part of your build process (Maven plugin)
  • upload with a nice command line tool
  • do not get stuck if the lib is missing a property or a type: add it without needing to recompile the lib (see below)
  • [PLAN] watch code and generate+upload on change (with the fast but limited Janino compiler)

Usage

TODO different use cases, development mode, CD

Maven

<dependency>
    <groupId>uk.co.szmg.grafana</groupId>
    <artifactId>grafana-dashboard-generator</artifactId>
    <version>2.0.0</version>
</dependency>

Basics

I suggest you play with this and learn it that way. It's really straightforward if you are familiar with Grafana. uk.co.szmg.grafana.domain.DomainFactories is a good place to start.

//import static uk.co.szmg.grafana.domain.DomainFactories.*

Target target = newTarget()
        .withTarget("maxSeries(humidity.peti.test.sensors)");

Row row1 = newRow()
        .withHeight("100px")
        .addPanel(newSingleStat()
                .withTitle("Single stat test")
                .addTarget(target)
                .withSpan(2))
        .addPanel(newText()
                .withContent("<div class=\"text-center dashboard-header\"><span>This is the test</span></div>")
                .withMode("html")
                .withTransparent(true)
                .withSpan(8))
        .addPanel(newSingleStat()
                .withTitle("Single stat test")
                .addTarget(target)
                .withSpan(2));

Row row2 = newRow()
        .addPanel(newGraph()
                .addTarget(target)
                .withSpan(12));

Dashboard dashboard = newDashboard()
        .withTitle("Test dashboard")
        .addRow(row1)
        .addRow(row2);

Higher level bits and bobs

TODO document

Write to stream (optional)

Convert to string or write onto a stream:

DashboardSerializer serializer = new DashboardSerializer();

// to String
System.out.println(serializer.toString(dashboard));

// write to Stream
serializer.write(testDashboard(), System.out);

Upload to Grafana

It's easy to update your dashboards from code.

// Create an endpoint first...
// Can use either API key or session cookie auth.
GrafanaEndpoint endpoint = new GrafanaEndpoint();
endpoint.setBaseUrl("https://grafana.mydomain.com");

// see http://docs.grafana.org/http_api/auth/
endpoint.setApiKey("some api key");

// copy "grafana_sess" cookie
//endpoint.setSessionCookie("123456789asd");

// for insecure Grafana installations...
endpoint.setSkipSSLValidation(true);


// ...then the uploader...
DashboardUploader uploader = new DashboardUploader(endpoint);


// ...and upload, overwriting any existing dashboard with
// that title. ("Test dashboard")
uploader.upload(dashboard, true);

Flexible domain objects

If a field that you want to use is missing or its type has changed (e.g., a new type of panel), you can quickly add/override it without changing the library code itself. (Although a PR is welcomed.)

So no one have to clone/modify/compile/raise PR/wait for trying out an unusual/new/forgotten field/type.

Every domain object has a generic setter, getter and "with" method:

// setter
dashboard.setField("templateInstance", "my favorite");
assert dashboard.getField("templateInstance") == "my favorite";

// "with" method
Dashboard sameDashboard = dashboard.withField("templateInstance", "my other favorite");
assert dashboard.getField("templateInstance") == "my other favorite"; // it's not immutable
assert sameDashboard.getField("templateInstance") == "my other favorite";

// use a new type of panel
row1.addPanel(new Panel.Generic()
        .withValue("type", "new type")
        .withValue("newField", "value"));

Technical stuff: every domain object is a Map<String, Object> wrapped into their class; every typed property reads and writes that map. By using the generic setters/getter, you can write that map, too.

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.

License

Apache License Version 2.0

Copyright (c) 2017 Mate Gabor Szvoboda

Versions

Version
2.0.0
1.1.0
1.0.0