Skip to content

Commit

Permalink
update cache service to not use ts maps
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-couchbase committed Jan 3, 2024
1 parent 1fe03cf commit 3f76ff3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/commands/iq/chat/intents/actionIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { availableActions } from "../utils";


export const actionIntenthandler = async (jsonObject: any, webview: WebviewView) => {
const actions = jsonObject?.actions;
const actions = jsonObject?.actions || [];
const resultingActions: string[] = [];

if(actions.length === 0){
return resultingActions;
}
Expand Down
11 changes: 6 additions & 5 deletions src/commands/iq/chat/intents/collectionSchemaIntent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ISchemaPatternCache, ISchemaCache, CacheService } from "../../../../util/cacheService/cacheService";
import { logger } from "../../../../logger/logger";
import { ISchemaCache, CacheService } from "../../../../util/cacheService/cacheService";
import { IAdditionalContext } from "../types";

const schemaPatternStringify = (schemaPattern: ISchemaPatternCache, indentLevel: number = 0) => {
const schemaPatternStringify = (schemaPattern: any, indentLevel: number = 0) => {
let result = '';
const indent = '-'.repeat(indentLevel * 2);
for (let [key, value] of schemaPattern.schemaNode) {
Expand Down Expand Up @@ -31,7 +32,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi
if (collections.length === 0) { // No Collections found, returning
return;
}

logger.info("getting collections data intent for " + collections);
let schemas = [];
for (let collection of collections) {
let scope = "";
Expand All @@ -42,7 +43,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi
if (schema !== undefined) {
// found correct pair, we can return the schema
const stringifiedSchema = {
schema: schemaStringify(schema),
schema: JSON.stringify(schema),
collection: `${scope}.${col}`
};
schemas.push(stringifiedSchema);
Expand All @@ -53,7 +54,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi
if (schema !== undefined) {
// found correct pair, we can return the schema
const stringifiedSchema = {
schema: schemaStringify(schema),
schema: JSON.stringify(schema),
collection: col
};
schemas.push(stringifiedSchema);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/iq/chat/iqChatHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ const getIntentOrResponse = async (userRequest: string, jwtToken: string, orgId:
6 - If you did not extract any entities above, simply respond to the user question. Don't send any JSON in this case
`; // TODO: Update all available collections
`;

let codeSelected = `The user has the following code selected:
`;
let codeSelectedAvailable: boolean = false;
// TODO: update based on code selected

let config = vscode.workspace.getConfiguration('couchbase');

const editor = vscode.window.activeTextEditor;
Expand All @@ -60,7 +60,7 @@ const getIntentOrResponse = async (userRequest: string, jwtToken: string, orgId:
${userRequest}
`;

let finalContent = basicQuestion + userQuestion + (codeSelectedAvailable ? codeSelected : ""); // TODO: update code selected as well in this
let finalContent = basicQuestion + userQuestion + (codeSelectedAvailable ? codeSelected : "");

previousMessages.allChats = [...previousMessages.allChats, {
role: "user",
Expand Down Expand Up @@ -95,7 +95,7 @@ const getFinalResponse = async (message: string, additionalContext: IAdditionalC
additionalContextPrompt += "the schema for collection " + schema.collection + " is " + schema.schema + "\n";
}

const finalContent = basicPrompt + additionalContextPrompt + `Please focus now on answering the question that is: ${message}`;
const finalContent = basicPrompt + additionalContextPrompt + `Please focus now on answering the question and do not return JSON unless specified in the question. \n ${message}`;

previousMessages.allChats = [...previousMessages.allChats, {
role: "user",
Expand Down
2 changes: 1 addition & 1 deletion src/reactViews/iq/pages/chatscreen/IqChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ const IqChat = ({ org }) => {
sender: "assistant",
feedbackSent: false,
msgDate: message.msgDate,
qaId: message.qaId, // TODO: get qaId
qaId: message.qaId,
};
const updatedMessages = [...messages.userChats, newMessage];

Expand Down
2 changes: 1 addition & 1 deletion src/reactViews/iq/pages/login/LoginPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

.divider {
display: inline-block;
width: 40%;
width: min(40%,calc(50% - 35px));
height: 1px;
background-color: var(
--vscode-settings-sashBorder
Expand Down
76 changes: 37 additions & 39 deletions src/util/cacheService/cacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { hasQueryService } from "../common";
import { Global } from "../util";
import { QueryIndex } from "couchbase";

type schemaPatternType = ISchemaPatternCache | string;

export interface ISchemaPatternCache {
schemaNode: Map<string, schemaPatternType>;
}
// type schemaPatternType = ISchemaPatternCache | string;

// export interface ISchemaPatternCache {
// schemaNode: any;
// }
export type SchemaCacheType = { [index: string]: any };
export interface ISchemaCache {
patterns: ISchemaPatternCache[];
patterns: SchemaCacheType[];
}

export interface ICollectionCache {
Expand Down Expand Up @@ -41,27 +41,27 @@ export class CacheService {

CacheService() { }

private schemaTreeTraversal(treeNode: any): ISchemaPatternCache {
private schemaTreeTraversal(treeNode: any): SchemaCacheType {
if (!treeNode) {
return { schemaNode: new Map<string, schemaPatternType>() };
return {};
}
const currentNodes = new Map<string, schemaPatternType>();
const currentNodes: SchemaCacheType = {};

Object.entries(treeNode).map(property => {
const propertyValue: any = property[1];
const type = propertyValue.type;
if (type === 'object') {
const children = this.schemaTreeTraversal(propertyValue.properties);
currentNodes.set(`${property[0]}`, children);
currentNodes[property[0]] = children;
} else if (type === 'array') {
try {
const items = propertyValue.items;
const itemType = items.type;
if (itemType === 'object') {
const children = this.schemaTreeTraversal(items.properties);
currentNodes.set(`${property[0]}`, children);
currentNodes[property[0]] = children;
} else {
currentNodes.set(`${property[0]}`, `array of ${itemType}`);
currentNodes[property[0]] = `array of ${itemType}`;
}
} catch (error) {
logger.error(`Error processing array type for ${property[0]}: ${error}`);
Expand All @@ -71,13 +71,13 @@ export class CacheService {
try {
let currentType: string = type.toString();
currentType = currentType.replace(',', " | ");
currentNodes.set(`${property[0]}`, currentType);
currentNodes[property[0]] = currentType;
} catch (e) {
logger.error("Type can't be stringified: " + e);
}
}
});
return { schemaNode: currentNodes };
return currentNodes;
}

public cacheSchemaForCollection = async (connection: IConnection, collection: ICollectionCache) => {
Expand All @@ -86,7 +86,7 @@ export class CacheService {
const result = await connection?.cluster?.query(query);

const patternCnt: number = result?.rows[0].length || 0;
const schemaPatternData: ISchemaPatternCache[] = [];
const schemaPatternData: SchemaCacheType[] = [];
for (let i = 0; i < patternCnt; i++) {
const row = result?.rows[0][i];
const childrenNode = this.schemaTreeTraversal(row.properties);
Expand All @@ -100,7 +100,7 @@ export class CacheService {

public async cacheIndexesForCollection(connection: IConnection, collection: ICollectionCache): Promise<QueryIndex[]> {
const indexesResult = await connection?.cluster?.queryIndexes().getAllIndexes(collection.bucketName, { scopeName: collection.scopeName, collectionName: collection.name });
collection.indexes = indexesResult?.map((index)=>{
collection.indexes = indexesResult?.map((index) => {
return JSON.stringify(index);
});
return indexesResult || [];
Expand All @@ -112,7 +112,7 @@ export class CacheService {
for (let [_, scope] of bucket.scopes) {
for (let [_, collection] of scope.collections) {
const indexes = await this.cacheIndexesForCollection(connection, collection);
if(indexes.length > 0){ // Only cache schema
if (indexes.length > 0) { // Only cache schema
await this.cacheSchemaForCollection(connection, collection);
}
};
Expand Down Expand Up @@ -176,7 +176,8 @@ export class CacheService {
if (connection) {
if (!forcedCacheUpdate) {
const isCacheSuccessful = await this.loadCache(connection);
if(isCacheSuccessful){
if (isCacheSuccessful) {
this.cacheStatus = true;
return;
}
}
Expand Down Expand Up @@ -212,12 +213,12 @@ export class CacheService {

};

public getCollectionSchemaWithCollectionName = async(collectionName: string):Promise<undefined | ISchemaCache> => {
public getCollectionSchemaWithCollectionName = async (collectionName: string): Promise<undefined | ISchemaCache> => {
if (this.cacheStatus === false) {
return undefined;
}
for await (let [_, bucketCache] of this.bucketsData) {
for await (let [_, scopeCache] of bucketCache.scopes){
for await (let [_, scopeCache] of bucketCache.scopes) {
const collections = scopeCache.collections;
if (collections !== undefined) {
const collectionData = collections.get(collectionName);
Expand All @@ -237,26 +238,20 @@ export class CacheService {
}
const allCollectionList = [];
for await (let [_, bucketCache] of this.bucketsData) {
for await (let [_, scopeCache] of bucketCache.scopes){
for await (let [_, collectionCache] of scopeCache.collections){
for await (let [_, scopeCache] of bucketCache.scopes) {
for await (let [_, collectionCache] of scopeCache.collections) {
allCollectionList.push(`${collectionCache.scopeName}.${collectionCache.name}`);
}

}
}
return allCollectionList;
};

private serializeSchema(schema: ISchemaCache): string {
// Convert nested Maps to plain objects for JSON
const patternsJson = JSON.stringify(schema.patterns.map((pattern) => {
if (typeof pattern === "string") {
return pattern;
}
return { schemaNode: Object.entries(pattern.schemaNode) };
}));

return JSON.stringify({ patterns: patternsJson });
const patternsJson = JSON.stringify(schema.patterns);
return patternsJson;
//return JSON.stringify({ patterns: patternsJson });
}

public async storeCache(connection: IConnection): Promise<void> {
Expand Down Expand Up @@ -298,7 +293,7 @@ export class CacheService {
if (!storedDataJson) {
return false; // No existing cache
}

logger.info("Loading existing cache");
const storedData = JSON.parse(storedDataJson);
this.bucketsData.clear();
for (const bucketName in storedData) {
Expand All @@ -308,16 +303,18 @@ export class CacheService {
scopes: new Map(),
};
this.bucketsData.set(bucketName, bucket);
logger.info("loading cache from bucket: "+ bucketName);

for (const scopeName in storedData[bucketName].scopes) {
const scope:IScopeCache = {
const scope: IScopeCache = {
name: storedData[bucketName].scopes[scopeName].name,
bucketName: bucketName,
collections: new Map(),
};
bucket.scopes.set(scopeName, scope);

for (const collectionName in storedData[bucketName].scopes[scopeName].collections) {
console.log(bucketName,scopeName, collectionName);
const collection: ICollectionCache = {
name: storedData[bucketName].scopes[scopeName].collections[collectionName].name,
schema: storedData[bucketName].scopes[scopeName].collections[collectionName].schema
Expand All @@ -335,17 +332,18 @@ export class CacheService {
}

private deserializeSchema(storedSchemaJson: string): ISchemaCache | undefined {
if (!storedSchemaJson) {


if (!storedSchemaJson) {
return undefined;
}

const storedSchema = JSON.parse(storedSchemaJson);
const patterns = JSON.parse(storedSchema.patterns);
const patterns = JSON.parse(storedSchemaJson);
return {
patterns: patterns.map((pattern: ISchemaPatternCache) => {
return { schemaNode: new Map(Object.entries(pattern.schemaNode)) }; // Convert empty array to Map
patterns: patterns.map((pattern: any):SchemaCacheType => {
return pattern; // Convert empty array to Map
}),
};
};

}
}

0 comments on commit 3f76ff3

Please sign in to comment.