diff --git a/main.go b/main.go index e121ba0..4912afc 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "time" "github.com/labstack/echo/v5" + "github.com/pocketbase/dbx" "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/core" @@ -659,6 +660,71 @@ func main() { //app.Router.GET("/verify-email", verifyEmailHandler) + app.OnBeforeServe().Add(func(e *core.ServeEvent) error { + + e.Router.GET("/api/custom/apicall", func(c echo.Context) error { + // Get kpi_id from query parameters + dataset := c.QueryParam("dataset") + if dataset == "" { + return c.JSON(http.StatusBadRequest, map[string]string{"error": "dataset is required"}) + } + + // Fetch all matching records manually + // Construct the dbx.Expression + 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 kpi_id '%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 kpi_id"}) + } + + var aggregatedResponse []map[string]interface{} + for _, record := range records { + // Extract the URL for each record + apiURL := record.GetString("url") + if apiURL == "" { + log.Printf("No URL for record '%v'", record) + continue // Skip records with no URL + } + + // Fetch and convert XML to JSON + response, err := fetchAndConvertXMLToJSON(apiURL) + if err != nil { + log.Printf("Failed to process URL '%s': %v", apiURL, err) + continue // Skip this record and proceed with others + } + + // 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, + } + + // Add the result to the aggregated response + aggregatedResponse = append(aggregatedResponse, recordResult) + + } + + 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, aggregatedResponse) + }) + + return nil + }) + // Start your custom HTTP server on port 8091 log.Println("Starting custom HTTP server on :8091") if err := http.ListenAndServe(":8091", nil); err != nil { diff --git a/pb_data/data.db-shm b/pb_data/data.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/pb_data/data.db-shm differ diff --git a/pb_data/data.db-wal b/pb_data/data.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/pb_data/logs.db b/pb_data/logs.db index bd299dc..232f408 100644 Binary files a/pb_data/logs.db and b/pb_data/logs.db differ diff --git a/pb_data/logs.db-shm b/pb_data/logs.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/pb_data/logs.db-shm differ diff --git a/pb_data/logs.db-wal b/pb_data/logs.db-wal new file mode 100644 index 0000000..e69de29