Skip to content

Commit 967718e

Browse files
committed
feat: update telemetry log to use email instead of userId; modify related components and services
1 parent 45814f5 commit 967718e

File tree

9 files changed

+44
-24
lines changed

9 files changed

+44
-24
lines changed

backend/src/dashboard/dto/telemetry-log-input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class TelemetryLogFilterInput {
1515
endpoint?: string;
1616

1717
@Field({ nullable: true })
18-
userId?: string;
18+
email?: string;
1919

2020
@Field({ nullable: true })
2121
handler?: string;

backend/src/interceptor/LoggingInterceptor.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,30 @@ import { Observable } from 'rxjs';
1010
import { tap } from 'rxjs/operators';
1111
import { GqlExecutionContext } from '@nestjs/graphql';
1212
import { TelemetryLogService } from './telemetry-log.service';
13-
import { GetUserIdFromToken } from 'src/decorator/get-auth-token.decorator';
13+
import { InjectRepository } from '@nestjs/typeorm';
14+
import { User } from 'src/user/user.model';
15+
import { Repository } from 'typeorm';
1416

1517
@Injectable()
1618
export class LoggingInterceptor implements NestInterceptor {
1719
private readonly logger = new Logger('RequestLogger');
1820
private startTime: number;
1921

20-
constructor(private telemetryLogService: TelemetryLogService) {}
22+
constructor(
23+
private telemetryLogService: TelemetryLogService,
24+
@InjectRepository(User)
25+
private readonly userRepository: Repository<User>,
26+
) {}
2127

22-
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
28+
async intercept(
29+
context: ExecutionContext,
30+
next: CallHandler,
31+
): Promise<Observable<any>> {
2332
const contextType = context.getType();
2433
this.logger.debug(`Intercepting request, Context Type: ${contextType}`);
2534

2635
if (contextType === ('graphql' as ContextType)) {
27-
return this.handleGraphQLRequest(context, next);
36+
return await this.handleGraphQLRequest(context, next);
2837
} else if (contextType === 'http') {
2938
return this.handleRestRequest(context, next);
3039
} else {
@@ -33,13 +42,18 @@ export class LoggingInterceptor implements NestInterceptor {
3342
}
3443
}
3544

36-
private handleGraphQLRequest(
45+
private async handleGraphQLRequest(
3746
context: ExecutionContext,
3847
next: CallHandler,
39-
): Observable<any> {
48+
): Promise<Observable<any>> {
4049
const ctx = GqlExecutionContext.create(context);
4150
const info = ctx.getInfo();
4251
const userId = ctx.getContext().req.user?.userId;
52+
const user = await this.userRepository.findOne({
53+
where: { id: userId },
54+
select: { email: true },
55+
});
56+
const email = user?.email;
4357
if (!info) {
4458
this.logger.warn(
4559
'GraphQL request detected, but ctx.getInfo() is undefined.',
@@ -76,6 +90,7 @@ export class LoggingInterceptor implements NestInterceptor {
7690
output: JSON.stringify(value),
7791
timeConsumed,
7892
userId,
93+
email,
7994
handler: 'GraphQL',
8095
});
8196
},

backend/src/interceptor/interceptor.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
33
import { TelemetryLog } from './telemetry-log.model';
44
import { TelemetryLogService } from './telemetry-log.service';
55
import { LoggingInterceptor } from './LoggingInterceptor';
6+
import { User } from 'src/user/user.model';
67

78
@Module({
8-
imports: [TypeOrmModule.forFeature([TelemetryLog])],
9+
imports: [TypeOrmModule.forFeature([TelemetryLog, User])],
910
providers: [TelemetryLogService, LoggingInterceptor],
1011
exports: [TelemetryLogService, LoggingInterceptor],
1112
})

backend/src/interceptor/telemetry-log.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class TelemetryLog {
4444
@Field({ nullable: true })
4545
userId: string;
4646

47+
@Column({ nullable: true })
48+
@Field({ nullable: true })
49+
email: string;
50+
4751
@Column({ nullable: true })
4852
@Field({ nullable: true })
4953
handler: string;

backend/src/interceptor/telemetry-log.service.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export class TelemetryLogService {
4848
endpoint: `%${filter.endpoint}%`,
4949
});
5050
}
51-
if (filter.userId) {
52-
query.andWhere('log.userId = :userId', { userId: filter.userId });
53-
}
5451
if (filter.handler) {
5552
query.andWhere('log.handler LIKE :handler', {
5653
handler: `%${filter.handler}%`,
@@ -102,8 +99,8 @@ export class TelemetryLogService {
10299
endpoint: `%${filter.endpoint}%`,
103100
});
104101
}
105-
if (filter.userId) {
106-
query.andWhere('log.userId = :userId', { userId: filter.userId });
102+
if (filter.email) {
103+
query.andWhere('log.email = :email', { email: filter.email });
107104
}
108105
if (filter.handler) {
109106
query.andWhere('log.handler LIKE :handler', {

dashboard/src/content/management/telemetry/TelemetryLogs.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface TelemetryLog {
3131
input?: string;
3232
output?: string;
3333
timeConsumed: number;
34-
userId?: string;
34+
email?: string;
3535
handler: string;
3636
}
3737

@@ -40,7 +40,7 @@ interface TelemetryLogFilterInput {
4040
endDate?: Date;
4141
requestMethod?: string;
4242
endpoint?: string;
43-
userId?: string;
43+
email?: string;
4444
handler?: string;
4545
minTimeConsumed?: number;
4646
maxTimeConsumed?: number;
@@ -125,10 +125,10 @@ const TelemetryLogs = () => {
125125
onChange={handleFilterChange('handler')}
126126
/>
127127
<TextField
128-
label="User ID"
128+
label="Email"
129129
size="small"
130-
value={filter.userId || ''}
131-
onChange={handleFilterChange('userId')}
130+
value={filter.email || ''}
131+
onChange={handleFilterChange('email')}
132132
/>
133133
</Box>
134134
)}
@@ -142,7 +142,7 @@ const TelemetryLogs = () => {
142142
<TableCell>Endpoint</TableCell>
143143
<TableCell>Handler</TableCell>
144144
<TableCell>Time (ms)</TableCell>
145-
<TableCell>User ID</TableCell>
145+
<TableCell>Email</TableCell>
146146
<TableCell>Actions</TableCell>
147147
</TableRow>
148148
</TableHead>
@@ -163,7 +163,7 @@ const TelemetryLogs = () => {
163163
<TableCell>{log.endpoint}</TableCell>
164164
<TableCell>{log.handler}</TableCell>
165165
<TableCell>{log.timeConsumed}</TableCell>
166-
<TableCell>{log.userId || '-'}</TableCell>
166+
<TableCell>{log.email || '-'}</TableCell>
167167
<TableCell>
168168
<Tooltip title="View Details">
169169
<IconButton

dashboard/src/graphql/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export const GET_DASHBOARD_TELEMETRY_LOGS = gql`
478478
input
479479
output
480480
timeConsumed
481-
userId
481+
email
482482
handler
483483
}
484484
}
@@ -494,7 +494,7 @@ export const GET_DASHBOARD_TELEMETRY_LOG = gql`
494494
input
495495
output
496496
timeConsumed
497-
userId
497+
email
498498
handler
499499
}
500500
}

frontend/src/graphql/schema.gql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ type SystemRole {
334334
}
335335

336336
type TelemetryLog {
337+
email: String
337338
endpoint: String!
338339
handler: String
339340
id: ID!
@@ -348,6 +349,7 @@ type TelemetryLog {
348349
}
349350

350351
input TelemetryLogFilterInput {
352+
email: String
351353
endDate: Date
352354
endpoint: String
353355
handler: String
@@ -356,7 +358,6 @@ input TelemetryLogFilterInput {
356358
requestMethod: String
357359
search: String
358360
startDate: Date
359-
userId: String
360361
}
361362

362363
input UpdateChatInput {

frontend/src/graphql/type.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ export type SystemRole = {
620620

621621
export type TelemetryLog = {
622622
__typename: 'TelemetryLog';
623+
email?: Maybe<Scalars['String']['output']>;
623624
endpoint: Scalars['String']['output'];
624625
handler?: Maybe<Scalars['String']['output']>;
625626
id: Scalars['ID']['output'];
@@ -634,6 +635,7 @@ export type TelemetryLog = {
634635
};
635636

636637
export type TelemetryLogFilterInput = {
638+
email?: InputMaybe<Scalars['String']['input']>;
637639
endDate?: InputMaybe<Scalars['Date']['input']>;
638640
endpoint?: InputMaybe<Scalars['String']['input']>;
639641
handler?: InputMaybe<Scalars['String']['input']>;
@@ -642,7 +644,6 @@ export type TelemetryLogFilterInput = {
642644
requestMethod?: InputMaybe<Scalars['String']['input']>;
643645
search?: InputMaybe<Scalars['String']['input']>;
644646
startDate?: InputMaybe<Scalars['Date']['input']>;
645-
userId?: InputMaybe<Scalars['String']['input']>;
646647
};
647648

648649
export type UpdateChatInput = {
@@ -1654,6 +1655,7 @@ export type TelemetryLogResolvers<
16541655
ParentType extends
16551656
ResolversParentTypes['TelemetryLog'] = ResolversParentTypes['TelemetryLog'],
16561657
> = ResolversObject<{
1658+
email?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
16571659
endpoint?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
16581660
handler?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
16591661
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;

0 commit comments

Comments
 (0)