Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate feeds worker to next api #4021

Merged
merged 1 commit into from
Nov 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions apps/api/pages/api/feed/getPublicationIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AlgorithmProvider } from '@hey/data/enums';
import { Errors } from '@hey/data/errors';
import type { NextApiRequest, NextApiResponse } from 'next';
import allowCors from 'utils/allowCors';
import { CACHE_AGE } from 'utils/constants';
import heyFeed from 'utils/feeds/providers/hey/heyFeed';
import k3lFeed from 'utils/feeds/providers/k3l/k3lFeed';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const provider = req.query.provider as string;
const strategy = req.query.strategy as string;
const profile = req.query.profile as string;
const limit = (parseInt(req.query?.limit as string) || 50) as number;
const offset = (parseInt(req.query?.offset as string) || 0) as number;

if (!provider || !strategy) {
return res.status(400).json({ success: false, error: Errors.NoBody });
}

try {
let ids: string[] = [];
switch (provider) {
case AlgorithmProvider.K3L:
ids = await k3lFeed(strategy, profile, limit, offset);
break;
case AlgorithmProvider.HEY:
ids = await heyFeed(strategy, limit, offset);
break;
default:
return res
.status(200)
.setHeader('Cache-Control', CACHE_AGE)
.json({ success: false, message: 'Invalid provider' });
}

return res
.status(200)
.setHeader('Cache-Control', CACHE_AGE)
.json({ success: true, ids });
} catch (error) {
throw error;
}
};

export default allowCors(handler);
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import createClickhouseClient from '@hey/clickhouse/createClickhouseClient';
import { Errors } from '@hey/data/errors';
import { PUBLICATION } from '@hey/data/tracking';

import randomizeIds from '../../../helpers/randomizeIds';
import type { Env } from '../../../types';
import createClickhouseClient from 'utils/createClickhouseClient';
import randomizeIds from 'utils/feeds/randomizeIds';

const interactionAndWeights = {
[PUBLICATION.COLLECT_MODULE.COLLECT]: 10,
Expand All @@ -30,15 +28,14 @@ const generateWeightedCaseStatement = () => {

const heyMostInteracted = async (
limit: number,
offset: number,
env: Env
offset: number
): Promise<any[]> => {
if (limit > 500) {
throw new Error(Errors.Limit500);
}

try {
const client = createClickhouseClient(env.CLICKHOUSE_PASSWORD);
const client = createClickhouseClient();
const rows = await client.query({
query: `
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import createClickhouseClient from '@hey/clickhouse/createClickhouseClient';
import { Errors } from '@hey/data/errors';
import { PAGEVIEW } from '@hey/data/tracking';
import createClickhouseClient from 'utils/createClickhouseClient';
import randomizeIds from 'utils/feeds/randomizeIds';
import removeParamsFromString from 'utils/feeds/removeParamsFromString';

import randomizeIds from '../../../helpers/randomizeIds';
import removeParamsFromString from '../../../helpers/removeParamsFromString';
import type { Env } from '../../../types';

const heyMostViewed = async (
limit: number,
offset: number,
env: Env
): Promise<any[]> => {
const heyMostViewed = async (limit: number, offset: number): Promise<any[]> => {
if (limit > 500) {
throw new Error(Errors.Limit500);
}

try {
const client = createClickhouseClient(env.CLICKHOUSE_PASSWORD);
const client = createClickhouseClient();
const rows = await client.query({
query: `
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import type { Env } from '../../types';
import heyMostInteracted from './algorithms/heyMostInteracted';
import heyMostViewed from './algorithms/heyMostViewed';

const heyFeed = async (
strategy: string,
limit: number,
offset: number,
env: Env
) => {
const heyFeed = async (strategy: string, limit: number, offset: number) => {
switch (strategy) {
case 'mostviewed':
return await heyMostViewed(limit, offset, env);
return await heyMostViewed(limit, offset);
case 'mostinteracted':
return await heyMostInteracted(limit, offset, env);
return await heyMostInteracted(limit, offset);
default:
return [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import urlcat from 'urlcat';

import randomizeIds from '../../../helpers/randomizeIds';
import randomizeIds from 'utils/feeds/randomizeIds';

const k3lGlobalFeed = async (
strategy: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import urlcat from 'urlcat';

import randomizeIds from '../../../helpers/randomizeIds';
import randomizeIds from 'utils/feeds/randomizeIds';

const k3lPersonalFeed = async (
strategy: string,
Expand Down
1 change: 0 additions & 1 deletion docs/ports.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ The following ports are exposed by the repository
| 8084 | Snapshot relay worker | http://localhost:8084 | https://snapshot-relay.hey.xyz |
| 8086 | Oembed worker | http://localhost:8086 | https://oembed.hey.xyz |
| 8088 | Stats worker | http://localhost:8088 | https://stats.hey.xyz |
| 8089 | Feeds gateway | http://localhost:8089 | https://feeds.hey.xyz |
| 8090 | Preferences worker | http://localhost:8090 | https://preferences.hey.xyz |
| 8091 | Groups worker | http://localhost:8091 | https://groups.hey.xyz |
| 8092 | NFT worker | http://localhost:8092 | https://nft.hey.xyz |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prettier:fix": "turbo run prettier:fix --parallel",
"start": "turbo run start --parallel",
"test:dev": "turbo run test:dev",
"test:e2e": "start-server-and-test start '8083|8086|8089' test:dev",
"test:e2e": "start-server-and-test start '8083|8086' test:dev",
"typecheck": "turbo run typecheck --parallel",
"typecheck:affected": "turbo run typecheck --parallel --filter=[origin/main]",
"workers:deploy": "turbo run worker:deploy --parallel"
Expand Down
3 changes: 0 additions & 3 deletions packages/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ export const OEMBED_WORKER_URL = IS_PRODUCTION
export const STATS_WORKER_URL = IS_PRODUCTION
? 'https://stats.hey.xyz'
: 'http://localhost:8088';
export const FEEDS_WORKER_URL = IS_PRODUCTION
? 'https://feeds.hey.xyz'
: 'http://localhost:8089';
export const PREFERENCES_WORKER_URL = IS_PRODUCTION
? 'https://preferences.hey.xyz'
: 'http://localhost:8090';
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/getPublicationIds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FEEDS_WORKER_URL } from '@hey/data/constants';
import { HEY_API_URL } from '@hey/data/constants';
import axios from 'axios';

/**
Expand All @@ -16,7 +16,7 @@ const getPublicationIds = async (
profile?: string
) => {
try {
const response = await axios.get(`${FEEDS_WORKER_URL}/ids`, {
const response = await axios.get(`${HEY_API_URL}/feed/getPublicationIds`, {
params: {
provider,
strategy,
Expand Down
1 change: 0 additions & 1 deletion packages/workers/feeds/.dev.vars.example

This file was deleted.

6 changes: 0 additions & 6 deletions packages/workers/feeds/.eslintrc.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/workers/feeds/README.md

This file was deleted.

31 changes: 0 additions & 31 deletions packages/workers/feeds/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/workers/feeds/src/constants.ts

This file was deleted.

51 changes: 0 additions & 51 deletions packages/workers/feeds/src/handlers/getPublicationIds.spec.ts

This file was deleted.

39 changes: 0 additions & 39 deletions packages/workers/feeds/src/handlers/getPublicationIds.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/workers/feeds/src/helpers/buildRequest.ts

This file was deleted.

43 changes: 0 additions & 43 deletions packages/workers/feeds/src/index.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/workers/feeds/src/providers/hey/README.md

This file was deleted.