add default targets for search results (#979)

Adds 5 most recently seen hosts + labels to the search targets response if the query is an empty string. 

Closes #921
This commit is contained in:
Victor Vrantchan 2017-01-17 09:51:04 -05:00 committed by GitHub
parent 89c0ac393b
commit 25c41cda94
2 changed files with 75 additions and 6 deletions

View file

@ -526,17 +526,53 @@ func (d *Datastore) searchHostsWithOmits(query string, omit ...uint) ([]*kolide.
return hosts, nil
}
func (d *Datastore) searchHostsDefault(omit ...uint) ([]*kolide.Host, error) {
sqlStatement := `
SELECT * FROM hosts
WHERE NOT deleted
AND id NOT IN (?)
ORDER BY seen_time DESC
LIMIT 5
`
var in interface{}
{
// use -1 if there are no values to omit.
//Avoids empty args error for `sqlx.In`
in = omit
if len(omit) == 0 {
in = -1
}
}
var hosts []*kolide.Host
sql, args, err := sqlx.In(sqlStatement, in)
if err != nil {
return nil, errors.Wrap(err, "searching default hosts")
}
sql = d.db.Rebind(sql)
err = d.db.Select(&hosts, sql, args...)
if err != nil {
return nil, errors.Wrap(err, "searching default hosts rebound")
}
if err := d.getNetInterfacesForHosts(hosts); err != nil {
return nil, errors.Wrap(err, "getting network interfaces for default search hosts")
}
return hosts, nil
}
// SearchHosts find hosts by query containing an IP address or a host name. Optionally
// pass a list of IDs to omit from the search
func (d *Datastore) SearchHosts(query string, omit ...uint) ([]*kolide.Host, error) {
if query == "" {
return d.searchHostsDefault(omit...)
}
if len(omit) > 0 {
return d.searchHostsWithOmits(query, omit...)
}
hostnameQuery := query
if len(hostnameQuery) > 0 {
hostnameQuery += "*"
}
hostnameQuery += "*"
// Needs quotes to avoid each . marking a word boundary
ipQuery := `"` + query + `"`

View file

@ -248,15 +248,48 @@ func (d *Datastore) searchLabelsWithOmits(query string, omit ...uint) ([]kolide.
return matches, nil
}
func (d *Datastore) searchLabelsDefault(omit ...uint) ([]kolide.Label, error) {
sqlStatement := `
SELECT *
FROM labels
WHERE NOT deleted
AND id NOT IN (?)
LIMIT 5
`
var in interface{}
{
// use -1 if there are no values to omit.
//Avoids empty args error for `sqlx.In`
in = omit
if len(omit) == 0 {
in = -1
}
}
var labels []kolide.Label
sql, args, err := sqlx.In(sqlStatement, in)
if err != nil {
return nil, errors.Wrap(err, "searching default labels")
}
sql = d.db.Rebind(sql)
err = d.db.Select(&labels, sql, args...)
if err != nil {
return nil, errors.Wrap(err, "searching default labels rebound")
}
return labels, nil
}
// SearchLabels performs wildcard searches on kolide.Label name
func (d *Datastore) SearchLabels(query string, omit ...uint) ([]kolide.Label, error) {
if query == "" {
return d.searchLabelsDefault(omit...)
}
if len(omit) > 0 {
return d.searchLabelsWithOmits(query, omit...)
}
if len(query) > 0 {
query += "*"
}
query += "*"
sqlStatement := `
SELECT *