5
5
"fmt"
6
6
"time"
7
7
"strings"
8
+ "encoding/json"
8
9
"log/slog"
9
10
"github.com/harness/harness-mcp/client/dto"
10
11
"github.com/harness/harness-mcp/client/ccmcommons"
@@ -20,7 +21,7 @@ func (r *CloudCostManagementService) PerspectiveGrid(ctx context.Context, scope
20
21
21
22
gqlQuery := ccmcommons .CCMPerspectiveGridQuery
22
23
variables := map [string ]any {
23
- "filters" : buildFilters (options .TimeFilter , options .Filters , options .KeyValueFilters ),
24
+ "filters" : buildFilters (options .ViewId , options . TimeFilter , options .Filters , options .KeyValueFilters ),
24
25
"groupBy" : buildGroupBy (options .GroupBy , outputFields , outputKeyValueFields ),
25
26
"limit" : options .Limit ,
26
27
"offset" : options .Offset ,
@@ -36,7 +37,7 @@ func (r *CloudCostManagementService) PerspectiveGrid(ctx context.Context, scope
36
37
"variables" : variables ,
37
38
}
38
39
39
- slog . Debug ("PerspectiveGrid" , "Payload " , payload )
40
+ debugPayload ("PerspectiveGrid" , payload )
40
41
result := new (dto.CCMPerspectiveGridResponse )
41
42
err := r .Client .Post (ctx , path , nil , payload , & result )
42
43
if err != nil {
@@ -60,7 +61,7 @@ func (r *CloudCostManagementService) PerspectiveTimeSeries(ctx context.Context,
60
61
}
61
62
62
63
variables := map [string ]any {
63
- "filters" : buildFilters (options .TimeFilter , options .Filters , options .KeyValueFilters ),
64
+ "filters" : buildFilters (options .ViewId , options . TimeFilter , options .Filters , options .KeyValueFilters ),
64
65
"groupBy" : []map [string ]any {timeTruncGroupBy , entityGroupBy [0 ]},
65
66
"limit" : options .Limit ,
66
67
"offset" : options .Offset ,
@@ -90,7 +91,7 @@ func (r *CloudCostManagementService) PerspectiveSummaryWithBudget(ctx context.Co
90
91
91
92
gqlQuery := ccmcommons .CCMPerspectiveSummaryWithBudgetQuery
92
93
variables := map [string ]any {
93
- "filters" : buildFilters (options .TimeFilter , options .Filters , options .KeyValueFilters ),
94
+ "filters" : buildFilters (options .ViewId , options . TimeFilter , options .Filters , options .KeyValueFilters ),
94
95
"groupBy" : buildGroupBy (options .GroupBy , outputFields , outputKeyValueFields ),
95
96
"limit" : options .Limit ,
96
97
"offset" : options .Offset ,
@@ -106,7 +107,7 @@ func (r *CloudCostManagementService) PerspectiveSummaryWithBudget(ctx context.Co
106
107
"variables" : variables ,
107
108
}
108
109
109
- slog . Debug ("PerspectiveSummaryWithBudget" , "Payload " , payload )
110
+ debugPayload ("PerspectiveSummaryWithBudget" , payload )
110
111
result := new (dto.CCMPerspectiveSummaryWithBudgetResponse )
111
112
err := r .Client .Post (ctx , path , nil , payload , & result )
112
113
if err != nil {
@@ -129,7 +130,7 @@ func (r *CloudCostManagementService) PerspectiveBudget(ctx context.Context, scop
129
130
"variables" : variables ,
130
131
}
131
132
132
- slog . Debug ("PerspectiveBudget" , "Payload " , payload )
133
+ debugPayload ("PerspectiveBudget" , payload )
133
134
result := new (dto.CCMPerspectiveBudgetResponse )
134
135
err := r .Client .Post (ctx , path , nil , payload , & result )
135
136
if err != nil {
@@ -151,7 +152,7 @@ func (r *CloudCostManagementService) GetCcmMetadata(ctx context.Context, scope d
151
152
"variables" : variables ,
152
153
}
153
154
154
- slog . Debug ("FetchCcmMetadata" , "Payload " , payload )
155
+ debugPayload ("FetchCcmMetadata" , payload )
155
156
result := new (dto.CCMMetadataResponse )
156
157
err := r .Client .Post (ctx , path , nil , payload , & result )
157
158
if err != nil {
@@ -160,13 +161,42 @@ func (r *CloudCostManagementService) GetCcmMetadata(ctx context.Context, scope d
160
161
return result , nil
161
162
}
162
163
163
- func buildFilters (timeFilters string , idFilters dto.CCMGraphQLFilters , keyValueFilters dto.CCMGraphQLKeyValueFilters ) ([]map [string ]any ) {
164
+ func (r * CloudCostManagementService ) PerspectiveRecommendations (ctx context.Context , scope dto.Scope , options * dto.CCMPerspectiveRecommendationsOptions ) (* dto.CCMPerspectiveRecommendationsResponse , error ) {
165
+ path := fmt .Sprintf (ccmPerspectiveGraphQLPath , options .AccountId , options .AccountId )
166
+
167
+ gqlQuery := ccmcommons .CCMPerspectiveRecommendationsQuery
168
+
169
+ variables := map [string ]any {
170
+ "filter" : map [string ]any {
171
+ "perspectiveFilters" : buildFilters (options .ViewId , options .TimeFilter , options .Filters , options .KeyValueFilters ),
172
+ "limit" : options .Limit ,
173
+ "offset" : options .Offset ,
174
+ "minSaving" : options .MinSaving ,
175
+ "recommendationStates" : options .RecommendationStates ,
176
+ },
177
+ }
178
+
179
+ payload := map [string ]any {
180
+ "query" : gqlQuery ,
181
+ "operationName" : "PerspectiveRecommendations" ,
182
+ "variables" : variables ,
183
+ }
184
+
185
+ debugPayload ("PerspectiveRecommendations" , payload )
186
+ result := new (dto.CCMPerspectiveRecommendationsResponse )
187
+ err := r .Client .Post (ctx , path , nil , payload , & result )
188
+ if err != nil {
189
+ return nil , fmt .Errorf ("failed to get perspective recommendations: %w" , err )
190
+ }
191
+ return result , nil
192
+ }
193
+
194
+ func buildFilters (viewId string ,timeFilters string , idFilters dto.CCMGraphQLFilters , keyValueFilters dto.CCMGraphQLKeyValueFilters ) ([]map [string ]any ) {
164
195
filters := []map [string ]any {}
165
196
viewFilter := []map [string ]any {
166
197
{
167
198
"viewMetadataFilter" : map [string ]any {
168
- //"viewId": options.ViewId,
169
- "viewId" : "VZf-WROOTyeczYa4FMkhYg" ,
199
+ "viewId" : viewId ,
170
200
"isPreview" : false ,
171
201
},
172
202
},
@@ -177,8 +207,6 @@ func buildFilters(timeFilters string, idFilters dto.CCMGraphQLFilters, keyValueF
177
207
filters = append (filters , buildFieldFilters (idFilters , outputFields )... )
178
208
filters = append (filters , buildKeyValueFieldFilters (keyValueFilters , outputKeyValueFields )... )
179
209
180
- slog .Debug ("PerspectiveGrid" , "FILTERS" , filters )
181
-
182
210
return filters
183
211
}
184
212
@@ -470,3 +498,18 @@ func buildGroupBy(input map[string]any, outputFields []map[string]string, output
470
498
}
471
499
return defaultGroupBy
472
500
}
501
+
502
+ func debugPayload (operation string , payload map [string ]any ) {
503
+ jsonPayload := mapToJSONString (payload )
504
+ slog .Debug ("-----------" , "----------" , "--------------" )
505
+ slog .Debug (operation , "Payload" , jsonPayload )
506
+ slog .Debug ("-----------" , "----------" , "--------------" )
507
+ }
508
+
509
+ func mapToJSONString (m map [string ]any ) (string ) {
510
+ b , err := json .MarshalIndent (m , "" , " " )
511
+ if err != nil {
512
+ return ""
513
+ }
514
+ return string (b )
515
+ }
0 commit comments