/
zone_responses.go
231 lines (191 loc) · 6.31 KB
/
zone_responses.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package server
import (
"fmt"
"net/http"
"slices"
"strings"
"time"
"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/babyapi"
"github.com/go-chi/render"
)
// ZoneResponse is used to represent a Zone in the response body with the additional Moisture data
// and hypermedia Links fields
type ZoneResponse struct {
*pkg.Zone
WeatherData *WeatherData `json:"weather_data,omitempty"`
NextWater NextWaterDetails `json:"next_water,omitempty"`
Links []Link `json:"links,omitempty"`
// History is only used in HTML responses and is excluded from JSON
History ZoneWaterHistoryResponse `json:"-"`
HistoryError string `json:"-"`
api *ZonesAPI
}
// NewZoneResponse creates a self-referencing ZoneResponse
func (api *ZonesAPI) NewZoneResponse(zone *pkg.Zone, links ...Link) *ZoneResponse {
return &ZoneResponse{
Zone: zone,
Links: links,
api: api,
}
}
func (zr *ZoneResponse) HTML(r *http.Request) string {
// ignoring errors here since this can only be reached for a valid request
timeRange, _ := rangeQueryParam(r)
limit, _ := limitQueryParam(r)
return zoneDetailsTemplate.Render(r, map[string]any{
"TimeRange": timeRange,
"Limit": limit,
"Response": zr,
})
}
// Render is used to make this struct compatible with the go-chi webserver for writing
// the JSON response
func (zr *ZoneResponse) Render(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
excludeWeatherData := excludeWeatherData(r)
logger := babyapi.GetLoggerFromContext(r.Context())
ws := []*pkg.WaterSchedule{}
for _, id := range zr.Zone.WaterScheduleIDs {
result, err := zr.api.storageClient.WaterSchedules.Get(ctx, id.String())
if err != nil {
return fmt.Errorf("unable to get WaterSchedule for ZoneResponse: %w", err)
}
ws = append(ws, result)
}
garden, httpErr := zr.api.getGardenFromRequest(r)
if httpErr != nil {
logger.Error("unable to get garden for zone", "error", httpErr)
return httpErr
}
gardenPath := fmt.Sprintf("%s/%s", gardenBasePath, garden.ID)
zr.Links = append(zr.Links,
Link{
"self",
fmt.Sprintf("%s%s/%s", gardenPath, zoneBasePath, zr.Zone.ID),
},
Link{
"garden",
gardenPath,
},
)
if zr.Zone.EndDated() {
return nil
}
zr.Links = append(zr.Links,
Link{
"action",
fmt.Sprintf("%s%s/%s/action", gardenPath, zoneBasePath, zr.Zone.ID),
},
Link{
"history",
fmt.Sprintf("%s%s/%s/history", gardenPath, zoneBasePath, zr.Zone.ID),
},
)
if render.GetAcceptedContentType(r) == render.ContentTypeHTML {
// only get history when rendering a ZoneDetail page
if zr.api.GetIDParam(r) != "" {
history, apiErr := zr.api.getWaterHistoryFromRequest(r, zr.Zone, logger)
if apiErr != nil {
logger.Error("error getting water history", "error", apiErr)
zr.HistoryError = apiErr.ErrorText
}
zr.History = NewZoneWaterHistoryResponse(history)
}
if r.Method == http.MethodPut {
w.Header().Add("HX-Trigger", "newZone")
}
}
nextWaterSchedule := zr.api.worker.GetNextActiveWaterSchedule(ws)
if nextWaterSchedule == nil {
zr.NextWater = NextWaterDetails{
Message: "no active WaterSchedules",
}
return nil
}
zr.NextWater = GetNextWaterDetails(nextWaterSchedule, zr.api.worker, excludeWeatherData)
zr.NextWater.WaterScheduleID = &nextWaterSchedule.ID.ID
if zr.Zone.SkipCount != nil && *zr.Zone.SkipCount > 0 {
zr.NextWater.Message = fmt.Sprintf("skip_count %d affected the time", *zr.Zone.SkipCount)
newNextTime := zr.NextWater.Time.Add(time.Duration(*zr.Zone.SkipCount) * nextWaterSchedule.Interval.Duration)
zr.NextWater.Time = &newNextTime
}
if nextWaterSchedule.HasWeatherControl() && !excludeWeatherData {
zr.WeatherData = getWeatherData(ctx, nextWaterSchedule, zr.api.storageClient)
if nextWaterSchedule.HasSoilMoistureControl() && garden != nil {
logger.Debug("getting moisture data for Zone")
soilMoisture, err := zr.api.getMoisture(ctx, garden, zr.Zone)
if err != nil {
logger.Warn("unable to get moisture data for Zone", "error", err)
} else {
logger.Debug("successfully got moisture data for Zone", "moisture", soilMoisture)
zr.WeatherData.SoilMoisturePercent = &soilMoisture
}
}
}
return nil
}
type AllZonesResponse struct {
babyapi.ResourceList[*ZoneResponse]
api *babyapi.API[*pkg.Zone]
}
func (azr AllZonesResponse) Render(w http.ResponseWriter, r *http.Request) error {
return azr.ResourceList.Render(w, r)
}
func (azr AllZonesResponse) HTML(r *http.Request) string {
slices.SortFunc(azr.Items, func(z *ZoneResponse, zz *ZoneResponse) int {
return strings.Compare(z.Name, zz.Name)
})
garden, err := babyapi.GetResourceFromContext[*pkg.Garden](r.Context(), azr.api.ParentContextKey())
if err != nil {
panic(err)
}
data := map[string]any{
"Items": azr.Items,
"Garden": garden,
}
if r.URL.Query().Get("refresh") == "true" {
return zonesTemplate.Render(r, data)
}
return zonesPageTemplate.Render(r, data)
}
// ZoneWaterHistoryResponse wraps a slice of WaterHistory structs plus some aggregate stats for an HTTP response
type ZoneWaterHistoryResponse struct {
History []pkg.WaterHistory `json:"history"`
Count int `json:"count"`
Average string `json:"average"`
Total string `json:"total"`
}
// NewZoneWaterHistoryResponse creates a response by creating some basic statistics about a list of history events
func NewZoneWaterHistoryResponse(history []pkg.WaterHistory) ZoneWaterHistoryResponse {
total := time.Duration(0)
for _, h := range history {
amountDuration, _ := time.ParseDuration(h.Duration)
total += amountDuration
}
count := len(history)
average := time.Duration(0)
if count != 0 {
average = time.Duration(int(total) / len(history))
}
return ZoneWaterHistoryResponse{
History: history,
Count: count,
Average: average.String(),
Total: time.Duration(total).String(),
}
}
// Render is used to make this struct compatible with the go-chi webserver for writing
// the JSON response
func (resp ZoneWaterHistoryResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
return nil
}
func filterZoneByGardenID(gardenID string) babyapi.FilterFunc[*pkg.Zone] {
return func(z *pkg.Zone) bool {
return z.GardenID.String() == gardenID
}
}
type ZoneActionResponse struct{}
func (*ZoneActionResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
return nil
}