-
Notifications
You must be signed in to change notification settings - Fork 0
/
statusql.go
103 lines (99 loc) · 2.6 KB
/
statusql.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
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
package main
import (
"fmt"
"github.com/cxuhua/xginx"
"github.com/graphql-go/graphql"
)
var ClientType = graphql.NewObject(graphql.ObjectConfig{
Name: "Client",
Fields: graphql.Fields{
"id": {
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cli := p.Source.(*xginx.Client)
return cli.ID(), nil
},
Description: "节点id",
},
"addr": {
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cli := p.Source.(*xginx.Client)
return cli.Addr.String(), nil
},
Description: "链接地址",
},
"service": {
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cli := p.Source.(*xginx.Client)
return fmt.Sprintf("%x", cli.Service), nil
},
Description: "对端提供的服务",
},
"height": {
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cli := p.Source.(*xginx.Client)
return cli.Height, nil
},
Description: "对端区块高度",
},
"ver": {
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cli := p.Source.(*xginx.Client)
return cli.Ver, nil
},
Description: "对端版本",
},
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*xginx.Client)
return ok
},
Description: "链接信息",
})
var statusInfo = &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "StatusInfo",
Fields: graphql.Fields{
"version": {
Type: graphql.Int,
Description: "运行版本",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
conf := xginx.GetConfig()
return conf.Ver, nil
},
},
"height": {
Type: graphql.Int,
Description: "区块链高度",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
bi := p.Source.(*xginx.BlockIndex)
return bi.Height(), nil
},
},
"cache": {
Type: graphql.Int,
Description: "缓存内存大小",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
bi := p.Source.(*xginx.BlockIndex)
return bi.CacheSize(), nil
},
},
"clients": {
Type: graphql.NewList(graphql.NewNonNull(ClientType)),
Description: "链接客户端",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return xginx.Server.Clients(), nil
},
},
},
}),
Description: "获取当前区块链状态",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
objs := Objects(p.Info.RootValue.(map[string]interface{}))
return objs.BlockIndex(), nil
},
}