perf_metrics.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func GetPerfMetricsSummary(c *gin.Context) {
  9. hours := 24
  10. if rawHours := c.Query("hours"); rawHours != "" {
  11. if parsed, err := strconv.Atoi(rawHours); err == nil {
  12. hours = parsed
  13. }
  14. }
  15. result, err := perfmetrics.QuerySummaryAll(hours)
  16. if err != nil {
  17. c.JSON(http.StatusInternalServerError, gin.H{
  18. "success": false,
  19. "message": err.Error(),
  20. })
  21. return
  22. }
  23. c.JSON(http.StatusOK, gin.H{
  24. "success": true,
  25. "data": result,
  26. })
  27. }
  28. func GetPerfMetrics(c *gin.Context) {
  29. modelName := c.Query("model")
  30. if modelName == "" {
  31. c.JSON(http.StatusBadRequest, gin.H{
  32. "success": false,
  33. "message": "model is required",
  34. })
  35. return
  36. }
  37. hours := 24
  38. if rawHours := c.Query("hours"); rawHours != "" {
  39. if parsed, err := strconv.Atoi(rawHours); err == nil {
  40. hours = parsed
  41. }
  42. }
  43. result, err := perfmetrics.Query(perfmetrics.QueryParams{
  44. Model: modelName,
  45. Group: c.Query("group"),
  46. Hours: hours,
  47. })
  48. if err != nil {
  49. c.JSON(http.StatusInternalServerError, gin.H{
  50. "success": false,
  51. "message": err.Error(),
  52. })
  53. return
  54. }
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": true,
  57. "data": result,
  58. })
  59. }