-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
110 lines (97 loc) · 2.65 KB
/
status.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
104
105
106
107
108
109
110
package chevalier
import (
"encoding/json"
"errors"
"fmt"
"time"
"code.google.com/p/goprotobuf/proto"
es "github.com/anchor/elastigo/core"
)
type ElasticsearchStatus struct {
IndicesInitialized bool `json:"indices_initialized"`
}
func NewElasticsearchStatus() *ElasticsearchStatus {
s := new(ElasticsearchStatus)
return s
}
func MarshalStatusResponse(s *StatusResponse) ([]byte, error) {
return proto.Marshal(s)
}
func (s *StatusResponse) ToJSON() ([]byte, error) {
return json.Marshal(s)
}
func UnmarshalStatusResponse(b []byte) (*StatusResponse, error) {
s := new(StatusResponse)
err := proto.Unmarshal(b, s)
return s, err
}
func NewStatusResponse() *StatusResponse {
s := new(StatusResponse)
s.Origins = make([]*StatusResponse_Origin, 0)
s.Errors = make([]string, 0)
return s
}
func (s *StatusResponse) addError(err error) {
s.Errors = append(s.Errors, fmt.Sprintf("%v", err))
}
func NewStatusResponse_Origin(origin string, sources uint64, updated time.Time) *StatusResponse_Origin {
o := new(StatusResponse_Origin)
originR := origin
sourcesR := sources
updatedR := uint64(updated.UnixNano())
o.Origin = &originR
o.Sources = &sourcesR
o.LastUpdated = &updatedR
return o
}
// runOriginQuery returns an elastigo/core.SearchResult for the
// specified origin in the metadata index.
func (e *QueryEngine) runOriginQuery(origin string) (*es.SearchResult, error) {
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": map[string]interface{}{
"query_string": map[string]interface{}{
"default_field": "origin",
"query": origin,
},
},
},
},
}
var args map[string]interface{}
r, err := es.SearchRequest(e.metaIndex, e.originType, args, query)
return &r, err
}
// GetOriginMetadata returns an ElasticsearchOrigin object for the
// specified origin.
func (e *QueryEngine) GetOriginMetadata(origin string) (*ElasticsearchOrigin, error) {
res, err := e.runOriginQuery(origin)
if err != nil {
return nil, err
}
if len(res.Hits.Hits) == 0 {
errMsg := fmt.Sprintf("no metadata available for origin %v", origin)
return nil, errors.New(errMsg)
}
mRes := res.Hits.Hits[0]
om := new(ElasticsearchOrigin)
err = json.Unmarshal(*mRes.Source, om)
if err != nil {
return nil, err
}
return om, nil
}
func (e *QueryEngine) GetStatus(origins []string) *StatusResponse {
s := new(StatusResponse)
for _, o := range origins {
esOrigin, err := e.GetOriginMetadata(o)
if err != nil {
s.addError(err)
continue
}
pbOrigin := NewStatusResponse_Origin(esOrigin.Origin, esOrigin.Count, esOrigin.LastUpdated)
s.Origins = append(s.Origins, pbOrigin)
}
return s
}