Skip to content

Commit

Permalink
fix time cost map api
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 committed Aug 7, 2024
1 parent cd7837b commit 0777908
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 31 deletions.
8 changes: 4 additions & 4 deletions service/account/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GetProperties(c *gin.Context) {
// @Tags ConsumptionAmount
// @Accept json
// @Produce json
// @Param request body helper.UserBaseReq true "User consumption amount request"
// @Param request body helper.ConsumptionRecordReq true "User consumption amount request"
// @Success 200 {object} map[string]interface{} "successfully retrieved user consumption amount"
// @Failure 400 {object} map[string]interface{} "failed to parse user consumption amount request"
// @Failure 401 {object} map[string]interface{} "authenticate error"
Expand Down Expand Up @@ -228,14 +228,14 @@ type CostsResultData struct {
// @Tags Costs
// @Accept json
// @Produce json
// @Param request body helper.UserBaseReq true "User costs amount request"
// @Param request body helper.ConsumptionRecordReq true "User costs amount request"
// @Success 200 {object} map[string]interface{} "successfully retrieved user costs"
// @Failure 400 {object} map[string]interface{} "failed to parse user hour costs amount request"
// @Failure 401 {object} map[string]interface{} "authenticate error"
// @Failure 500 {object} map[string]interface{} "failed to get user costs"
// @Router /account/v1alpha1/costs [post]
func GetCosts(c *gin.Context) {
req, err := helper.ParseUserBaseReq(c)
req, err := helper.ParseConsumptionRecordReq(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user hour costs amount request: %v", err)})
return
Expand All @@ -244,7 +244,7 @@ func GetCosts(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)})
return
}
costs, err := dao.DBClient.GetCosts(req.Auth.Owner, req.TimeRange.StartTime, req.TimeRange.EndTime)
costs, err := dao.DBClient.GetCosts(*req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to get cost : %v", err)})
return
Expand Down
75 changes: 54 additions & 21 deletions service/account/dao/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
type Interface interface {
GetBillingHistoryNamespaceList(req *helper.NamespaceBillingHistoryReq) ([][]string, error)
GetProperties() ([]common.PropertyQuery, error)
GetCosts(user string, startTime, endTime time.Time) (common.TimeCostsMap, error)
GetCosts(req helper.ConsumptionRecordReq) (common.TimeCostsMap, error)
GetAppCosts(req *helper.AppCostsReq) (*common.AppCosts, error)
GetAppCostTimeRange(req helper.GetCostAppListReq) (helper.TimeRange, error)
GetCostOverview(req helper.GetCostAppListReq) (helper.CostOverviewResp, error)
Expand Down Expand Up @@ -166,31 +166,64 @@ func (m *MongoDB) GetProperties() ([]common.PropertyQuery, error) {
return propertiesQuery, nil
}

func (m *MongoDB) GetCosts(user string, startTime, endTime time.Time) (common.TimeCostsMap, error) {
filter := bson.M{
"type": 0,
"time": bson.M{
"$gte": startTime,
"$lte": endTime,
},
"owner": user,
func (m *MongoDB) GetCosts(req helper.ConsumptionRecordReq) (common.TimeCostsMap, error) {
owner, startTime, endTime := req.Owner, req.TimeRange.StartTime, req.TimeRange.EndTime
appType, appName := req.AppType, req.AppName

timeMatchValue := bson.D{
primitive.E{Key: "$gte", Value: startTime},
primitive.E{Key: "$lte", Value: endTime},
}
cursor, err := m.getBillingCollection().Find(context.Background(), filter, options.Find().SetSort(bson.M{"time": 1}))
if err != nil {
return nil, fmt.Errorf("failed to get billing collection: %v", err)
matchValue := bson.D{
primitive.E{Key: "time", Value: timeMatchValue},
primitive.E{Key: "owner", Value: owner},
primitive.E{Key: "type", Value: 0},
}
defer cursor.Close(context.Background())
var (
accountBalanceList []struct {
Time time.Time `bson:"time"`
Amount int64 `bson:"amount"`
}

if appType != "" {
matchValue = append(matchValue, primitive.E{Key: "app_type", Value: resources.AppType[strings.ToUpper(appType)]})
}
if req.Namespace != "" {
matchValue = append(matchValue, primitive.E{Key: "namespace", Value: req.Namespace})
}

pipeline := bson.A{
bson.D{{Key: "$match", Value: matchValue}},
}

project := bson.D{
primitive.E{Key: "time", Value: 1},
primitive.E{Key: "amount", Value: 1},
}
if appType != "" && appName != "" && appType != resources.AppStore {
pipeline = append(pipeline,
bson.D{{Key: "$unwind", Value: "$app_costs"}},
bson.D{{Key: "$match", Value: bson.D{{Key: "app_costs.name", Value: appName}}}},
)
project[2] = primitive.E{Key: "amount", Value: "$app_costs.amount"}
}

pipeline = append(pipeline,
bson.D{{Key: "$sort", Value: bson.D{{Key: "time", Value: 1}}}},
bson.D{{Key: "$project", Value: project}},
)
err = cursor.All(context.Background(), &accountBalanceList)

cursor, err := m.getBillingCollection().Aggregate(context.Background(), pipeline)
if err != nil {
return nil, fmt.Errorf("failed to decode all billing record: %w", err)
return nil, fmt.Errorf("failed to aggregate billing collection: %v", err)
}
defer cursor.Close(context.Background())

var accountBalanceList []struct {
Time time.Time `bson:"time"`
Amount int64 `bson:"amount"`
}

if err := cursor.All(context.Background(), &accountBalanceList); err != nil {
return nil, fmt.Errorf("failed to decode all billing records: %w", err)
}
var costsMap = make(common.TimeCostsMap, len(accountBalanceList))

costsMap := make(common.TimeCostsMap, len(accountBalanceList))
for i := range accountBalanceList {
costsMap[i] = append(costsMap[i], accountBalanceList[i].Time.Unix())
costsMap[i] = append(costsMap[i], strconv.FormatInt(accountBalanceList[i].Amount, 10))
Expand Down
43 changes: 41 additions & 2 deletions service/account/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/helper.UserBaseReq"
"$ref": "#/definitions/helper.ConsumptionRecordReq"
}
}
],
Expand Down Expand Up @@ -508,7 +508,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/helper.UserBaseReq"
"$ref": "#/definitions/helper.ConsumptionRecordReq"
}
}
],
Expand Down Expand Up @@ -1095,6 +1095,45 @@ const docTemplate = `{
}
}
},
"helper.ConsumptionRecordReq": {
"type": "object",
"properties": {
"appName": {
"description": "@Summary App Name\n@Description App Name",
"type": "string",
"example": "app"
},
"appType": {
"description": "@Summary App type\n@Description App type",
"type": "string",
"example": "app"
},
"endTime": {
"type": "string",
"example": "2021-12-01T00:00:00Z"
},
"kubeConfig": {
"type": "string"
},
"namespace": {
"description": "@Summary Namespace\n@Description Namespace",
"type": "string",
"example": "ns-admin"
},
"owner": {
"type": "string",
"example": "admin"
},
"startTime": {
"type": "string",
"example": "2021-01-01T00:00:00Z"
},
"userID": {
"type": "string",
"example": "admin"
}
}
},
"helper.CostApp": {
"type": "object",
"properties": {
Expand Down
43 changes: 41 additions & 2 deletions service/account/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/helper.UserBaseReq"
"$ref": "#/definitions/helper.ConsumptionRecordReq"
}
}
],
Expand Down Expand Up @@ -501,7 +501,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/helper.UserBaseReq"
"$ref": "#/definitions/helper.ConsumptionRecordReq"
}
}
],
Expand Down Expand Up @@ -1088,6 +1088,45 @@
}
}
},
"helper.ConsumptionRecordReq": {
"type": "object",
"properties": {
"appName": {
"description": "@Summary App Name\n@Description App Name",
"type": "string",
"example": "app"
},
"appType": {
"description": "@Summary App type\n@Description App type",
"type": "string",
"example": "app"
},
"endTime": {
"type": "string",
"example": "2021-12-01T00:00:00Z"
},
"kubeConfig": {
"type": "string"
},
"namespace": {
"description": "@Summary Namespace\n@Description Namespace",
"type": "string",
"example": "ns-admin"
},
"owner": {
"type": "string",
"example": "admin"
},
"startTime": {
"type": "string",
"example": "2021-01-01T00:00:00Z"
},
"userID": {
"type": "string",
"example": "admin"
}
}
},
"helper.CostApp": {
"type": "object",
"properties": {
Expand Down
39 changes: 37 additions & 2 deletions service/account/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ definitions:
example: admin
type: string
type: object
helper.ConsumptionRecordReq:
properties:
appName:
description: |-
@Summary App Name
@Description App Name
example: app
type: string
appType:
description: |-
@Summary App type
@Description App type
example: app
type: string
endTime:
example: "2021-12-01T00:00:00Z"
type: string
kubeConfig:
type: string
namespace:
description: |-
@Summary Namespace
@Description Namespace
example: ns-admin
type: string
owner:
example: admin
type: string
startTime:
example: "2021-01-01T00:00:00Z"
type: string
userID:
example: admin
type: string
type: object
helper.CostApp:
properties:
appName:
Expand Down Expand Up @@ -636,7 +671,7 @@ paths:
name: request
required: true
schema:
$ref: '#/definitions/helper.UserBaseReq'
$ref: '#/definitions/helper.ConsumptionRecordReq'
produces:
- application/json
responses:
Expand Down Expand Up @@ -712,7 +747,7 @@ paths:
name: request
required: true
schema:
$ref: '#/definitions/helper.UserBaseReq'
$ref: '#/definitions/helper.ConsumptionRecordReq'
produces:
- application/json
responses:
Expand Down

0 comments on commit 0777908

Please sign in to comment.