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

@@ -158,3 +158,124 @@ func GetDevicesByContract(c *gin.Context) {
// Respond with the devices
c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
}
func GetCompanyRelatedDeviceInfoCount(c *gin.Context) {
// Get the Company ID from query parameters
companyIDStr := c.DefaultQuery("company_id", "")
if companyIDStr == "" {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
return
}
// Convert string to uint for CompanyID
companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
if err != nil {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
return
}
// Perform the counting
count, err := device.CountDeviceInfoByCompany(uint(companyID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Respond with the count
c.JSON(http.StatusOK, gin.H{"data": count})
}
func GetCompanyRelatedDeviceInfoCountWithTempRange(c *gin.Context) {
// Get the Company ID from query parameters
companyIDStr := c.DefaultQuery("company_id", "")
startTimeStr := c.DefaultQuery("start_time", "")
endTimeStr := c.DefaultQuery("end_time", "")
if companyIDStr == "" {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
return
}
if startTimeStr == "" {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: startTime ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Start time is required"})
return
}
if endTimeStr == "" {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: endTime is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "End time is required"})
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)
// Convert string to uint for CompanyID
companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
if err != nil {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
return
}
// Get the counts
inRangeCount, outOfRangeCount, monthlyCount, err := device.CountDeviceBreachedAndNormalDevicesByCompany(uint(companyID), startTime, endTime)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
data := models.NormalAndBreachedDevicesResponse{Breached: outOfRangeCount, Normal: inRangeCount, MonthlyCounts: monthlyCount}
// Respond with both counts
c.JSON(http.StatusOK, gin.H{"data": data})
}
func GetContractsMatchingDeviceLocation(c *gin.Context) {
startTimeStr := c.DefaultQuery("start_time", "")
endTimeStr := c.DefaultQuery("end_time", "")
companyIDStr := c.DefaultQuery("company_id", "")
if companyIDStr == "" {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Company ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Company ID is required"})
return
}
// Convert string to uint for CompanyID
companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
if err != nil {
log.Printf("GetCompanyRelatedDeviceInfoCount Error: Invalid Company ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Company ID"})
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)
matches, err := device.FetchMatchingContractsAndDeviceInfo(companyID, startTime, endTime)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal Server Error",
})
return
}
c.JSON(http.StatusOK, gin.H{
"data": len(matches),
})
}