Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .infra/crons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export const crons: Cron[] = [
name: 'post-analytics-history-day-clickhouse',
schedule: '3-59/5 * * * *',
},
{
name: 'user-profile-analytics-clickhouse',
schedule: '7 */1 * * *',
},
{
name: 'user-profile-analytics-history-clickhouse',
schedule: '15 */1 * * *',
},
{
name: 'clean-zombie-opportunities',
schedule: '30 6 * * *',
Expand Down
220 changes: 220 additions & 0 deletions __tests__/cron/userProfileAnalyticsClickhouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
import { crons } from '../../src/cron/index';
import { userProfileAnalyticsClickhouseCron as cron } from '../../src/cron/userProfileAnalyticsClickhouse';
import {
expectSuccessfulCron,
mockClickhouseClientOnce,
mockClickhouseQueryJSONOnce,
saveFixtures,
} from '../helpers';
import { userProfileAnalyticsFixture } from '../fixture/userProfileAnalytics';
import { usersFixture, plusUsersFixture } from '../fixture/user';
import createOrGetConnection from '../../src/db';
import type { DataSource } from 'typeorm';
import { UserProfileAnalytics } from '../../src/entity/user/UserProfileAnalytics';
import { User } from '../../src/entity/user/User';
import { deleteRedisKey, getRedisHash } from '../../src/redis';
import { generateStorageKey, StorageTopic } from '../../src/config';
import { format, startOfToday } from 'date-fns';

let con: DataSource;

beforeAll(async () => {
con = await createOrGetConnection();
});

const cronConfigRedisKey = generateStorageKey(
StorageTopic.Cron,
cron.name,
'config',
);

beforeEach(async () => {
jest.clearAllMocks();
await deleteRedisKey(cronConfigRedisKey);
await saveFixtures(con, User, [...usersFixture, ...plusUsersFixture]);
});

const userIds = ['1', '2', '3', '4', '5'];

describe('userProfileAnalyticsClickhouse cron', () => {
it('should be registered', () => {
const registeredWorker = crons.find((item) => item.name === cron.name);

expect(registeredWorker).toBeDefined();
});

it('should sync user profile analytics data', async () => {
const clickhouseClientMock = mockClickhouseClientOnce();

const queryJSONSpy = mockClickhouseQueryJSONOnce(
clickhouseClientMock,
userProfileAnalyticsFixture.map((item, index) => ({
...item,
updatedAt: new Date(Date.now() + index * 1000).toISOString(),
id: userIds[index],
})),
);

const now = new Date();

await expectSuccessfulCron(cron);

expect(queryJSONSpy).toHaveBeenCalledTimes(1);
expect(queryJSONSpy).toHaveBeenCalledWith({
query: expect.stringContaining('SELECT'),
format: 'JSONEachRow',
query_params: {
lastRunAt: format(startOfToday(), 'yyyy-MM-dd HH:mm:ss'),
},
});

const userProfileAnalytics = await con
.getRepository(UserProfileAnalytics)
.find({
order: {
updatedAt: 'ASC',
},
});

expect(userProfileAnalytics.length).toBe(
userProfileAnalyticsFixture.length,
);

userProfileAnalytics.forEach((item, index) => {
expect(item).toEqual({
id: userIds[index],
updatedAt: expect.any(Date),
createdAt: expect.any(Date),
uniqueVisitors: userProfileAnalyticsFixture[index].uniqueVisitors,
} as UserProfileAnalytics);
});

const cronConfig = await getRedisHash(cronConfigRedisKey);

expect(cronConfig).toBeDefined();
expect(cronConfig.lastRunAt).toBeDefined();
expect(new Date(cronConfig.lastRunAt).getTime()).toBeGreaterThan(
now.getTime(),
);
});

it('should use lastRunAt from previous run', async () => {
let clickhouseClientMock = mockClickhouseClientOnce();

mockClickhouseQueryJSONOnce(
clickhouseClientMock,
userProfileAnalyticsFixture.map((item, index) => ({
...item,
updatedAt: new Date(Date.now() + index * 1000).toISOString(),
id: userIds[index],
})),
);

await expectSuccessfulCron(cron);

const userProfileAnalytics = await con
.getRepository(UserProfileAnalytics)
.find({
order: {
updatedAt: 'ASC',
},
});

expect(userProfileAnalytics.length).toBe(
userProfileAnalyticsFixture.length,
);

userProfileAnalytics.forEach((item, index) => {
expect(item).toEqual({
id: userIds[index],
updatedAt: expect.any(Date),
createdAt: expect.any(Date),
uniqueVisitors: userProfileAnalyticsFixture[index].uniqueVisitors,
} as UserProfileAnalytics);
});

const lastCronConfig = await getRedisHash(cronConfigRedisKey);

clickhouseClientMock = mockClickhouseClientOnce();

const queryJSONSpy = mockClickhouseQueryJSONOnce(
clickhouseClientMock,
userProfileAnalyticsFixture.map((item, index) => ({
...item,
updatedAt: new Date(Date.now() + index * 1000).toISOString(),
id: userIds[index],
})),
);

await expectSuccessfulCron(cron);

expect(queryJSONSpy).toHaveBeenCalledTimes(1);
expect(queryJSONSpy).toHaveBeenCalledWith({
query: expect.stringContaining('SELECT'),
format: 'JSONEachRow',
query_params: {
lastRunAt: format(
new Date(lastCronConfig.lastRunAt),
'yyyy-MM-dd HH:mm:ss',
),
},
});

const cronConfig = await getRedisHash(cronConfigRedisKey);

expect(cronConfig).toBeDefined();
expect(cronConfig.lastRunAt).toBeDefined();
expect(new Date(cronConfig.lastRunAt).getTime()).toBeGreaterThan(
new Date(lastCronConfig.lastRunAt).getTime(),
);
});

it('should upsert user profile analytics data on repeated runs', async () => {
let clickhouseClientMock = mockClickhouseClientOnce();

mockClickhouseQueryJSONOnce(
clickhouseClientMock,
userProfileAnalyticsFixture.map((item, index) => ({
...item,
updatedAt: new Date(Date.now() + index * 1000).toISOString(),
id: userIds[index],
})),
);

await expectSuccessfulCron(cron);

clickhouseClientMock = mockClickhouseClientOnce();

mockClickhouseQueryJSONOnce(
clickhouseClientMock,
userProfileAnalyticsFixture.map((item, index) => ({
...item,
updatedAt: new Date(Date.now() + index * 1000).toISOString(),
id: userIds[index],
uniqueVisitors: item.uniqueVisitors + 100,
})),
);

await expectSuccessfulCron(cron);

const userProfileAnalytics = await con
.getRepository(UserProfileAnalytics)
.find({
select: ['id', 'uniqueVisitors'],
order: {
updatedAt: 'ASC',
},
});

expect(userProfileAnalytics.length).toBe(
userProfileAnalyticsFixture.length,
);

userProfileAnalytics.forEach((item, index) => {
expect(item).toEqual({
id: userIds[index],
uniqueVisitors: userProfileAnalyticsFixture[index].uniqueVisitors + 100,
} as UserProfileAnalytics);
});
});
});
Loading
Loading