Added measurements, and devices statsh

Added stats endpoints
This commit is contained in:
Nedim
2023-10-20 12:03:59 +02:00
parent 6892c56c1e
commit d40b225e4e
12 changed files with 1120 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package controllers
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"gitlab.com/pactual1/backend/database/invoice"
@@ -104,3 +105,38 @@ func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceRespo
}
return listInvoiceResponses
}
func GetInvoiceCountByStatus(c *gin.Context) {
companyID := c.DefaultQuery("company_id", "0")
startTimeStr := c.DefaultQuery("start_time", "")
endTimeStr := c.DefaultQuery("end_time", "")
// Convert to uint and time.Time
companyIDUint, err := strconv.ParseUint(companyID, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid companyID",
})
return
}
// Convert string to Unix timestamp
startUnix, _ := strconv.ParseInt(startTimeStr, 10, 64)
endUnix, _ := strconv.ParseInt(endTimeStr, 10, 64)
// Convert Unix timestamps to time.Time
startTime := time.Unix(startUnix, 0)
endTime := time.Unix(endUnix, 0)
activeCount, executedCount, monthly, err := invoice.CountInvoicesByMultipleStatuses(uint(companyIDUint), startTime, endTime)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal Server Error",
})
return
}
c.JSON(http.StatusOK, gin.H{"data": models.ActiveInvoiceResponse{Claimed: activeCount, Issued : executedCount, MonthlyInvoices: monthly}})
}