grafana/public/app/core/services/search_srv.ts

168 lines
4.6 KiB
TypeScript
Raw Normal View History

import { clone, keys, sortBy, take, values } from 'lodash';
import { contextSrv } from 'app/core/services/context_srv';
2017-12-20 05:33:33 -06:00
import impressionSrv from 'app/core/services/impression_srv';
import store from 'app/core/store';
import { SECTION_STORAGE_KEY } from 'app/features/search/constants';
import { DashboardSection, DashboardSearchItemType, DashboardSearchHit, SearchLayout } from 'app/features/search/types';
import { hasFilters } from 'app/features/search/utils';
Chore: Remove angular dependency from backendSrv (#20999) * Chore: Remove angular dependency from backendSrv * Refactor: Naive soultion for logging out unauthorized users * Refactor: Restructures to different streams * Refactor: Restructures datasourceRequest * Refactor: Flipped back if statement * Refactor: Extracted getFromFetchStream * Refactor: Extracts toFailureStream operation * Refactor: Fixes issue when options.params contains arrays * Refactor: Fixes broken test (but we need a lot more) * Refactor: Adds explaining comments * Refactor: Adds latest RxJs version so cancellations work * Refactor: Cleans up the takeUntil code * Refactor: Adds tests for request function * Refactor: Separates into smaller functions * Refactor: Adds last error tests * Started to changed so we require getBackendSrv from the @grafana-runtime when applicable. * Using the getBackendSrv from @grafana/runtime. * Changed so we use the getBackendSrv from the @grafana-runtime when possible. * Fixed so Server Admin -> Orgs works again. * Removed unused dependency. * Fixed digest issues on the Server Admin -> Users page. * Fix: Fixes digest problems in Playlists * Fix: Fixes digest issues in VersionHistory * Tests: Fixes broken tests * Fix: Fixes digest issues in Alerting => Notification channels * Fixed digest issues on the Intive page. * Fixed so we run digest after password reset email sent. * Fixed digest issue when trying to sign up account. * Fixed so the Server Admin -> Edit Org works with backendSrv * Fixed so Server Admin -> Users works with backend srv. * Fixed digest issues in Server Admin -> Orgs * Fix: Fixes digest issues in DashList plugin * Fixed digest issues on Server Admin -> users. * Fix: Fixes digest issues with Snapshots * Fixed digest issue when deleting a user. * Fix: Fixes digest issues with dashLink * Chore: Changes RxJs version to 6.5.4 which includes the same cancellation fix * Fix: Fixes digest issue when toggling folder in manage dashboards * Fix: Fixes bug in executeInOrder * Fix: Fixes digest issue with CreateFolderCtrl and FolderDashboardsCtrl * Fix: Fixes tslint error in test * Refactor: Changes default behaviour for emitted messages as before migration * Fix: Fixes various digest issues when saving, starring or deleting dashboards * Fix: Fixes digest issues with FolderPickerCtrl * Fixed digest issue. * Fixed digest issues. * Fixed issues with angular digest. * Removed the this.digest pattern. Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com> Co-authored-by: Marcus Andersson <systemvetaren@gmail.com>
2020-01-21 03:08:07 -06:00
import { backendSrv } from './backend_srv';
interface Sections {
[key: string]: Partial<DashboardSection>;
}
export class SearchSrv {
private getRecentDashboards(sections: DashboardSection[] | any) {
return this.queryForRecentDashboards().then((result: any[]) => {
2017-11-24 09:18:56 -06:00
if (result.length > 0) {
2017-12-20 05:33:33 -06:00
sections['recent'] = {
title: 'Recent',
icon: 'clock-nine',
2017-11-24 09:18:56 -06:00
score: -1,
expanded: store.getBool(`${SECTION_STORAGE_KEY}.recent`, true),
2017-12-20 05:33:33 -06:00
items: result,
type: DashboardSearchItemType.DashFolder,
2017-11-24 09:18:56 -06:00
};
}
});
}
2019-04-15 05:11:52 -05:00
private queryForRecentDashboards(): Promise<DashboardSearchHit[]> {
const dashIds: number[] = take(impressionSrv.getDashboardOpened(), 30);
2017-11-24 09:18:56 -06:00
if (dashIds.length === 0) {
return Promise.resolve([]);
}
return backendSrv.search({ dashboardIds: dashIds }).then((result) => {
return dashIds
.map((orderId) => result.find((result) => result.id === orderId))
.filter((hit) => hit && !hit.isStarred) as DashboardSearchHit[];
2017-11-24 09:18:56 -06:00
});
}
private getStarred(sections: DashboardSection): Promise<any> {
2017-11-24 09:54:55 -06:00
if (!contextSrv.isSignedIn) {
return Promise.resolve();
}
return backendSrv.search({ starred: true, limit: 30 }).then((result) => {
2017-11-24 09:18:56 -06:00
if (result.length > 0) {
(sections as any)['starred'] = {
2017-12-20 05:33:33 -06:00
title: 'Starred',
icon: 'star',
2017-11-24 09:18:56 -06:00
score: -2,
expanded: store.getBool(`${SECTION_STORAGE_KEY}.starred`, true),
items: result,
type: DashboardSearchItemType.DashFolder,
2017-11-24 09:18:56 -06:00
};
}
});
}
search(options: any) {
const sections: any = {};
const promises = [];
const query = clone(options);
const filters = hasFilters(options) || query.folderIds?.length > 0;
query.folderIds = query.folderIds || [];
if (query.layout === SearchLayout.List) {
return backendSrv
.search({ ...query, type: DashboardSearchItemType.DashDB })
.then((results) => (results.length ? [{ title: '', items: results }] : []));
}
if (!filters) {
query.folderIds = [0];
}
if (!options.skipRecent && !filters) {
promises.push(this.getRecentDashboards(sections));
}
if (!options.skipStarred && !filters) {
promises.push(this.getStarred(sections));
}
promises.push(
backendSrv.search(query).then((results) => {
return this.handleSearchResult(sections, results);
})
);
2017-11-24 09:18:56 -06:00
return Promise.all(promises).then(() => {
return sortBy(values(sections), 'score');
});
}
private handleSearchResult(sections: Sections, results: DashboardSearchHit[]): any {
2017-12-13 08:51:59 -06:00
if (results.length === 0) {
return sections;
2017-11-20 08:11:32 -06:00
}
2017-12-13 08:51:59 -06:00
// create folder index
for (const hit of results) {
2017-12-20 05:33:33 -06:00
if (hit.type === 'dash-folder') {
2017-12-13 08:51:59 -06:00
sections[hit.id] = {
id: hit.id,
uid: hit.uid,
2017-12-13 08:51:59 -06:00
title: hit.title,
expanded: false,
items: [],
url: hit.url,
Search/migrate search results (#22930) * Search: Setup SearchResults.tsx * Search: add watchDepth * Search: Use SearchResults.tsx in Angular template * Search: Render search result header * Search: Move new search components to features/search * Search: Render nested dashboards * Search: Expand dashboard folder * Search: Remove fa prefix from icon names * Search: Enable search results toggling * Search: Add onItemClick handler * Search: Add missing aria-label * Search: Add no results message * Search: Fix e2e selectors * Search: Update SearchField imports * Search: Add conditional classes * Search: Abstract DashboardCheckbox * Search: Separate ResultItem * Search: Style ResultItem * Search: Separate search components * Search: Tweak checkbox styling * Search: Simplify component names * Search: Separate tag component * Search: Checkbox docs * Search: Remove inline on click * Add Tag component * Add Tag story * Add TagList * Group Tab and TabList * Fix typechecks * Remove Meta * Use forwardRef for the Tag * Search: Use TagList from grafana/ui * Search: Add media query for TagList * Search: Add types * Search: Remove selectThemeVariant from SearchItem.tsx * Search: Style section + header * Search: Use semantic html * Search: Adjust section padding * Search: Setup tests * Search: Fix tests * Search: tweak result styles * Search: Expand SearchResults tests * Search: Add SearchItem tests * Search: Use SearchResults in search.html * Search: Toggle search result sections * Search: Make selected prop optional * Search: Fix tag selection * Search: Fix tag filter onChange * Search: Fix uncontrolled state change warning * Search: Update icon names * Search: memoize SearchCheckbox.tsx * Search: Update types * Search: Cleanup events * Search: Semantic html * Use styleMixins * Search: Tweak styling * Search: useCallback for checkbox toggle * Search: Add stylesFactory Co-authored-by: CirceCI <circleci@grafana.com>
2020-03-26 04:09:08 -05:00
icon: 'folder',
score: keys(sections).length,
Search/refactor dashboard search (#23274) * Search: add search wrapper * Search: add DashboardSearch.tsx * Search: enable search * Search: update types * Search: useReducer for saving search results * Search: use default query * Search: add toggle custom action * Search: add onQueryChange * Search: debounce search * Search: pas dispatch as a prop * Search: add tag filter * Search: Fix types * Search: revert changes * Search: close overlay on esc * Search: enable tag filtering * Search: clear query * Search: add autofocus to search field * Search: Rename close to closeSearch * Search: Add no results message * Search: Add loading state * Search: Remove Select from Forms namespace * Remove Add selectedIndex * Remove Add getFlattenedSections * Remove Enable selecting items * Search: add hasId * Search: preselect first item * Search: Add utils tests * Search: Fix moving selection down * Search: Add findSelected * Search: Add type to section * Search: Handle Enter key press on item highlight * Search: Move reducer et al. to separate files * Search: Remove redundant render check * Search: Close overlay on Esc and ArrowLeft press * Search: Add close button * Search: Document utils * Search: use Icon for remove icon * Search: Add DashboardSearch.test.tsx * Search: Move test data to a separate file * Search: Finalise DashboardSearch.test.tsx * Add search reducer tests * Search: Add search results loading indicator * Search: Remove inline function * Search: Do not mutate item * Search: Tweak utils * Search: Do not clear query on tag clear * Search: Fix folder:current search * Search: Fix results scroll * Search: Update tests * Search: Close overlay on cog icon click * Add mobile styles for close button * Search: Use CustomScrollbar * Search: Memoize TagList.tsx * Search: Fix type errors * Search: More strictNullChecks fixes * Search: Consistent handler names * Search: Fix search items types in test * Search: Fix merge conflicts * Search: Fix strictNullChecks errors
2020-04-08 10:14:03 -05:00
type: hit.type,
2017-12-13 08:51:59 -06:00
};
}
2017-12-13 08:51:59 -06:00
}
for (const hit of results) {
2017-12-20 05:33:33 -06:00
if (hit.type === 'dash-folder') {
2017-12-13 08:51:59 -06:00
continue;
}
2017-11-20 08:11:32 -06:00
2017-12-13 08:51:59 -06:00
let section = sections[hit.folderId || 0];
if (!section) {
if (hit.folderId) {
section = {
id: hit.folderId,
uid: hit.folderUid,
2017-12-13 08:51:59 -06:00
title: hit.folderTitle,
url: hit.folderUrl,
2017-12-13 08:51:59 -06:00
items: [],
Search/migrate search results (#22930) * Search: Setup SearchResults.tsx * Search: add watchDepth * Search: Use SearchResults.tsx in Angular template * Search: Render search result header * Search: Move new search components to features/search * Search: Render nested dashboards * Search: Expand dashboard folder * Search: Remove fa prefix from icon names * Search: Enable search results toggling * Search: Add onItemClick handler * Search: Add missing aria-label * Search: Add no results message * Search: Fix e2e selectors * Search: Update SearchField imports * Search: Add conditional classes * Search: Abstract DashboardCheckbox * Search: Separate ResultItem * Search: Style ResultItem * Search: Separate search components * Search: Tweak checkbox styling * Search: Simplify component names * Search: Separate tag component * Search: Checkbox docs * Search: Remove inline on click * Add Tag component * Add Tag story * Add TagList * Group Tab and TabList * Fix typechecks * Remove Meta * Use forwardRef for the Tag * Search: Use TagList from grafana/ui * Search: Add media query for TagList * Search: Add types * Search: Remove selectThemeVariant from SearchItem.tsx * Search: Style section + header * Search: Use semantic html * Search: Adjust section padding * Search: Setup tests * Search: Fix tests * Search: tweak result styles * Search: Expand SearchResults tests * Search: Add SearchItem tests * Search: Use SearchResults in search.html * Search: Toggle search result sections * Search: Make selected prop optional * Search: Fix tag selection * Search: Fix tag filter onChange * Search: Fix uncontrolled state change warning * Search: Update icon names * Search: memoize SearchCheckbox.tsx * Search: Update types * Search: Cleanup events * Search: Semantic html * Use styleMixins * Search: Tweak styling * Search: useCallback for checkbox toggle * Search: Add stylesFactory Co-authored-by: CirceCI <circleci@grafana.com>
2020-03-26 04:09:08 -05:00
icon: 'folder-open',
score: keys(sections).length,
type: DashboardSearchItemType.DashFolder,
2017-12-13 08:51:59 -06:00
};
} else {
section = {
id: 0,
title: 'General',
2017-12-13 08:51:59 -06:00
items: [],
Search/migrate search results (#22930) * Search: Setup SearchResults.tsx * Search: add watchDepth * Search: Use SearchResults.tsx in Angular template * Search: Render search result header * Search: Move new search components to features/search * Search: Render nested dashboards * Search: Expand dashboard folder * Search: Remove fa prefix from icon names * Search: Enable search results toggling * Search: Add onItemClick handler * Search: Add missing aria-label * Search: Add no results message * Search: Fix e2e selectors * Search: Update SearchField imports * Search: Add conditional classes * Search: Abstract DashboardCheckbox * Search: Separate ResultItem * Search: Style ResultItem * Search: Separate search components * Search: Tweak checkbox styling * Search: Simplify component names * Search: Separate tag component * Search: Checkbox docs * Search: Remove inline on click * Add Tag component * Add Tag story * Add TagList * Group Tab and TabList * Fix typechecks * Remove Meta * Use forwardRef for the Tag * Search: Use TagList from grafana/ui * Search: Add media query for TagList * Search: Add types * Search: Remove selectThemeVariant from SearchItem.tsx * Search: Style section + header * Search: Use semantic html * Search: Adjust section padding * Search: Setup tests * Search: Fix tests * Search: tweak result styles * Search: Expand SearchResults tests * Search: Add SearchItem tests * Search: Use SearchResults in search.html * Search: Toggle search result sections * Search: Make selected prop optional * Search: Fix tag selection * Search: Fix tag filter onChange * Search: Fix uncontrolled state change warning * Search: Update icon names * Search: memoize SearchCheckbox.tsx * Search: Update types * Search: Cleanup events * Search: Semantic html * Use styleMixins * Search: Tweak styling * Search: useCallback for checkbox toggle * Search: Add stylesFactory Co-authored-by: CirceCI <circleci@grafana.com>
2020-03-26 04:09:08 -05:00
icon: 'folder-open',
score: keys(sections).length,
type: DashboardSearchItemType.DashFolder,
2017-12-13 08:51:59 -06:00
};
2017-11-20 08:11:32 -06:00
}
2017-12-13 08:51:59 -06:00
// add section
sections[hit.folderId || 0] = section;
2017-11-20 08:11:32 -06:00
}
2017-12-13 08:51:59 -06:00
section.expanded = true;
section.items && section.items.push(hit);
2017-12-13 08:51:59 -06:00
}
2017-11-20 08:11:32 -06:00
}
getDashboardTags() {
Chore: Remove angular dependency from backendSrv (#20999) * Chore: Remove angular dependency from backendSrv * Refactor: Naive soultion for logging out unauthorized users * Refactor: Restructures to different streams * Refactor: Restructures datasourceRequest * Refactor: Flipped back if statement * Refactor: Extracted getFromFetchStream * Refactor: Extracts toFailureStream operation * Refactor: Fixes issue when options.params contains arrays * Refactor: Fixes broken test (but we need a lot more) * Refactor: Adds explaining comments * Refactor: Adds latest RxJs version so cancellations work * Refactor: Cleans up the takeUntil code * Refactor: Adds tests for request function * Refactor: Separates into smaller functions * Refactor: Adds last error tests * Started to changed so we require getBackendSrv from the @grafana-runtime when applicable. * Using the getBackendSrv from @grafana/runtime. * Changed so we use the getBackendSrv from the @grafana-runtime when possible. * Fixed so Server Admin -> Orgs works again. * Removed unused dependency. * Fixed digest issues on the Server Admin -> Users page. * Fix: Fixes digest problems in Playlists * Fix: Fixes digest issues in VersionHistory * Tests: Fixes broken tests * Fix: Fixes digest issues in Alerting => Notification channels * Fixed digest issues on the Intive page. * Fixed so we run digest after password reset email sent. * Fixed digest issue when trying to sign up account. * Fixed so the Server Admin -> Edit Org works with backendSrv * Fixed so Server Admin -> Users works with backend srv. * Fixed digest issues in Server Admin -> Orgs * Fix: Fixes digest issues in DashList plugin * Fixed digest issues on Server Admin -> users. * Fix: Fixes digest issues with Snapshots * Fixed digest issue when deleting a user. * Fix: Fixes digest issues with dashLink * Chore: Changes RxJs version to 6.5.4 which includes the same cancellation fix * Fix: Fixes digest issue when toggling folder in manage dashboards * Fix: Fixes bug in executeInOrder * Fix: Fixes digest issue with CreateFolderCtrl and FolderDashboardsCtrl * Fix: Fixes tslint error in test * Refactor: Changes default behaviour for emitted messages as before migration * Fix: Fixes various digest issues when saving, starring or deleting dashboards * Fix: Fixes digest issues with FolderPickerCtrl * Fixed digest issue. * Fixed digest issues. * Fixed issues with angular digest. * Removed the this.digest pattern. Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com> Co-authored-by: Marcus Andersson <systemvetaren@gmail.com>
2020-01-21 03:08:07 -06:00
return backendSrv.get('/api/dashboards/tags');
}
getSortOptions() {
return backendSrv.get('/api/search/sorting');
}
}