added analytics route

This commit is contained in:
SwiftyOS
2024-09-12 15:48:11 +02:00
parent 9cb55c5ac0
commit 9b58faeeb6
2 changed files with 57 additions and 3 deletions

View File

@@ -410,4 +410,30 @@ func Search(ctx context.Context, db *pgxpool.Pool, query string, categories []st
logger.Info("Search completed", zap.Int("results", len(agents)))
return agents, nil
}
}
func CreateAgentInstalledEvent(ctx context.Context, db *pgxpool.Pool, eventData models.InstallTracker) error {
logger := zap.L().With(zap.String("function", "CreateAgentInstalledEvent"))
logger.Info("Creating agent installed event")
query := `
INSERT INTO install_tracker (marketplace_agent_id, installed_agent_id, installation_location)
VALUES ($1, $2, $3)
`
_, err := db.Exec(ctx, query,
eventData.MarketplaceAgentID,
eventData.InstalledAgentID,
eventData.InstallationLocation,
)
if err != nil {
logger.Error("Failed to create agent installed event", zap.Error(err))
return fmt.Errorf("failed to create agent installed event: %w", err)
}
logger.Info("Agent installed event created successfully")
return nil
}

View File

@@ -1,13 +1,41 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"go.uber.org/zap"
"github.com/swiftyos/market/database"
"github.com/swiftyos/market/models"
)
func AgentInstalled(db *pgxpool.Pool) gin.HandlerFunc {
return func(c *gin.Context) {
// TODO: Implement AgentInstalled
c.JSON(501, gin.H{"message": "Not Implemented: AgentInstalled"})
logger := zap.L().With(zap.String("function", "AgentInstalled"))
var eventData models.InstallTracker
if err := c.ShouldBindJSON(&eventData); err != nil {
logger.Error("Failed to bind JSON", zap.Error(err))
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
err := database.CreateAgentInstalledEvent(c.Request.Context(), db, models.InstallTracker{
MarketplaceAgentID: eventData.MarketplaceAgentID,
InstalledAgentID: eventData.InstalledAgentID,
InstallationLocation: eventData.InstallationLocation,
})
if err != nil {
logger.Error("Failed to process agent installed event", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to process agent installed event"})
return
}
logger.Info("Agent installed event processed successfully",
zap.String("marketplaceAgentID", eventData.MarketplaceAgentID),
zap.String("installedAgentID", eventData.InstalledAgentID),
zap.String("installationLocation", string(eventData.InstallationLocation)))
c.JSON(http.StatusOK, gin.H{"message": "Agent installed event processed successfully"})
}
}