Skip to content

Commit

Permalink
sort answers by accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
DilanRamirez committed Oct 19, 2021
1 parent 1c5363e commit 300eb98
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 52 deletions.
7 changes: 3 additions & 4 deletions client/src/components/chat/chat-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export function ChatItem(props: {
children: React.ReactNode;
node: { url: string };
}) {
const linkAnswer =
props.href.length > 30 ? props.href.slice(0, 30) : props.href;
// const linkAnswer =
// props.href.length > 30 ? props.href.slice(0, 30) : props.href;
const chatLink = hrefToChatLink(props?.node?.url || "", message);
return chatLink.type === LINK_TYPE_ASK ? (
<a
Expand All @@ -121,7 +121,7 @@ export function ChatItem(props: {
</a>
) : (
<a href={props.href} target="_blank" rel="noreferrer">
{linkAnswer}
{props.children}
</a>
);
}
Expand Down Expand Up @@ -210,7 +210,6 @@ export function ChatItem(props: {
{message.name}
</p>
);

return (
<div>
{!isUser && isVisible ? mentorBubbleName : null}
Expand Down
34 changes: 33 additions & 1 deletion client/src/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function Chat(args: {
setVisibilityShowAllPref,
mentorNameForChatMsg,
rePlayQuestionVideo,
mentorNameById,
} = useWithChatData();

const colorByMentorId = useSelector<State, Record<string, string>>((s) => {
Expand All @@ -93,6 +94,7 @@ export function Chat(args: {
});

const chatData = useSelector<State, ChatData>((s) => s.chat);
const questionSent = useSelector<State, boolean>((s) => s.chat.questionSent);
useEffect(() => {
animateScroll.scrollToBottom({
containerId: "chat-thread",
Expand Down Expand Up @@ -149,6 +151,37 @@ export function Chat(args: {
setVisibilityShowAllPref(visibilityShowAllPref);
}, [visibilityShowAllPref]);

const totalMentors = Object.keys(mentorNameById).length;
if (
totalMentors > 1 &&
chatData.messages.length >= 2 + totalMentors &&
!questionSent &&
mentorType !== "CHAT"
) {
// get last mentors answers to sort them
const lastAnswers = chatData.messages.slice(
-Object.keys(mentorNameById).length
);
lastAnswers.map((a) => console.log(a));

const elemToDelete = Object.keys(mentorNameById).length;

// remove answers to then append the sorted ones
chatData.messages.length > Object.keys(mentorNameById).length
? chatData.messages.splice(
chatData.messages.length - elemToDelete,
chatData.messages.length
)
: chatData.messages;

// sort last mentors answers
const answersSorted = lastAnswers.sort((a, b) =>
String(b.confidence).localeCompare(String(a.confidence))
);
// concat sorted answers to the previous ones
chatData.messages = chatData.messages.concat(answersSorted);
}

return (
<div
data-cy="history-chat"
Expand All @@ -165,7 +198,6 @@ export function Chat(args: {
className={[styles.list, "chat-thread"].join(" ")}
style={{
width: shouldDisplayPortrait() ? "100%" : width ? width : "40vw",
// height: shouldDisplayPortrait() || windowHeight ? height : "300px",
}}
disablePadding={true}
id="chat-thread"
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/chat/use-chat-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface UseWithChatData {
mentorType: string;
lastQuestionId: string;
visibilityShowAllPref: boolean;
mentorNameById: Record<string, string>;
getQuestionVisibilityPref: (questionId: string) => ItemVisibilityPrefs;
setQuestionVisibilityPref: (questionId: string, show: boolean) => void;
setVisibilityShowAllPref: (show: boolean) => void;
Expand Down Expand Up @@ -130,5 +131,6 @@ export function useWithChatData(): UseWithChatData {
setVisibilityShowAllPref,
mentorNameForChatMsg,
rePlayQuestionVideo,
mentorNameById,
};
}
1 change: 0 additions & 1 deletion client/src/components/video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ function Video(args: { playing?: boolean }): JSX.Element {
function onPlay() {
setHideLinkLabel(false);
dispatch(onMentorDisplayAnswer(false, curMentor));

if (isIdle) {
setHideLinkLabel(true);
dispatch(answerFinished());
Expand Down
2 changes: 1 addition & 1 deletion client/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ export const sendQuestion =
);
};

const NEXT_MENTOR_DELAY = 3000;
const NEXT_MENTOR_DELAY = 1000;
let timer: NodeJS.Timer | null;
export const answerFinished =
() =>
Expand Down
6 changes: 5 additions & 1 deletion client/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const initialState: State = {
chat: {
messages: [],
replay: false,
questionSent: false,
},
config: {
cmi5Enabled: false,
Expand Down Expand Up @@ -329,6 +330,7 @@ function onQuestionSent(state: State, action: QuestionSentAction): State {
isFeedbackSendInProgress: false,
},
],
questionSent: true,
},
curQuestion: action.payload.question,
curQuestionSource: action.payload.source,
Expand Down Expand Up @@ -499,7 +501,7 @@ function onQuestionAnswered(
isIntro: false,
isUser: false,
questionId: action.payload.questionId,
text: action.payload.answerText,
text: action.payload.answerText.trim(),
feedback: Feedback.NONE,
feedbackId: action.payload.answerFeedbackId,
isFeedbackSendInProgress: false,
Expand All @@ -512,8 +514,10 @@ function onQuestionAnswered(
answerMedia: mentor.answer_media,
answerId: mentor.answer_id,
replay: false,
confidence: mentor.confidence,
},
],
questionSent: false,
},

isIdle: false,
Expand Down
2 changes: 2 additions & 0 deletions client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ChatData {
lastAnswerAt?: Date;
messages: ChatMsg[];
replay: boolean;
questionSent: boolean;
}

export const LINK_TYPE_ASK = "ask";
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface ChatMsg {
answerId?: string;
replay?: boolean;
isVideoInProgress?: boolean;
confidence?: number;
}

export interface MentorClientData {
Expand Down
2 changes: 1 addition & 1 deletion cypress/cypress/fixtures/response_with_feedback.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "http://videos.org/answer_id10.mp4"
}
],
"confidence": -0.5140574318328217,
"confidence": 1,
"feedback_id": "feedback_id",
"classifier": ""
}
2 changes: 1 addition & 1 deletion cypress/cypress/fixtures/response_with_feedback2.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "http://videos.org/answer_id2.mp4"
}
],
"confidence": -0.5140574318328217,
"confidence": 0.1140574318328217,
"feedback_id": "feedback_id2",
"classifier": ""
}
3 changes: 2 additions & 1 deletion cypress/cypress/fixtures/response_with_feedback3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"query": "how old are you",
"answer_id": "answer_id_feedback11",
"answer_text": "Give me feedback.",

"answer_media": [
{
"type": "video",
Expand All @@ -14,7 +15,7 @@
"url": "http://videos.org/answer_id11.mp4"
}
],
"confidence": -0.5140574318328217,
"confidence": -0.9,
"feedback_id": "feedback_id",
"classifier": ""
}
2 changes: 1 addition & 1 deletion cypress/cypress/integration/chat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("Chat", () => {
cy.get("[data-cy=chat-thread]").should("exist");
cy.get("[data-cy=input-field]").type("test");
cy.get("[data-cy=input-send]").trigger("mouseover").click();
cy.get("[data-cy=chat-msg-2]").contains("Click https://www.google.com");
cy.get("[data-cy=chat-msg-2]").contains("Click here");
cy.get("[data-cy=chat-msg-2] a").should(
"have.attr",
"href",
Expand Down
53 changes: 26 additions & 27 deletions cypress/cypress/integration/history-links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Chat History (Video Mentors Links)", () => {
cy.get("[data-cy=input-field]").type("Question 1");
cy.get("[data-cy=input-send]").trigger("mouseover").click();
cy.get("[data-cy=history-chat]").should("exist");
cy.get("[data-cy=chat-msg-2]").contains("Click https://www.google.com");
cy.get("[data-cy=chat-msg-2]").contains("Click here");
cy.get("[data-cy=chat-msg-2] a").should(
"have.attr",
"href",
Expand Down Expand Up @@ -100,7 +100,6 @@ describe("Chat History (Video Mentors Links)", () => {
cy.intercept("**/questions/?mentor=carlos&query=*", {
fixture: "response_with_feedback.json",
});

cy.get("[data-cy=video-container]").within(() => {
cy.get("[data-cy=answer-link-card]").should("not.exist");
});
Expand All @@ -111,9 +110,16 @@ describe("Chat History (Video Mentors Links)", () => {
cy.get("[data-cy=input-field]").type("Question 1");
cy.get("[data-cy=input-send]").trigger("mouseover").click();

// wait for it to finish
cy.get("[data-cy=video-container]", { timeout: 30000 }).should(
"have.attr",
"data-test-replay",
"http://videos.org/answer_id.mp4"
);

cy.get("[data-cy=history-chat").within(($hc) => {
cy.get("[data-cy=chat-thread]").within(($hc) => {
cy.get("[data-cy=chat-msg-2]").contains("Click https://www.google.com");
cy.get("[data-cy=chat-msg-3]").contains("Click here");
});
});
// Compare last answer link with video label
Expand Down Expand Up @@ -182,40 +188,33 @@ describe("Chat History (Video Mentors Links)", () => {
);
cy.get("[data-cy=input-send]").trigger("mouseover").click();

// play video
cy.get("video")
.should("have.prop", "paused", true)
.and("have.prop", "ended", false)
.then(($video) => {
$video[$video.length - 1].play();
});
// wait for it to finish
cy.get("[data-cy=video-container]", { timeout: 30000 }).should(
"have.attr",
"data-test-replay",
"http://videos.org/answer_id.mp4"
);

cy.get("[data-cy=history-chat]").within(($hc) => {
cy.get("[data-cy=chat-thread]").within(($hc) => {
cy.get("[data-cy=chat-msg-2]")
.scrollIntoView()
.within(() => {
cy.get("[data-cy=ask-link-0]")
.should("exist")
.trigger("mouseover")
.click();
});
cy.get("[data-cy=chat-msg-3]").within(() => {
cy.get("[data-cy=ask-link-0]")
.should((el) => {
expect(Cypress.dom.isAttached(el), "is attached").to.eq(true);
})
.should("exist")
.click({ force: true });
});
cy.get("[data-cy=chat-msg-4]").contains(
"what does a computer programmer do?"
);
});
});
cy.get("[data-cy=visibility-switch]").find("input").check();
// wait for it to finish
cy.get("[data-cy=video-container]", { timeout: 30000 }).should(
"have.attr",
"data-test-replay",
"http://videos.org/answer_id.mp4"
);

cy.get("[data-cy=chat-msg-2]")
cy.get("[data-cy=chat-msg-3]")
.scrollIntoView()
.within(() => {
cy.get("[data-cy=ask-icon-2]");
cy.get("[data-cy=ask-icon-3]");
});
});

Expand Down
Loading

0 comments on commit 300eb98

Please sign in to comment.