Skip to content

Commit 4f1dd94

Browse files
author
arthosofteq
committed
add analytics for ft.info
1 parent 417dfb6 commit 4f1dd94

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

redisinsight/api/src/constants/telemetry-events.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ export enum TelemetryEvents {
4747
CliClientDeleted = 'CLI_CLIENT_DELETED',
4848
CliClientRecreated = 'CLI_CLIENT_RECREATED',
4949
CliCommandExecuted = 'CLI_COMMAND_EXECUTED',
50+
CliIndexInfoSubmitted = 'CLI_INDEX_INFO_SUBMITTED',
5051
CliClusterNodeCommandExecuted = 'CLI_CLUSTER_COMMAND_EXECUTED',
5152
CliCommandErrorReceived = 'CLI_COMMAND_ERROR_RECEIVED',
5253

5354
// Events for workbench tool
5455
WorkbenchCommandExecuted = 'WORKBENCH_COMMAND_EXECUTED',
56+
WorkbenchIndexInfoSubmitted = 'WORKBENCH_INDEX_INFO_SUBMITTED',
5557
WorkbenchCommandErrorReceived = 'WORKBENCH_COMMAND_ERROR_RECEIVED',
5658
WorkbenchCommandDeleted = 'WORKBENCH_COMMAND_DELETE_COMMAND',
5759
// Custom tutorials

redisinsight/api/src/modules/cli/services/cli-analytics/cli-analytics.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ export class CliAnalyticsService extends CommandTelemetryBaseService {
7676
}
7777
}
7878

