package eu.seaclouds.policy.utils;

import eu.seaclouds.policy.SeaCloudsManagementPolicy;

public class SeaCloudsPolicyRequestComposer {


    public static String setupObserverInitializerPayload(SeaCloudsManagementPolicy policy) {
        // T4C JSON payload to attach a INFLUXDB Observer to T4C
        return "{ \"format\":\"INFLUXDB\", \"protocol\":\"HTTP\",\"callbackUrl\":" +
                "\"" + policy.getConfig(SeaCloudsManagementPolicy.INFLUXDB_ENDPOINT) + "/db/" + policy.getConfig(SeaCloudsManagementPolicy.INFLUXDB_DATABASE) +
                "/series?u=" + policy.getConfig(SeaCloudsManagementPolicy.INFLUXDB_USERNAME) + "&p=" + policy.getConfig(SeaCloudsManagementPolicy.INFLUXDB_PASSWORD) + "\" }";
    }

    public static String setupGrafanaInitializerPayload(SeaCloudsManagementPolicy policy, String id) {
        final String globalGrafanaOptionsJSON =
                "{  " +
                        "\"overwrite\": false," +
                        "\"dashboard\": {" +
                        "\"id\": null," +
                        "\"title\": \"" + id + " Dashboard\"," +
                        "\"tags\": [\"SeaClouds\", \"AUTOGENERATED\"]," +
                        "\"style\": \"dark\"," +
                        "\"timezone\": \"browser\"," +
                        "\"editable\": true," +
                        "\"hideControls\": false," +
                        "\"sharedCrosshair\": true," +
                        "\"time\": { \"from\": \"now-6h\", \"to\": \"now\" }," +
                        "\"timepicker\": { " +
                        "                   \"refresh_intervals\": [\"1m\", \"1h\", \"1d\"]," +
                        "                   \"time_options\": [\"1h\", \"12h\", \"24h\", \"2d\", \"7d\", \"30d\"] " +
                        "}," +
                        "\"templating\": { \"list\": []  }," +
                        "\"annotations\": { \"list\": []  }," +
                        "\"schemaVersion\": 7," +
                        "\"version\": 1," +
                        "\"links\": []," +
                        "\"rows\": [";


        String rowOptions = "";

        for (String metricName : policy.extractMetricNames()) {
            int index = 0;
            String currentRow =
                    "{" +
                            "\"collapse\": false," +
                            "\"editable\": false," +
                            "\"height\": \"250px\"," +
                            "\"panels\": [{" +
                            "                  \"id\":" + index + "," +
                            "                  \"aliasColors\": {}," +
                            "                  \"bars\": false," +
                            "                  \"datasource\": null," +
                            "                  \"editable\": true," +
                            "                  \"error\": false," +
                            "                  \"fill\": 1," +
                            "                  \"grid\": { \"leftLogBase\": 1, \"leftMax\": null, \"leftMin\": null, \"rightLogBase\": 1, \"rightMax\": null, \"rightMin\": null, \"threshold1\": null,  \"threshold1Color\": \"rgba(216, 200, 27, 0.27)\", \"threshold2\": null, \"threshold2Color\": \"rgba(234, 112, 112, 0.22)\" }," +
                            "                  \"legend\": { \"avg\": true, \"current\": false, \"max\": true, \"min\": true, \"show\": true, \"total\": false, \"values\": false}," +
                            "                  \"lines\": true," +
                            "                  \"linewidth\": 2," +
                            "                  \"links\": []," +
                            "                  \"nullPointMode\": \"connected\"," +
                            "                  \"percentage\": false," +
                            "                  \"pointradius\": 5," +
                            "                  \"points\": false," +
                            "                  \"renderer\": \"flot\"," +
                            "                  \"seriesOverrides\": []," +
                            "                  \"span\": 12," +
                            "                  \"stack\": false," +
                            "                  \"steppedLine\": false," +
                            "                  \"targets\": [{ " +
                            "                                    \"column\": \"value\"," +
                            "                                    \"function\": \"mean\"," +
                            "                                    \"query\": \"select mean(value) from \\\"" + metricName + "\\\" where $timeFilter group by time($interval) order asc\"," +
                            "                                    \"refId\": \"A\"," +
                            "                                    \"series\": \"" + metricName + "\"" +
                            "                               }]," +
                            "                  \"timeFrom\": null," +
                            "                  \"timeShift\": null," +
                            "                  \"title\": \"" + metricName + "\"," +
                            "                  \"tooltip\": { \"shared\": true, \"value_type\": \"cumulative\"}," +
                            "                  \"type\": \"graph\"," +
                            "                  \"x-axis\": true," +
                            "                  \"y-axis\": true," +
                            "                  \"y_formats\": [\"short\",\"short\"]" +
                            "              }]," +
                            "      \"title\": \"" + id + "\"" +
                            "},";
            rowOptions += currentRow;
        }

        // Remove last ,
        rowOptions = rowOptions.substring(0, rowOptions.length() - 1);
        return globalGrafanaOptionsJSON + rowOptions + "] } }";
    }


}

@@ -82,16 +82,62 @@
 tPolicy;
+%0Aimport org.apache.brooklyn.util.text.Strings;
 %0A%0Apublic
@@ -5954,68 +5954,45 @@
     
-rowOptions = rowOptions.substring(0, rowOptions.length() - 1
+Strings.removeFromEnd(rowOptions, %22,%22
 );%0A 

Fix empty rules