@@ -6,6 +6,10 @@ package api
66
77import (
88 "context"
9+ "net/http"
10+ "strconv"
11+ "time"
12+
913 "gitlab.com/lightmeter/controlcenter/dashboard"
1014 "gitlab.com/lightmeter/controlcenter/httpauth/auth"
1115 "gitlab.com/lightmeter/controlcenter/httpmiddleware"
@@ -14,8 +18,6 @@ import (
1418 "gitlab.com/lightmeter/controlcenter/util/httputil"
1519 "gitlab.com/lightmeter/controlcenter/util/timeutil"
1620 "gitlab.com/lightmeter/controlcenter/version"
17- "net/http"
18- "time"
1921)
2022
2123type handler struct {
@@ -128,6 +130,80 @@ func (h deliveryStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
128130 return servePairsFromTimeInterval (w , r , h .dashboard .DeliveryStatus , interval )
129131}
130132
133+ type trafficBySenderOverTimeHandler struct {
134+ f func (context.Context , timeutil.TimeInterval , int ) (dashboard.MailTrafficPerSenderOverTimeResult , error )
135+ }
136+
137+ // @Summary Messages sent by mailbox over time
138+ // @Param from query string true "Initial date in the format 1999-12-23"
139+ // @Param to query string true "Final date in the format 1999-12-23"
140+ // @Param granularity query integer 12 "Time granularity in hours"
141+ // @Produce json
142+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
143+ // @Failure 422 {string} string "desc"
144+ // @Router /api/v0/sentMailsByMailbox [get]
145+
146+ // @Summary Messages bounced by mailbox over time
147+ // @Param from query string true "Initial date in the format 1999-12-23"
148+ // @Param to query string true "Final date in the format 1999-12-23"
149+ // @Param granularity query integer 12 "Time granularity in hours"
150+ // @Produce json
151+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
152+ // @Failure 422 {string} string "desc"
153+ // @Router /api/v0/bouncedMailsByMailbox [get]
154+
155+ // @Summary Messages deferred by mailbox over time
156+ // @Param from query string true "Initial date in the format 1999-12-23"
157+ // @Param to query string true "Final date in the format 1999-12-23"
158+ // @Param granularity query integer 12 "Time granularity in hours"
159+ // @Produce json
160+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
161+ // @Failure 422 {string} string "desc"
162+ // @Router /api/v0/deferredMailsByMailbox [get]
163+
164+ // @Summary Messages expired by mailbox over time
165+ // @Param from query string true "Initial date in the format 1999-12-23"
166+ // @Param to query string true "Final date in the format 1999-12-23"
167+ // @Param granularity query integer 12 "Time granularity in hours"
168+ // @Produce json
169+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
170+ // @Failure 422 {string} string "desc"
171+ // @Router /api/v0/expiredMailsByMailbox [get]
172+
173+ // @Summary Messages received by mailbox over time
174+ // @Param from query string true "Initial date in the format 1999-12-23"
175+ // @Param to query string true "Final date in the format 1999-12-23"
176+ // @Param granularity query integer 12 "Time granularity in hours"
177+ // @Produce json
178+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
179+ // @Failure 422 {string} string "desc"
180+ // @Router /api/v0/receivedMailsByMailbox [get]
181+
182+ // @Summary Number of inbound replies
183+ // @Param from query string true "Initial date in the format 1999-12-23"
184+ // @Param to query string true "Final date in the format 1999-12-23"
185+ // @Param granularity query integer 12 "Time granularity in hours"
186+ // @Produce json
187+ // @Success 200 {object} dashboard.MailTrafficPerSenderOverTimeResult
188+ // @Failure 422 {string} string "desc"
189+ // @Router /api/v0/inboundRepliesByMailbox [get]
190+
191+ func (h trafficBySenderOverTimeHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) error {
192+ interval := httpmiddleware .GetIntervalFromContext (r )
193+
194+ granularity , err := strconv .Atoi (r .Form .Get ("granularity" ))
195+ if err != nil {
196+ return httperror .NewHTTPStatusCodeError (http .StatusUnprocessableEntity , err )
197+ }
198+
199+ result , err := h .f (r .Context (), interval , granularity )
200+ if err != nil {
201+ return err
202+ }
203+
204+ return httputil .WriteJson (w , result , http .StatusOK )
205+ }
206+
131207type appVersionHandler struct {}
132208
133209type appVersion struct {
@@ -144,14 +220,25 @@ func (appVersionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error
144220 return httputil .WriteJson (w , appVersion {Version : version .Version , Commit : version .Commit , TagOrBranch : version .TagOrBranch }, http .StatusOK )
145221}
146222
147- func HttpDashboard (auth * auth.Authenticator , mux * http.ServeMux , timezone * time.Location , dashboard dashboard.Dashboard ) {
223+ func HttpDashboard (auth * auth.Authenticator , mux * http.ServeMux , timezone * time.Location , d dashboard.Dashboard ) {
148224 authenticated := httpmiddleware .WithDefaultStack (auth , httpmiddleware .RequestWithInterval (timezone ))
149225 unauthenticated := httpmiddleware .WithDefaultStackWithoutAuth ()
150226
151- mux .Handle ("/api/v0/countByStatus" , authenticated .WithEndpoint (countByStatusHandler {dashboard }))
152- mux .Handle ("/api/v0/topBusiestDomains" , authenticated .WithEndpoint (topBusiestDomainsHandler {dashboard }))
153- mux .Handle ("/api/v0/topBouncedDomains" , authenticated .WithEndpoint (topBouncedDomainsHandler {dashboard }))
154- mux .Handle ("/api/v0/topDeferredDomains" , authenticated .WithEndpoint (topDeferredDomainsHandler {dashboard }))
155- mux .Handle ("/api/v0/deliveryStatus" , authenticated .WithEndpoint (deliveryStatusHandler {dashboard }))
227+ for k , v := range map [string ]func (context.Context , timeutil.TimeInterval , int ) (dashboard.MailTrafficPerSenderOverTimeResult , error ){
228+ "/api/v0/sentMailsByMailbox" : d .SentMailsByMailbox ,
229+ "/api/v0/bouncedMailsByMailbox" : d .BouncedMailsByMailbox ,
230+ "/api/v0/deferredMailsByMailbox" : d .DeferredMailsByMailbox ,
231+ "/api/v0/expiredMailsByMailbox" : d .ExpiredMailsByMailbox ,
232+ "/api/v0/receivedMailsByMailbox" : d .ReceivedMailsByMailbox ,
233+ "/api/v0/inboundRepliesByMailbox" : d .InboundRepliesByMailbox ,
234+ } {
235+ mux .Handle (k , authenticated .WithEndpoint (trafficBySenderOverTimeHandler {v }))
236+ }
237+
238+ mux .Handle ("/api/v0/countByStatus" , authenticated .WithEndpoint (countByStatusHandler {d }))
239+ mux .Handle ("/api/v0/topBusiestDomains" , authenticated .WithEndpoint (topBusiestDomainsHandler {d }))
240+ mux .Handle ("/api/v0/topBouncedDomains" , authenticated .WithEndpoint (topBouncedDomainsHandler {d }))
241+ mux .Handle ("/api/v0/topDeferredDomains" , authenticated .WithEndpoint (topDeferredDomainsHandler {d }))
242+ mux .Handle ("/api/v0/deliveryStatus" , authenticated .WithEndpoint (deliveryStatusHandler {d }))
156243 mux .Handle ("/api/v0/appVersion" , unauthenticated .WithEndpoint (appVersionHandler {}))
157244}
0 commit comments