turash/bugulma/backend/internal/handler/analytics_handler.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

150 lines
4.5 KiB
Go

package handler
import (
"bugulma/backend/internal/service"
"net/http"
"github.com/gin-gonic/gin"
)
// AnalyticsHandler handles platform analytics and statistics
type AnalyticsHandler struct {
service *service.AnalyticsService
}
// NewAnalyticsHandler creates a new analytics handler
func NewAnalyticsHandler(service *service.AnalyticsService) *AnalyticsHandler {
return &AnalyticsHandler{service: service}
}
// GetPlatformStatistics returns overall platform statistics
// @Summary Get platform statistics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.PlatformStatistics
// @Router /api/analytics/platform-stats [get]
func (h *AnalyticsHandler) GetPlatformStatistics(c *gin.Context) {
stats, err := h.service.GetPlatformStatistics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, stats)
}
// GetOrganizationStatistics returns statistics for a specific organization
// @Summary Get organization statistics
// @Tags analytics
// @Produce json
// @Param organizationId path string true "Organization ID"
// @Success 200 {object} service.OrganizationStatistics
// @Router /api/analytics/organizations/{organizationId}/stats [get]
func (h *AnalyticsHandler) GetOrganizationStatistics(c *gin.Context) {
orgID := c.Param("organizationId")
stats, err := h.service.GetOrganizationStatistics(c.Request.Context(), orgID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Organization not found"})
return
}
c.JSON(http.StatusOK, stats)
}
// GetMatchingStatistics returns matching engine performance statistics
// @Summary Get matching statistics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.MatchingStatistics
// @Router /api/analytics/matching-stats [get]
func (h *AnalyticsHandler) GetMatchingStatistics(c *gin.Context) {
stats, err := h.service.GetMatchingStatistics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, stats)
}
// GetResourceFlowStatistics returns resource flow statistics
// @Summary Get resource flow statistics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.ResourceFlowStatistics
// @Router /api/analytics/resource-flow-stats [get]
func (h *AnalyticsHandler) GetResourceFlowStatistics(c *gin.Context) {
stats, err := h.service.GetResourceFlowStatistics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, stats)
}
// GetImpactMetrics returns environmental and economic impact metrics
// @Summary Get impact metrics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.ImpactMetrics
// @Router /api/analytics/impact-metrics [get]
func (h *AnalyticsHandler) GetImpactMetrics(c *gin.Context) {
metrics, err := h.service.GetImpactMetrics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, metrics)
}
// GetDashboardStatistics returns dashboard statistics
// @Summary Get dashboard statistics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.DashboardStatistics
// @Router /api/analytics/dashboard [get]
func (h *AnalyticsHandler) GetDashboardStatistics(c *gin.Context) {
stats, err := h.service.GetDashboardStatistics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, stats)
}
// GetConnectionStatistics returns connection statistics
// @Summary Get connection statistics
// @Tags analytics
// @Produce json
// @Success 200 {object} service.ConnectionStatistics
// @Router /api/analytics/connections [get]
func (h *AnalyticsHandler) GetConnectionStatistics(c *gin.Context) {
stats, err := h.service.GetConnectionStatistics(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, stats)
}
// GetSupplyDemandAnalysis returns supply and demand analysis
// @Summary Get supply and demand analysis
// @Tags analytics
// @Produce json
// @Success 200 {object} service.SupplyDemandAnalysis
// @Router /api/analytics/supply-demand [get]
func (h *AnalyticsHandler) GetSupplyDemandAnalysis(c *gin.Context) {
analysis, err := h.service.GetSupplyDemandAnalysis(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, analysis)
}