Chart calculation : GWM
This commit is contained in:
parent
bbe9e2db9c
commit
4f23722e3e
55904
kpi_files/divorces.json
55904
kpi_files/divorces.json
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
2312
kpi_files/hotel_occupancy_rate1.json
Normal file
2312
kpi_files/hotel_occupancy_rate1.json
Normal file
File diff suppressed because it is too large
Load Diff
135
main.go
135
main.go
@ -605,64 +605,22 @@ func main() {
|
||||
cardLogo,
|
||||
)
|
||||
}
|
||||
|
||||
// log.Println("Gowtham", chartData)
|
||||
cResRaw := utils.ProcessChartData(chartData, languageSourceConverted, langKey)
|
||||
|
||||
// Convert cResRaw ([]map[string]interface{}) to []utils.Observation
|
||||
var cRes []utils.Observation
|
||||
cResJSON, err := json.Marshal(cResRaw)
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding chart data:", err)
|
||||
}
|
||||
err = json.Unmarshal(cResJSON, &cRes)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding chart data:", err)
|
||||
}
|
||||
|
||||
// card data calculation
|
||||
var cardResults []map[string]string
|
||||
if record.GetString("is_chart") == "false" {
|
||||
|
||||
// Extract `card_type` JSON from `record.Get("card_type")`
|
||||
requestData := record.Get("card_type_json")
|
||||
requestJSON, err := json.Marshal(requestData) // Convert interface{} to JSON string
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding request data:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Decode JSON into an array of `utils.Request`
|
||||
var requests []utils.Request
|
||||
err = json.Unmarshal(requestJSON, &requests)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding request data:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Process each request and store results
|
||||
for _, req := range requests {
|
||||
result, err := utils.Calculate(cRes, req)
|
||||
if err != nil {
|
||||
fmt.Println("Error processing data:", err)
|
||||
continue // Skip this request and move to the next
|
||||
}
|
||||
cardResults = append(cardResults, result)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
log.Println("Gowtham", "loop")
|
||||
//responce data structure
|
||||
chartHeadingKey := "chart_heading_" + language
|
||||
tabHeadingKey := "tab_heading_" + language
|
||||
// Correct conditional assignment for "response"
|
||||
recordResult := map[string]interface{}{
|
||||
"url": apiURL,
|
||||
"dataset": record.GetString("dataset"),
|
||||
"main_id": record.GetString("main_id"),
|
||||
"sub_id": record.GetString("sub_id"),
|
||||
"kpi": record.GetString("kpi"),
|
||||
"is_chart": record.GetString("is_chart"),
|
||||
"chart_type": record.GetString("chart_type"),
|
||||
// "response": record.GetString("is_chart") = true cRes : cardResults,
|
||||
"url": apiURL,
|
||||
"dataset": record.GetString("dataset"),
|
||||
"main_id": record.GetString("main_id"),
|
||||
"sub_id": record.GetString("sub_id"),
|
||||
"kpi": record.GetString("kpi"),
|
||||
"is_chart": record.GetString("is_chart"),
|
||||
"chart_type": record.GetString("chart_type"),
|
||||
"response": cResRaw,
|
||||
"group_by": record.GetString("group_by"),
|
||||
"chart_heading": record.GetString(chartHeadingKey),
|
||||
"tab_heading": record.GetString(tabHeadingKey),
|
||||
@ -673,10 +631,75 @@ func main() {
|
||||
"tab_order": record.GetString("tab_order"),
|
||||
"chart_type_json": record.Get("chart_type_json"),
|
||||
}
|
||||
if record.GetString("is_chart") == "true" {
|
||||
recordResult["response"] = cResRaw
|
||||
} else {
|
||||
|
||||
// card data calculation
|
||||
// Convert cResRaw ([]map[string]interface{}) to []utils.Observation
|
||||
var cRes []utils.Observation
|
||||
cResJSON, err := json.Marshal(cResRaw)
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding chart data:", err)
|
||||
}
|
||||
err = json.Unmarshal(cResJSON, &cRes)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding chart data:", err)
|
||||
}
|
||||
var cardResults []map[string]string
|
||||
if record.GetString("is_chart") == "false" {
|
||||
|
||||
// Extract `card_type` JSON from `record.Get("card_type")`
|
||||
requestData := record.Get("card_type_json")
|
||||
requestJSON, err := json.Marshal(requestData) // Convert interface{} to JSON string
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding request data:", err)
|
||||
}
|
||||
|
||||
// Decode JSON into an array of `utils.Request`
|
||||
var requests []utils.CardCalculationRequest
|
||||
err = json.Unmarshal(requestJSON, &requests)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding request data:", err)
|
||||
}
|
||||
|
||||
// Process each request and store results
|
||||
for _, req := range requests {
|
||||
result, err := utils.CardCalculation(cRes, req)
|
||||
if err != nil {
|
||||
fmt.Println("Error processing data:", err)
|
||||
continue // Skip this request and move to the next
|
||||
}
|
||||
cardResults = append(cardResults, result)
|
||||
}
|
||||
|
||||
recordResult["response"] = cardResults
|
||||
|
||||
} else {
|
||||
|
||||
if record.GetString("custom_calculation") == "false" {
|
||||
recordResult["response"] = cResRaw
|
||||
} else {
|
||||
|
||||
customCalculationReq := record.Get("chart_custom_calculation_json")
|
||||
|
||||
var chartCustomCalculationRequest utils.ChartCalculationRequest
|
||||
|
||||
jsonData, err := json.Marshal(customCalculationReq)
|
||||
if err != nil {
|
||||
log.Println("Error marshaling to JSON:", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonData, &chartCustomCalculationRequest)
|
||||
if err != nil {
|
||||
log.Println("Error unmarshalling JSON:", err)
|
||||
}
|
||||
customResult, err := utils.CalculateGroupedSum(cRes, chartCustomCalculationRequest)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
}
|
||||
|
||||
recordResult["response"] = customResult
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Add the result to the aggregated response
|
||||
|
||||
BIN
pb_data/data.db
BIN
pb_data/data.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,10 +1,10 @@
|
||||
// utils/calculations.go
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
@ -17,7 +17,7 @@ type Observation struct {
|
||||
} `json:"ObsValue"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
type CardCalculationRequest struct {
|
||||
Formula string `json:"formula"`
|
||||
RecordCount string `json:"record_count"`
|
||||
RecodeIndex string `json:"record_index"`
|
||||
@ -26,7 +26,7 @@ type Request struct {
|
||||
Conversion string `json:"conversion"`
|
||||
}
|
||||
|
||||
func Calculate(data []Observation, req Request) (map[string]string, error) {
|
||||
func CardCalculation(data []Observation, req CardCalculationRequest) (map[string]string, error) {
|
||||
// Sort the data in descending order based on TimePeriod
|
||||
sort.Slice(data, func(i, j int) bool {
|
||||
return data[i].ObsKey["TIME_PERIOD"] > data[j].ObsKey["TIME_PERIOD"]
|
||||
@ -198,3 +198,84 @@ func extractRecordKeyValue(data []Observation, key string, formula string, targe
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
|
||||
// ChartCalculationRequest struct for request parameters
|
||||
type ChartCalculationRequest struct {
|
||||
GroupByAndSum string `json:"groupby_and_sum"`
|
||||
GetTop string `json:"get_top"`
|
||||
Conversion string `json:"conversion"`
|
||||
}
|
||||
|
||||
// CalculateGroupedSum processes the observations based on the given request
|
||||
func CalculateGroupedSum(observations []Observation, request ChartCalculationRequest) ([]Observation, error) {
|
||||
// Map to store the summed values
|
||||
summedData := make(map[string]float64)
|
||||
|
||||
// Iterate through observations and sum values based on GroupBy key
|
||||
for _, obs := range observations {
|
||||
groupKey, exists := obs.ObsKey[request.GroupByAndSum]
|
||||
if !exists {
|
||||
continue // Skip if the key does not exist in ObsKey
|
||||
}
|
||||
|
||||
value, err := obs.ObsValue.Value.Float64()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid number format: %v", err)
|
||||
}
|
||||
|
||||
summedData[groupKey] += value
|
||||
}
|
||||
|
||||
// Convert map to slice of Observation
|
||||
var result []Observation
|
||||
for key, sum := range summedData {
|
||||
result = append(result, Observation{
|
||||
ObsKey: map[string]string{
|
||||
request.GroupByAndSum: key,
|
||||
},
|
||||
ObsValue: struct {
|
||||
Value json.Number `json:"Value"`
|
||||
}{
|
||||
// Value: json.Number(fmt.Sprintf("%.0f", sum)),
|
||||
Value: json.Number(strconv.FormatFloat(convertValue(sum, request.Conversion), 'f', 6, 64)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Sort by summed values in descending order
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
val1, _ := result[i].ObsValue.Value.Float64()
|
||||
val2, _ := result[j].ObsValue.Value.Float64()
|
||||
return val1 > val2 // Sort descending
|
||||
})
|
||||
|
||||
// Get the top N values
|
||||
topN, err := strconv.Atoi(request.GetTop)
|
||||
if err != nil || topN <= 0 || topN > len(result) {
|
||||
topN = len(result) // If invalid input, return all
|
||||
}
|
||||
|
||||
return result[:topN], nil
|
||||
}
|
||||
|
||||
// Convert values based on conversion type
|
||||
func convertValue(value float64, conversion string) float64 {
|
||||
switch conversion {
|
||||
case "M": // Million
|
||||
return round(value/1000000, 2)
|
||||
case "T": // Trillion
|
||||
return round(value/1000000000000, 2)
|
||||
case "K": // Thousand
|
||||
return round(value/1000, 2)
|
||||
default: // No conversion
|
||||
return round(value, 2)
|
||||
}
|
||||
}
|
||||
|
||||
// Function to round a float to 2 decimal places
|
||||
func round(val float64, precision int) float64 {
|
||||
p := math.Pow(10, float64(precision))
|
||||
return math.Round(val*p) / p
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user