###########################################################################
# Copyright 2019 IBM Corporation
#
# 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
# imitations under the License.
###########################################################################

apiVersion: v1
kind: ConfigMap
metadata:
  name: kappnav.actions.deployment-liberty
data:
  url-actions: |
    [   
      {
        "name":"metrics-mp11",
        "text":"View 1.1 Metrics",
        "text.nls":"action.url.liberty.metrics.mp1.1.text",
        "description":"View Liberty mp 1.1 Grafana dashboard",
        "description.nls":"action.url.liberty.metrics.mp1.1.desc",
        "url-pattern":"${snippet.grafanaurl(${var.grafanahost},/d/websphere-liberty/${builtin.grafana-dashboard})}?refresh=10s&orgId=1&var-app=${resource.$.metadata.name}",
        "open-window": "tab",
        "enablement-label":"kappnav.action.metrics.mp11"
      },
      {
        "name":"metrics-mp20",
        "text":"View 2.0 Metrics",
        "text.nls":"action.url.liberty.metrics.mp2.0.text",
        "description":"View Liberty mp 2.0 Grafana dashboard",
        "description.nls":"action.url.liberty.metrics.mp2.0.desc",
        "url-pattern":"${snippet.grafanaurl(${var.grafanahost},/d/websphere-liberty-m2/${builtin.grafana-m2-dashboard})}?refresh=10s&orgId=1&var-app=${resource.$.metadata.name}",
        "open-window": "tab",
        "enablement-label":"kappnav.action.metrics.mp20"
      },  
      {
        "name":"klog",
        "text":"View Kibana Logs",
        "text.nls":"action.url.liberty.klog.text",
        "description":"View Liberty Kibana problem dashboard to see logs",
        "description.nls":"action.url.liberty.klog.desc",
        "url-pattern":"${snippet.create_kibana_log_url(${snippet.kibanaurl(${var.kibanahost},${builtin.liberty-problems-dashboard})},${func.podlist()})}",
        "open-window": "tab"
      }
    ]
  variables: |
    {
       "grafanahost": "${func.kubectlGet(route,grafana,-n,grafana,-o,jsonpath=${snippet.host()})}",
       "kibanahost": "${func.kubectlGet(route,logging-kibana,-n,openshift-logging,-o,jsonpath=${snippet.host()})}"
    }
  snippets: |
    {
      "host": "function host() { return '{@.spec.host}'; }",
      "kibanaurl": "function kibanaurl(host, dash)
                   {
                      if (host.indexOf('undefined')==-1) return 'https://'+host+'/#dashboard/'+dash; else return 'https://github.com/kappnav/README/blob/master/kibana-error.md'; 
                   }",
      "grafanaurl": "function grafanaurl(host, dash)
                    {
                      if (host.indexOf('undefined')==-1) return 'https://'+host+dash; else return 'https://github.com/kappnav/README/blob/master/grafana-error.md'; 
                    }",
      "create_kibana_log_url": "function createKibanaLogUrl(kibanaUrl, json) 
                                { var timeSlot = '15m';
                                  var pods = JSON.parse(json).pods;
                                  var podsString = '';
                                  var matchPhrases = '';
                                  pods.map(function(nextPod) {
                                    if(podsString.length>0) podsString = podsString + ',';
                                    if(matchPhrases.length>0) matchPhrases = matchPhrases + ',';
                                    podsString = podsString + '%7B%22term%22:%7B%22kubernetes.pod_name%22:%22' + nextPod +'%22%7D%7D';
                                    matchPhrases = matchPhrases + '(term:(kubernetes.pod_name:' + nextPod + '))'
                                  })
                                  return kibanaUrl 
                                      + \"?_g=(refreshInterval:(display:Off,pause:!f,value:0),time:(from:now-15m,mode:quick,to:now))&_a=(description:'',filters:!(('$state':(store:appState),meta:(alias:'Pod%20Filter',disabled:!f,index:'logstash-*',key:query,negate:!f,type:custom,value:'%7B%22bool%22:%7B%22minimum_should_match%22:1,%22should%22:%5B\" 
                                      + podsString 
                                      + \"%5D%7D%7D'),query:(bool:(minimum_should_match:1,should:!(\"
                                      + matchPhrases 
                                      + \"))))),options:(darkTheme:!f),panels:!((col:1,id:liberty_probleminfo,panelIndex:1,row:1,size_x:12,size_y:1,type:visualization),(col:1,id:liberty_topMessageIds,panelIndex:5,row:6,size_x:12,size_y:3,type:visualization),(col:1,id:liberty_messageGraph,panelIndex:6,row:9,size_x:4,size_y:2,type:visualization),(col:9,id:liberty_ffdcGraph,panelIndex:7,row:9,size_x:4,size_y:2,type:visualization),(col:5,id:liberty_traceGraph,panelIndex:8,row:9,size_x:4,size_y:2,type:visualization),(col:1,id:b1afa090-c266-11e8-b344-7d7a26e293da,panelIndex:19,row:2,size_x:4,size_y:2,type:visualization),(col:5,id:c1f2fce0-c266-11e8-b344-7d7a26e293da,panelIndex:20,row:2,size_x:4,size_y:2,type:visualization),(col:9,id:d29457b0-c266-11e8-b344-7d7a26e293da,panelIndex:21,row:2,size_x:4,size_y:2,type:visualization),(col:1,columns:!(loglevel,module,message,kubernetes.pod,ibm_sequence),id:liberty_messageSearch,panelIndex:25,row:11,size_x:12,size_y:4,sort:!('@timestamp',desc),type:search),(col:1,columns:!(loglevel,module,message,kubernetes.pod,ibm_sequence),id:liberty_traceSearch,panelIndex:26,row:15,size_x:12,size_y:4,sort:!('@timestamp',desc),type:search),(col:1,columns:!(ibm_className,ibm_probeID,ibm_exceptionName,kubernetes.pod,ibm_sequence),id:liberty_ffdcSearch,panelIndex:27,row:19,size_x:12,size_y:4,sort:!('@timestamp',desc),type:search),(col:1,id:bbdafb10-8a2e-11e9-bccd-ad683bf7518d,panelIndex:28,row:4,size_x:4,size_y:2,type:visualization),(col:5,id:f04cbb40-8a2e-11e9-bccd-ad683bf7518d,panelIndex:29,row:4,size_x:4,size_y:2,type:visualization),(col:9,id:'21ecbdd0-8a2f-11e9-bccd-ad683bf7518d',panelIndex:30,row:4,size_x:4,size_y:2,type:visualization)),query:(query_string:(analyze_wildcard:!t,query:'*')),timeRestore:!t,title:Liberty-Problems-K5-20190909,uiState:(P-19:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-20:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-21:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-28:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-29:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-30:(vis:(params:(sort:(columnIndex:!n,direction:!n)))),P-6:(vis:(colors:(AUDIT:%233F2B5B,INFO:%237EB26D,SEVERE:%23BF1B00,WARNING:%23F2C96D)))),viewMode:view)\";
                                }"
    }
