@@ -126,3 +126,63 @@ func ResponseTimeChart(c *fiber.Ctx) error {
126126 }
127127 return nil
128128}
129+
130+ func ResponseTimeHistory (c * fiber.Ctx ) error {
131+ duration := c .Params ("duration" )
132+ var from time.Time
133+ switch duration {
134+ case "30d" :
135+ from = time .Now ().Truncate (time .Hour ).Add (- 30 * 24 * time .Hour )
136+ case "7d" :
137+ from = time .Now ().Truncate (time .Hour ).Add (- 7 * 24 * time .Hour )
138+ case "24h" :
139+ from = time .Now ().Truncate (time .Hour ).Add (- 24 * time .Hour )
140+ default :
141+ return c .Status (400 ).SendString ("Durations supported: 30d, 7d, 24h" )
142+ }
143+ endpointKey , err := url .QueryUnescape (c .Params ("key" ))
144+ if err != nil {
145+ return c .Status (400 ).SendString ("invalid key encoding" )
146+ }
147+ hourlyAverageResponseTime , err := store .Get ().GetHourlyAverageResponseTimeByKey (endpointKey , from , time .Now ())
148+ if err != nil {
149+ if errors .Is (err , common .ErrEndpointNotFound ) {
150+ return c .Status (404 ).SendString (err .Error ())
151+ }
152+ if errors .Is (err , common .ErrInvalidTimeRange ) {
153+ return c .Status (400 ).SendString (err .Error ())
154+ }
155+ return c .Status (500 ).SendString (err .Error ())
156+ }
157+ if len (hourlyAverageResponseTime ) == 0 {
158+ return c .Status (200 ).JSON (map [string ]interface {}{
159+ "timestamps" : []int64 {},
160+ "values" : []int {},
161+ })
162+ }
163+ hourlyTimestamps := make ([]int , 0 , len (hourlyAverageResponseTime ))
164+ earliestTimestamp := int64 (0 )
165+ for hourlyTimestamp := range hourlyAverageResponseTime {
166+ hourlyTimestamps = append (hourlyTimestamps , int (hourlyTimestamp ))
167+ if earliestTimestamp == 0 || hourlyTimestamp < earliestTimestamp {
168+ earliestTimestamp = hourlyTimestamp
169+ }
170+ }
171+ for earliestTimestamp > from .Unix () {
172+ earliestTimestamp -= int64 (time .Hour .Seconds ())
173+ hourlyTimestamps = append (hourlyTimestamps , int (earliestTimestamp ))
174+ }
175+ sort .Ints (hourlyTimestamps )
176+ timestamps := make ([]int64 , 0 , len (hourlyTimestamps ))
177+ values := make ([]int , 0 , len (hourlyTimestamps ))
178+ for _ , hourlyTimestamp := range hourlyTimestamps {
179+ timestamp := int64 (hourlyTimestamp )
180+ averageResponseTime := hourlyAverageResponseTime [timestamp ]
181+ timestamps = append (timestamps , timestamp * 1000 )
182+ values = append (values , averageResponseTime )
183+ }
184+ return c .Status (http .StatusOK ).JSON (map [string ]interface {}{
185+ "timestamps" : timestamps ,
186+ "values" : values ,
187+ })
188+ }
0 commit comments