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

@@ -241,3 +241,61 @@ func GetContractByID(contractID uint) (models.Contract, int, error) {
return contract, http.StatusOK, nil
}
func CountContractsByMultipleStatusesAndTotal(companyID uint, startTime, endTime time.Time) (int64, int64, int64, map[string]map[string]int64, error) {
var activeCount, executedCount, totalCount int64
monthlyCounts := make(map[string]map[string]int64)
var contract models.Contract
// Iterate through each month within the specified date range
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
monthEnd := dt.AddDate(0, 1, 0)
if monthEnd.After(endTime) {
monthEnd = endTime
}
// Initialize monthlyCounts for the current month
monthKey := dt.Format("2006-01")
monthlyCounts[monthKey] = map[string]int64{"active": 0, "executed": 0, "total": 0}
// Query for 'active' contracts for the current month
var active int64
err := shared.GetDb().Model(&contract).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "active", companyID, companyID, dt, monthEnd).
Count(&active).Error
if err != nil {
return 0, 0, 0, nil, err
}
// Query for 'executed' contracts for the current month
var executed int64
err = shared.GetDb().Model(&contract).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "executed", companyID, companyID, dt, monthEnd).
Count(&executed).Error
if err != nil {
return 0, 0, 0, nil, err
}
// Query for total contracts for the current month
var total int64
err = shared.GetDb().Model(&contract).
Where("(buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", companyID, companyID, dt, monthEnd).
Count(&total).Error
if err != nil {
return 0, 0, 0, nil, err
}
// Update the counts for the current month
monthlyCounts[monthKey]["active"] = active
monthlyCounts[monthKey]["executed"] = executed
monthlyCounts[monthKey]["total"] = total
// Update the total counts
activeCount += active
executedCount += executed
totalCount += total
}
return activeCount, executedCount, totalCount, monthlyCounts, nil
}