worked on search

This commit is contained in:
Torkel Ödegaard
2017-11-24 16:18:56 +01:00
parent 93d21bec75
commit bc81298d4c
13 changed files with 277 additions and 110 deletions

View File

@@ -1,23 +1,87 @@
import _ from 'lodash';
import coreModule from 'app/core/core_module';
import impressionSrv from 'app/core/services/impression_srv';
import store from 'app/core/store';
export class SearchSrv {
recentIsOpen: boolean;
starredIsOpen: boolean;
/** @ngInject */
constructor(private backendSrv) {
constructor(private backendSrv, private $q) {
this.recentIsOpen = store.getBool('search.sections.recent', true);
this.starredIsOpen = store.getBool('search.sections.starred', true);
}
browse() {
private getRecentDashboards(sections) {
return this.queryForRecentDashboards().then(result => {
if (result.length > 0) {
sections['recent'] = {
title: 'Recent Boards',
icon: 'fa fa-clock-o',
score: -1,
expanded: this.recentIsOpen,
toggle: this.toggleRecent.bind(this),
items: result,
};
}
});
}
private queryForRecentDashboards() {
var dashIds = _.take(impressionSrv.getDashboardOpened(), 5);
if (dashIds.length === 0) {
return Promise.resolve([]);
}
return this.backendSrv.search({ dashboardIds: dashIds }).then(result => {
return dashIds.map(orderId => {
return this.transformToViewModel(_.find(result, { id: orderId }));
}).filter(item => !item.isStarred);
});
}
private toggleRecent(section) {
this.recentIsOpen = section.expanded = !section.expanded;
store.set('search.sections.recent', this.recentIsOpen);
if (!section.expanded || section.items.length) {
return;
}
return this.queryForRecentDashboards().then(result => {
section.items = result;
});
}
private toggleStarred(section) {
this.starredIsOpen = section.expanded = !section.expanded;
store.set('search.sections.starred', this.starredIsOpen);
}
private getStarred(sections) {
return this.backendSrv.search({starred: true, limit: 5}).then(result => {
if (result.length > 0) {
sections['starred'] = {
title: 'Starred Boards',
icon: 'fa fa-star-o',
score: -2,
expanded: this.starredIsOpen,
toggle: this.toggleStarred.bind(this),
items: this.transformToViewModel(result),
};
}
});
}
private getDashboardsAndFolders(sections) {
const rootFolderId = 0;
let query = {
folderIds: [rootFolderId]
folderIds: [rootFolderId],
};
return this.backendSrv.search(query).then(results => {
let sections: any = {};
for (let hit of results) {
if (hit.type === 'dash-folder') {
sections[hit.id] = {
@@ -26,7 +90,8 @@ export class SearchSrv {
items: [],
icon: 'fa fa-folder',
score: _.keys(sections).length,
uri: hit.uri
uri: hit.uri,
toggle: this.toggleFolder.bind(this),
};
}
}
@@ -37,7 +102,7 @@ export class SearchSrv {
items: [],
icon: 'fa fa-folder-open',
score: _.keys(sections).length,
expanded: true
expanded: true,
};
for (let hit of results) {
@@ -45,18 +110,36 @@ export class SearchSrv {
continue;
}
let section = sections[hit.folderId || 0];
hit.url = 'dashboard/' + hit.uri;
section.items.push(hit);
if (section) {
section.items.push(this.transformToViewModel(hit));
} else {
console.log('Error: dashboard returned from browse search but not folder', hit.id, hit.folderId);
}
}
});
}
private browse() {
let sections: any = {};
let promises = [
this.getRecentDashboards(sections),
this.getStarred(sections),
this.getDashboardsAndFolders(sections),
];
return this.$q.all(promises).then(() => {
return _.sortBy(_.values(sections), 'score');
});
}
private transformToViewModel(hit) {
hit.url = 'dashboard/' + hit.uri;
return hit;
}
search(options) {
if (!options.query &&
(!options.tag || options.tag.length === 0) &&
!options.starred) {
if (!options.query && (!options.tag || options.tag.length === 0) && !options.starred) {
return this.browse();
}
@@ -65,7 +148,6 @@ export class SearchSrv {
query.type = 'dash-db';
return this.backendSrv.search(query).then(results => {
let section = {
hideHeader: true,
items: [],
@@ -76,15 +158,14 @@ export class SearchSrv {
if (hit.type === 'dash-folder') {
continue;
}
hit.url = 'dashboard/' + hit.uri;
section.items.push(hit);
section.items.push(this.transformToViewModel(hit));
}
return [section];
});
}
toggleFolder(section) {
private toggleFolder(section) {
section.expanded = !section.expanded;
section.icon = section.expanded ? 'fa fa-folder-open' : 'fa fa-folder';
@@ -93,17 +174,18 @@ export class SearchSrv {
}
let query = {
folderIds: [section.id]
folderIds: [section.id],
};
return this.backendSrv.search(query).then(results => {
for (let hit of results) {
hit.url = 'dashboard/' + hit.uri;
section.items.push(hit);
}
section.items = _.map(results, this.transformToViewModel);
});
}
toggleSection(section) {
section.toggle(section);
}
getDashboardTags() {
return this.backendSrv.get('/api/dashboards/tags');
}