Skip to content

Commit 250de28

Browse files
committed
#### Version 0.9.2
* Opt: CounterOutputHttpHandler、TaskOutputHttpHandler输出UI优化 * About HttpOutput: - CounterOutputHttpHandler: 用于输出当前时刻所有Task的Counter信息 - TaskOutputHttpHandler: 用于输出所有Task的配置信息 - Code Example Link: https://github.com/devfeel/dotweb-start/blob/master/server/router.go#L20 * 2019-11-19 19:00
1 parent d1bf90f commit 250de28

2 files changed

Lines changed: 178 additions & 5 deletions

File tree

tasks_httphandler.go

Lines changed: 170 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,183 @@
11
package task
22

33
import (
4-
"encoding/json"
4+
"fmt"
55
"net/http"
6+
"strings"
67
)
78

89
// CounterOutputHttpHandler Http Handler for output counter info
910
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))
1227
}
1328

1429
// TaskOutputHttpHandler Http Handler for output task info
1530
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))
1853
}
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+
`

version.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## devfeel/dottask
22

3+
#### Version 0.9.2
4+
* Opt: CounterOutputHttpHandler、TaskOutputHttpHandler输出UI优化
5+
* About HttpOutput:
6+
- CounterOutputHttpHandler: 用于输出当前时刻所有Task的Counter信息
7+
- TaskOutputHttpHandler: 用于输出所有Task的配置信息
8+
- Code Example Link: https://github.com/devfeel/dotweb-start/blob/master/server/router.go#L20
9+
* 2019-11-19 19:00
10+
311
#### Version 0.9.1
412
* feature: 新增输出Task信息与Counter信息的go原生HandlerFunc
513
* fix: 修复CronTask对RunCounter统计逻辑BUG

0 commit comments

Comments
 (0)