2022-10-06 20:13:30 +05:30
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2023-10-11 12:44:08 +00:00
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/ee/query-service/constants"
|
|
|
|
|
"github.com/SigNoz/signoz/ee/query-service/model"
|
2022-10-06 20:13:30 +05:30
|
|
|
)
|
|
|
|
|
|
2024-03-12 22:45:31 +05:30
|
|
|
type DayWiseBreakdown struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Breakdown []DayWiseData `json:"breakdown"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DayWiseData struct {
|
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
|
Count float64 `json:"count"`
|
|
|
|
|
Size float64 `json:"size"`
|
|
|
|
|
UnitPrice float64 `json:"unitPrice"`
|
|
|
|
|
Quantity float64 `json:"quantity"`
|
|
|
|
|
Total float64 `json:"total"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 12:44:08 +00:00
|
|
|
type tierBreakdown struct {
|
|
|
|
|
UnitPrice float64 `json:"unitPrice"`
|
2023-10-19 08:30:34 +00:00
|
|
|
Quantity float64 `json:"quantity"`
|
2023-10-11 12:44:08 +00:00
|
|
|
TierStart int64 `json:"tierStart"`
|
|
|
|
|
TierEnd int64 `json:"tierEnd"`
|
|
|
|
|
TierCost float64 `json:"tierCost"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type usageResponse struct {
|
2024-03-12 22:45:31 +05:30
|
|
|
Type string `json:"type"`
|
|
|
|
|
Unit string `json:"unit"`
|
|
|
|
|
Tiers []tierBreakdown `json:"tiers"`
|
|
|
|
|
DayWiseBreakdown DayWiseBreakdown `json:"dayWiseBreakdown"`
|
2023-10-11 12:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type details struct {
|
|
|
|
|
Total float64 `json:"total"`
|
|
|
|
|
Breakdown []usageResponse `json:"breakdown"`
|
|
|
|
|
BaseFee float64 `json:"baseFee"`
|
2023-10-16 23:57:44 +05:30
|
|
|
BillTotal float64 `json:"billTotal"`
|
2023-10-11 12:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type billingDetails struct {
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
Data struct {
|
|
|
|
|
BillingPeriodStart int64 `json:"billingPeriodStart"`
|
|
|
|
|
BillingPeriodEnd int64 `json:"billingPeriodEnd"`
|
|
|
|
|
Details details `json:"details"`
|
|
|
|
|
Discount float64 `json:"discount"`
|
2024-01-29 00:42:19 +05:30
|
|
|
SubscriptionStatus string `json:"subscriptionStatus"`
|
2023-10-11 12:44:08 +00:00
|
|
|
} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ah *APIHandler) getBilling(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
licenseKey := r.URL.Query().Get("licenseKey")
|
|
|
|
|
|
|
|
|
|
if licenseKey == "" {
|
|
|
|
|
RespondError(w, model.BadRequest(fmt.Errorf("license key is required")), nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
billingURL := fmt.Sprintf("%s/usage?licenseKey=%s", constants.LicenseSignozIo, licenseKey)
|
|
|
|
|
|
|
|
|
|
hClient := &http.Client{}
|
|
|
|
|
req, err := http.NewRequest("GET", billingURL, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, model.InternalError(err), nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req.Header.Add("X-SigNoz-SecretKey", constants.LicenseAPIKey)
|
|
|
|
|
billingResp, err := hClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
RespondError(w, model.InternalError(err), nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// decode response body
|
|
|
|
|
var billingResponse billingDetails
|
|
|
|
|
if err := json.NewDecoder(billingResp.Body).Decode(&billingResponse); err != nil {
|
|
|
|
|
RespondError(w, model.InternalError(err), nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(srikanthccv):Fetch the current day usage and add it to the response
|
|
|
|
|
ah.Respond(w, billingResponse.Data)
|
|
|
|
|
}
|