This commit is contained in:
venbaittech 2024-12-20 16:33:12 +05:30
parent 39862c0516
commit 156d96adfd
6 changed files with 66 additions and 0 deletions

66
main.go
View File

@ -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 {

BIN
pb_data/data.db-shm Normal file

Binary file not shown.

0
pb_data/data.db-wal Normal file
View File

Binary file not shown.

BIN
pb_data/logs.db-shm Normal file

Binary file not shown.

0
pb_data/logs.db-wal Normal file
View File