-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistrict_rollup.go
88 lines (79 loc) · 3.1 KB
/
district_rollup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package handler
import (
"net/http"
"time"
"github.com/USACE/instrumentation-api/api/internal/db"
"github.com/USACE/instrumentation-api/api/internal/httperr"
"github.com/USACE/instrumentation-api/api/internal/util"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
const timeRangeErrMessage = "maximum requested time range exceeded (5 years)"
// ListEvaluationDistrictRollup godoc
//
// @Summary lists monthly evaluation statistics for a district by project id
// @Tags district-rollup
// @Produce json
// @Param project_id path string true "project id" Format(uuid)
// @Success 200 {array} db.VDistrictRollup
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/district_rollup/evaluation_submittals [get]
func (h *ApiHandler) ListProjectEvaluationDistrictRollup(c echo.Context) error {
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
httperr.MalformedID(err)
}
var tw util.TimeWindow
from, to := c.QueryParam("from_timestamp_month"), c.QueryParam("to_timestamp_month")
if err := tw.SetWindow(from, to, time.Now().AddDate(-1, 0, 0), time.Now()); err != nil {
return httperr.MalformedDateRFC3339(err)
}
if fiveYrsAfterStart := tw.After.AddDate(5, 0, 0); tw.Before.After(fiveYrsAfterStart) {
return httperr.Message(http.StatusBadRequest, timeRangeErrMessage)
}
project, err := h.DBService.DistrictRollupListEvaluationForProjectAlertConfig(c.Request().Context(), db.DistrictRollupListEvaluationForProjectAlertConfigParams{
ProjectID: pID,
StartMonthTime: tw.After,
EndMonthTime: tw.Before,
})
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, project)
}
// ListMeasurementDistrictRollup godoc
//
// @Summary lists monthly measurement statistics for a district by project id
// @Tags district-rollup
// @Produce json
// @Param project_id path string true "project id" Format(uuid)
// @Success 200 {array} db.VDistrictRollup
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/district_rollup/measurement_submittals [get]
func (h *ApiHandler) ListProjectMeasurementDistrictRollup(c echo.Context) error {
pID, err := uuid.Parse(c.Param("project_id"))
if err != nil {
return httperr.MalformedID(err)
}
var tw util.TimeWindow
from, to := c.QueryParam("from_timestamp_month"), c.QueryParam("to_timestamp_month")
if err := tw.SetWindow(from, to, time.Now().AddDate(-1, 0, 0), time.Now()); err != nil {
return httperr.MalformedDateRFC3339(err)
}
if fiveYrsAfterStart := tw.After.AddDate(5, 0, 0); tw.Before.After(fiveYrsAfterStart) {
return httperr.Message(http.StatusBadRequest, timeRangeErrMessage)
}
project, err := h.DBService.DistrictRollupListMeasurementForProjectAlertConfig(c.Request().Context(), db.DistrictRollupListMeasurementForProjectAlertConfigParams{
ProjectID: pID,
StartMonthTime: tw.After,
EndMonthTime: tw.Before,
})
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, project)
}