Unset key value from response : GWM
This commit is contained in:
parent
0fb19bb4e2
commit
a67071c873
95
main.go
95
main.go
@ -333,6 +333,29 @@ func TranslateFilters(app *pocketbase.PocketBase, dataset string, filterGroups [
|
|||||||
return filterGroups, nil
|
return filterGroups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generic function to filter JSON data dynamically based on key-value pairs
|
||||||
|
func filterDynamicChartData(chartData []map[string]interface{}, keyToFilter, valueToExclude string) []map[string]interface{} {
|
||||||
|
var filteredData []map[string]interface{}
|
||||||
|
|
||||||
|
for _, item := range chartData {
|
||||||
|
obsKey, ok := item["ObsKey"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
// Skip items without "ObsKey" or malformed data
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the key exists and matches the value to exclude
|
||||||
|
if obsKey[keyToFilter] == valueToExclude {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add item to filtered data
|
||||||
|
filteredData = append(filteredData, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredData
|
||||||
|
}
|
||||||
|
|
||||||
var app *pocketbase.PocketBase
|
var app *pocketbase.PocketBase
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -396,8 +419,6 @@ func main() {
|
|||||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for the given dataset"})
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for the given dataset"})
|
||||||
}
|
}
|
||||||
|
|
||||||
var aggregatedResponse []map[string]interface{}
|
|
||||||
|
|
||||||
param := dbx.NewExp("dataset = {:dataset}", dbx.Params{"dataset": dataset})
|
param := dbx.NewExp("dataset = {:dataset}", dbx.Params{"dataset": dataset})
|
||||||
languageSource, err := app.Dao().FindRecordsByExpr("charts_variables", param)
|
languageSource, err := app.Dao().FindRecordsByExpr("charts_variables", param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -409,31 +430,8 @@ func main() {
|
|||||||
languageSourceConverted = append(languageSourceConverted, record.SchemaData())
|
languageSourceConverted = append(languageSourceConverted, record.SchemaData())
|
||||||
}
|
}
|
||||||
|
|
||||||
//get all filter based of kpi
|
//get chart and card data
|
||||||
var dataSetKpis []struct {
|
var aggregatedResponse []map[string]interface{}
|
||||||
Kpi string `db:"kpi" json:"kpi"`
|
|
||||||
}
|
|
||||||
err = app.DB().Select("kpi").From("charts").Where(dbx.HashExp{"dataset": dataset}).GroupBy("kpi").All(&dataSetKpis)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to get kpi groupby data: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
aggregatedFilterResponse := make(map[string]interface{})
|
|
||||||
for _, dataSetKpi := range dataSetKpis {
|
|
||||||
|
|
||||||
kpi := dataSetKpi.Kpi
|
|
||||||
filterData, err := fetchfilterJSON(kpi)
|
|
||||||
|
|
||||||
// Translate filter data using the desired dataset and language key
|
|
||||||
fRes, err := TranslateFilters(app, dataset, filterData, "value_en")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error translating filters: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
aggregatedFilterResponse[kpi] = fRes
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
// Extract the URL for each record
|
// Extract the URL for each record
|
||||||
apiURL := record.GetString("url")
|
apiURL := record.GetString("url")
|
||||||
@ -460,6 +458,21 @@ func main() {
|
|||||||
log.Printf("Failed to read JSON for KPI '%s': %v", file_name, err)
|
log.Printf("Failed to read JSON for KPI '%s': %v", file_name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve the unset key and value
|
||||||
|
keyToFilter := record.GetString("unset_key")
|
||||||
|
valueToExclude := record.GetString("unset_value")
|
||||||
|
|
||||||
|
// Check if both the key and value are empty
|
||||||
|
if keyToFilter == "" || valueToExclude == "" {
|
||||||
|
fmt.Println("unset_key or unset_value is empty. Skipping processing.")
|
||||||
|
} else {
|
||||||
|
// Log the extracted key and value for debugging purposes
|
||||||
|
fmt.Printf("Filtering with Key: %s, Value: %s\n", keyToFilter, valueToExclude)
|
||||||
|
|
||||||
|
// Filter the data dynamically and update chartData
|
||||||
|
chartData = filterDynamicChartData(chartData, keyToFilter, valueToExclude)
|
||||||
|
}
|
||||||
|
|
||||||
langKey := "value_en"
|
langKey := "value_en"
|
||||||
cRes := utils.ProcessChartData(chartData, languageSourceConverted, langKey)
|
cRes := utils.ProcessChartData(chartData, languageSourceConverted, langKey)
|
||||||
|
|
||||||
@ -475,13 +488,41 @@ func main() {
|
|||||||
"response": cRes,
|
"response": cRes,
|
||||||
"group_by": record.GetString("group_by"),
|
"group_by": record.GetString("group_by"),
|
||||||
"chart_heading": record.GetString("chart_heading"),
|
"chart_heading": record.GetString("chart_heading"),
|
||||||
|
"unset_value": record.Get("unset_value"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the result to the aggregated response
|
// Add the result to the aggregated response
|
||||||
aggregatedResponse = append(aggregatedResponse, recordResult)
|
aggregatedResponse = append(aggregatedResponse, recordResult)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get all filter based on kpi
|
||||||
|
var dataSetKpis []struct {
|
||||||
|
Kpi string `db:"kpi" json:"kpi"`
|
||||||
|
}
|
||||||
|
err = app.DB().Select("kpi").From("charts").Where(dbx.HashExp{"dataset": dataset}).GroupBy("kpi").All(&dataSetKpis)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to get kpi groupby data: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
aggregatedFilterResponse := make(map[string]interface{})
|
||||||
|
for _, dataSetKpi := range dataSetKpis {
|
||||||
|
|
||||||
|
kpi := dataSetKpi.Kpi
|
||||||
|
filterData, err := fetchfilterJSON(kpi)
|
||||||
|
|
||||||
|
// Translate filter data using the desired dataset and language key
|
||||||
|
fRes, err := TranslateFilters(app, dataset, filterData, "value_en")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error translating filters: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
aggregatedFilterResponse[kpi] = fRes
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
temp, err := fetchfilterJSON(dataset)
|
temp, err := fetchfilterJSON(dataset)
|
||||||
|
|
||||||
// Create the response structure
|
// Create the response structure
|
||||||
response := map[string]interface{}{
|
response := map[string]interface{}{
|
||||||
"filter_data": temp,
|
"filter_data": temp,
|
||||||
|
|||||||
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.
Loading…
Reference in New Issue
Block a user