-
Notifications
You must be signed in to change notification settings - Fork 403
/
call_list.go
75 lines (62 loc) · 1.53 KB
/
call_list.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
package server
import (
"net/http"
"strconv"
"time"
"github.com/fnproject/fn/api"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/models"
"github.com/gin-gonic/gin"
)
func (s *Server) handleCallList(c *gin.Context) {
ctx := c.Request.Context()
var err error
fnID := c.Param(api.FnID)
if fnID == "" {
handleErrorResponse(c, models.ErrFnsMissingID)
return
}
_, err = s.datastore.GetFnByID(ctx, c.Param(api.FnID))
if err != nil {
handleErrorResponse(c, err)
return
}
filter := models.CallFilter{FnID: fnID}
filter.Cursor, filter.PerPage = pageParams(c)
filter.FromTime, filter.ToTime, err = timeParams(c)
if err != nil {
handleErrorResponse(c, err)
return
}
calls, err := s.logstore.GetCalls(ctx, &filter)
if err != nil {
handleErrorResponse(c, err)
}
c.JSON(http.StatusOK, calls)
}
// "" gets parsed to a zero time, which is fine (ignored in query)
func timeParams(c *gin.Context) (fromTime, toTime common.DateTime, err error) {
fromStr := c.Query("from_time")
toStr := c.Query("to_time")
var ok bool
if fromStr != "" {
fromTime, ok = strToTime(fromStr)
if !ok {
return fromTime, toTime, models.ErrInvalidFromTime
}
}
if toStr != "" {
toTime, ok = strToTime(toStr)
if !ok {
return fromTime, toTime, models.ErrInvalidToTime
}
}
return fromTime, toTime, nil
}
func strToTime(str string) (common.DateTime, bool) {
sec, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return common.DateTime(time.Time{}), false
}
return common.DateTime(time.Unix(sec, 0)), true
}