chore(analytics): remove tinybird (#11146)

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Antoine Moreaux
2025-03-26 21:59:42 +01:00
committed by GitHub
parent e64c4b8ab1
commit 4c7d71e325
61 changed files with 11 additions and 2575 deletions

View File

@ -1,4 +1,3 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { JwtModule } from 'src/engine/core-modules/jwt/jwt.module';
@ -6,16 +5,9 @@ import { JwtModule } from 'src/engine/core-modules/jwt/jwt.module';
import { AnalyticsResolver } from './analytics.resolver';
import { AnalyticsService } from './analytics.service';
const TINYBIRD_BASE_URL = 'https://api.eu-central-1.aws.tinybird.co/v0';
@Module({
providers: [AnalyticsResolver, AnalyticsService],
imports: [
JwtModule,
HttpModule.register({
baseURL: TINYBIRD_BASE_URL,
}),
],
imports: [JwtModule],
exports: [AnalyticsService],
})
export class AnalyticsModule {}

View File

@ -1,11 +1,6 @@
import { HttpService } from '@nestjs/axios';
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios';
import { AnalyticsTinybirdJwtMap } from 'src/engine/core-modules/analytics/entities/analytics-tinybird-jwts.entity';
import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';
type CreateEventInput = {
action: string;
@ -14,14 +9,7 @@ type CreateEventInput = {
@Injectable()
export class AnalyticsService {
private readonly logger = new Logger(AnalyticsService.name);
private readonly defaultDatasource = 'event';
constructor(
private readonly jwtWrapperService: JwtWrapperService,
private readonly environmentService: EnvironmentService,
private readonly httpService: HttpService,
) {}
constructor(private readonly environmentService: EnvironmentService) {}
async create(
createEventInput: CreateEventInput,
@ -32,11 +20,11 @@ export class AnalyticsService {
return { success: true };
}
let data;
let _data;
switch (createEventInput.action) {
case 'pageview':
data = {
_data = {
timestamp: new Date().toISOString(),
version: '1',
userId: userId,
@ -45,7 +33,7 @@ export class AnalyticsService {
};
break;
default:
data = {
_data = {
action: createEventInput.action,
timestamp: new Date().toISOString(),
version: '1',
@ -58,88 +46,8 @@ export class AnalyticsService {
break;
}
const config: AxiosRequestConfig = {
headers: {
Authorization:
'Bearer ' + this.environmentService.get('TINYBIRD_INGEST_TOKEN'),
},
};
const datasource =
createEventInput.action === 'pageview'
? 'pageview'
: this.defaultDatasource;
try {
await this.httpService.axiosRef.post(
`/events?name=${datasource}`,
data,
config,
);
} catch (error) {
this.logger.error('Error occurred:', error);
if (error.response) {
this.logger.error(
`Error response body: ${JSON.stringify(error.response.data)}`,
);
}
return { success: false };
}
// TODO: send event to clickhouse
return { success: true };
}
generateWorkspaceJwt(
workspaceId: string | undefined,
): AnalyticsTinybirdJwtMap | null {
if (!this.environmentService.get('ANALYTICS_ENABLED')) {
return null;
}
const jwtPayload = {
name: 'analytics_jwt',
workspace_id: this.environmentService.get('TINYBIRD_WORKSPACE_UUID'),
scopes: [
{
type: 'PIPES:READ',
resource: '',
fixed_params: { workspaceId },
},
],
};
const jwtOptions = {
secret: this.environmentService.get('TINYBIRD_GENERATE_JWT_TOKEN'),
expiresIn: '7d',
};
const analyticsProperties = [
'getWebhookAnalytics',
'getPageviewsAnalytics',
'getUsersAnalytics',
'getServerlessFunctionDuration',
'getServerlessFunctionSuccessRate',
'getServerlessFunctionErrorCount',
];
return analyticsProperties.reduce(
(acc, property) => ({
...acc,
[property]: this.jwtWrapperService.sign(
{
...jwtPayload,
scopes: [
{
...jwtPayload.scopes[0],
resource: property,
},
],
},
jwtOptions,
),
}),
{},
) as AnalyticsTinybirdJwtMap;
}
}

View File

@ -1,22 +0,0 @@
import { Field, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class AnalyticsTinybirdJwtMap {
@Field(() => String)
getWebhookAnalytics: string;
@Field(() => String)
getPageviewsAnalytics: string;
@Field(() => String)
getUsersAnalytics: string;
@Field(() => String)
getServerlessFunctionDuration: string;
@Field(() => String)
getServerlessFunctionSuccessRate: string;
@Field(() => String)
getServerlessFunctionErrorCount: string;
}

View File

@ -101,7 +101,7 @@ export const ENVIRONMENT_VARIABLES_GROUP_METADATA: Record<
'We use this to setup a small support chat on the bottom left. Currently powered by Front.',
isHiddenOnLoad: true,
},
[EnvironmentVariablesGroup.TinybirdConfig]: {
[EnvironmentVariablesGroup.AnalyticsConfig]: {
position: 1700,
description:
'Were running a test to perform analytics within the app. This will evolve.',

View File

@ -15,6 +15,6 @@ export enum EnvironmentVariablesGroup {
ServerlessConfig = 'serverless-config',
SSL = 'ssl',
SupportChatConfig = 'support-chat-config',
TinybirdConfig = 'tinybird-config',
AnalyticsConfig = 'analytics-config',
TokensDuration = 'tokens-duration',
}

View File

@ -453,7 +453,7 @@ export class EnvironmentVariables {
SERVERLESS_LAMBDA_SECRET_ACCESS_KEY: string;
@EnvironmentVariablesMetadata({
group: EnvironmentVariablesGroup.TinybirdConfig,
group: EnvironmentVariablesGroup.AnalyticsConfig,
description: 'Enable or disable analytics for telemetry',
})
@CastToBoolean()
@ -470,33 +470,6 @@ export class EnvironmentVariables {
@IsBoolean()
TELEMETRY_ENABLED = true;
@EnvironmentVariablesMetadata({
group: EnvironmentVariablesGroup.TinybirdConfig,
sensitive: true,
description: 'Ingest token for Tinybird analytics',
})
@IsString()
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_INGEST_TOKEN: string;
@EnvironmentVariablesMetadata({
group: EnvironmentVariablesGroup.TinybirdConfig,
sensitive: true,
description: 'Workspace UUID for Tinybird analytics',
})
@IsString()
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_WORKSPACE_UUID: string;
@EnvironmentVariablesMetadata({
group: EnvironmentVariablesGroup.TinybirdConfig,
sensitive: true,
description: 'JWT token for Tinybird analytics',
})
@IsString()
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_GENERATE_JWT_TOKEN: string;
@EnvironmentVariablesMetadata({
group: EnvironmentVariablesGroup.BillingConfig,
description: 'Enable or disable billing features',

View File

@ -20,7 +20,6 @@ import { SupportDriver } from 'src/engine/core-modules/environment/interfaces/su
import { FileFolder } from 'src/engine/core-modules/file/interfaces/file-folder.interface';
import { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service';
import { AnalyticsTinybirdJwtMap } from 'src/engine/core-modules/analytics/entities/analytics-tinybird-jwts.entity';
import {
AuthException,
AuthExceptionCode,
@ -280,11 +279,6 @@ export class UserResolver {
return getHMACKey(parent.email, key);
}
@ResolveField(() => AnalyticsTinybirdJwtMap, { nullable: true })
analyticsTinybirdJwts(@AuthWorkspace() workspace: Workspace | undefined) {
return this.analyticsService.generateWorkspaceJwt(workspace?.id);
}
@Mutation(() => String)
async uploadProfilePicture(
@AuthUser() { id }: User,