All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yoomoney.gradle.plugins.grafana.CollectGrafanaDashboardsTask Maven / Gradle / Ivy

Go to download

Gradle plugin by YooMoney. See README: https://github.com/yoomoney/grafana-dashboard-plugin

The newest version!
package ru.yoomoney.gradle.plugins.grafana;

import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.TaskAction;
import org.json.JSONObject;
import ru.yoomoney.gradle.plugins.grafana.impl.GrafanaDashboard;
import ru.yoomoney.gradle.plugins.grafana.impl.GrafanaDashboardCollector;
import ru.yoomoney.gradle.plugins.grafana.impl.KotlinScriptContentCreator;
import ru.yoomoney.gradle.plugins.grafana.impl.RawContentCreator;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * Task for collect and print all dashboards
 */
public class CollectGrafanaDashboardsTask extends DefaultTask {

    private final Logger log = Logging.getLogger(CollectGrafanaDashboardsTask.class);

    private Configuration grafanaFromDirConfiguration;
    private Configuration grafanaFromArtifactConfiguration;
    private GrafanaDashboardExtension grafanaDashboardExtension;

    /**
     * Main action
     */
    @TaskAction
    void collectGrafanaDashboards() {
        log.lifecycle("Collect Grafana dashboards: printCollectedDashboards={}", grafanaDashboardExtension.printCollectedDashboards);

        List dashboardsContentFromArtifact = getDashboardsContent(grafanaFromArtifactConfiguration,
                Paths.get(getProject().getBuildDir().toString(), GrafanaDashboardPlugin.DASHBOARDS_FROM_ARTIFACT_DIR).toFile());

        List dashboardsContentFromDir = getDashboardsContent(grafanaFromDirConfiguration,
                Paths.get(getProject().getProjectDir().toString(), grafanaDashboardExtension.dir).toFile());

        if (!grafanaDashboardExtension.printCollectedDashboards) {
            return;
        }

        printDashboards(dashboardsContentFromArtifact);
        printDashboards(dashboardsContentFromDir);
    }

    private List getDashboardsContent(Configuration configuration, File targetDir) {
        return new GrafanaDashboardCollector(Arrays.asList(new RawContentCreator(), kotlinScriptContentCreator(configuration)))
                .collectDashboards(targetDir);
    }

    private void printDashboards(List dashboardsContent) {
        dashboardsContent
                .forEach(dashboard -> log.lifecycle("Collect dashboard: fileName={} json={}",
                        dashboard.getFileName(), new JSONObject(dashboard.getContent()).toString(4)));

    }

    private KotlinScriptContentCreator kotlinScriptContentCreator(Configuration grafanaConfiguration) {
        return new KotlinScriptContentCreator(grafanaConfiguration, grafanaDashboardExtension.classpath);
    }

    void setGrafanaFromArtifactConfiguration(Configuration artifactConfiguration) {
        this.grafanaFromArtifactConfiguration = artifactConfiguration;
    }

    void setGrafanaFromDirConfiguration(Configuration dirConfiguration) {
        this.grafanaFromDirConfiguration = dirConfiguration;
    }

    void setGrafanaDashboardExtension(GrafanaDashboardExtension grafanaDashboardExtension) {
        this.grafanaDashboardExtension = grafanaDashboardExtension;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy