Skip to content

Commit

Permalink
lambda classifier url pulled from config and used (#184)
Browse files Browse the repository at this point in the history
* lambda classifier url pulled from config and used

* add accesstoken to trainmentor req

* hits classifier lambda endpoint
  • Loading branch information
aaronshiel authored May 1, 2022
1 parent 04fe4f3 commit 98983ae
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 14 deletions.
37 changes: 27 additions & 10 deletions client/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ function getDataFromAxiosResponse(res: AxiosResponse, path: string | string[]) {
export async function fetchFollowUpQuestions(
categoryId: string,
mentorId: string,
accessToken: string
accessToken: string,
classifierLambdaEndpoint?: string
): Promise<FollowUpQuestion[]> {
return execHttp<FollowUpQuestion[]>(
"POST",
urljoin(
CLASSIFIER_ENTRYPOINT,
classifierLambdaEndpoint || CLASSIFIER_ENTRYPOINT,
"followups",
"category",
categoryId,
Expand All @@ -236,6 +237,7 @@ export async function fetchConfig(): Promise<Config> {
googleClientId
urlVideoIdleTips
videoRecorderMaxLength
classifierLambdaEndpoint
}
}
`,
Expand Down Expand Up @@ -998,18 +1000,33 @@ export async function deleteImportTask(
);
}

export async function trainMentor(mentorId: string): Promise<AsyncJob> {
return execHttp("POST", urljoin(CLASSIFIER_ENTRYPOINT, "train"), {
axiosConfig: {
data: { mentor: mentorId },
},
});
export async function trainMentor(
mentorId: string,
accessToken: string,
classifierLambdaEndpoint?: string
): Promise<AsyncJob> {
return execHttp(
"POST",
urljoin(classifierLambdaEndpoint || CLASSIFIER_ENTRYPOINT, "train"),
{
axiosConfig: {
data: { mentor: mentorId },
},
accessToken,
}
);
}

export async function fetchTrainingStatus(
statusUrl: string
statusUrl: string,
accessToken?: string,
classifierLambdaEndpoint?: string
): Promise<TaskStatus<TrainingInfo>> {
return execHttp("GET", `${statusUrl}?v=${Math.random()}`);
return execHttp(
"GET",
`${classifierLambdaEndpoint || ""}${statusUrl}?v=${Math.random()}`,
{ accessToken }
);
}

export async function uploadThumbnail(
Expand Down
9 changes: 8 additions & 1 deletion client/src/hooks/graphql/use-with-followups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FollowupsReducer,
FollowupsActionType,
} from "./followups-reducer";
import { useWithConfig } from "store/slices/config/useWithConfig";

export interface UseWithFollowups {
mentorId?: string;
Expand All @@ -46,6 +47,7 @@ export function useWithFollowups(props: {
const [toRecordFollowUpQs, setToRecordFollowUpQs] = useState<string[]>([]);
const { state: loginState } = useWithLogin();
const { getData, loadMentor } = useActiveMentor();
const { state: configState } = useWithConfig();
const { categoryId, subjectId } = props;
const mentorId = getData((state) => state.data?._id);
const curSubject: Subject = getData((state) =>
Expand All @@ -58,7 +60,12 @@ export function useWithFollowups(props: {
return;
}
dispatch({ type: FollowupsActionType.GENERATING_FOLLOWUPS });
fetchFollowUpQuestions(categoryId, mentorId, loginState.accessToken)
fetchFollowUpQuestions(
categoryId,
mentorId,
loginState.accessToken,
configState?.config?.classifierLambdaEndpoint
)
.then((data) => {
const followUps = data
? data.map((d) => {
Expand Down
3 changes: 3 additions & 0 deletions client/src/hooks/graphql/use-with-setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface UseWithSetup {
setupStep: number;
setupSteps: SetupStep[];
idleTipsVideoUrl: string;
classifierLambdaEndpoint: string;
mentor?: Mentor;
isEdited: boolean;
isLoading: boolean;
Expand Down Expand Up @@ -255,6 +256,8 @@ export function useWithSetup(search?: { i?: string }): UseWithSetup {
setupStep: idx,
setupSteps: steps,
idleTipsVideoUrl: configState.config?.urlVideoIdleTips || "",
classifierLambdaEndpoint:
configState.config?.classifierLambdaEndpoint || "",
mentor: editedMentor,
isEdited: isMentorEdited,
isLoading: isMentorLoading,
Expand Down
16 changes: 14 additions & 2 deletions client/src/hooks/task/use-with-train.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ The full terms of this copyright and license should always be found in the root
*/
import { fetchTrainingStatus, trainMentor } from "api";
import { useEffect } from "react";
import { useWithConfig } from "store/slices/config/useWithConfig";
import { useWithLogin } from "store/slices/login/useWithLogin";
import useActiveMentor from "store/slices/mentor/useActiveMentor";
import { JobState, TrainingInfo } from "types";
import { Task, useWithTask } from "./use-with-task";

export function useWithTraining(
pollingInterval = 1000
): Task<TrainingInfo, string> {
const { state: configState } = useWithConfig();
const { state: loginState } = useWithLogin();
const { status, statusUrl, error, isPolling, startTask, clearError } =
useWithTask<TrainingInfo, string>(train, poll, pollingInterval);
const { loadMentor } = useActiveMentor();
Expand All @@ -24,11 +28,19 @@ export function useWithTraining(
}, [status]);

function train(mentorId: string) {
return trainMentor(mentorId);
return trainMentor(
mentorId,
loginState.accessToken || "",
configState?.config?.classifierLambdaEndpoint
);
}

function poll(statusUrl: string) {
return fetchTrainingStatus(statusUrl);
return fetchTrainingStatus(
statusUrl,
loginState.accessToken,
configState?.config?.classifierLambdaEndpoint
);
}

return {
Expand Down
1 change: 1 addition & 0 deletions client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Config {
googleClientId: string;
urlVideoIdleTips: string;
videoRecorderMaxLength: number;
classifierLambdaEndpoint: string;
}

export interface Connection<T> {
Expand Down
2 changes: 1 addition & 1 deletion cypress/cypress/integration/home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ describe("My Mentor Page", () => {
cy.location("pathname").then(($el) => {
assert($el.replace("/admin", ""), "/setup");
});
cy.location("search").should("contain", "?i=7");
cy.contains("Repeat After Me questions");
});

it("can create a mentor question and save it", () => {
Expand Down
1 change: 1 addition & 0 deletions cypress/cypress/integration/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ describe("Setup", () => {
subject: repeatAfterMe,
gqlQueries: [
mockGQL("UpdateAnswer", { me: { updateAnswer: true } }),
mockGQL("ImportTask", { importTask: null }),
mockGQL("FetchUploadTasks", [
{
me: {
Expand Down
2 changes: 2 additions & 0 deletions cypress/cypress/support/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ export interface Config {
googleClientId: string;
urlVideoIdleTips: string;
videoRecorderMaxLength: number;
classifierLambdaEndpoint: string;
}

export const CONFIG_DEFAULT: Config = {
googleClientId: "fake-google-client-id",
urlVideoIdleTips: "",
videoRecorderMaxLength: 300,
classifierLambdaEndpoint: "",
};

export function mockGQLConfig(config: Partial<Config>): MockGraphQLQuery {
Expand Down

0 comments on commit 98983ae

Please sign in to comment.