search_srv.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import _ from 'lodash';
  2. // @ts-ignore
  3. import { IQService } from 'angular';
  4. import coreModule from 'app/core/core_module';
  5. import impressionSrv from 'app/core/services/impression_srv';
  6. import store from 'app/core/store';
  7. import { contextSrv } from 'app/core/services/context_srv';
  8. import { BackendSrv } from './backend_srv';
  9. import { Section } from '../components/manage_dashboards/manage_dashboards';
  10. import { DashboardSearchHit } from 'app/types/search';
  11. interface Sections {
  12. [key: string]: Partial<Section>;
  13. }
  14. export class SearchSrv {
  15. recentIsOpen: boolean;
  16. starredIsOpen: boolean;
  17. /** @ngInject */
  18. constructor(private backendSrv: BackendSrv, private $q: IQService) {
  19. this.recentIsOpen = store.getBool('search.sections.recent', true);
  20. this.starredIsOpen = store.getBool('search.sections.starred', true);
  21. }
  22. private getRecentDashboards(sections: Sections) {
  23. return this.queryForRecentDashboards().then((result: any[]) => {
  24. if (result.length > 0) {
  25. sections['recent'] = {
  26. title: 'Recent',
  27. icon: 'fa fa-clock-o',
  28. score: -1,
  29. removable: true,
  30. expanded: this.recentIsOpen,
  31. toggle: this.toggleRecent.bind(this),
  32. items: result,
  33. };
  34. }
  35. });
  36. }
  37. private queryForRecentDashboards(): Promise<DashboardSearchHit[]> {
  38. const dashIds: number[] = _.take(impressionSrv.getDashboardOpened(), 30);
  39. if (dashIds.length === 0) {
  40. return Promise.resolve([]);
  41. }
  42. return this.backendSrv.search({ dashboardIds: dashIds }).then(result => {
  43. return dashIds
  44. .map(orderId => {
  45. return _.find(result, { id: orderId });
  46. })
  47. .filter(hit => hit && !hit.isStarred);
  48. });
  49. }
  50. private toggleRecent(section: Section) {
  51. this.recentIsOpen = section.expanded = !section.expanded;
  52. store.set('search.sections.recent', this.recentIsOpen);
  53. if (!section.expanded || section.items.length) {
  54. return Promise.resolve(section);
  55. }
  56. return this.queryForRecentDashboards().then(result => {
  57. section.items = result;
  58. return Promise.resolve(section);
  59. });
  60. }
  61. private toggleStarred(section: Section) {
  62. this.starredIsOpen = section.expanded = !section.expanded;
  63. store.set('search.sections.starred', this.starredIsOpen);
  64. return Promise.resolve(section);
  65. }
  66. private getStarred(sections: Sections) {
  67. if (!contextSrv.isSignedIn) {
  68. return Promise.resolve();
  69. }
  70. return this.backendSrv.search({ starred: true, limit: 30 }).then(result => {
  71. if (result.length > 0) {
  72. sections['starred'] = {
  73. title: 'Starred',
  74. icon: 'fa fa-star-o',
  75. score: -2,
  76. expanded: this.starredIsOpen,
  77. toggle: this.toggleStarred.bind(this),
  78. items: result,
  79. };
  80. }
  81. });
  82. }
  83. search(options: any) {
  84. const sections: any = {};
  85. const promises = [];
  86. const query = _.clone(options);
  87. const hasFilters =
  88. options.query ||
  89. (options.tag && options.tag.length > 0) ||
  90. options.starred ||
  91. (options.folderIds && options.folderIds.length > 0);
  92. if (!options.skipRecent && !hasFilters) {
  93. promises.push(this.getRecentDashboards(sections));
  94. }
  95. if (!options.skipStarred && !hasFilters) {
  96. promises.push(this.getStarred(sections));
  97. }
  98. query.folderIds = query.folderIds || [];
  99. if (!hasFilters) {
  100. query.folderIds = [0];
  101. }
  102. promises.push(
  103. this.backendSrv.search(query).then(results => {
  104. return this.handleSearchResult(sections, results);
  105. })
  106. );
  107. return this.$q.all(promises).then(() => {
  108. return _.sortBy(_.values(sections), 'score');
  109. });
  110. }
  111. private handleSearchResult(sections: Sections, results: DashboardSearchHit[]): any {
  112. if (results.length === 0) {
  113. return sections;
  114. }
  115. // create folder index
  116. for (const hit of results) {
  117. if (hit.type === 'dash-folder') {
  118. sections[hit.id] = {
  119. id: hit.id,
  120. uid: hit.uid,
  121. title: hit.title,
  122. expanded: false,
  123. items: [],
  124. toggle: this.toggleFolder.bind(this),
  125. url: hit.url,
  126. icon: 'fa fa-folder',
  127. score: _.keys(sections).length,
  128. };
  129. }
  130. }
  131. for (const hit of results) {
  132. if (hit.type === 'dash-folder') {
  133. continue;
  134. }
  135. let section = sections[hit.folderId || 0];
  136. if (!section) {
  137. if (hit.folderId) {
  138. section = {
  139. id: hit.folderId,
  140. uid: hit.folderUid,
  141. title: hit.folderTitle,
  142. url: hit.folderUrl,
  143. items: [],
  144. icon: 'fa fa-folder-open',
  145. toggle: this.toggleFolder.bind(this),
  146. score: _.keys(sections).length,
  147. };
  148. } else {
  149. section = {
  150. id: 0,
  151. title: 'General',
  152. items: [],
  153. icon: 'fa fa-folder-open',
  154. toggle: this.toggleFolder.bind(this),
  155. score: _.keys(sections).length,
  156. };
  157. }
  158. // add section
  159. sections[hit.folderId || 0] = section;
  160. }
  161. section.expanded = true;
  162. section.items.push(hit);
  163. }
  164. }
  165. private toggleFolder(section: Section) {
  166. section.expanded = !section.expanded;
  167. section.icon = section.expanded ? 'fa fa-folder-open' : 'fa fa-folder';
  168. if (section.items.length) {
  169. return Promise.resolve(section);
  170. }
  171. const query = {
  172. folderIds: [section.id],
  173. };
  174. return this.backendSrv.search(query).then(results => {
  175. section.items = results;
  176. return Promise.resolve(section);
  177. });
  178. }
  179. getDashboardTags() {
  180. return this.backendSrv.get('/api/dashboards/tags');
  181. }
  182. }
  183. coreModule.service('searchSrv', SearchSrv);