Fix db returning not the newst stats

Getting stats for a host returned allways the oldest item.
Now inserting an item stores it at the beginning of the list ->
without changing the getting stategy it will now return the newst item.
This commit is contained in:
Augusto Dwenger 2020-11-11 10:25:59 +01:00
parent 131a71620d
commit 6d9049820f

View file

@ -88,10 +88,18 @@ func (db *InMemoryDB) InsertStats(hostname string, stats Stats) error {
return nil
}
host.Stats = append(host.Stats, stats)
host.Stats = db.insertAtBeginning(host.Stats, stats)
host.HostInfo.DataPoints++
host.HostInfo.LastInsert = time.Now()
db.storage[hostname] = host
return nil
}
func (db *InMemoryDB) insertAtBeginning(stats []Stats, stat Stats) []Stats {
stats = append(stats, Stats{})
copy(stats[1:], stats)
stats[0] = stat
return stats
}