From 2e9b83539400cccd7c09ab5c43f6ce7c9c330f35 Mon Sep 17 00:00:00 2001 From: Lokesh Goel Date: Thu, 23 May 2024 13:29:57 +0530 Subject: [PATCH] update filtering and squash any bugs --- package.json | 24 ++++---- .../documentFilters/clearDocumentFilter.ts | 8 +-- .../documentFilters/kvTypeFilterDocuments.ts | 7 +-- .../queryTypeFilterDocuments.ts | 10 ++-- src/model/CollectionNode.ts | 60 ++++++++++++------- src/model/ScopeNode.ts | 1 - src/pages/FavoriteQueries/FavoriteQueries.ts | 1 - src/util/apis/CouchbaseRestAPI.ts | 2 - 8 files changed, 56 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 29155cfa..c007fa63 100644 --- a/package.json +++ b/package.json @@ -478,12 +478,12 @@ }, { "command": "vscode-couchbase.queryTypeDocumentFilter", - "title": "Add Query Based Document Filter", + "title": "Add Query Type Document Filter", "category": "Couchbase" }, { "command": "vscode-couchbase.kvTypeDocumentFilter", - "title": "Add KV Range Based Document Filter", + "title": "Add KV Range Type Document Filter", "category": "Couchbase" }, { @@ -917,17 +917,17 @@ }, { "command": "vscode-couchbase.createDocument", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter || viewItem == collection_filter)", + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter_kv || viewItem == default_collection_filter_query || viewItem == collection_filter_kv || viewItem == collection_filter_query)", "group": "primary" }, { "command": "vscode-couchbase.searchDocument", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter || viewItem == collection_filter)", + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter_kv || viewItem == default_collection_filter_query || viewItem == collection_filter_kv || viewItem == collection_filter_query)", "group": "primary" }, { "submenu": "vscode-couchbase.filterDocumentsMenu", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter || viewItem == collection_filter)", + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter_kv || viewItem == default_collection_filter_query || viewItem == collection_filter_kv || viewItem == collection_filter_query)", "group": "secondary" }, { @@ -940,7 +940,7 @@ }, { "command": "vscode-couchbase.refreshCollection", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter || viewItem == collection_filter)", + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == default_collection_filter_kv || viewItem == default_collection_filter_query || viewItem == collection_filter_kv || viewItem == collection_filter_query)", "group": "tertiary" }, { @@ -968,7 +968,7 @@ }, { "command": "vscode-couchbase.removeCollection", - "when": "view == couchbase && (viewItem == collection || viewItem == collection_filter)", + "when": "view == couchbase && (viewItem == collection || viewItem == collection_filter_kv || viewItem == collection_filter_query)", "group": "tertiary" }, { @@ -1047,26 +1047,26 @@ { "command": "vscode-couchbase.queryTypeDocumentFilter", "group": "navigation", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == collection_filter || viewItem == default_collection_filter)" + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == collection_filter_kv || viewItem == default_collection_filter_kv) && !isKVCluster" }, { "command": "vscode-couchbase.kvTypeDocumentFilter", "group": "navigation", - "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == collection_filter || viewItem == default_collection_filter)" + "when": "view == couchbase && (viewItem == collection || viewItem == default_collection || viewItem == collection_filter_query || viewItem == default_collection_filter_query)" }, { "command": "vscode-couchbase.editQueryTypeDocumentFilter", - "when": "view == couchbase && (viewItem == collection_filter || viewItem == default_collection_filter)", + "when": "view == couchbase && (viewItem == collection_filter_query || viewItem == default_collection_filter_query) && !isKVCluster", "group": "secondary" }, { "command": "vscode-couchbase.editKvTypeDocumentFilter", - "when": "view == couchbase && (viewItem == collection_filter || viewItem == default_collection_filter)", + "when": "view == couchbase && (viewItem == collection_filter_kv || viewItem == default_collection_filter_kv)", "group": "secondary" }, { "command": "vscode-couchbase.clearDocumentFilter", - "when": "view == couchbase && (viewItem == collection_filter || viewItem == default_collection_filter)", + "when": "view == couchbase && (viewItem == collection_filter_kv || viewItem == default_collection_filter_kv || viewItem == collection_filter_query || viewItem == default_collection_filter_query)", "group": "tertiary" } ] diff --git a/src/commands/documents/documentFilters/clearDocumentFilter.ts b/src/commands/documents/documentFilters/clearDocumentFilter.ts index 5a4ec779..9c6d97cd 100644 --- a/src/commands/documents/documentFilters/clearDocumentFilter.ts +++ b/src/commands/documents/documentFilters/clearDocumentFilter.ts @@ -3,18 +3,12 @@ import CollectionNode from "../../../model/CollectionNode"; import { getActiveConnection } from "../../../util/connections"; import { Memory } from "../../../util/util"; -export const clearDocumentFilter = async (node: CollectionNode) => { +export const clearDocumentFilter = (node: CollectionNode) => { const connection = getActiveConnection(); if(!connection){ return; } - commands.executeCommand( - "setContext", - "vscode-couchbase.documentFilterType", - "" - ); - Memory.state.update( `filterDocumentsType-${connection.connectionIdentifier}-${node.bucketName}-${node.scopeName}-${node.collectionName}`, `` diff --git a/src/commands/documents/documentFilters/kvTypeFilterDocuments.ts b/src/commands/documents/documentFilters/kvTypeFilterDocuments.ts index f6a7a34b..7c09e137 100644 --- a/src/commands/documents/documentFilters/kvTypeFilterDocuments.ts +++ b/src/commands/documents/documentFilters/kvTypeFilterDocuments.ts @@ -2,6 +2,7 @@ import CollectionNode from "../../../model/CollectionNode"; import { getActiveConnection } from "../../../util/connections"; import * as vscode from "vscode"; import { Memory } from "../../../util/util"; +import { clearDocumentFilter } from "./clearDocumentFilter"; export const kvTypeFilterDocuments = async (node: CollectionNode) => { const connection = getActiveConnection(); @@ -41,11 +42,7 @@ export const kvTypeFilterDocuments = async (node: CollectionNode) => { return; } - vscode.commands.executeCommand( - "setContext", - "vscode-couchbase.documentFilterType", - "kv" - ); + clearDocumentFilter(node); Memory.state.update( `filterDocumentsType-${connection.connectionIdentifier}-${node.bucketName}-${node.scopeName}-${node.collectionName}`, diff --git a/src/commands/documents/documentFilters/queryTypeFilterDocuments.ts b/src/commands/documents/documentFilters/queryTypeFilterDocuments.ts index a792b12a..23bde8f9 100644 --- a/src/commands/documents/documentFilters/queryTypeFilterDocuments.ts +++ b/src/commands/documents/documentFilters/queryTypeFilterDocuments.ts @@ -4,6 +4,7 @@ import { Memory } from "../../../util/util"; import { logger } from "../../../logger/logger"; import { ParsingFailureError, PlanningFailureError } from "couchbase"; import { getActiveConnection } from "../../../util/connections"; +import { clearDocumentFilter } from "./clearDocumentFilter"; export const queryTypeFilterDocuments = async (node: CollectionNode) => { const connection = getActiveConnection(); @@ -35,7 +36,7 @@ export const queryTypeFilterDocuments = async (node: CollectionNode) => { title: `Apply filter for collection \`${collectionName}\``, placeHolder: `airline="AI" OR country="United States"`, value: filterStmt, - prompt: `SELECT meta.id() FROM \`${collectionName}\` WHERE [Your Filter] | `, + prompt: `SELECT meta.id() FROM \`${collectionName}\` WHERE [Your Filter] | This may reset other document filters | `, validateInput: (input) => { const tokens = input.split(" "); for (const token of tokens) { @@ -73,11 +74,8 @@ export const queryTypeFilterDocuments = async (node: CollectionNode) => { return; } - vscode.commands.executeCommand( - "setContext", - "vscode-couchbase.documentFilterType", - "query" - ); + // Reset any existing document filter + clearDocumentFilter(node); Memory.state.update( `filterDocumentsType-${connection.connectionIdentifier}-${node.bucketName}-${node.scopeName}-${node.collectionName}`, diff --git a/src/model/CollectionNode.ts b/src/model/CollectionNode.ts index 32688527..15fac004 100644 --- a/src/model/CollectionNode.ts +++ b/src/model/CollectionNode.ts @@ -39,7 +39,6 @@ export default class CollectionNode implements INode { public readonly documentCount: number, public readonly bucketName: string, public readonly collectionName: string, - public readonly filter: boolean, public readonly collapsibleState: vscode.TreeItemCollapsibleState, public cacheService: CacheService, public limit: number = 10, @@ -56,28 +55,33 @@ export default class CollectionNode implements INode { if (!connection) { return null; } - const idxs = await connection.cluster?.queryIndexes().getAllIndexes(this.bucketName, { scopeName: this.scopeName, collectionName: this.collectionName }); - let filter: string | null = null; - if (!idxs) { - return null; - } + try { + const idxs = await connection.cluster?.queryIndexes().getAllIndexes(this.bucketName, { scopeName: this.scopeName, collectionName: this.collectionName }); + let filter: string | null = null; + if (!idxs) { + return null; + } - for (const idx of idxs) { - if (idx.isPrimary) { - return "meta().id"; - } else { - const result = this.getValidIndexKey(idx.indexKey); - if (result) { - if (!idx.condition && result[1]) { - return result[0]; - } else { - filter = result[0]; + for (const idx of idxs) { + if (idx.isPrimary) { + return "meta().id"; + } else { + const result = this.getValidIndexKey(idx.indexKey); + if (result) { + if (!idx.condition && result[1]) { + return result[0]; + } else { + filter = result[0]; + } } } } - } - return filter; + return filter; + } catch (e) { + logger.error("error getting indexed fields: " + e); + return null; + } } private getValidIndexKey(array: any[]): [string, boolean] | null { @@ -96,27 +100,38 @@ export default class CollectionNode implements INode { } public async getTreeItem(): Promise { + const connection = getActiveConnection(); + if (!connection) { + return new vscode.TreeItem(this.collectionName, vscode.TreeItemCollapsibleState.None); + } + const filterDocumentsType = Memory.state.get( + `filterDocumentsType-${connection.connectionIdentifier}-${this.bucketName}-${this.scopeName}-${this.collectionName}` + ) ?? ""; + + return { label: `${this.collectionName} (${abbreviateCount(this.documentCount)})`, collapsibleState: vscode.TreeItemCollapsibleState.Collapsed, contextValue: (this.collectionName === "_default" ? "default_collection" - : "collection") + (this.filter ? "_filter" : ""), + : "collection") + (filterDocumentsType !== "" ? "_filter" : "") + + (filterDocumentsType === "kv" ? "_kv" : "") + + (filterDocumentsType === "query" ? "_query" : ""), iconPath: { light: path.join( __filename, "..", "..", "images/light", - this.filter ? "filter.svg" : "documents-icon.svg" + filterDocumentsType !== "" ? "filter.svg" : "documents-icon.svg" ), dark: path.join( __filename, "..", "..", "images/dark", - this.filter ? "filter.svg" : "documents-icon.svg" + filterDocumentsType !== "" ? "filter.svg" : "documents-icon.svg" ), }, }; @@ -170,14 +185,13 @@ export default class CollectionNode implements INode { const couchbaseRestAPI = new CouchbaseRestAPI(connection); if (!hasQueryService(connection?.services!) || isKVFilterEnabled) { // KV Based Fetching Documents - console.log("inside"); let [startingDocId, endingDocId]: [string | undefined, string | undefined] = [undefined, undefined]; if (isKVFilterEnabled) { [startingDocId, endingDocId] = Memory.state.get( `kvTypeFilterDocuments-${connection.connectionIdentifier}-${this.bucketName}-${this.scopeName}-${this.collectionName}` )?.split('|') ?? ["", ""]; } - console.log("docIds", startingDocId, endingDocId); + result = await couchbaseRestAPI.getKVDocuments(this.bucketName, this.scopeName, this.collectionName, 0, this.limit, startingDocId, endingDocId); result.rows = result.rows.map((item: any) => item.id); } diff --git a/src/model/ScopeNode.ts b/src/model/ScopeNode.ts index c817d990..265e3295 100644 --- a/src/model/ScopeNode.ts +++ b/src/model/ScopeNode.ts @@ -115,7 +115,6 @@ export class ScopeNode implements INode { rowCount, this.bucketName, collection.name, - filterDocumentsType !== "", vscode.TreeItemCollapsibleState.None, this.cacheService ); diff --git a/src/pages/FavoriteQueries/FavoriteQueries.ts b/src/pages/FavoriteQueries/FavoriteQueries.ts index 39495082..3eff3814 100644 --- a/src/pages/FavoriteQueries/FavoriteQueries.ts +++ b/src/pages/FavoriteQueries/FavoriteQueries.ts @@ -13,7 +13,6 @@ export interface IFavoriteQueriesWebviewState { } export const fetchFavoriteQueries = (context: vscode.ExtensionContext) => { - console.log("context is", context); const favoriteQueryWebviewDetails = Memory.state.get(Constants.FAVORITE_QUERIES_WEBVIEW); if (favoriteQueryWebviewDetails) { // Favorite Queries Webview already exists, Closing existing and creating new diff --git a/src/util/apis/CouchbaseRestAPI.ts b/src/util/apis/CouchbaseRestAPI.ts index eb325a2e..0b12425c 100644 --- a/src/util/apis/CouchbaseRestAPI.ts +++ b/src/util/apis/CouchbaseRestAPI.ts @@ -108,7 +108,6 @@ export class CouchbaseRestAPI { url += `&endkey=%22${endDocId}%22`; } - console.log(url); process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; try { let content = await axios.get(url, { @@ -120,7 +119,6 @@ export class CouchbaseRestAPI { rejectUnauthorized: false, }) }); - console.log(content.data) return content.data; } catch (error) {