-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataselectquery.go
119 lines (105 loc) · 4.18 KB
/
dataselectquery.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dataselect
import (
"github.com/kubernetes/dashboard/src/app/backend/resource/metric"
)
// Options for GenericDataSelect which takes []GenericDataCell and returns selected data.
// Can be extended to include any kind of selection - for example filtering.
// Currently included only Pagination and Sort options.
type DataSelectQuery struct {
PaginationQuery *PaginationQuery
SortQuery *SortQuery
// Filter *FilterQuery
MetricQuery *MetricQuery
}
var NoMetrics = NewMetricQuery(nil, nil)
// StandardMetrics query results in a standard metrics being returned.
// standard metrics are: cpu usage, memory usage and aggregation = sum.
var StandardMetrics = NewMetricQuery([]string{"cpu/usage_rate", "memory/usage"}, metric.OnlySumAggregation)
// MetricQuery holds parameters for metric extraction process.
// It accepts list of metrics to be downloaded and a list of aggregations that should be performed for each metric.
// Query has this format metrics=metric1,metric2,...&aggregations=aggregation1,aggregation2,...
type MetricQuery struct {
// Metrics to download, all available metric names can be found here:
// https://github.com/kubernetes/heapster/blob/master/docs/storage-schema.md
MetricNames []string
// Aggregations to be performed for each metric. Check available aggregations in aggregation.go.
// If empty, default aggregation will be used (sum).
Aggregations metric.AggregationNames
}
// NewMetricQuery returns a metric query from provided settings.
func NewMetricQuery(metricNames []string, aggregations metric.AggregationNames) *MetricQuery {
return &MetricQuery{
MetricNames: metricNames,
Aggregations: aggregations,
}
}
// SortQuery holds options for sort functionality of data select.
type SortQuery struct {
SortByList []SortBy
}
// SortBy holds the name of the property that should be sorted and whether order should be ascending or descending.
type SortBy struct {
Property PropertyName
Ascending bool
}
// NoSort is as option for no sort.
var NoSort = &SortQuery{
SortByList: []SortBy{},
}
// NoDataSelect is an option for no data select (same data will be returned).
var NoDataSelect = NewDataSelectQuery(NoPagination, NoSort, NoMetrics)
// NewDataSelectQuery creates DataSelectQuery object from simpler data select queries.
func NewDataSelectQuery(paginationQuery *PaginationQuery, sortQuery *SortQuery, graphQuery *MetricQuery) *DataSelectQuery {
return &DataSelectQuery{
PaginationQuery: paginationQuery,
SortQuery: sortQuery,
MetricQuery: graphQuery,
}
}
// NewSortQuery takes raw sort options list and returns SortQuery object. For example:
// ["a", "parameter1", "d", "parameter2"] - means that the data should be sorted by
// parameter1 (ascending) and later - for results that return equal under parameter 1 sort - by parameter2 (descending)
func NewSortQuery(sortByListRaw []string) *SortQuery {
if sortByListRaw == nil || len(sortByListRaw)%2 == 1 {
// Empty sort list or invalid (odd) length
return NoSort
}
sortByList := []SortBy{}
for i := 0; i+1 < len(sortByListRaw); i += 2 {
// parse order option
var ascending bool
orderOption := sortByListRaw[i]
if orderOption == "a" {
ascending = true
} else if orderOption == "d" {
ascending = false
} else {
// Invalid order option. Only ascending (a), descending (d) options are supported
return NoSort
}
// parse property name
propertyName := sortByListRaw[i+1]
sortBy := SortBy{
Property: PropertyName(propertyName),
Ascending: ascending,
}
// Add to the sort options.
sortByList = append(sortByList, sortBy)
}
return &SortQuery{
SortByList: sortByList,
}
}