Skip to content

Commit

Permalink
feat(semantic-engine): show status
Browse files Browse the repository at this point in the history
  • Loading branch information
yan42685 committed Feb 24, 2024
1 parent 44bd0b7 commit a23b0c2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 76 deletions.
14 changes: 7 additions & 7 deletions src/services/obsidian/setting-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,12 @@ class SemanticSearchModal extends Modal {
.setButtonText("Test connection")
.onClick(() => this.semanticEngine.testConnection()),
)
.addButton(b=>b.setButtonText("Refresh states").onClick(async ()=>{
const count = await this.semanticEngine.docsCount();
if (count) {
new MyNotice(`Indexed docs count: ${count}`, 5000)
}
}))
.addButton((b) => b.setButtonText("Reindex").onClick(() => {}));
// .addButton(b=>b.setButtonText("Refresh states").onClick(async ()=>{
// const count = await this.semanticEngine.docsCount();
// if (count) {
// new MyNotice(`Indexed docs count: ${count}`, 5000)
// }
// }))
// .addButton((b) => b.setButtonText("Reindex").onClick(() => {}));
}
}
133 changes: 69 additions & 64 deletions src/services/search/semantic-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import { ViewRegistry, ViewType } from "../obsidian/view-registry";
export class SemanticEngine {
private request = getInstance(RemoteRequest);
private viewRegistry = getInstance(ViewRegistry);
public isRunning = false;
public isIndexing = false;
private _status: "stopped" | "indexing" | "ready" = "stopped";

get status() {
return this._status;
}

async testConnection(): Promise<void> {
const connected = await this.request.testConnection();
Expand All @@ -33,49 +36,73 @@ export class SemanticEngine {
}

async reindexAll(indexedDocs: IndexedDocument[]) {
this.isIndexing = true;
this._status = "indexing";
const docs = this.convertToDocuments(indexedDocs);
await this.request.reindexAll(docs);
this.isIndexing = false;
try {
await this.request.reindexAll(docs);
this._status = "ready";
} catch (e) {
logger.error(e);
this._status = "stopped";
}
}

async addDocuments(indexedDocs: IndexedDocument[]): Promise<boolean> {
this.isIndexing = true;
this._status = "indexing";
const docs = this.convertToDocuments(indexedDocs);
const result = await this.request.addDocuments(docs);
this.isIndexing = false;
return result;
try {
const result = await this.request.addDocuments(docs);
this._status = "ready";
return result;
} catch (e) {
logger.error(e);
this._status = "stopped";
return false;
}
}

async deleteDocuments(paths: string[]): Promise<boolean> {
this.isIndexing = true;
const result = await this.request.deleteDocuments(paths);
this.isIndexing = false;
return result;
this._status = "indexing";
try {
const result = await this.request.deleteDocuments(paths);
this._status = "ready";
return result;
} catch (e) {
logger.error(e);
this._status = "stopped";
return false;
}
}

async search(queryText: string, viewType: ViewType): Promise<FileItem[]> {
const rawResults = await this.request.search(queryText, viewType);

return rawResults.map((rawResult) => {
const subItems = rawResult.subItems.map(
(subItemData) =>
new FileSubItem(
subItemData.text,
subItemData.row,
subItemData.col,
),
);

return new FileItem(
rawResult.engineType,
rawResult.path,
rawResult.queryTerms,
rawResult.matchedTerms,
subItems,
rawResult.previewContent,
);
});
try {
const rawResults = await this.request.search(queryText, viewType);
this._status = "ready";

return rawResults.map((rawResult) => {
const subItems = rawResult.subItems.map(
(subItemData) =>
new FileSubItem(
subItemData.text,
subItemData.row,
subItemData.col,
),
);

return new FileItem(
rawResult.engineType,
rawResult.path,
rawResult.queryTerms,
rawResult.matchedTerms,
subItems,
rawResult.previewContent,
);
});
} catch (e) {
logger.error(e);
this._status = "stopped";
return [];
}
}

async docsCount(): Promise<number | null> {
Expand Down Expand Up @@ -136,17 +163,13 @@ class RemoteRequest {
const res = await this.client.get("doesCollectionExist", undefined);
return res;
} catch (e) {
logger.error(e)
logger.error(e);
return null;
}
}

async reindexAll(docs: Document[]) {
try {
await this.client.post("reindexAll", undefined, docs);
} catch (e) {
logger.error(e);
}
await this.client.post("reindexAll", undefined, docs);
}

async docsCount(): Promise<number | null> {
Expand All @@ -160,36 +183,18 @@ class RemoteRequest {
}

async addDocuments(docs: Document[]): Promise<boolean> {
try {
await this.client.post("addDocuments", undefined, docs);
return true;
} catch (e) {
logger.error(e);
return false;
}
return await this.client.post("addDocuments", undefined, docs);
}

async deleteDocuments(paths: string[]): Promise<boolean> {
try {
await this.client.post("deleteDocuments", undefined, paths);
return true;
} catch (e) {
logger.error(e);
return false;
}
return await this.client.post("deleteDocuments", undefined, paths);
}

async search(queryText: string, viewType: ViewType): Promise<FileItem[]> {
try {
const res = await this.client.get("search", {
queryText,
viewType,
});
return res || [];
} catch (e) {
logger.error(e);
return [];
}
return await this.client.get("search", {
queryText,
viewType,
});
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/ui/MountedModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import { logger } from "src/utils/logger";
import {
TO_BE_IMPL,
getInstance,
isDevEnvironment,
getInstance
} from "src/utils/my-lib";
import { onDestroy, tick } from "svelte";
import { debounce } from "throttle-debounce";
Expand Down Expand Up @@ -270,9 +269,7 @@
</ul>
{:else}
<span>
{isDevEnvironment
? "no result or to be impl"
: "no matched content"}
{viewHelper.showNoResult(isSemantic)}
</span>
{/if}
{/if}
Expand Down
14 changes: 14 additions & 0 deletions src/ui/view-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "src/globals/search-types";
import { PrivateApi } from "src/services/obsidian/private-api";
import { ViewType } from "src/services/obsidian/view-registry";
import { SemanticEngine } from "src/services/search/semantic-engine";
import { logger } from "src/utils/logger";
import { getInstance } from "src/utils/my-lib";
import { singleton } from "tsyringe";
Expand Down Expand Up @@ -120,6 +121,19 @@ export class ViewHelper {
}, 0);
}

showNoResult(isSemantic: boolean) {
if (isSemantic) {
const semanticEngineStatus = getInstance(SemanticEngine).status;
if (semanticEngineStatus === "ready") {
return "No matched content";
} else {
return `Semantic engine is ${semanticEngineStatus}`;
}
} else {
return "No matched content";
}
}

private async jumpInVaultAsync(path: string, row: number, col: number) {
let alreadyOpen = false;
this.app.workspace.iterateAllLeaves((leaf) => {
Expand Down

0 comments on commit a23b0c2

Please sign in to comment.