Group by and sum calculation for card : GWM

This commit is contained in:
Gowtham M 2025-02-24 14:37:23 +05:30
parent 7ca84f775d
commit a99b53873b
4 changed files with 54 additions and 12 deletions

View File

@ -632,6 +632,7 @@ func main() {
"card_type_json": record.Get("card_type_json"),
"tab_order": record.GetString("tab_order"),
"chart_type_json": record.Get("chart_type_json"),
"card_full_length": record.Get("card_full_length"),
}
// card data calculation

Binary file not shown.

Binary file not shown.

View File

@ -19,14 +19,20 @@ type Observation struct {
}
// ------------------------------------------------------------ card calculation---------------------------------------------------
type GroupByAndSum struct {
GroupByAndSum string `json:"groupby_and_sum"`
GetTop int `json:"get_top"`
Conversion int `json:"conversion"`
}
type CardCalculationRequest struct {
Formula string `json:"formula"`
RecordCount string `json:"record_count"`
RecodeIndex string `json:"record_index"`
RecordKey string `json:"record_key"`
AdditionalRecordKey string `json:"additional_record_key"`
NumberFormat string `json:"number_format"`
Conversion string `json:"conversion"`
Formula string `json:"formula"`
RecordCount string `json:"record_count"`
RecodeIndex string `json:"record_index"`
RecordKey string `json:"record_key"`
AdditionalRecordKey string `json:"additional_record_key"`
NumberFormat string `json:"number_format"`
Conversion string `json:"conversion"`
GroupByAndSum GroupByAndSum `json:"groupby_and_sum"`
}
func CardCalculation(data []Observation, req CardCalculationRequest) (map[string]string, error) {
@ -72,16 +78,50 @@ func CardCalculation(data []Observation, req CardCalculationRequest) (map[string
result = lowest(selectedData)
case "different":
result = difference(selectedData)
case "groupby_and_sum":
customCalculationReq := req.GroupByAndSum
var chartCustomCalculationRequest ChartCalculationRequest
jsonData, err := json.Marshal(customCalculationReq)
if err != nil {
fmt.Println("Error:", err)
}
err = json.Unmarshal(jsonData, &chartCustomCalculationRequest)
if err != nil {
fmt.Println("Error:", err)
}
selectedData, err = CalculateGroupedSum(selectedData, chartCustomCalculationRequest)
if err != nil {
fmt.Println("Error:", err)
}
if len(selectedData) > 0 {
value, err := selectedData[0].ObsValue.Value.Float64()
if err == nil {
result = value
} else {
fmt.Println("Error converting first observation value to float64:", err)
}
} else {
fmt.Println("No observations returned from CalculateGroupedSum")
}
default:
return nil, errors.New("invalid formula")
}
fmt.Println("Selected Data :\n", (selectedData))
fmt.Println("RecordKey Data :\n", (req.RecordKey))
convertedValue := applyConversion(result, req.Conversion, req.NumberFormat)
fmt.Println("AdditionalRecordKey:", req.AdditionalRecordKey)
displayValue := extractRecordKeyValue(selectedData, req.RecordKey, req.AdditionalRecordKey, req.Formula, result)
color := ""
if req.Formula == "different" {
if req.Formula == "different" || req.Conversion == "%" {
// Remove the percentage sign if present
convertedValueStr := strings.TrimSuffix(convertedValue, "%")
@ -264,15 +304,15 @@ func extractRecordKeyValue(data []Observation, key string, additionalkey string,
}
switch formula {
case "highest", "lowest", "none":
case "highest", "lowest", "none", "groupby_and_sum":
for _, obs := range data {
value, err := obs.ObsValue.Value.Float64()
if err == nil && value == targetValue {
if val, exists := obs.ObsKey[key]; exists {
fmt.Println("additionalkey:", additionalkey)
// Check if additionalkey exists in obs.ObsKey
if val2, exists2 := obs.ObsKey[additionalkey]; exists2 {
fmt.Println("val2:", val2)
return val + " - " + val2
} else {
return val
@ -309,6 +349,7 @@ type ChartCalculationRequest struct {
// 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)