Skip to content

Commit d30f982

Browse files
authored
Merge pull request #1577 from powerkimhub/refactor/priceinfo-simple-api-and-memory-analysis-util-improve
[PriceInfo] Refactor PriceInfo simple mode and improve memory analysis utilities
2 parents b1dcb0d + a923ef1 commit d30f982

File tree

20 files changed

+213
-146
lines changed

20 files changed

+213
-146
lines changed

api-runtime/common-runtime/PriceInfoHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func ListProductFamily(connectionName string, regionName string) ([]string, erro
5959
return listProductFamily, nil
6060
}
6161

62-
func GetPriceInfo(connectionName string, productFamily string, regionName string, filterList []cres.KeyValue) (string, error) {
62+
func GetPriceInfo(connectionName string, productFamily string, regionName string, filterList []cres.KeyValue, simpleVMSpecInfo bool) (string, error) {
6363
cblog.Info("call GetPriceInfo()")
6464

6565
// check empty and trim user inputs
@@ -103,7 +103,7 @@ func GetPriceInfo(connectionName string, productFamily string, regionName string
103103
}
104104

105105
cspProductFamily := getProviderSpecificPFName(providerName, productFamily)
106-
priceInfo, err := handler.GetPriceInfo(cspProductFamily, regionName, filterList)
106+
priceInfo, err := handler.GetPriceInfo(cspProductFamily, regionName, filterList, simpleVMSpecInfo)
107107
if err != nil {
108108
cblog.Error(err)
109109
return "", err

api-runtime/rest-runtime/PriceInfoRest.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type PriceInfoResponse struct {
8282
// @Accept json
8383
// @Produce json
8484
// @Param RegionName path string true "The name of the Region to retrieve vm price information for"
85+
// @Param simple query bool false "Return simplified VM specification information (only VMSpecName). Default: false"
8586
// @Param PriceInfoRequest body PriceInfoRequest false "The request body containing additional filters for vm price information"
8687
// @Success 200 {object} PriceInfoResponse "VM Price Information Details"
8788
// @Failure 400 {object} SimpleMsg "Bad Request, possibly due to invalid query parameter"
@@ -102,9 +103,18 @@ func GetVMPriceInfo(c echo.Context) error {
102103
req.ConnectionName = c.QueryParam("ConnectionName")
103104
}
104105

106+
// Parse simple mode query parameter
107+
// If 'simple' parameter exists (regardless of value), enable simple mode
108+
simpleVMSpecInfo := false
109+
110+
if c.Request().URL.Query().Has("simple") {
111+
// If 'simple' parameter exists, enable simple mode regardless of value
112+
simpleVMSpecInfo = true
113+
}
114+
105115
// Call common-runtime API
106-
// result, err := cmrt.GetPriceInfo(req.ConnectionName, c.Param("ProductFamily"), c.Param("RegionName"), req.FilterList)
107-
result, err := cmrt.GetPriceInfo(req.ConnectionName, cres.RSTypeString(cres.VM), c.Param("RegionName"), req.FilterList)
116+
// result, err := cmrt.GetPriceInfo(req.ConnectionName, c.Param("ProductFamily"), c.Param("RegionName"), req.FilterList, simpleVMSpecInfo)
117+
result, err := cmrt.GetPriceInfo(req.ConnectionName, cres.RSTypeString(cres.VM), c.Param("RegionName"), req.FilterList, simpleVMSpecInfo)
108118
if err != nil {
109119
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
110120
}

api-runtime/rest-runtime/admin-web/AdminWeb-Common.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ func getResource_JsonByte(resourceName string, name string) ([]byte, error) {
179179
return resBody, err
180180
}
181181

182-
func getPriceInfoJsonString(connConfig string, resourceName string, productFamily string, regionName string, filter []cres.KeyValue, target interface{}) error {
183-
url := fmt.Sprintf("http://localhost:1024/spider/%s/%s/%s", resourceName, productFamily, regionName)
182+
func getPriceInfoJsonString(connConfig string, resourceName string, productFamily string, regionName string, filter []cres.KeyValue, simpleVMSpecInfo bool, target interface{}) error {
183+
url := fmt.Sprintf("http://localhost:1024/spider/%s/%s/%s?ConnectionName=%s", resourceName, productFamily, regionName, connConfig)
184+
185+
if simpleVMSpecInfo {
186+
url += "&simple=true"
187+
}
184188

185189
reqBody := struct {
186190
ConnectionName string `json:"ConnectionName"`

api-runtime/rest-runtime/admin-web/AdminWeb-PriceInfo-TableList.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,15 @@ func PriceInfoTableList(c echo.Context) error {
143143

144144
json.Unmarshal([]byte(c.QueryParam("filterlist")), &info)
145145

146-
// Handle simplemode parameter and set environment variable
146+
// Handle simplemode parameter
147147
simpleMode := c.QueryParam("simplemode")
148-
if simpleMode != "" {
149-
if simpleMode == "ON" || simpleMode == "OFF" {
150-
// Temporarily set the environment variable for this request
151-
originalMode := os.Getenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO")
152-
os.Setenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO", simpleMode)
153-
cblog.Infof("Set VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO to: %s", simpleMode)
154-
155-
// Restore original value after processing (defer)
156-
defer func() {
157-
os.Setenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO", originalMode)
158-
cblog.Infof("Restored VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO to: %s", originalMode)
159-
}()
160-
}
148+
simpleVMSpecInfo := false
149+
if simpleMode == "ON" {
150+
simpleVMSpecInfo = true
161151
}
162152

163153
var data cres.CloudPrice
164-
err := getPriceInfoJsonString(connConfig, "priceinfo", c.Param("ProductFamily"), c.Param("RegionName"), info.FilterList, &data)
154+
err := getPriceInfoJsonString(connConfig, "priceinfo", c.Param("ProductFamily"), c.Param("RegionName"), info.FilterList, simpleVMSpecInfo, &data)
165155
if err != nil {
166156
cblog.Error(err)
167157
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
@@ -173,10 +163,10 @@ func PriceInfoTableList(c echo.Context) error {
173163
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to save JSON file: "+err.Error())
174164
}
175165

176-
// Get current simple mode setting
177-
currentSimpleMode := os.Getenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO")
178-
if currentSimpleMode == "" {
179-
currentSimpleMode = "OFF" // default value
166+
// Set SimpleMode based on query parameter
167+
currentSimpleMode := "OFF" // default value
168+
if simpleVMSpecInfo {
169+
currentSimpleMode = "ON"
180170
}
181171

182172
tmplData := TemplateData{

api-runtime/rest-runtime/admin-web/html/priceinfo-tablelist-template.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ <h3>{{.Data.CloudName}}
243243
<td class="center-text">{{$.Data.RegionName}}/{{.ZoneName}}</td>
244244
<td>
245245
<ul>
246-
{{if .ProductInfo.VMSpecInfo}}
246+
{{if and (eq $.SimpleMode "ON") .ProductInfo.VMSpecName}}
247+
<!-- Simple Mode: VMSpecName only -->
248+
<li><strong>VMSpec: {{.ProductInfo.VMSpecName}}</strong></li>
249+
<li><em>Simple mode - detailed specs not available</em></li>
250+
{{else if .ProductInfo.VMSpecInfo}}
247251
<!-- Detailed Mode: Full VMSpecInfo -->
248252
<li><strong>VMSpec: {{.ProductInfo.VMSpecInfo.Name}}</strong></li>
249253
<li>VCPU: {{.ProductInfo.VMSpecInfo.VCpu.Count}}</li>
@@ -257,7 +261,7 @@ <h3>{{.Data.CloudName}}
257261
{{end}}
258262
{{end}}
259263
{{else if .ProductInfo.VMSpecName}}
260-
<!-- Simple Mode: VMSpecName only -->
264+
<!-- Fallback: VMSpecName only when VMSpecInfo is not available -->
261265
<li><strong>VMSpec: {{.ProductInfo.VMSpecName}}</strong></li>
262266
<li><em>Simple mode - detailed specs not available</em></li>
263267
{{else}}

api/docs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5522,6 +5522,12 @@ const docTemplate = `{
55225522
"in": "path",
55235523
"required": true
55245524
},
5525+
{
5526+
"type": "boolean",
5527+
"description": "Return simplified VM specification information (only VMSpecName). Default: false",
5528+
"name": "simple",
5529+
"in": "query"
5530+
},
55255531
{
55265532
"description": "The request body containing additional filters for vm price information",
55275533
"name": "PriceInfoRequest",

api/swagger.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5519,6 +5519,12 @@
55195519
"in": "path",
55205520
"required": true
55215521
},
5522+
{
5523+
"type": "boolean",
5524+
"description": "Return simplified VM specification information (only VMSpecName). Default: false",
5525+
"name": "simple",
5526+
"in": "query"
5527+
},
55225528
{
55235529
"description": "The request body containing additional filters for vm price information",
55245530
"name": "PriceInfoRequest",

api/swagger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7019,6 +7019,11 @@ paths:
70197019
name: RegionName
70207020
required: true
70217021
type: string
7022+
- description: 'Return simplified VM specification information (only VMSpecName).
7023+
Default: false'
7024+
in: query
7025+
name: simple
7026+
type: boolean
70227027
- description: The request body containing additional filters for vm price information
70237028
in: body
70247029
name: PriceInfoRequest

cloud-control-manager/cloud-driver/drivers/alibaba/resources/PriceInfoHandler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package resources
33
import (
44
"encoding/json"
55
"fmt"
6-
"os"
76
"reflect"
87
"strconv"
98
"strings"
@@ -114,7 +113,7 @@ func (priceInfoHandler *AlibabaPriceInfoHandler) ListProductFamily(regionName st
114113
return familyList, nil
115114
}
116115

117-
func (priceInfoHandler *AlibabaPriceInfoHandler) GetPriceInfo(productFamily string, regionName string, filterList []irs.KeyValue) (string, error) {
116+
func (priceInfoHandler *AlibabaPriceInfoHandler) GetPriceInfo(productFamily string, regionName string, filterList []irs.KeyValue, simpleVMSpecInfo bool) (string, error) {
118117
priceMap := make(map[string]irs.Price)
119118

120119
priceMutex := &sync.Mutex{}
@@ -283,9 +282,7 @@ func (priceInfoHandler *AlibabaPriceInfoHandler) GetPriceInfo(productFamily stri
283282

284283
productInfo.ProductId = "NA"
285284

286-
simpleMode := strings.ToUpper(os.Getenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO")) == "ON"
287-
288-
if simpleMode {
285+
if simpleVMSpecInfo {
289286
productInfo.VMSpecName = instanceType
290287
} else {
291288
var gpuInfo []irs.GpuInfo

cloud-control-manager/cloud-driver/drivers/aws/resources/PriceInfoHandler.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"os"
87
"strconv"
98
"strings"
109

@@ -67,7 +66,7 @@ func (priceInfoHandler *AwsPriceInfoHandler) ListProductFamily(regionName string
6766
return result, nil
6867
}
6968

70-
func (priceInfoHandler *AwsPriceInfoHandler) GetPriceInfo(productFamily string, regionName string, filterList []irs.KeyValue) (string, error) {
69+
func (priceInfoHandler *AwsPriceInfoHandler) GetPriceInfo(productFamily string, regionName string, filterList []irs.KeyValue, simpleVMSpecInfo bool) (string, error) {
7170
currentRegion := regionName
7271
if currentRegion == "" {
7372
currentRegion = priceInfoHandler.Region.Region
@@ -166,7 +165,7 @@ func (priceInfoHandler *AwsPriceInfoHandler) GetPriceInfo(productFamily string,
166165
if productFamilyVal != "Compute Instance" && productFamilyVal != "Compute Instance (bare metal)" {
167166
continue
168167
}
169-
productInfo, err := ExtractProductInfo(awsPrice, productFamilyVal)
168+
productInfo, err := ExtractProductInfo(awsPrice, productFamilyVal, simpleVMSpecInfo)
170169
if err != nil {
171170
cblogger.Error(err)
172171
continue
@@ -402,7 +401,7 @@ func setProductsInputRequestFilter(filterList []irs.KeyValue) ([]*pricing.Filter
402401
return requestFilters, nil
403402
}
404403

405-
func ExtractProductInfo(jsonValue aws.JSONValue, productFamily string) (irs.ProductInfo, error) {
404+
func ExtractProductInfo(jsonValue aws.JSONValue, productFamily string, simpleVMSpecInfo bool) (irs.ProductInfo, error) {
406405
var productInfo irs.ProductInfo
407406

408407
jsonString, err := json.MarshalIndent(jsonValue["product"].(map[string]interface{})["attributes"], "", " ")
@@ -412,7 +411,7 @@ func ExtractProductInfo(jsonValue aws.JSONValue, productFamily string) (irs.Prod
412411
}
413412
switch productFamily {
414413
case "Compute Instance", "Compute Instance (bare metal)":
415-
err := setVMspecInfo(&productInfo, string(jsonString))
414+
err := setVMspecInfo(&productInfo, string(jsonString), simpleVMSpecInfo)
416415
if err != nil {
417416
return productInfo, err
418417
}
@@ -438,7 +437,7 @@ func ExtractProductInfo(jsonValue aws.JSONValue, productFamily string) (irs.Prod
438437
return productInfo, nil
439438
}
440439

441-
func setVMspecInfo(productInfo *resources.ProductInfo, jsonValueString string) error {
440+
func setVMspecInfo(productInfo *resources.ProductInfo, jsonValueString string, simpleVMSpecInfo bool) error {
442441
var jsonData map[string]string
443442
if err := json.Unmarshal([]byte(jsonValueString), &jsonData); err != nil {
444443
return fmt.Errorf("failed to parse JSON: %w", err)
@@ -449,9 +448,7 @@ func setVMspecInfo(productInfo *resources.ProductInfo, jsonValueString string) e
449448
return errors.New("missing required field: instanceType")
450449
}
451450

452-
simpleMode := strings.ToUpper(os.Getenv("VMSPECINFO_SIMPLE_MODE_IN_PRICEINFO")) == "ON"
453-
454-
if simpleMode {
451+
if simpleVMSpecInfo {
455452
productInfo.VMSpecName = instanceType
456453
return nil
457454
}

0 commit comments

Comments
 (0)