github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/imageunpacker/httpd/status.go (about)

     1  package httpd
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"sort"
     9  
    10  	"github.com/Cloud-Foundations/Dominator/lib/format"
    11  	"github.com/Cloud-Foundations/Dominator/lib/html"
    12  )
    13  
    14  func (s state) statusHandler(w http.ResponseWriter, req *http.Request) {
    15  	if req.URL.Path != "/" {
    16  		http.NotFound(w, req)
    17  		return
    18  	}
    19  	writer := bufio.NewWriter(w)
    20  	defer writer.Flush()
    21  	fmt.Fprintln(writer, "<title>image-unpacker status page</title>")
    22  	fmt.Fprintln(writer, `<style>
    23                            table, th, td {
    24                            border-collapse: collapse;
    25                            }
    26                            </style>`)
    27  	fmt.Fprintln(writer, "<body>")
    28  	fmt.Fprintln(writer, "<center>")
    29  	fmt.Fprintln(writer, "<h1>image-unpacker status page</h1>")
    30  	fmt.Fprintln(writer, "</center>")
    31  	html.WriteHeaderWithRequest(writer, req)
    32  	fmt.Fprintln(writer, "<h3>")
    33  	s.writeDashboard(writer)
    34  	for _, htmlWriter := range htmlWriters {
    35  		htmlWriter.WriteHtml(writer)
    36  	}
    37  	fmt.Fprintln(writer, "</h3>")
    38  	fmt.Fprintln(writer, "<hr>")
    39  	html.WriteFooter(writer)
    40  	fmt.Fprintln(writer, "</body>")
    41  }
    42  
    43  func (s state) writeDashboard(writer io.Writer) {
    44  	status := s.unpacker.GetStatus()
    45  	fmt.Fprintln(writer, "Image streams:<br>")
    46  	fmt.Fprintln(writer, `<table border="1">`)
    47  	tw, _ := html.NewTableWriter(writer, true,
    48  		"Image Stream", "Device Id", "Device Name", "Size", "Status")
    49  	streamNames := make([]string, 0, len(status.ImageStreams))
    50  	for streamName := range status.ImageStreams {
    51  		streamNames = append(streamNames, streamName)
    52  	}
    53  	sort.Strings(streamNames)
    54  	for _, streamName := range streamNames {
    55  		stream := status.ImageStreams[streamName]
    56  		tw.WriteRow("", "",
    57  			fmt.Sprintf("<a href=\"showFileSystem?%s\">%s</a>",
    58  				streamName, streamName),
    59  			stream.DeviceId,
    60  			status.Devices[stream.DeviceId].DeviceName,
    61  			func() string {
    62  				size := status.Devices[stream.DeviceId].Size
    63  				if size < 1 {
    64  					return ""
    65  				}
    66  				return format.FormatBytes(size)
    67  			}(),
    68  			stream.Status.String(),
    69  		)
    70  	}
    71  	fmt.Fprintln(writer, "</table><br>")
    72  	fmt.Fprintln(writer, "Devices:<br>")
    73  	fmt.Fprintln(writer, `<table border="1">`)
    74  	tw, _ = html.NewTableWriter(writer, true, "Device Id", "Device Name",
    75  		"Size", "Image Stream", "Status")
    76  	deviceIds := make([]string, 0, len(status.Devices))
    77  	for deviceId := range status.Devices {
    78  		deviceIds = append(deviceIds, deviceId)
    79  	}
    80  	sort.Strings(deviceIds)
    81  	for _, deviceId := range deviceIds {
    82  		deviceInfo := status.Devices[deviceId]
    83  		stream, ok := status.ImageStreams[deviceInfo.StreamName]
    84  		tw.WriteRow("", "",
    85  			deviceId,
    86  			deviceInfo.DeviceName,
    87  			format.FormatBytes(deviceInfo.Size),
    88  			func() string {
    89  				if ok {
    90  					return fmt.Sprintf("<a href=\"showFileSystem?%s\">%s</a>",
    91  						deviceInfo.StreamName, deviceInfo.StreamName)
    92  				}
    93  				return ""
    94  			}(),
    95  			func() string {
    96  				if ok {
    97  					return stream.Status.String()
    98  				}
    99  				return ""
   100  			}(),
   101  		)
   102  	}
   103  	fmt.Fprintln(writer, "</table><br>")
   104  }