1
1
package ccmcommons
2
2
3
3
import (
4
- "time"
5
- "strings"
6
4
"encoding/json"
5
+ "fmt"
7
6
"log/slog"
7
+ "strings"
8
+ "time"
9
+
8
10
"github.com/harness/harness-mcp/client/dto"
9
- "fmt"
10
11
)
11
12
12
- func BuildFilters (viewId string , timeFilters string , idFilters dto.CCMGraphQLFilters , keyValueFilters dto.CCMGraphQLKeyValueFilters ) ( []map [string ]any ) {
13
+ func BuildFilters (viewId string , timeFilters string , idFilters dto.CCMGraphQLFilters , keyValueFilters dto.CCMGraphQLKeyValueFilters ) []map [string ]any {
13
14
filters := []map [string ]any {}
14
- viewFilter := BuildViewFilter (viewId )
15
+ viewFilter := BuildViewFilter (viewId )
15
16
filters = append (filters , viewFilter ... )
16
17
filters = append (filters , BuildTimeFilters (timeFilters )... )
17
18
filters = append (filters , BuildFieldFilters (idFilters , OutputFields )... )
@@ -24,39 +25,39 @@ func BuildViewFilter(viewId string) []map[string]any {
24
25
return []map [string ]any {
25
26
{
26
27
"viewMetadataFilter" : map [string ]any {
27
- "viewId" : viewId ,
28
+ "viewId" : viewId ,
28
29
"isPreview" : false ,
29
30
},
30
31
},
31
32
}
32
33
}
33
34
34
- func BuildFilterValues (options * dto.CCMPerspectiveFilterValuesOptions ) []map [string ]any {
35
+ func BuildFilterValues (options * dto.CCMPerspectiveFilterValuesOptions ) []map [string ]any {
35
36
filters := []map [string ]any {}
36
- viewFilter := BuildViewFilter (options .ViewId )
37
+ viewFilter := BuildViewFilter (options .ViewId )
37
38
filters = append (filters , viewFilter ... )
38
39
filters = append (filters , BuildTimeFilters (options .TimeFilter )... )
39
40
40
- if options .ValueType == dto .ValueTypeCostCategory ||
41
- options .ValueType == dto .ValueTypeLabel ||
41
+ if options .ValueType == dto .ValueTypeCostCategory ||
42
+ options .ValueType == dto .ValueTypeLabel ||
42
43
options .ValueType == dto .ValueTypeLabelV2 {
43
44
44
45
filter , err := BuildKeyValueFieldFilter (options .ValueType , options .ValueSubType , BuildOutputFieldsMap ())
45
46
if err == nil {
46
47
filters = append (filters , filter ... )
47
48
}
48
- } else if options .ValueType == dto .ValueTypeLabelKey ||
49
+ } else if options .ValueType == dto .ValueTypeLabelKey ||
49
50
options .ValueType == dto .ValueTypeLabelV2Key {
50
51
filter , err := BuildKeyFieldFilter (options .ValueType , BuildOutputFieldsMap ())
51
52
if err == nil {
52
53
filters = append (filters , filter ... )
53
54
}
54
-
55
+
55
56
} else {
56
57
filter := BuildFieldFilters (buildFilterForValue (options .ValueType ), OutputFields )
57
58
filters = append (filters , filter ... )
58
59
}
59
- return filters
60
+ return filters
60
61
}
61
62
62
63
func buildFilterForValue (valueType string ) dto.CCMGraphQLFilters {
@@ -66,13 +67,13 @@ func buildFilterForValue(valueType string) dto.CCMGraphQLFilters {
66
67
func buildFilterForKeyValue (valueType string , valueSubtype string ) dto.CCMGraphQLKeyValueFilters {
67
68
68
69
filterType := "labels.value"
69
- if strings .EqualFold (valueType , dto .ValueTypeCostCategory ) {
70
+ if strings .EqualFold (valueType , dto .ValueTypeCostCategory ) {
70
71
filterType = "business_mapping"
71
72
}
72
73
73
74
return map [string ]map [string ]any {
74
75
filterType : {
75
- "filterL1" : valueSubtype ,
76
+ "filterL1" : valueSubtype ,
76
77
"filterL2" : filterType ,
77
78
},
78
79
}
@@ -104,32 +105,32 @@ func BuildTimeFilters(timeFilter string) []map[string]any {
104
105
},
105
106
},
106
107
}
107
- }
108
+ }
108
109
109
- func BuildAggregateFunction () ( []map [string ]any ) {
110
+ func BuildAggregateFunction () []map [string ]any {
110
111
111
112
return []map [string ]any {
112
113
{
113
114
"operationType" : "SUM" ,
114
- "columnName" : "cost" ,
115
+ "columnName" : "cost" ,
115
116
},
116
117
}
117
- }
118
+ }
118
119
119
- func BuildPreferences () ( map [string ]any ) {
120
+ func BuildPreferences () map [string ]any {
120
121
return map [string ]any {
121
- "includeOthers" : false ,
122
+ "includeOthers" : false ,
122
123
"includeUnallocatedCost" : false ,
123
124
"awsPreferences" : map [string ]any {
124
125
"includeDiscounts" : false ,
125
- "includeCredits" : false ,
126
- "includeRefunds" : false ,
127
- "includeTaxes" : false ,
128
- "awsCost" : "UNBLENDED" ,
126
+ "includeCredits" : false ,
127
+ "includeRefunds" : false ,
128
+ "includeTaxes" : false ,
129
+ "awsCost" : "UNBLENDED" ,
129
130
},
130
- "gcpPreferences" : nil ,
131
+ "gcpPreferences" : nil ,
131
132
"azureViewPreferences" : nil ,
132
- "showAnomalies" : false ,
133
+ "showAnomalies" : false ,
133
134
}
134
135
}
135
136
@@ -186,11 +187,11 @@ func BuildFieldFiltersWithOperator(input map[string][]string, output []map[strin
186
187
"idFilter" : map [string ]any {
187
188
"values" : values ,
188
189
"operator" : operator ,
189
- "field" : out ,
190
+ "field" : out ,
190
191
},
191
192
}
192
193
193
- result = append (result , idFilterMap )
194
+ result = append (result , idFilterMap )
194
195
}
195
196
}
196
197
}
@@ -209,15 +210,15 @@ func BuildKeyValueFieldFiltersWithOperator(input dto.CCMGraphQLKeyValueFilters,
209
210
if strings .EqualFold (fName , out ["identifier" ]) {
210
211
fieldName , ok := values ["filterL1" ].(string )
211
212
if ok {
212
- out ["fieldName" ] = fieldName
213
+ out ["fieldName" ] = fieldName
213
214
var idFilterMap = map [string ]any {
214
215
"idFilter" : map [string ]any {
215
216
"values" : values ["filterL2" ],
216
217
"operator" : operator ,
217
- "field" : out ,
218
+ "field" : out ,
218
219
},
219
220
}
220
- result = append (result , idFilterMap )
221
+ result = append (result , idFilterMap )
221
222
}
222
223
}
223
224
}
@@ -231,7 +232,7 @@ func BuildKeyFieldFilter(input string, output map[string]map[string]any) ([]map[
231
232
232
233
func BuildKeyFieldFilterWithOperator (fieldKey string , output map [string ]map [string ]any , operator string ) ([]map [string ]any , error ) {
233
234
result := make ([]map [string ]any , 0 )
234
-
235
+
235
236
fieldOut , ok := output [fieldKey ]
236
237
if ! ok {
237
238
return nil , fmt .Errorf ("Field key not found when building filter %s" , fieldKey )
@@ -243,9 +244,9 @@ func BuildKeyFieldFilterWithOperator(fieldKey string, output map[string]map[stri
243
244
}
244
245
var idFilterMap = map [string ]any {
245
246
"idFilter" : map [string ]any {
246
- "values" : []string {"" },
247
+ "values" : []string {"" },
247
248
"operator" : operator ,
248
- "field" : out ,
249
+ "field" : out ,
249
250
},
250
251
}
251
252
result = append (result , idFilterMap )
@@ -258,7 +259,7 @@ func BuildKeyValueFieldFilter(fieldKey string, fieldSubKey string, output map[st
258
259
259
260
func BuildKeyValueFieldFilterWithOperator (fieldKey string , fieldSubKey string , output map [string ]map [string ]any , operator string ) ([]map [string ]any , error ) {
260
261
result := make ([]map [string ]any , 0 )
261
-
262
+
262
263
fieldOut , ok := output [fieldKey ]
263
264
264
265
if ! ok {
@@ -273,9 +274,9 @@ func BuildKeyValueFieldFilterWithOperator(fieldKey string, fieldSubKey string, o
273
274
out ["fieldName" ] = fieldSubKey
274
275
var idFilterMap = map [string ]any {
275
276
"idFilter" : map [string ]any {
276
- "values" : []string {"" },
277
+ "values" : []string {"" },
277
278
"operator" : operator ,
278
- "field" : out ,
279
+ "field" : out ,
279
280
},
280
281
}
281
282
result = append (result , idFilterMap )
@@ -286,8 +287,8 @@ func BuildGroupBy(input map[string]any, outputFields []map[string]string, output
286
287
// Get the value when grouping by field only.
287
288
field , ok1 := input ["field" ].(string )
288
289
if ok1 == false {
289
- return DefaultGroupBy
290
- }
290
+ return DefaultGroupBy
291
+ }
291
292
292
293
for _ , out := range outputFields {
293
294
if strings .EqualFold (field , out ["fieldId" ]) {
@@ -302,7 +303,7 @@ func BuildGroupBy(input map[string]any, outputFields []map[string]string, output
302
303
// Get the value when grouping by key,value.
303
304
value , ok2 := input ["value" ].(string )
304
305
if ok2 == false {
305
- return DefaultGroupBy
306
+ return DefaultGroupBy
306
307
}
307
308
for _ , out := range outputKeyValueFields {
308
309
if strings .EqualFold (field , out ["identifier" ]) {
@@ -314,7 +315,7 @@ func BuildGroupBy(input map[string]any, outputFields []map[string]string, output
314
315
}
315
316
}
316
317
}
317
- return DefaultGroupBy
318
+ return DefaultGroupBy
318
319
}
319
320
320
321
func DebugPayload (operation string , payload map [string ]any ) {
@@ -324,7 +325,7 @@ func DebugPayload(operation string, payload map[string]any) {
324
325
slog .Debug ("-----------" , "----------" , "--------------" )
325
326
}
326
327
327
- func MapToJSONString (m map [string ]any ) ( string ) {
328
+ func MapToJSONString (m map [string ]any ) string {
328
329
b , err := json .MarshalIndent (m , "" , " " )
329
330
if err != nil {
330
331
return ""
0 commit comments