-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery.go
146 lines (125 loc) · 3.6 KB
/
query.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package lytics
import (
"encoding/json"
"net/url"
"time"
)
var (
// Ensure we can write out as a table
_ TableWriter = (*Query)(nil)
)
const (
queryEndpoint = "query/:id"
queryListEndpoint = "query"
queryTestEndpoint = "query/_test"
queryValidateEndpoint = "query/_validate"
)
type (
// Query represents an LQL Statement structure
Query struct {
Id string `json:"id"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Alias string `json:"alias"`
Table string `json:"table"`
From string `json:"from"`
Text string `json:"text"`
Fields map[string]QueryField `json:"fields"`
}
// A field in a query
// - very similar to catalog, query fields create catalog fields
QueryField struct {
As string `json:"as"`
IsBy bool `json:"is_by"`
Type string `json:"type"`
ShortDesc string `json:"shortdesc"`
LongDesc string `json:"longdesc"`
Identities []string `json:"identities"`
Froms []string `json:"froms"`
}
)
func (m *Query) Headers() []interface{} {
return []interface{}{
"ID", "table", "alias", "from", "created", "updated",
}
}
func (m *Query) Row() []interface{} {
return []interface{}{
m.Id, m.Table, m.Alias, m.From, m.Created.Format(time.RFC3339), m.Updated.Format(time.RFC3339),
}
}
// GetQueries returns a list of all queries associated with this account
// https://learn.lytics.com/api-docs/query
func (l *Client) GetQueries() ([]*Query, error) {
res := ApiResp{}
data := []*Query{}
// make the request
err := l.Get(queryListEndpoint, nil, nil, &res, &data)
if err != nil {
return data, err
}
return data, nil
}
// GetQueries returns a list of all queries associated with this account
// https://learn.lytics.com/api-docs/query
func (l *Client) GetQuery(alias string) (Query, error) {
res := ApiResp{}
data := Query{}
// make the request
err := l.Get(parseLyticsURL(queryEndpoint, map[string]string{"id": alias}), nil, nil, &res, &data)
if err != nil {
return data, err
}
return data, nil
}
// GetQueryTest returns the evaluated entity from given query
// https://learn.lytics.com/api-docs/query
func (l *Client) GetQueryTest(qs url.Values, query string) (Entity, error) {
res := ApiResp{}
data := Entity{}
// make the request
err := l.Post(queryTestEndpoint, qs, query, &res, &data.Fields)
if err != nil {
return data, err
}
return data, nil
}
// PostQueryValidate returns the query and how it is interpreted
// https://learn.lytics.com/api-docs/query
func (l *Client) PostQueryValidate(query string) ([]*Query, error) {
res := ApiResp{}
data := []*Query{}
// make the request
err := l.PostType("text/plain", queryValidateEndpoint, nil, query, &res, &data)
if err != nil {
return data, err
}
return data, nil
}
// PostQueryValidateSegment checks query interpretation & validates query against existing segments/ schemas
func (l *Client) PostQueryValidateSegment(query string) (interface{}, error) {
res := ApiResp{}
djson := json.RawMessage{}
v := url.Values{}
v.Set("segments", "true")
// make the request
err := l.PostType("text/plain", queryValidateEndpoint, v, query, &res, &djson)
var data interface{}
// unmarshal response `data` field into appropriate type depending on response status & message
// add cases?
switch res.Status {
case float64(400):
if res.Message == "Invalid schema errors" {
d := []string{}
e := json.Unmarshal(djson, &d)
if e != nil {
return nil, e
}
data = d
}
}
if err != nil {
return data, err
}
return data, nil
}