/
es_request_constructor.go
134 lines (124 loc) · 6.74 KB
/
es_request_constructor.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
// Copyright 2018 MSolution.IO
//
// 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 ec2
import (
"time"
"gopkg.in/olivere/elastic.v5"
)
const maxAggregationSize = 0x7FFFFFFF
// getDateForDailyReport returns the end and the begin of the date of the report based on a date
// if the date given as parameter is in the actual month, it returns the the the begin of the month et now at midnight
// if the date is before the actual month, it returns the begin and the end of the month given as parameter
func getDateForDailyReport(date time.Time) (begin, end time.Time) {
now := time.Now().UTC()
if date.Year() == now.Year() && date.Month() == now.Month() {
end = now
begin = time.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, end.Location()).UTC()
return
} else {
begin = date
end = time.Date(date.Year(), date.Month()+1, 0, 23, 59, 59, 999999999, date.Location()).UTC()
return
}
}
// createQueryAccountFilterEc2 creates and return a new *elastic.TermsQuery on the accountList array
func createQueryAccountFilterEc2(accountList []string) *elastic.TermsQuery {
accountListFormatted := make([]interface{}, len(accountList))
for i, v := range accountList {
accountListFormatted[i] = v
}
return elastic.NewTermsQuery("account", accountListFormatted...)
}
// getElasticSearchEc2DailyParams is used to construct an ElasticSearch *elastic.SearchService used to perform a request on ES
// It takes as parameters :
// - params Ec2QueryParams : contains the list of accounts and the date
// - client *elastic.Client : an instance of *elastic.Client that represent an Elastic Search client.
// It needs to be fully configured and ready to execute a client.Search()
// - index string : The Elastic Search index on which to execute the query. In this context the default value
// should be "ec2-reports"
// This function excepts arguments passed to it to be sanitize. If they are not, the following cases will make
// it crash :
// - If the client is nil or malconfigured, it will crash
// - If the index is not an index present in the ES, it will crash
func getElasticSearchEc2DailyParams(params Ec2QueryParams, client *elastic.Client, index string) *elastic.SearchService {
query := elastic.NewBoolQuery()
if len(params.AccountList) > 0 {
query = query.Filter(createQueryAccountFilterEc2(params.AccountList))
}
query = query.Filter(elastic.NewTermQuery("reportType", "daily"))
dateStart, dateEnd := getDateForDailyReport(params.Date)
query = query.Filter(elastic.NewRangeQuery("reportDate").
From(dateStart).To(dateEnd))
search := client.Search().Index(index).Size(0).Query(query)
search.Aggregation("accounts", elastic.NewTermsAggregation().Field("account").
SubAggregation("dates", elastic.NewTermsAggregation().Field("reportDate").
SubAggregation("instances", elastic.NewTopHitsAggregation().Sort("reportDate", false).Size(maxAggregationSize))))
return search
}
// getElasticSearchEc2MonthlyParams is used to construct an ElasticSearch *elastic.SearchService used to perform a request on ES
// It takes as parameters :
// - params Ec2QueryParams : contains the list of accounts and the date
// - client *elastic.Client : an instance of *elastic.Client that represent an Elastic Search client.
// It needs to be fully configured and ready to execute a client.Search()
// - index string : The Elastic Search index on which to execute the query. In this context the default value
// should be "ec2-reports"
// This function excepts arguments passed to it to be sanitize. If they are not, the following cases will make
// it crash :
// - If the client is nil or malconfigured, it will crash
// - If the index is not an index present in the ES, it will crash
func getElasticSearchEc2MonthlyParams(params Ec2QueryParams, client *elastic.Client, index string) *elastic.SearchService {
query := elastic.NewBoolQuery()
if len(params.AccountList) > 0 {
query = query.Filter(createQueryAccountFilterEc2(params.AccountList))
}
query = query.Filter(elastic.NewTermQuery("reportType", "monthly"))
query = query.Filter(elastic.NewTermQuery("reportDate", params.Date))
search := client.Search().Index(index).Size(0).Query(query)
search.Aggregation("accounts", elastic.NewTermsAggregation().Field("account").
SubAggregation("instances", elastic.NewTopHitsAggregation().Sort("reportDate", false).Size(maxAggregationSize)))
return search
}
// createQueryAccountFilterBill creates and return a new *elastic.TermsQuery on the accountList array
func createQueryAccountFilterBill(accountList []string) *elastic.TermsQuery {
accountListFormatted := make([]interface{}, len(accountList))
for i, v := range accountList {
accountListFormatted[i] = v
}
return elastic.NewTermsQuery("usageAccountId", accountListFormatted...)
}
// getElasticSearchCostParams is used to construct an ElasticSearch *elastic.SearchService used to perform a request on ES
// It takes as parameters :
// - params Ec2QueryParams : contains the list of accounts and the date
// - client *elastic.Client : an instance of *elastic.Client that represent an Elastic Search client.
// It needs to be fully configured and ready to execute a client.Search()
// - index string : The Elastic Search index on which to execute the query
// This function excepts arguments passed to it to be sanitize. If they are not, the following cases will make
// it crash :
// - If the client is nil or malconfigured, it will crash
// - If the index is not an index present in the ES, it will crash
func getElasticSearchCostParams(params Ec2QueryParams, client *elastic.Client, index string) *elastic.SearchService {
query := elastic.NewBoolQuery()
if len(params.AccountList) > 0 {
query = query.Filter(createQueryAccountFilterBill(params.AccountList))
}
query = query.Filter(elastic.NewTermsQuery("productCode", "AmazonEC2", "AmazonCloudWatch"))
dateStart, dateEnd := getDateForDailyReport(params.Date)
query = query.Filter(elastic.NewRangeQuery("usageStartDate").
From(dateStart).To(dateEnd))
search := client.Search().Index(index).Size(0).Query(query)
search.Aggregation("accounts", elastic.NewTermsAggregation().Field("usageAccountId").Size(maxAggregationSize).
SubAggregation("instances", elastic.NewTermsAggregation().Field("resourceId").Size(maxAggregationSize).
SubAggregation("cost", elastic.NewSumAggregation().Field("unblendedCost"))))
return search
}