mirror of https://github.com/grafana/grafana
parent
27acaf3830
commit
c1fcfdb536
@ -0,0 +1,66 @@ |
||||
import _ from 'lodash'; |
||||
import coreModule from 'app/core/core_module'; |
||||
|
||||
export class SearchSrv { |
||||
|
||||
/** @ngInject */ |
||||
constructor(private backendSrv) { |
||||
} |
||||
|
||||
search(query) { |
||||
return this.backendSrv.search(query).then(results => { |
||||
|
||||
let sections: any = {}; |
||||
|
||||
// sections["starred"] = {
|
||||
// score: 0,
|
||||
// icon: 'fa fa-star-o',
|
||||
// title: "Starred dashboards",
|
||||
// items: [
|
||||
// {title: 'Frontend Nginx'},
|
||||
// {title: 'Cassandra overview'}
|
||||
// ]
|
||||
// };
|
||||
//
|
||||
// sections["recent"] = {
|
||||
// score: 1,
|
||||
// icon: 'fa fa-clock-o',
|
||||
// title: "Recent dashboards",
|
||||
// items: [
|
||||
// {title: 'Frontend Nginx'},
|
||||
// {title: 'Cassandra overview'}
|
||||
// ]
|
||||
// };
|
||||
|
||||
// create folder index
|
||||
for (let hit of results) { |
||||
let section = sections[hit.folderId]; |
||||
if (!section) { |
||||
section = { |
||||
id: hit.folderId, |
||||
title: hit.folderTitle, |
||||
items: [], |
||||
icon: 'fa fa-folder-open' |
||||
}; |
||||
// handle root
|
||||
if (!hit.folderId) { |
||||
section.title = "Dashboards"; |
||||
section.icon = "fa fa-circle-o"; |
||||
} |
||||
sections[hit.folderId] = section; |
||||
} |
||||
|
||||
hit.url = 'dashboard/' + hit.uri; |
||||
section.items.push(hit); |
||||
} |
||||
|
||||
return _.sortBy(_.values(sections), 'score'); |
||||
}); |
||||
} |
||||
|
||||
getDashboardTags() { |
||||
return this.backendSrv.get('/api/dashboards/tags'); |
||||
} |
||||
} |
||||
|
||||
coreModule.service('searchSrv', SearchSrv); |
||||
@ -0,0 +1,48 @@ |
||||
import { SearchSrv } from 'app/core/services/search_srv'; |
||||
import { BackendSrvMock } from 'test/mocks/backend_srv'; |
||||
|
||||
describe('SearchSrv', () => { |
||||
let searchSrv, backendSrvMock; |
||||
|
||||
beforeEach(() => { |
||||
backendSrvMock = new BackendSrvMock(); |
||||
searchSrv = new SearchSrv(backendSrvMock); |
||||
}); |
||||
|
||||
describe("with no query string and dashboards with folders returned", () => { |
||||
let results; |
||||
|
||||
beforeEach(() => { |
||||
backendSrvMock.search = jest.fn().mockReturnValue(Promise.resolve([ |
||||
{ |
||||
title: 'dash with no folder', |
||||
}, |
||||
{ |
||||
title: 'dash in folder1 1', |
||||
folderId: 1, |
||||
folderTitle: 'folder1' |
||||
}, |
||||
{ |
||||
title: 'dash in folder1 2', |
||||
folderId: 1, |
||||
folderTitle: 'folder1' |
||||
}, |
||||
{ |
||||
title: 'dahs in folder2 1', |
||||
folderId: 2, |
||||
folderTitle: 'folder2' |
||||
} |
||||
])); |
||||
|
||||
return searchSrv.search({query: ''}).then(res => { |
||||
results = res; |
||||
}); |
||||
}); |
||||
|
||||
it("should create sections for each folder and root", () => { |
||||
expect(results).toHaveLength(3); |
||||
}); |
||||
|
||||
}); |
||||
|
||||
}); |
||||
@ -0,0 +1,8 @@ |
||||
export class BackendSrvMock { |
||||
search: any; |
||||
|
||||
constructor() { |
||||
} |
||||
|
||||
} |
||||
|
||||
Loading…
Reference in new issue