|
1 | 1 | package task |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
| 4 | + "fmt" |
5 | 5 | "net/http" |
| 6 | + "strings" |
6 | 7 | ) |
7 | 8 |
|
8 | 9 | // CounterOutputHttpHandler Http Handler for output counter info |
9 | 10 | func (service *TaskService) CounterOutputHttpHandler(w http.ResponseWriter, r *http.Request) { |
10 | | - str, _ := json.Marshal(service.GetAllTaskCountInfo()) |
11 | | - w.Write([]byte(str)) |
| 11 | + tableData := "" |
| 12 | + for _, v := range service.GetAllTaskCountInfo() { |
| 13 | + tableData += "<tr><td>" + v.TaskID + "</td><td>" + v.Lable + "</td><td>" + fmt.Sprint(v.Count) + "</td></tr>" |
| 14 | + } |
| 15 | + col := `<colgroup> |
| 16 | + <col width="40%"> |
| 17 | + <col width="30%"> |
| 18 | + <col width="30%"> |
| 19 | + </colgroup>` |
| 20 | + header := `<tr> |
| 21 | + <th>TaskID</th> |
| 22 | + <th>Lable</th> |
| 23 | + <th>Value</th> |
| 24 | + </tr>` |
| 25 | + html := createOneTableHtml(col, "TaskCounterInfo", header, tableData) |
| 26 | + w.Write([]byte(html)) |
12 | 27 | } |
13 | 28 |
|
14 | 29 | // TaskOutputHttpHandler Http Handler for output task info |
15 | 30 | func (service *TaskService) TaskOutputHttpHandler(w http.ResponseWriter, r *http.Request) { |
16 | | - str, _ := json.Marshal(service.GetAllTasks()) |
17 | | - w.Write([]byte(str)) |
| 31 | + tableData := "" |
| 32 | + for _, v := range service.GetAllTasks() { |
| 33 | + tableData += "<tr><td>" + v.TaskID() + "</td><td>" + v.GetConfig().TaskType + "</td><td>" + fmt.Sprint(v.GetConfig().IsRun) + "</td><td>" + v.GetConfig().Express + "</td><td>" + fmt.Sprint(v.GetConfig().DueTime) + "</td><td>" + fmt.Sprint(v.GetConfig().Interval) + "</td></tr>" |
| 34 | + } |
| 35 | + col := `<colgroup> |
| 36 | + <col width="20%"> |
| 37 | + <col width="10%"> |
| 38 | + <col width="10%"> |
| 39 | + <col width="30%"> |
| 40 | + <col width="15%"> |
| 41 | + <col width="15%"> |
| 42 | + </colgroup>` |
| 43 | + header := `<tr> |
| 44 | + <th>TaskID</th> |
| 45 | + <th>TaskType</th> |
| 46 | + <th>IsRun</th> |
| 47 | + <th>Express</th> |
| 48 | + <th>DueTime</th> |
| 49 | + <th>Interval</th> |
| 50 | + </tr>` |
| 51 | + html := createOneTableHtml(col, "TaskInfo", header, tableData) |
| 52 | + w.Write([]byte(html)) |
18 | 53 | } |
| 54 | + |
| 55 | +func createOneTableHtml(col, title, header, body string) string { |
| 56 | + template := `<br><table class="bordered"> |
| 57 | + {{col}} |
| 58 | + <caption>{{title}}</caption> |
| 59 | + <thead> |
| 60 | + {{header}} |
| 61 | + </thead> |
| 62 | + {{body}} |
| 63 | + </table>` |
| 64 | + data := strings.Replace(template, "{{col}}", col, -1) |
| 65 | + data = strings.Replace(data, "{{title}}", title, -1) |
| 66 | + data = strings.Replace(data, "{{header}}", header, -1) |
| 67 | + data = strings.Replace(data, "{{body}}", body, -1) |
| 68 | + html := strings.Replace(tableHtml, "{{tableBody}}", data, -1) |
| 69 | + |
| 70 | + return html |
| 71 | +} |
| 72 | + |
| 73 | +var tableHtml = `<html> |
| 74 | +<html><head> |
| 75 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 76 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 77 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"> |
| 78 | + |
| 79 | + <meta name="Generator" content="EditPlus"> |
| 80 | + <meta name="Author" content=""> |
| 81 | + <meta name="Keywords" content=""> |
| 82 | + <meta name="Description" content=""> |
| 83 | + <title>DotTask</title> |
| 84 | + <style> |
| 85 | + .overtable { |
| 86 | + width: 100%; |
| 87 | + overflow: hidden; |
| 88 | + overflow-x: auto; |
| 89 | + } |
| 90 | + body { |
| 91 | + max-width: 780px; |
| 92 | + margin:0 auto; |
| 93 | + font-family: 'trebuchet MS', 'Lucida sans', Arial; |
| 94 | + font-size: 1rem; |
| 95 | + color: #444; |
| 96 | + } |
| 97 | + table { |
| 98 | + font-family: 'trebuchet MS', 'Lucida sans', Arial; |
| 99 | + *border-collapse: collapse; |
| 100 | + /* IE7 and lower */ |
| 101 | + border-spacing: 0; |
| 102 | + width: 100%; |
| 103 | + border-collapse: collapse; |
| 104 | + overflow-x: auto |
| 105 | + } |
| 106 | + caption { |
| 107 | + font-family: 'Microsoft Yahei', 'trebuchet MS', 'Lucida sans', Arial; |
| 108 | + text-align: left; |
| 109 | + padding: .5rem; |
| 110 | + font-weight: bold; |
| 111 | + font-size: 110%; |
| 112 | + color: #666; |
| 113 | + } |
| 114 | + tr { |
| 115 | + border-top: 1px solid #dfe2e5 |
| 116 | + } |
| 117 | + tr:nth-child(2n) { |
| 118 | + background-color: #f6f8fa |
| 119 | + } |
| 120 | + td, |
| 121 | + th { |
| 122 | + border: 1px solid #dfe2e5; |
| 123 | + } |
| 124 | + .bordered tr:hover { |
| 125 | + background: #fbf8e9; |
| 126 | + } |
| 127 | + .bordered td, |
| 128 | + .bordered th { padding: .6em 1em; |
| 129 | +
|
| 130 | + border: 1px solid #ccc; |
| 131 | + padding: 10px; |
| 132 | + text-align: left; |
| 133 | + } |
| 134 | + </style> |
| 135 | + <script> |
| 136 | + |
| 137 | +(function(doc, win) { |
| 138 | + window.MPIXEL_RATIO = (function () { |
| 139 | + var Mctx = document.createElement("canvas").getContext("2d"), |
| 140 | + Mdpr = window.devicePixelRatio || 1, |
| 141 | + Mbsr = Mctx.webkitBackingStorePixelRatio || |
| 142 | + Mctx.mozBackingStorePixelRatio || |
| 143 | + Mctx.msBackingStorePixelRatio || |
| 144 | + Mctx.oBackingStorePixelRatio || |
| 145 | + Mctx.backingStorePixelRatio || 1; |
| 146 | + |
| 147 | + return Mdpr/Mbsr; |
| 148 | + })(); |
| 149 | +
|
| 150 | + function addEventListeners(ele,type,callback){ |
| 151 | + |
| 152 | + try{ // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本 |
| 153 | + ele.addEventListener(type,callback,false); |
| 154 | + }catch(e){ |
| 155 | + try{ // IE8.0及其以下版本 |
| 156 | + ele.attachEvent('on' + type,callback); |
| 157 | + }catch(e){ // 早期浏览器 |
| 158 | + ele['on' + type] = callback; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +
|
| 163 | + var docEl = doc.documentElement, |
| 164 | + resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'; |
| 165 | + window.recalc = function() { |
| 166 | + var clientWidth = docEl.clientWidth < 768 ? docEl.clientWidth : 768; |
| 167 | + if (!clientWidth) return; |
| 168 | + docEl.style.fontSize = 10 * (clientWidth / 320) * window.MPIXEL_RATIO + 'px'; |
| 169 | + }; |
| 170 | + window.recalc(); |
| 171 | + |
| 172 | + addEventListeners(win, resizeEvt, recalc); |
| 173 | +})(document, window); |
| 174 | +
|
| 175 | +</script> |
| 176 | +</head> |
| 177 | +<body> |
| 178 | +<div class="overtable"> |
| 179 | +{{tableBody}} |
| 180 | +</div> |
| 181 | +</body> |
| 182 | +</html> |
| 183 | +` |
0 commit comments