Skip to content

Commit

Permalink
[CAI-181] update lambda trigger (#1208)
Browse files Browse the repository at this point in the history
* add healthz call on chatbot popup

* add changeset

* Move getHealthz code on separate script

* remove solutions.ts empty file, move makeError to file
  • Loading branch information
MarBert authored Oct 24, 2024
1 parent 2f93d2d commit 980fe99
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-bugs-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": patch
---

Add healthz call on chatbot popoup
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import React from 'react';
import { Query } from '@/lib/chatbot/queries';
import { useTranslations } from 'next-intl';
import { ChatbotErrorsType } from '@/helpers/chatbot.helper';
import { getChatbotHealthz } from '@/lib/chatbotApi';

type ChatbotLayoutProps = {
queries: Query[];
Expand Down Expand Up @@ -45,6 +46,7 @@ const ChatbotLayout = ({
);

const handleClick = () => {
if (!open) getChatbotHealthz();
setAnchorEl(ref.current);
return null;
};
Expand Down
34 changes: 34 additions & 0 deletions apps/nextjs-website/src/lib/chatbot/chatbotHealthz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { pipe } from 'fp-ts/lib/function';
import { ChatbotEnv } from '@/lib/chatbot/chatbotEnv';
import * as E from 'fp-ts/lib/Either';
import * as R from 'fp-ts/lib/Reader';
import * as TE from 'fp-ts/lib/TaskEither';
import { makeError } from '../makeError';

export const getHealthz = () =>
pipe(
R.ask<ChatbotEnv>(),
R.map(({ config: { CHATBOT_HOST: chatbotHost }, getAuthToken, fetch }) =>
pipe(
// handle any promise result
TE.tryCatch(() => getAuthToken(), E.toError),
TE.chainTaskK(
(authToken) => () =>
fetch(`${chatbotHost}/healthz`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
},
})
),
TE.chain((response) => {
if (response.status === 200) {
return TE.tryCatch(() => response.json(), E.toError);
} else {
return TE.left(makeError(response));
}
})
)()
)
);
5 changes: 1 addition & 4 deletions apps/nextjs-website/src/lib/chatbot/chatbotQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import * as E from 'fp-ts/lib/Either';
import * as PR from '@/lib/strapi/PathReporter';
import * as R from 'fp-ts/lib/Reader';
import * as TE from 'fp-ts/lib/TaskEither';

const makeError = ({ status, statusText }: Response) => {
return new Error(`${status} - ${statusText}`);
};
import { makeError } from '../makeError';

export const postQuery = (input: QueryInput) =>
pipe(
Expand Down
5 changes: 1 addition & 4 deletions apps/nextjs-website/src/lib/chatbot/chatbotSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import * as PR from '@/lib/strapi/PathReporter';
import * as R from 'fp-ts/lib/Reader';
import * as TE from 'fp-ts/lib/TaskEither';
import qs from 'qs';

const makeError = ({ status, statusText }: Response) => {
return new Error(`${status} - ${statusText}`);
};
import { makeError } from '../makeError';

export const getSessions = (page: number, pageSize: number) =>
pipe(
Expand Down
3 changes: 3 additions & 0 deletions apps/nextjs-website/src/lib/chatbotApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { makeChatbotEnv } from '@/lib/chatbot/chatbotEnv';
import { makeChatbotConfig, publicEnv } from '@/lib/chatbot/chatbotConfig';
import qs from 'qs';
import { getHealthz } from './chatbot/chatbotHealthz';
import { deleteSession, getSessions } from './chatbot/chatbotSessions';

const chatbotApiEnv = pipe(
Expand All @@ -28,6 +29,8 @@ export const getChatbotQueries = (sessionId?: string) =>
chatbotApiEnv
);

export const getChatbotHealthz = () => getHealthz()(chatbotApiEnv);

export const sendChatbotFeedback = (
feedback: boolean,
sessionId: string,
Expand Down
3 changes: 3 additions & 0 deletions apps/nextjs-website/src/lib/makeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const makeError = ({ status, statusText }: Response) => {
return new Error(`${status} - ${statusText}`);
};
Empty file.
5 changes: 1 addition & 4 deletions apps/nextjs-website/src/lib/strapi/fetchFromStrapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as E from 'fp-ts/lib/Either';
import * as TE from 'fp-ts/lib/TaskEither';
import * as PR from './PathReporter';
import { StrapiEnv } from '@/lib/strapi/StrapiEnv';
import { makeError } from '../makeError';

// Function to invoke in order to retrieve data from Strapi.
export const fetchFromStrapi = <A, O, I>(
Expand Down Expand Up @@ -57,7 +58,3 @@ export const fetchFromStrapi = <A, O, I>(
)()
)
);

const makeError = ({ status, statusText }: Response) => {
return new Error(`${status} - ${statusText}`);
};

0 comments on commit 980fe99

Please sign in to comment.