summaryrefslogtreecommitdiffstats
path: root/container-core/src/main/scala/com/yahoo/container/handler/observability/OverviewHandler.scala
blob: 7973ee8c8119eaf465562c044252fdf415ad85a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler.observability

import java.util.concurrent.Executor

import HtmlUtil._
import OverviewHandler._
import com.yahoo.container.jdisc.{HttpResponse, HttpRequest, ThreadedHttpRequestHandler}
import com.yahoo.text.Utf8
import java.io.{PrintWriter, OutputStream}


/**
 * @author gjoranv
 * @author tonytv
 */
class OverviewHandler(executor: Executor) extends ThreadedHttpRequestHandler(executor) {

  @volatile
  private var dotGraph: String = _

  def handle(request: HttpRequest): HttpResponse = {
    val path = request.getUri.getPath

    try {
      if (path.endsWith("/ComponentGraph"))
        handleComponentGraph(request)
      else if (path.endsWith("/Overview"))
        handleOverview(request)
      else
        null
    } catch {
      case e: Exception => errorResponse(e.getMessage)
    }

  }

  def handleOverview(request: HttpRequest): HttpResponse = {
    new HttpResponse(com.yahoo.jdisc.Response.Status.OK) {
      def render(stream: OutputStream) {
        stream.write(Utf8.toBytes(overviewPageText))
      }

      override def getContentType: String = {
        "text/html"
      }
    }
  }

  def errorResponse(message: String): HttpResponse = {
    new HttpResponse(com.yahoo.jdisc.Response.Status.BAD_REQUEST) {
      def render(stream: OutputStream) {
        new PrintWriter(stream).println(message)
      }
    }
  }

  def handleComponentGraph(request: HttpRequest): HttpResponse = {
    var graphType = request.getProperty("type");
    if (graphType == null)
      graphType = "text"

    graphType match {
      case "text" => textualComponentGraph(dotGraph)
      case t if componentGraphTypes.contains(t) => graphicalComponentGraph(t, Graphviz.runDot(graphType,dotGraph))
      case t => errorResponse(t)
    }
  }

  def textualComponentGraph(dotGraph: String) =
    new HttpResponse(com.yahoo.jdisc.Response.Status.OK) {
      def render(stream: OutputStream) {
        stream.write(Utf8.toBytes(dotGraph))
      }

      override def getContentType: String = {
        "text/plain"
      }
    }

  def graphicalComponentGraph(graphType: String, image: Array[Byte] ): HttpResponse =
    new HttpResponse(com.yahoo.jdisc.Response.Status.OK) {
      def render(output: OutputStream) {
        output.write(image)
      }

      override def getContentType: String = {
        componentGraphTypes(graphType)
      }
    }

  def setDotGraph(dotGraph: String) {
    this.dotGraph = dotGraph
  }
}

object OverviewHandler {
  val componentGraphTypes = Map(
    "svg"  -> "image/svg+xml",
    "png"  -> "image/png",
    "text" -> "text/plain")

  val overviewPageText = prettyPrintXhtml(overviewPage)

  private def overviewPage = {
    def componentGraphLink(graphType: String) = link("Overview/ComponentGraph?type=" + graphType, graphType)


    html(
      title = "Container Overview",
      body =
        h1("Container Overview"),
        unorderedList(
          li(link("ApplicationStatus")),
          li("ComponentGraph" +: (componentGraphTypes.keys map {componentGraphLink}).toSeq :_*)))
  }

}