Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ Run only one application instance per database. Back up SQLite using its backup

- `GET /api/targets` — target status and most recent latency
- `POST /api/targets` with `{"address":"1.1.1.1"}` — add a target
- `GET /api/targets/{id}/history?range=1h` — aggregated samples and overlapping outages for `1h` (default), `3h`, `8h`, `1d`, `2d`, or `7d`; includes window `start`/`end` (Unix seconds; null latency means a bucket contains failures)
- `GET /api/targets/{id}/history?range=1h` — aggregated samples and overlapping outages for `1h` (default), `3h`, `8h`, `1d`, `2d`, or `7d`; includes window `start`/`end` (Unix seconds; null latency means a bucket contains failures). Alternatively pass `?start=<unix>&end=<unix>` for a custom window of up to 30 days
- `DELETE /api/targets/{id}` — remove a target and its history
24 changes: 17 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,24 @@ def do_GET(self):
target_id = int(match.group(1))
if not db.execute('SELECT id FROM targets WHERE id=?', (target_id,)).fetchone():
return self.respond(404, {'error': 'Target not found'})
ranges = {'1h': 3600, '3h': 10800, '8h': 28800, '1d': 86400, '2d': 172800, '7d': 604800}
period = parse_qs(urlsplit(self.path).query).get('range', ['1h'])[0]
if period not in ranges:
return self.respond(400, {'error': 'Invalid range'})
end = time.time()
start = end - ranges[period]
query = parse_qs(urlsplit(self.path).query)
if 'start' in query or 'end' in query:
# Custom window: explicit epoch seconds, at most 30 days wide.
try:
start, end = float(query['start'][0]), float(query['end'][0])
except (KeyError, ValueError):
return self.respond(400, {'error': 'Invalid range'})
if not (end > start and end - start <= 30 * 86400):
return self.respond(400, {'error': 'Invalid range'})
else:
ranges = {'1h': 3600, '3h': 10800, '8h': 28800, '1d': 86400, '2d': 172800, '7d': 604800}
period = query.get('range', ['1h'])[0]
if period not in ranges:
return self.respond(400, {'error': 'Invalid range'})
end = time.time()
start = end - ranges[period]
# At most 1,000 buckets; mixed success/failure buckets remain gaps.
width = ranges[period] / 1000
width = (end - start) / 1000
samples = db.execute('''
SELECT AVG(timestamp) AS timestamp,
CASE WHEN COUNT(latency)=COUNT(*) THEN AVG(latency) END AS latency
Expand Down
Loading
Loading