79+
sendIndexInfoEvent(
80+
databaseId: string,
81+
additionalData: object = {},
82+
): void {
83+
try {
84+
this.sendEvent(
85+
TelemetryEvents.CliIndexInfoSubmitted,
86+
{
87+
databaseId,
88+
...additionalData,
89+
},
90+
);
91+
} catch (e) {
92+
// ignore error
93+
}
94+
}
95+
7996
public async sendCommandExecutedEvent(
8097
databaseId: string,
8198
additionalData: object = {},

redisinsight/api/src/modules/cli/services/cli-business/cli-business.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ import { DatabaseRecommendationService } from 'src/modules/database-recommendati
3030
import { RedisClient } from 'src/modules/redis/client';
3131
import { DatabaseClientFactory } from 'src/modules/database/providers/database.client.factory';
3232
import { v4 as uuidv4 } from 'uuid';
33+
import { getAnalyticsDataFromIndexInfo } from 'src/utils';
3334
import { OutputFormatterManager } from './output-formatter/output-formatter-manager';
3435
import { CliOutputFormatterTypes } from './output-formatter/output-formatter.interface';
3536
import { TextFormatterStrategy } from './output-formatter/strategies/text-formatter.strategy';
3637
import { RawFormatterStrategy } from './output-formatter/strategies/raw-formatter.strategy';
38+
import {inspect} from "util";
3739

3840
@Injectable()
3941
export class CliBusinessService {
@@ -167,6 +169,13 @@ export class CliBusinessService {
167169
},
168170
);
169171

172+
if (command.toLowerCase() === 'ft.info') {
173+
this.cliAnalyticsService.sendIndexInfoEvent(
174+
clientMetadata.databaseId,
175+
getAnalyticsDataFromIndexInfo(reply as string[]),
176+
);
177+
}
178+
170179
this.logger.log('Succeed to execute redis CLI command.');
171180

172181
return {

redisinsight/api/src/modules/redis/utils/reply.util.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chunk } from 'lodash';
1+
import { chunk, isArray } from 'lodash';
22
import { IRedisClusterNode } from 'src/models';
33

44
/**
@@ -20,13 +20,20 @@ import { IRedisClusterNode } from 'src/models';
2020
* }
2121
* ```
2222
* @param input
23+
* @param options
2324
*/
24-
export const convertArrayReplyToObject = (input: string[]): { [key: string]: any } => chunk(
25+
export const convertArrayReplyToObject = (
26+
input: string[],
27+
options: { utf?: boolean } = {},
28+
): { [key: string]: any } => chunk(
2529
input,
2630
2,
2731
).reduce((prev: any, current: string[]) => {
2832
const [key, value] = current;
29-
return { ...prev, [key.toString().toLowerCase()]: value };
33+
return {
34+
...prev,
35+
[key.toString().toLowerCase()]: options.utf && !isArray(value) ? value?.toString() : value,
36+
};
3037
}, {});
3138

3239
/**

redisinsight/api/src/modules/workbench/providers/workbench-commands.executor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
UTF8FormatterStrategy,
2020
} from 'src/common/transformers';
2121
import { RedisClient } from 'src/modules/redis/client';
22+
import { getAnalyticsDataFromIndexInfo } from 'src/utils';
2223
import { WorkbenchAnalyticsService } from '../services/workbench-analytics/workbench-analytics.service';
2324

2425
@Injectable()
@@ -75,6 +76,13 @@ export class WorkbenchCommandsExecutor {
7576
{ command, rawMode: mode === RunQueryMode.Raw },
7677
);
7778

79+
if (command.toLowerCase() === 'ft.info') {
80+
this.analyticsService.sendIndexInfoEvent(
81+
client.clientMetadata.databaseId,
82+
getAnalyticsDataFromIndexInfo(response as string[]),
83+
);
84+
}
85+
7886
return result;
7987
} catch (error) {
8088
this.logger.error('Failed to execute workbench command.', error);

redisinsight/api/src/modules/workbench/services/workbench-analytics/workbench-analytics.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ export class WorkbenchAnalyticsService extends CommandTelemetryBaseService {
2121
super(eventEmitter, commandsService);
2222
}
2323

24+
sendIndexInfoEvent(
25+
databaseId: string,
26+
additionalData: object = {},
27+
): void {
28+
try {
29+
this.sendEvent(
30+
TelemetryEvents.WorkbenchIndexInfoSubmitted,
31+
{
32+
databaseId,
33+
...additionalData,
34+
},
35+
);
36+
} catch (e) {
37+
// ignore error
38+
}
39+
}
40+
2441
public async sendCommandExecutedEvents(
2542
databaseId: string,
2643
results: IExecResult[],

redisinsight/api/src/utils/analytics-helper.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isNil } from 'lodash';
1+
import { includes, isNil, map } from 'lodash';
2+
import { convertArrayReplyToObject } from 'src/modules/redis/utils/reply.util';
23

34
export const TOTAL_KEYS_BREAKPOINTS = [
45
500000,
@@ -70,3 +71,47 @@ export const calculateRedisHitRatio = (
7071
};
7172

7273
export const getIsPipelineEnable = (size: number): boolean => size > 1;
74+
75+
export const getAnalyticsDataFromIndexInfo = (reply: string[]): object => {
76+
const analyticsData = {};
77+
78+
try {
79+
const replyInfo = convertArrayReplyToObject(reply, { utf: true });
80+
const definition = convertArrayReplyToObject(replyInfo.index_definition, { utf: true });
81+
82+
analyticsData['key_type'] = definition?.key_type;
83+
analyticsData['default_score'] = definition?.default_score;
84+
analyticsData['num_docs'] = replyInfo?.num_docs;
85+
analyticsData['max_doc_id'] = replyInfo?.max_doc_id;
86+
analyticsData['num_terms'] = replyInfo?.num_terms;
87+
analyticsData['num_records'] = replyInfo?.num_records;
88+
analyticsData['total_indexing_time'] = replyInfo?.total_indexing_time;
89+
analyticsData['number_of_uses'] = replyInfo?.number_of_uses;
90+
analyticsData['cleaning'] = replyInfo?.cleaning;
91+
92+
if (replyInfo.dialect_stats) {
93+
analyticsData['dialect_stats'] = convertArrayReplyToObject(replyInfo.dialect_stats, { utf: true });
94+
}
95+
96+
analyticsData['attributes'] = map(replyInfo?.attributes, ((attr) => {
97+
const attrArray = map(attr, (str) => str.toString().toLowerCase());
98+
const attrObject = convertArrayReplyToObject(attr, { utf: true });
99+
100+
return {
101+
type: attrObject?.['type'],
102+
weight: attrObject?.['weight'] || undefined,
103+
phonetic: attrObject?.['phonetic'] || undefined,
104+
sortable: includes(attrArray, 'sortable') || undefined,
105+
nostem: includes(attrArray, 'nostem') || undefined,
106+
unf: includes(attrArray, 'unf') || undefined,
107+
noindex: includes(attrArray, 'noindex') || undefined,
108+
casesensitive: includes(attrArray, 'casesensitive') || undefined,
109+
};
110+
}));
111+
112+
return analyticsData;
113+
} catch (e) {
114+
// ignore errors
115+
return null;
116+
}
117+
};

0 commit comments

Comments
 (0)