Improve precision in reporting of unseen hosts via host status webhook automations (#5889)

This commit is contained in:
gillespi314 2022-05-25 10:54:56 -05:00 committed by GitHub
parent b7e59f23b4
commit 85c6ca0e5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -0,0 +1 @@
- Switched from `DATEDIFF` to `TIMESTAMPDIFF` to improve precision when calculating unseen hosts reported by host status webhook

View file

@ -981,15 +981,20 @@ func (ds *Datastore) TotalAndUnseenHostsSince(ctx context.Context, daysCount int
Total int `db:"total"`
Unseen int `db:"unseen"`
}
// convert daysCount to integer number of seconds for more precision in sql query
unseenSeconds := daysCount * 24 * 60 * 60
err = sqlx.GetContext(ctx, ds.reader, &counts,
`SELECT
COUNT(*) as total,
SUM(IF(DATEDIFF(CURRENT_DATE, COALESCE(hst.seen_time, h.created_at)) >= ?, 1, 0)) as unseen
SUM(IF(TIMESTAMPDIFF(SECOND, COALESCE(hst.seen_time, h.created_at), CURRENT_TIMESTAMP) >= ?, 1, 0)) as unseen
FROM hosts h
LEFT JOIN host_seen_times hst
ON h.id = hst.host_id`,
daysCount,
unseenSeconds,
)
if err != nil {
return 0, 0, ctxerr.Wrap(ctx, err, "getting total and unseen host counts")
}

View file

@ -1815,6 +1815,24 @@ func testHostsTotalAndUnseenSince(t *testing.T, ds *Datastore) {
require.NoError(t, err)
assert.Equal(t, 3, total)
assert.Equal(t, 2, unseen)
// host not counted as unseen if less than a full 24 hours has passed
_, err = ds.writer.ExecContext(context.Background(), `UPDATE host_seen_times SET seen_time = ? WHERE host_id = 2`, time.Now().Add(-1*time.Duration(1)*86399*time.Second))
require.NoError(t, err)
total, unseen, err = ds.TotalAndUnseenHostsSince(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, 3, total)
assert.Equal(t, 1, unseen)
// host counted as unseen if more than 24 hours has passed
_, err = ds.writer.ExecContext(context.Background(), `UPDATE host_seen_times SET seen_time = ? WHERE host_id = 2`, time.Now().Add(-1*time.Duration(1)*86401*time.Second))
require.NoError(t, err)
total, unseen, err = ds.TotalAndUnseenHostsSince(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, 3, total)
assert.Equal(t, 2, unseen)
}
func testHostsListByPolicy(t *testing.T, ds *Datastore) {