Skip to content

Commit

Permalink
fix: dockerfile and also make Jitsu TypeORM logging async so we don't…
Browse files Browse the repository at this point in the history
… await on it
  • Loading branch information
evereq committed Nov 29, 2023
1 parent 0a4f0ba commit a27bc68
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .deploy/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ LABEL maintainer="[email protected]"
LABEL org.opencontainers.image.source https://github.com/ever-co/ever-gauzy

ENV CI=true
ENV NODE_ENV=${NODE_ENV:-production}

# ENV NODE_ENV=${NODE_ENV:-production}

# Set Python interpreter for `node-gyp` to use
ENV PYTHON /usr/bin/python
Expand Down
89 changes: 23 additions & 66 deletions packages/core/src/jitsu-analytics/jitsu-events-subscriber.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { AnalyticsInterface } from '@jitsu/js';
import { Logger } from '@nestjs/common';
import * as chalk from 'chalk';
import {
InsertEvent,
RemoveEvent,
EntitySubscriberInterface,
UpdateEvent,
EventSubscriber,
} from 'typeorm';
import { InsertEvent, RemoveEvent, EntitySubscriberInterface, UpdateEvent, EventSubscriber } from 'typeorm';
import { environment } from '@gauzy/config';
import { createJitsu } from './jitsu-helper';

Expand All @@ -32,75 +26,55 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
host: jitsu.serverHost,
writeKey: jitsu.serverWriteKey,
debug: jitsu.debug,
echoEvents: jitsu.echoEvents,
echoEvents: jitsu.echoEvents
};
this.logger.log(
`JITSU Configuration`,
chalk.magenta(JSON.stringify(jitsuConfig))
);
this.logger.log(`JITSU Configuration`, chalk.magenta(JSON.stringify(jitsuConfig)));
// Create an instance of Jitsu Analytics with configuration
this.jitsuAnalytics = createJitsu(jitsuConfig);
} else {
console.error(
chalk.yellow(
`Jitsu Analytics initialization failed: Missing host or writeKey.`
)
);
console.error(chalk.yellow(`Jitsu Analytics initialization failed: Missing host or writeKey.`));
}
} catch (error) {
console.error(
chalk.red(
`Jitsu Analytics initialization failed: ${error.message}`
)
);
console.error(chalk.red(`Jitsu Analytics initialization failed: ${error.message}`));
}
}

/**
* Called after entity insertion.
*/
async afterInsert(event: InsertEvent<any>) {
if (this.logEnabled)
this.logger.log(
`AFTER ENTITY INSERTED: `,
JSON.stringify(event.entity)
);
if (this.logEnabled) this.logger.log(`AFTER ENTITY INSERTED: `, JSON.stringify(event.entity));

// Track an event with Jitsu Analytics
await this.analyticsTrack('afterInsert', {
data: { ...event.entity },
// NOTE: we do not await before we want to track events asynchronously
this.analyticsTrack('afterInsert', {
data: { ...event.entity }
});
}

/**
* Called after entity update.
*/
async afterUpdate(event: UpdateEvent<any>) {
if (this.logEnabled)
this.logger.log(
`AFTER ENTITY UPDATED: `,
JSON.stringify(event.entity)
);
if (this.logEnabled) this.logger.log(`AFTER ENTITY UPDATED: `, JSON.stringify(event.entity));

// Track an event with Jitsu Analytics
await this.analyticsTrack('afterUpdate', {
data: { ...event.entity },
// NOTE: we do not await before we want to track events asynchronously
this.analyticsTrack('afterUpdate', {
data: { ...event.entity }
});
}

/**
* Called after entity removal.
*/
async afterRemove(event: RemoveEvent<any>) {
if (this.logEnabled)
this.logger.log(
`AFTER ENTITY REMOVED: `,
JSON.stringify(event.entity)
);
if (this.logEnabled) this.logger.log(`AFTER ENTITY REMOVED: `, JSON.stringify(event.entity));

// Track an event with Jitsu Analytics
await this.analyticsTrack('afterRemove', {
data: { ...event.entity },
// NOTE: we do not await before we want to track events asynchronously
this.analyticsTrack('afterRemove', {
data: { ...event.entity }
});
}

Expand All @@ -110,10 +84,7 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
* @param properties Additional event properties (optional).
* @returns A promise that resolves when the event is tracked.
*/
async analyticsTrack(
event: string,
properties?: Record<string, any> | null
): Promise<void> {
async analyticsTrack(event: string, properties?: Record<string, any> | null): Promise<void> {
// Check if this.jitsu is defined and both host and writeKey are defined
if (this.jitsuAnalytics) {
try {
Expand All @@ -126,14 +97,9 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
const tracked = await this.trackEvent(event, properties);

if (this.logEnabled)
this.logger.log(
`After Jitsu Tracked Entity Events`,
chalk.blue(JSON.stringify(tracked))
);
this.logger.log(`After Jitsu Tracked Entity Events`, chalk.blue(JSON.stringify(tracked)));
} catch (error) {
this.logger.error(
`Error while Jitsu tracking event. Unable to track event: ${error.message}`
);
this.logger.error(`Error while Jitsu tracking event. Unable to track event: ${error.message}`);
}
}
}
Expand All @@ -144,10 +110,7 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
* @param properties Additional data or properties associated with the event.
* @returns A Promise that resolves when the event is tracked.
*/
async trackEvent(
event: string,
properties?: Record<string, any> | null
): Promise<any> {
async trackEvent(event: string, properties?: Record<string, any> | null): Promise<any> {
return await this.jitsuAnalytics.track(event, properties);
}

Expand All @@ -157,10 +120,7 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
* @param traits User traits or properties to associate with the user.
* @returns A Promise that resolves when the user is identified.
*/
async identify(
id: string | object,
traits?: Record<string, any> | null
): Promise<any> {
async identify(id: string | object, traits?: Record<string, any> | null): Promise<any> {
return await this.jitsuAnalytics.identify(id, traits);
}

Expand All @@ -170,10 +130,7 @@ export class JitsuEventsSubscriber implements EntitySubscriberInterface {
* @param traits Additional data or traits associated with the group.
* @returns A Promise that resolves when the users are grouped.
*/
async group(
id: string | object,
traits?: Record<string, any> | null
): Promise<any> {
async group(id: string | object, traits?: Record<string, any> | null): Promise<any> {
return await this.jitsuAnalytics.group(id, traits);
}
}

0 comments on commit a27bc68

Please sign in to comment.