search_srv.ts 5.7 KB

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