-
Notifications
You must be signed in to change notification settings - Fork 218
/
txnz.go
62 lines (54 loc) · 1.2 KB
/
txnz.go
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package v1
import (
"strconv"
"time"
"proxy"
"github.com/ant0ine/go-json-rest/rest"
"github.com/xelabs/go-mysqlstack/xlog"
)
// TxnzHandler impl.
func TxnzHandler(log *xlog.Log, proxy *proxy.Proxy) rest.HandlerFunc {
f := func(w rest.ResponseWriter, r *rest.Request) {
txnzHandler(log, proxy, w, r)
}
return f
}
func txnzHandler(log *xlog.Log, proxy *proxy.Proxy, w rest.ResponseWriter, r *rest.Request) {
type txn struct {
TxnID uint64 `json:"txnid"`
Start time.Time `json:"start"`
Duration time.Duration `json:"duration"`
State string `json:"state"`
XaState string `json:"xa-state"`
Color string `json:"color"`
}
limit := 100
if v, err := strconv.Atoi(r.PathParam("limit")); err == nil {
limit = v
}
var rsp []txn
scatter := proxy.Scatter()
rows := scatter.Txnz().GetTxnzRows()
for i, row := range rows {
if i >= limit {
break
}
r := txn{
TxnID: row.TxnID,
Start: row.Start,
Duration: row.Duration,
State: row.State,
XaState: row.XaState,
Color: row.Color,
}
rsp = append(rsp, r)
}
w.WriteJson(rsp)
}