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: add streaksList endpoint #3255

Merged
merged 1 commit into from
Jul 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/workers/achievements/src/handlers/streaksList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { error } from 'itty-router';

import filteredEvents from '../helpers/filteredNames';
import type { Env } from '../types';

export default async (id: string, date: string, env: Env) => {
if (!id) {
return error(400, 'Bad request!');
}

try {
const streaksResponse = await fetch(
`${env.CLICKHOUSE_REST_ENDPOINT}&default_format=JSONCompact`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
cf: { cacheTtl: 600, cacheEverything: true },
body: `
SELECT
name,
created
FROM events
WHERE actor = '${id}' AND created >= now() - INTERVAL 1 YEAR
AND name IN (${filteredEvents.map((name) => `'${name}'`).join(',')})
AND DATE(created) = '${date}';
`
}
);

if (streaksResponse.status !== 200) {
return new Response(
JSON.stringify({ success: false, error: 'Status code is not 200!' })
);
}

const json: {
data: [string, string][];
} = await streaksResponse.json();

let response = new Response(
JSON.stringify({
success: true,
data: json.data.map(([event, date]) => ({ event, date }))
})
);

// Cache for 10 minutes
response.headers.set('Cache-Control', 'max-age=600');

return response;
} catch (error) {
console.error('Failed to get streaks', error);
return new Response(
JSON.stringify({ success: false, error: 'Something went wrong!' })
);
}
};
10 changes: 8 additions & 2 deletions packages/workers/achievements/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createCors, error, json, Router } from 'itty-router';

import streaks from './handlers/streaks';
import streaksCalendar from './handlers/streaksCalendar';
import streaksList from './handlers/streaksList';
import type { Env } from './types';

const { preflight, corsify } = createCors({
Expand All @@ -12,7 +13,12 @@ const router = Router();

router.all('*', preflight);
router.get('/', () => new Response('gm, to achievements service πŸ‘‹'));
router.get('/streaks/:id', ({ params }, env) => streaks(params.id, env));
router.get('/streaks/:id', ({ params }, env) =>
streaksCalendar(params.id, env)
);
router.get('/streaks/:id/:date', ({ params }, env) =>
streaksList(params.id, params.date, env)
);

const routerHandleStack = (request: Request, env: Env, ctx: ExecutionContext) =>
router.handle(request, env, ctx).then(json);
Expand Down
Loading