dataselectquery.go 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16 17
package dataselect

import (
18
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
19
	"github.com/kubernetes/dashboard/src/app/backend/resource/metric"
20
)
21 22 23 24 25 26 27

// 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
28 29
	FilterQuery     *FilterQuery
	MetricQuery     *MetricQuery
30 31 32 33
}

var NoMetrics = NewMetricQuery(nil, nil)

34 35
// StandardMetrics query results in a standard metrics being returned.
// standard metrics are: cpu usage, memory usage and aggregation = sum.
36 37
var StandardMetrics = NewMetricQuery([]string{common.CpuUsage, common.MemoryUsage},
	metric.OnlySumAggregation)
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// 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,
	}
57
}
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
// 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{},
}

75 76 77 78 79 80 81 82 83 84 85 86 87
type FilterQuery struct {
	FilterByList []FilterBy
}

type FilterBy struct {
	Property PropertyName
	Value    ComparableValue
}

var NoFilter = &FilterQuery{
	FilterByList: []FilterBy{},
}

88
// NoDataSelect is an option for no data select (same data will be returned).
89
var NoDataSelect = NewDataSelectQuery(NoPagination, NoSort, NoFilter, NoMetrics)
90

91
// StdMetricsDataSelect does not perform any data select, just downloads standard metrics.
92
var StdMetricsDataSelect = NewDataSelectQuery(NoPagination, NoSort, NoFilter, StandardMetrics)
93 94

// DefaultDataSelect downloads first 10 items from page 1 with no sort and no metrics.
95
var DefaultDataSelect = NewDataSelectQuery(DefaultPagination, NoSort, NoFilter, NoMetrics)
96 97

// DefaultDataSelectWithMetrics downloads first 10 items from page 1 with no sort. Also downloads and includes standard metrics.
98
var DefaultDataSelectWithMetrics = NewDataSelectQuery(DefaultPagination, NoSort, NoFilter, StandardMetrics)
99

100
// NewDataSelectQuery creates DataSelectQuery object from simpler data select queries.
101
func NewDataSelectQuery(paginationQuery *PaginationQuery, sortQuery *SortQuery, filterQuery *FilterQuery, graphQuery *MetricQuery) *DataSelectQuery {
102 103 104
	return &DataSelectQuery{
		PaginationQuery: paginationQuery,
		SortQuery:       sortQuery,
105
		FilterQuery:     filterQuery,
106
		MetricQuery:     graphQuery,
107 108 109 110 111 112
	}
}

// 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)
113 114
func NewSortQuery(sortByListRaw []string) *SortQuery {
	if sortByListRaw == nil || len(sortByListRaw)%2 == 1 {
115 116 117 118
		// Empty sort list or invalid (odd) length
		return NoSort
	}
	sortByList := []SortBy{}
119
	for i := 0; i+1 < len(sortByListRaw); i += 2 {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		// 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{
135
			Property:  PropertyName(propertyName),
136 137 138 139 140 141 142 143 144
			Ascending: ascending,
		}
		// Add to the sort options.
		sortByList = append(sortByList, sortBy)
	}
	return &SortQuery{
		SortByList: sortByList,
	}
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

// NewFilterQuery takes raw filter options list and returns FilterQuery object. For example:
// ["parameter1", "value1", "parameter2", "value2"] - means that the data should be filtered by
// parameter1 equals value1 and parameter2 equals value2
func NewFilterQuery(filterByListRaw []string) *FilterQuery {
	if filterByListRaw == nil || len(filterByListRaw)%2 == 1 {
		return NoFilter
	}
	filterByList := []FilterBy{}
	for i := 0; i+1 < len(filterByListRaw); i += 2 {
		propertyName := filterByListRaw[i]
		propertyValue := filterByListRaw[i+1]
		filterBy := FilterBy{
			Property: PropertyName(propertyName),
			Value:    StdComparableString(propertyValue),
		}
		// Add to the filter options.
		filterByList = append(filterByList, filterBy)
	}
	return &FilterQuery{
		FilterByList: filterByList,
	}
}