/* * Copyright 2014 SeaClouds * Contact: SeaClouds * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package eu.seaclouds.policy.utils; import eu.seaclouds.policy.SeaCloudsManagementPolicy; import org.apache.brooklyn.util.text.Strings; 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 = Strings.removeFromEnd(rowOptions, ","); return globalGrafanaOptionsJSON + rowOptions + "] } }"; } }