-
Notifications
You must be signed in to change notification settings - Fork 12
/
query.go
211 lines (179 loc) · 6.91 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package models
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iottwinmaker"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
type TwinMakerQueryType = string
const (
QueryTypeListWorkspace TwinMakerQueryType = "ListWorkspace" // each datasource will have a default workspace
QueryTypeListScenes TwinMakerQueryType = "ListScenes" // required for scene viewer
QueryTypeListEntities TwinMakerQueryType = "ListEntities" //
QueryTypeGetEntity TwinMakerQueryType = "GetEntity" //
QueryTypeGetPropertyValue TwinMakerQueryType = "GetPropertyValue"
QueryTypeComponentHistory TwinMakerQueryType = "ComponentHistory"
QueryTypeEntityHistory TwinMakerQueryType = "EntityHistory"
QueryTypeGetAlarms TwinMakerQueryType = "GetAlarms"
)
type TwinMakerResultOrder = string
const (
ResultOrderAsc TwinMakerResultOrder = "ASCENDING"
ResultOrderDesc TwinMakerResultOrder = "DESCENDING"
)
type TwinMakerFilterValue struct {
BooleanValue *bool `json:"booleanValue,omitempty"`
DoubleValue *float64 `json:"doubleValue,omitempty"`
IntegerValue *int64 `json:"integerValue,omitempty"`
LongValue *int64 `json:"longValue,omitempty"`
StringValue *string `json:"stringValue,omitempty"`
}
type TwinMakerPropertyFilter struct {
Name string `json:"name"`
Value TwinMakerFilterValue `json:"value"`
Op string `json:"op,omitempty"`
}
func (v *TwinMakerFilterValue) ToTwinMakerDataValue() *iottwinmaker.DataValue {
if v.BooleanValue != nil {
return &iottwinmaker.DataValue{BooleanValue: v.BooleanValue}
} else if v.DoubleValue != nil {
return &iottwinmaker.DataValue{DoubleValue: v.DoubleValue}
} else if v.IntegerValue != nil {
return &iottwinmaker.DataValue{IntegerValue: v.IntegerValue}
} else if v.LongValue != nil {
return &iottwinmaker.DataValue{LongValue: v.LongValue}
} else if v.StringValue != nil {
return &iottwinmaker.DataValue{StringValue: v.StringValue}
}
return &iottwinmaker.DataValue{}
}
func (v *TwinMakerFilterValue) DataValueToString() string {
str := ""
if v.BooleanValue != nil {
str = strconv.FormatBool(*v.BooleanValue)
} else if v.DoubleValue != nil {
str = strconv.FormatFloat(*v.DoubleValue, 'f', -1, 64)
} else if v.IntegerValue != nil {
str = strconv.FormatInt(*v.IntegerValue, 10)
} else if v.LongValue != nil {
str = strconv.FormatInt(*v.LongValue, 10)
} else if v.StringValue != nil {
str = *v.StringValue
}
return str
}
type TwinMakerListEntitiesFilter struct {
ComponentTypeId string `json:"componentTypeId,omitempty"`
ExternalId string `json:"externalId,omitempty"`
ParentEntityId string `json:"parentEntityId,omitempty"`
}
func (f *TwinMakerPropertyFilter) ToTwinMakerFilter() *iottwinmaker.PropertyFilter {
filter := &iottwinmaker.PropertyFilter{
PropertyName: aws.String(f.Name),
Value: f.Value.ToTwinMakerDataValue(),
}
if f.Op != "" {
filter.SetOperator(f.Op) // ???
}
return filter
}
type TwinMakerOrderBy struct {
Order TwinMakerResultOrder `json:"order,omitempty"`
PropertyName string `json:"propertyName,omitempty"`
}
func (orderBy *TwinMakerOrderBy) ToTwinMakerOrderBy() *iottwinmaker.OrderBy {
return &iottwinmaker.OrderBy{
Order: aws.String(orderBy.Order),
PropertyName: aws.String(orderBy.PropertyName),
}
}
type TwinMakerTabularConditions struct {
OrderBy []TwinMakerOrderBy `json:"orderBy,omitempty"`
PropertyFilter []TwinMakerPropertyFilter `json:"propertyFilter,omitempty"`
}
func (tabularConditions *TwinMakerTabularConditions) ToTwinMakerTabularConditions() *iottwinmaker.TabularConditions {
var orders []*iottwinmaker.OrderBy
for _, o := range tabularConditions.OrderBy {
orders = append(orders, o.ToTwinMakerOrderBy())
}
var filters []*iottwinmaker.PropertyFilter
for _, pf := range tabularConditions.PropertyFilter {
filters = append(filters, pf.ToTwinMakerFilter())
}
tabularCondition := &iottwinmaker.TabularConditions{}
if len(orders) > 0 {
tabularCondition.OrderBy = orders
}
if len(filters) > 0 {
tabularCondition.PropertyFilters = filters
}
return tabularCondition
}
// TwinMakerQuery model
type TwinMakerQuery struct {
GrafanaLiveEnabled bool `json:"grafanaLiveEnabled,omitempty"`
IsStreaming bool `json:"isStreaming,omitempty"`
WorkspaceId string `json:"workspaceId,omitempty"`
EntityId string `json:"entityId,omitempty"`
Properties []*string `json:"properties,omitempty"`
// Optional metadata saved with the query. When this matches properties used in the results, it will
// replace the display name
PropertyDisplayNames map[string]string `json:"propertyDisplayNames,omitempty"`
NextToken string `json:"nextToken,omitempty"`
ComponentName string `json:"componentName,omitempty"`
ComponentTypeId string `json:"componentTypeId,omitempty"`
PropertyFilter []TwinMakerPropertyFilter `json:"filter,omitempty"`
ListEntitiesFilter []TwinMakerListEntitiesFilter `json:"listEntitiesFilter,omitempty"`
Order TwinMakerResultOrder `json:"order,omitempty"`
MaxResults int `json:"maxResults,omitempty"`
// Athena Data Connector parameters for iottwinmaker.GetPropertyValue
TabularConditions TwinMakerTabularConditions `json:"tabularConditions,omitempty"`
PropertyGroupName string `json:"propertyGroupName,omitempty"`
IntervalStreamingSeconds int `json:"intervalStreaming,string,omitempty"`
IntervalStreaming time.Duration `json:"_"`
// Direct from the gRPC interfaces
QueryType TwinMakerQueryType `json:"-"`
TimeRange backend.TimeRange `json:"-"`
}
func (q *TwinMakerQuery) CacheKey(prefix string) string {
if q.NextToken != "" {
return "" // not cacheable
}
key := prefix + "~" + q.WorkspaceId + "/" + q.EntityId + "/" + q.ComponentName + "/" + q.ComponentTypeId
for _, p := range q.Properties {
if p != nil {
key += "#" + *p
}
}
for _, f := range q.PropertyFilter {
key += "!" + f.Name + f.Op + f.Value.DataValueToString()
}
// TODO: does it break the filter?
for _, ef := range q.ListEntitiesFilter {
key += "&" + ef.ExternalId
}
key += "@" + q.Order
return key
}
// ReadQuery will read and validate Settings from the DataSourceConfig
func ReadQuery(query backend.DataQuery) (TwinMakerQuery, error) {
model := TwinMakerQuery{}
if err := json.Unmarshal(query.JSON, &model); err != nil {
return model, fmt.Errorf("could not read query: %w", err)
}
switch i := model.IntervalStreamingSeconds; {
case i >= 5:
model.IntervalStreaming = time.Duration(i) * time.Second
case i < 5:
model.IntervalStreaming = 5 * time.Second
default:
model.IntervalStreaming = 30 * time.Second
}
// From the raw query
model.TimeRange = query.TimeRange
model.QueryType = query.QueryType
return model, nil
}