utils
This commit is contained in:
parent
de6eb8b24f
commit
285d2051f1
604
main.go
604
main.go
@ -11,6 +11,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"pocketbase/utils"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -337,6 +339,311 @@ func main() {
|
|||||||
|
|
||||||
app = pocketbase.New()
|
app = pocketbase.New()
|
||||||
|
|
||||||
|
//app.Router.GET("/verify-email", verifyEmailHandler)
|
||||||
|
|
||||||
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
|
|
||||||
|
e.Router.GET("/api/custom/apicalltest", func(c echo.Context) error {
|
||||||
|
// Get the database instance
|
||||||
|
db := app.Dao().DB()
|
||||||
|
|
||||||
|
// Define the struct for the expected result
|
||||||
|
type Chart struct {
|
||||||
|
Dataset string `json:"dataset"`
|
||||||
|
Key string `json:"key"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
ValueEn string `json:"value_en"`
|
||||||
|
ValueAr string `json:"value_ar"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query to fetch the data
|
||||||
|
sqlQuery := "SELECT * FROM charts_variables"
|
||||||
|
var results []Chart
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
err := db.NewQuery(sqlQuery).All(&results)
|
||||||
|
if err != nil {
|
||||||
|
// Log the error details for better debugging
|
||||||
|
log.Printf("Failed to execute query: %v", err)
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to execute query", "details": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if results are empty
|
||||||
|
if len(results) == 0 {
|
||||||
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the results
|
||||||
|
return c.JSON(http.StatusOK, results)
|
||||||
|
})
|
||||||
|
|
||||||
|
e.Router.GET("/api/getDataSet", func(c echo.Context) error {
|
||||||
|
|
||||||
|
dataset := c.QueryParam("dataset")
|
||||||
|
if dataset == "" {
|
||||||
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "dataset is required"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch all matching records manually
|
||||||
|
filter := dbx.NewExp("dataset = {:dataset}", dbx.Params{"dataset": dataset})
|
||||||
|
records, err := app.Dao().FindRecordsByExpr("charts", filter)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch records for dataset '%s': %v", dataset, err)
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
||||||
|
}
|
||||||
|
if len(records) == 0 {
|
||||||
|
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})
|
||||||
|
languageSource, err := app.Dao().FindRecordsByExpr("charts_variables", param)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch language source: %v", err)
|
||||||
|
}
|
||||||
|
// Convert languageSource to []map[string]interface{}
|
||||||
|
languageSourceConverted := []map[string]interface{}{}
|
||||||
|
for _, record := range languageSource {
|
||||||
|
languageSourceConverted = append(languageSourceConverted, record.SchemaData())
|
||||||
|
}
|
||||||
|
|
||||||
|
filterData, err := fetchfilterJSON(dataset)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, record := range records {
|
||||||
|
// Extract the URL for each record
|
||||||
|
apiURL := record.GetString("url")
|
||||||
|
file_name := record.GetString("kpi_file_name")
|
||||||
|
if apiURL == "" {
|
||||||
|
log.Printf("No URL for record '%v'", record)
|
||||||
|
continue // Skip records with no URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch and convert XML to JSON
|
||||||
|
saveToFile := record.GetBool("file_status")
|
||||||
|
chartId := record.GetString("id")
|
||||||
|
if !saveToFile { // Check if saveToFile is false
|
||||||
|
_, err := fetchAndConvertXMLToJSON(apiURL, "kpi_files", file_name, "charts", chartId)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to process URL '%s': %v", apiURL, err)
|
||||||
|
continue // Skip this record and proceed with others
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the JSON data from the file
|
||||||
|
chartData, err := readJSONFromFile("kpi_files", file_name)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to read JSON for KPI '%s': %v", file_name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
langKey := "value_en"
|
||||||
|
cRes := utils.ProcessChartData(chartData, languageSourceConverted, langKey)
|
||||||
|
|
||||||
|
// Prepare the structure with URL and its 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": cRes,
|
||||||
|
"group_by": record.GetString("group_by"),
|
||||||
|
"chart_heading": record.GetString("chart_heading"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the result to the aggregated response
|
||||||
|
aggregatedResponse = append(aggregatedResponse, recordResult)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the response structure
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"filter_data": fRes,
|
||||||
|
"data": aggregatedResponse,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(aggregatedResponse) == 0 {
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "No data could be fetched from the provided URLs"})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, response)
|
||||||
|
})
|
||||||
|
|
||||||
|
e.Router.GET("/api/getHomePageData", func(c echo.Context) error {
|
||||||
|
|
||||||
|
// Define the struct for holding the query results
|
||||||
|
mainTopics := []struct {
|
||||||
|
MainTopic string `db:"main_topic" json:"main_topic"`
|
||||||
|
MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
|
||||||
|
ColorPattern string `db:"color_pattern" json:"color_pattern"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
err := app.DB().
|
||||||
|
Select("main_topic", "main_topic_list_order", "color_pattern").
|
||||||
|
From("home_screen").
|
||||||
|
GroupBy("main_topic", "color_pattern").
|
||||||
|
OrderBy("main_topic_list_order ASC").
|
||||||
|
All(&mainTopics)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch home_screen data: %v", err)
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no records are found
|
||||||
|
if len(mainTopics) == 0 {
|
||||||
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final result structure
|
||||||
|
result := []map[string]interface{}{}
|
||||||
|
|
||||||
|
// Loop through main topics and fetch sub-topic data for each
|
||||||
|
for _, mainTopic := range mainTopics {
|
||||||
|
|
||||||
|
dataSets := []struct {
|
||||||
|
DataSet string `db:"data_set" json:"data_set"`
|
||||||
|
DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
|
||||||
|
ValueSource string `db:"value_source" json:"value_source"`
|
||||||
|
Value string `db:"value" json:"value"`
|
||||||
|
DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := app.DB().
|
||||||
|
NewQuery("SELECT data_set, data_set_tile_heading, value_source, value, data_set_list_order FROM home_screen WHERE main_topic={:topic} ").
|
||||||
|
Bind(dbx.Params{
|
||||||
|
"topic": mainTopic.MainTopic,
|
||||||
|
}).
|
||||||
|
All(&dataSets)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching data sets: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the main topic and its sub-topics to the result
|
||||||
|
result = append(result, map[string]interface{}{
|
||||||
|
"main_topic": mainTopic.MainTopic,
|
||||||
|
"main_topic_list_order": mainTopic.MainTopicListOrder,
|
||||||
|
"color_pattern": mainTopic.ColorPattern,
|
||||||
|
"tile_data": dataSets,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the combined result as JSON
|
||||||
|
return c.JSON(http.StatusOK, result)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
e.Router.GET("/api/getUAENumbersData", func(c echo.Context) error {
|
||||||
|
|
||||||
|
// Define the struct for holding the query results
|
||||||
|
mainTopics := []struct {
|
||||||
|
MainTopic string `db:"main_topic" json:"main_topic"`
|
||||||
|
MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
|
||||||
|
ColorPattern string `db:"color_pattern" json:"color_pattern"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
err := app.DB().
|
||||||
|
Select("main_topic", "main_topic_list_order", "color_pattern").
|
||||||
|
From("uae_numbers_screen").
|
||||||
|
GroupBy("main_topic", "color_pattern").
|
||||||
|
OrderBy("main_topic_list_order ASC").
|
||||||
|
All(&mainTopics)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch uae_numbers_screen data: %v", err)
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no records are found
|
||||||
|
if len(mainTopics) == 0 {
|
||||||
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final result structure
|
||||||
|
result := []map[string]interface{}{}
|
||||||
|
|
||||||
|
// Loop through main topics and fetch sub-topic data for each
|
||||||
|
for _, mainTopic := range mainTopics {
|
||||||
|
subTopics := []struct {
|
||||||
|
SubTopic string `db:"sub_topic" json:"sub_topic"`
|
||||||
|
SubTopicListOrder string `db:"sub_topic_list_order" json:"sub_topic_list_order"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
// Query to get sub-topics for the current main topic
|
||||||
|
err := app.DB().
|
||||||
|
Select("sub_topic", "sub_topic_list_order").
|
||||||
|
From("uae_numbers_screen").
|
||||||
|
Where(dbx.HashExp{"main_topic": mainTopic.MainTopic}).
|
||||||
|
GroupBy("sub_topic").
|
||||||
|
OrderBy("sub_topic_list_order ASC").
|
||||||
|
All(&subTopics)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch sub-topics for main topic %s: %v", mainTopic.MainTopic, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result2 := []map[string]interface{}{}
|
||||||
|
// Loop through sub Topics topics and fetch dataSet data for each
|
||||||
|
for _, subTopic := range subTopics {
|
||||||
|
|
||||||
|
dataSets := []struct {
|
||||||
|
DataSet string `db:"data_set" json:"data_set"`
|
||||||
|
DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
|
||||||
|
ValueSource string `db:"value_source" json:"value_source"`
|
||||||
|
Value string `db:"value" json:"value"`
|
||||||
|
DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
// Query to get data-set for the current sub topic
|
||||||
|
err := app.DB().
|
||||||
|
Select("data_set", "data_set_tile_heading", "value_source", "value", "data_set_list_order").
|
||||||
|
From("uae_numbers_screen").
|
||||||
|
Where(dbx.HashExp{"sub_topic": subTopic.SubTopic}).
|
||||||
|
OrderBy("data_set_list_order ASC").
|
||||||
|
All(&dataSets)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to fetch data-set for sub topic %s: %v", subTopic.SubTopic, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result2 = append(result2, map[string]interface{}{
|
||||||
|
"sub_topic": subTopic.SubTopic,
|
||||||
|
"sub_topic_list_order": subTopic.SubTopicListOrder,
|
||||||
|
"tile_data": dataSets,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the main topic and its sub-topics to the result
|
||||||
|
result = append(result, map[string]interface{}{
|
||||||
|
"main_topic": mainTopic.MainTopic,
|
||||||
|
"main_topic_list_order": mainTopic.MainTopicListOrder,
|
||||||
|
"color_pattern": mainTopic.ColorPattern,
|
||||||
|
"sub_topics": result2,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the combined result as JSON
|
||||||
|
return c.JSON(http.StatusOK, result)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
//send mail while status changed from app admin
|
//send mail while status changed from app admin
|
||||||
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
app.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
||||||
if e.Collection.Name == "users" {
|
if e.Collection.Name == "users" {
|
||||||
@ -884,303 +1191,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//app.Router.GET("/verify-email", verifyEmailHandler)
|
|
||||||
|
|
||||||
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
||||||
|
|
||||||
e.Router.GET("/api/custom/apicalltest", func(c echo.Context) error {
|
|
||||||
// Get the database instance
|
|
||||||
db := app.Dao().DB()
|
|
||||||
|
|
||||||
// Define the struct for the expected result
|
|
||||||
type Chart struct {
|
|
||||||
Dataset string `json:"dataset"`
|
|
||||||
Key string `json:"key"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
ValueEn string `json:"value_en"`
|
|
||||||
ValueAr string `json:"value_ar"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Query to fetch the data
|
|
||||||
sqlQuery := "SELECT * FROM charts_variables"
|
|
||||||
var results []Chart
|
|
||||||
|
|
||||||
// Execute the query
|
|
||||||
err := db.NewQuery(sqlQuery).All(&results)
|
|
||||||
if err != nil {
|
|
||||||
// Log the error details for better debugging
|
|
||||||
log.Printf("Failed to execute query: %v", err)
|
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to execute query", "details": err.Error()})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if results are empty
|
|
||||||
if len(results) == 0 {
|
|
||||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the results
|
|
||||||
return c.JSON(http.StatusOK, results)
|
|
||||||
})
|
|
||||||
|
|
||||||
e.Router.GET("/api/getDataSet", func(c echo.Context) error {
|
|
||||||
|
|
||||||
dataset := c.QueryParam("dataset")
|
|
||||||
if dataset == "" {
|
|
||||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "dataset is required"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch all matching records manually
|
|
||||||
filter := dbx.NewExp("dataset = {:dataset}", dbx.Params{"dataset": dataset})
|
|
||||||
records, err := app.Dao().FindRecordsByExpr("charts", filter)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to fetch records for dataset '%s': %v", dataset, err)
|
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
|
||||||
}
|
|
||||||
if len(records) == 0 {
|
|
||||||
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})
|
|
||||||
// languageSourceRaw, err := app.Dao().FindRecordsByExpr("charts_variables", param)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Printf("Failed to fetch language source: %v", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
filterData, err := fetchfilterJSON(dataset)
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
// }
|
|
||||||
|
|
||||||
for _, record := range records {
|
|
||||||
// Extract the URL for each record
|
|
||||||
apiURL := record.GetString("url")
|
|
||||||
kpi := record.GetString("kpi")
|
|
||||||
if apiURL == "" {
|
|
||||||
log.Printf("No URL for record '%v'", record)
|
|
||||||
continue // Skip records with no URL
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch and convert XML to JSON
|
|
||||||
saveToFile := record.GetBool("file_status")
|
|
||||||
chartId := record.GetString("id")
|
|
||||||
if !saveToFile { // Check if saveToFile is false
|
|
||||||
_, err := fetchAndConvertXMLToJSON(apiURL, "kpi_files", kpi, "charts", chartId)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to process URL '%s': %v", apiURL, err)
|
|
||||||
continue // Skip this record and proceed with others
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the JSON data from the file
|
|
||||||
response, err := readJSONFromFile("kpi_files", kpi)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to read JSON for KPI '%s': %v", kpi, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare the structure with URL and its 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": response,
|
|
||||||
"group_by": record.GetString("group_by"),
|
|
||||||
"chart_heading": record.GetString("chart_heading"),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the result to the aggregated response
|
|
||||||
aggregatedResponse = append(aggregatedResponse, recordResult)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the response structure
|
|
||||||
response := map[string]interface{}{
|
|
||||||
"filter_data": filterData,
|
|
||||||
"data": aggregatedResponse,
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(aggregatedResponse) == 0 {
|
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "No data could be fetched from the provided URLs"})
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, response)
|
|
||||||
})
|
|
||||||
|
|
||||||
e.Router.GET("/api/getHomePageData", func(c echo.Context) error {
|
|
||||||
|
|
||||||
// Define the struct for holding the query results
|
|
||||||
mainTopics := []struct {
|
|
||||||
MainTopic string `db:"main_topic" json:"main_topic"`
|
|
||||||
MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
|
|
||||||
ColorPattern string `db:"color_pattern" json:"color_pattern"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
// Execute the query
|
|
||||||
err := app.DB().
|
|
||||||
Select("main_topic", "main_topic_list_order", "color_pattern").
|
|
||||||
From("home_screen").
|
|
||||||
GroupBy("main_topic", "color_pattern").
|
|
||||||
OrderBy("main_topic_list_order ASC").
|
|
||||||
All(&mainTopics)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to fetch home_screen data: %v", err)
|
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no records are found
|
|
||||||
if len(mainTopics) == 0 {
|
|
||||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final result structure
|
|
||||||
result := []map[string]interface{}{}
|
|
||||||
|
|
||||||
// Loop through main topics and fetch sub-topic data for each
|
|
||||||
for _, mainTopic := range mainTopics {
|
|
||||||
|
|
||||||
dataSets := []struct {
|
|
||||||
DataSet string `db:"data_set" json:"data_set"`
|
|
||||||
DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
|
|
||||||
ValueSource string `db:"value_source" json:"value_source"`
|
|
||||||
Value string `db:"value" json:"value"`
|
|
||||||
DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
// Query to get data-set for the current sub topic
|
|
||||||
err := app.DB().
|
|
||||||
Select("data_set", "data_set_tile_heading", "value_source", "value", "data_set_list_order").
|
|
||||||
From("home_screen").
|
|
||||||
Where(dbx.HashExp{"show_in_home_page": 1}).
|
|
||||||
OrderBy("data_set_list_order ASC").
|
|
||||||
All(&dataSets)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the main topic and its sub-topics to the result
|
|
||||||
result = append(result, map[string]interface{}{
|
|
||||||
"main_topic": mainTopic.MainTopic,
|
|
||||||
"main_topic_list_order": mainTopic.MainTopicListOrder,
|
|
||||||
"color_pattern": mainTopic.ColorPattern,
|
|
||||||
"tile_data": dataSets,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the combined result as JSON
|
|
||||||
return c.JSON(http.StatusOK, result)
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
e.Router.GET("/api/getUAENumbersData", func(c echo.Context) error {
|
|
||||||
|
|
||||||
// Define the struct for holding the query results
|
|
||||||
mainTopics := []struct {
|
|
||||||
MainTopic string `db:"main_topic" json:"main_topic"`
|
|
||||||
MainTopicListOrder string `db:"main_topic_list_order" json:"main_topic_list_order"`
|
|
||||||
ColorPattern string `db:"color_pattern" json:"color_pattern"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
// Execute the query
|
|
||||||
err := app.DB().
|
|
||||||
Select("main_topic", "main_topic_list_order", "color_pattern").
|
|
||||||
From("home_screen").
|
|
||||||
GroupBy("main_topic", "color_pattern").
|
|
||||||
OrderBy("main_topic_list_order ASC").
|
|
||||||
All(&mainTopics)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to fetch home_screen data: %v", err)
|
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to fetch records"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no records are found
|
|
||||||
if len(mainTopics) == 0 {
|
|
||||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "No records found for home screen"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final result structure
|
|
||||||
result := []map[string]interface{}{}
|
|
||||||
|
|
||||||
// Loop through main topics and fetch sub-topic data for each
|
|
||||||
for _, mainTopic := range mainTopics {
|
|
||||||
subTopics := []struct {
|
|
||||||
SubTopic string `db:"sub_topic" json:"sub_topic"`
|
|
||||||
SubTopicListOrder string `db:"sub_topic_list_order" json:"sub_topic_list_order"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
// Query to get sub-topics for the current main topic
|
|
||||||
err := app.DB().
|
|
||||||
Select("sub_topic", "sub_topic_list_order").
|
|
||||||
From("home_screen").
|
|
||||||
Where(dbx.HashExp{"main_topic": mainTopic.MainTopic}).
|
|
||||||
GroupBy("sub_topic").
|
|
||||||
OrderBy("sub_topic_list_order ASC").
|
|
||||||
All(&subTopics)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to fetch sub-topics for main topic %s: %v", mainTopic.MainTopic, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result2 := []map[string]interface{}{}
|
|
||||||
// Loop through sub Topics topics and fetch dataSet data for each
|
|
||||||
for _, subTopic := range subTopics {
|
|
||||||
|
|
||||||
dataSets := []struct {
|
|
||||||
DataSet string `db:"data_set" json:"data_set"`
|
|
||||||
DataSetTileHeading string `db:"data_set_tile_heading" json:"data_set_tile_heading"`
|
|
||||||
ValueSource string `db:"value_source" json:"value_source"`
|
|
||||||
Value string `db:"value" json:"value"`
|
|
||||||
DataSetListOrder string `db:"data_set_list_order" json:"data_set_list_order"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
// Query to get data-set for the current sub topic
|
|
||||||
err := app.DB().
|
|
||||||
Select("data_set", "data_set_tile_heading", "value_source", "value", "data_set_list_order").
|
|
||||||
From("home_screen").
|
|
||||||
Where(dbx.HashExp{"sub_topic": subTopic.SubTopic}).
|
|
||||||
OrderBy("data_set_list_order ASC").
|
|
||||||
All(&dataSets)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to fetch data-set for sub topic %s: %v", subTopic.SubTopic, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
result2 = append(result2, map[string]interface{}{
|
|
||||||
"sub_topic": subTopic.SubTopic,
|
|
||||||
"sub_topic_list_order": subTopic.SubTopicListOrder,
|
|
||||||
"tile_data": dataSets,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the main topic and its sub-topics to the result
|
|
||||||
result = append(result, map[string]interface{}{
|
|
||||||
"main_topic": mainTopic.MainTopic,
|
|
||||||
"main_topic_list_order": mainTopic.MainTopicListOrder,
|
|
||||||
"color_pattern": mainTopic.ColorPattern,
|
|
||||||
"sub_topics": result2,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the combined result as JSON
|
|
||||||
return c.JSON(http.StatusOK, result)
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
// Start your custom HTTP server on port 8091
|
// Start your custom HTTP server on port 8091
|
||||||
log.Println("Starting custom HTTP server on :8091")
|
log.Println("Starting custom HTTP server on :8091")
|
||||||
if err := http.ListenAndServe(":8091", nil); err != nil {
|
if err := http.ListenAndServe(":8091", nil); err != nil {
|
||||||
|
|||||||
48
utils/process_chart_data.go
Normal file
48
utils/process_chart_data.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
// ProcessChartData processes chart data with language source data to produce the result based on the language key.
|
||||||
|
func ProcessChartData(chartData []map[string]interface{}, languageSource []map[string]interface{}, langKey string) []map[string]interface{} {
|
||||||
|
// Helper function to find translation
|
||||||
|
findTranslation := func(key string, value string) string {
|
||||||
|
for _, langEntry := range languageSource {
|
||||||
|
if langEntry["key"] == key && langEntry["value"] == value {
|
||||||
|
if translatedValue, ok := langEntry[langKey]; ok {
|
||||||
|
return translatedValue.(string)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value // Return the original value if no translation is found
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract unique translation keys from languageSource
|
||||||
|
translationKeys := map[string]bool{}
|
||||||
|
for _, langEntry := range languageSource {
|
||||||
|
if key, ok := langEntry["key"].(string); ok {
|
||||||
|
translationKeys[key] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process each chart data entry
|
||||||
|
result := []map[string]interface{}{}
|
||||||
|
for _, entry := range chartData {
|
||||||
|
obsKey := entry["ObsKey"].(map[string]interface{})
|
||||||
|
translatedObsKey := map[string]interface{}{}
|
||||||
|
|
||||||
|
// Translate fields in ObsKey dynamically based on extracted translation keys
|
||||||
|
for key, value := range obsKey {
|
||||||
|
if translationKeys[key] {
|
||||||
|
translatedObsKey[key] = findTranslation(key, value.(string))
|
||||||
|
} else {
|
||||||
|
translatedObsKey[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the translated entry to the result
|
||||||
|
result = append(result, map[string]interface{}{
|
||||||
|
"ObsKey": translatedObsKey,
|
||||||
|
"ObsValue": entry["ObsValue"],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user