Skip to content

Commit ac00047

Browse files
committed
feat: Search for AppMap data using @appland/search
1 parent 2b76b92 commit ac00047

File tree

17 files changed

+666
-578
lines changed

17 files changed

+666
-578
lines changed

packages/cli/src/cmds/search/search.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import yargs from 'yargs';
2-
2+
import sqlite3 from 'better-sqlite3';
33
import assert from 'assert';
44
import { readFileSync } from 'fs';
55
import { writeFile } from 'fs/promises';
66
import { AppMap, AppMapFilter, buildAppMap, deserializeFilter } from '@appland/models';
7+
import { FileIndex } from '@appland/search';
78

89
import { handleWorkingDirectory } from '../../lib/handleWorkingDirectory';
910
import { verbose } from '../../utils';
1011
import searchSingleAppMap, { SearchOptions as SingleSearchOptions } from './searchSingleAppMap';
11-
import AppMapIndex, {
12-
SearchResponse as DiagramsSearchResponse,
13-
SearchOptions,
14-
} from '../../fulltext/AppMapIndex';
12+
import { SearchResponse as DiagramsSearchResponse } from '../../fulltext/appmap-match';
1513
import {
1614
SearchResult as EventSearchResult,
1715
SearchResponse as EventSearchResponse,
1816
} from '../../fulltext/FindEvents';
1917
import { openInBrowser } from '../open/openers';
18+
import { buildAppMapIndex, search } from '../../fulltext/appmap-index';
19+
import buildIndex from '../../rpc/explain/buildIndex';
2020

2121
export const command = 'search <query>';
2222
export const describe =
@@ -83,7 +83,21 @@ export const builder = (args: yargs.Argv) => {
8383
return args.strict();
8484
};
8585

86-
export const handler = async (argv: any) => {
86+
type ArgumentTypes = {
87+
directory: string;
88+
query: string;
89+
appmap: string;
90+
contextDepth: number;
91+
maxSize: string;
92+
filter: string;
93+
show: boolean;
94+
maxResults: number;
95+
findEvents: boolean;
96+
format: 'json' | 'appmap';
97+
verbose: boolean;
98+
};
99+
100+
export const handler = async (argv: ArgumentTypes) => {
87101
verbose(argv.verbose);
88102

89103
const { directory, query, appmap, contextDepth, show, maxResults, findEvents, format } = argv;
@@ -160,19 +174,29 @@ export const handler = async (argv: any) => {
160174
maxResults,
161175
};
162176
const { maxSize, filter: filterStr } = argv;
163-
if (maxSize) options.maxSize = maxSize;
177+
if (maxSize) options.maxSize = parseInt(maxSize);
164178
if (filterStr) options.filter = deserializeFilter(filterStr);
165179
const response = await searchSingleAppMap(appmap, query, options);
166180
await presentResults(response);
167181
} else {
168-
const options: SearchOptions = {
182+
const options = {
169183
maxResults,
170184
};
171-
const response = await AppMapIndex.search([process.cwd()], query, options);
185+
186+
const index = await buildIndex('appmaps', async (indexFile) => {
187+
const db = new sqlite3(indexFile);
188+
const fileIndex = new FileIndex(db);
189+
await buildAppMapIndex(fileIndex, [process.cwd()]);
190+
return fileIndex;
191+
});
192+
193+
const response = await search(index.index, query.split(/\s+/).join(' OR '), maxResults);
194+
index.close();
195+
172196
if (findEvents) {
173197
const eventOptions: SingleSearchOptions = { maxResults };
174198
const { maxSize, filter: filterStr } = argv;
175-
if (maxSize) eventOptions.maxSize = maxSize;
199+
if (maxSize) eventOptions.maxSize = parseInt(maxSize, 10);
176200
if (filterStr) eventOptions.filter = deserializeFilter(filterStr);
177201

178202
const { results } = response;

0 commit comments

Comments
 (0)