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

solomon/history-service #55

Merged
merged 20 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
26 changes: 21 additions & 5 deletions .github/workflows/test.yml
solomonng2001 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ jobs:
QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }}
USER_SERVICE_URL: ${{ vars.USER_SERVICE_URL }}
MATCHING_SERVICE_URL: ${{ vars.MATCHING_SERVICE_URL }}
HISTORY_SERVICE_URL: ${{ vars.HISTORY_SERVICE_URL }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
QUESTION_FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
HISTORY_FIREBASE_CREDENTIAL_PATH: ${{ vars.HISTORY_SERVICE_FIREBASE_CREDENTIAL_PATH }}
DB_CLOUD_URI: ${{ secrets.USER_SERVICE_DB_CLOUD_URI }}
USER_SERVICE_PORT: ${{ vars.USER_SERVICE_PORT }}
MATCHING_SERVICE_PORT: ${{ vars.MATCHING_SERVICE_PORT }}
HISTORY_SERVICE_PORT: ${{ vars.HISTORY_SERVICE_PORT }}
MATCHING_SERVICE_TIMEOUT: ${{ vars.MATCHING_SERVICE_TIMEOUT }}
REDIS_URL: ${{ vars.REDIS_URL }}
QUESTION_SERVICE_GRPC_URL: ${{ vars.QUESTION_SERVICE_GPRC_URL }}
Expand All @@ -42,9 +45,10 @@ jobs:
echo "NEXT_PUBLIC_QUESTION_SERVICE_URL=$QUESTION_SERVICE_URL" >> .env
echo "NEXT_PUBLIC_USER_SERVICE_URL=$USER_SERVICE_URL" >> .env
echo "NEXT_PUBLIC_MATCHING_SERVICE_URL=$MATCHING_SERVICE_URL" >> .env
echo "NEXT_PUBLIC_HISTORY_SERVICE_URL=$HISTORY_SERVICE_URL" >> .env

cd ../question-service
echo "FIREBASE_CREDENTIAL_PATH=$FIREBASE_CREDENTIAL_PATH" >> .env
echo "FIREBASE_CREDENTIAL_PATH=$QUESTION_FIREBASE_CREDENTIAL_PATH" >> .env
echo "JWT_SECRET=$JWT_SECRET" >> .env

cd ../user-service
Expand All @@ -59,13 +63,22 @@ jobs:
echo "REDIS_URL=$REDIS_URL" >> .env
echo "QUESTION_SERVICE_GRPC_URL=$QUESTION_SERVICE_GRPC_URL" >> .env

cd ../history-service
echo "FIREBASE_CREDENTIAL_PATH=$HISTORY_FIREBASE_CREDENTIAL_PATH" >> .env
echo "PORT=$HISTORY_SERVICE_PORT" >> .env

- name: Create Database Credential Files
env:
FIREBASE_JSON: ${{ secrets.QUESTION_SERVICE_FIREBASE_CREDENTIAL }}
FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
QUESTION_FIREBASE_JSON: ${{ secrets.QUESTION_SERVICE_FIREBASE_CREDENTIAL }}
QUESTION_FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }}
HISTORY_FIREBASE_JSON: ${{ secrets.HISTORY_SERVICE_FIREBASE_CREDENTIAL }}
HISTORY_FIREBASE_CREDENTIAL_PATH: ${{ vars.HISTORY_SERVICE_FIREBASE_CREDENTIAL_PATH }}
run: |
cd ./apps/question-service
echo "$FIREBASE_JSON" > "./$FIREBASE_CREDENTIAL_PATH"
echo "$QUESTION_FIREBASE_JSON" > "./$QUESTION_FIREBASE_CREDENTIAL_PATH"

cd ../history-service
echo "$HISTORY_FIREBASE_JSON" > "./$HISTORY_FIREBASE_CREDENTIAL_PATH"

- name: Build and Run Services
run: |
Expand All @@ -87,13 +100,16 @@ jobs:
USER_SERVICE_URL: ${{ vars.USER_SERVICE_URL }}
QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }}
MATCHING_SERVICE_URL: ${{ vars.MATCHING_SERVICE_URL }}
HISTORY_SERVICE_URL: ${{ vars.HISTORY_SERVICE_URL }}
run: |
echo "Testing Question Service..."
curl -sSL -o /dev/null $QUESTION_SERVICE_URL && echo "Question Service is up"
echo "Testing User Service..."
curl -fsSL -o /dev/null $USER_SERVICE_URL && echo "User Service is up"
echo "Testing Frontend..."
curl -fsSL -o /dev/null $FRONTEND_URL && echo "Frontend is up"
echo "Testing History Service..."
curl -fsSL -o /dev/null $HISTORY_SERVICE_URL && echo "History Service is up"
echo "Testing Matching Service..."
if ! (echo "Hello" | websocat $MATCHING_SERVICE_URL); then
echo "WebSocket for Matching Service is not live"
Expand Down
13 changes: 13 additions & 0 deletions apps/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ services:
depends_on:
- redis

history-service:
build:
context: ./history-service
dockerfile: Dockerfile
ports:
- 8082:8082
env_file:
- ./history-service/.env
networks:
- apps_network
volumes:
- ./history-service:/history-service

redis:
image: redis:latest
networks:
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080/"
NEXT_PUBLIC_USER_SERVICE_URL="http://localhost:3001/"
NEXT_PUBLIC_MATCHING_SERVICE_URL="ws://localhost:8081/match"
NEXT_PUBLIC_SIGNALLING_SERVICE_URL="ws://localhost:4444/"
NEXT_PUBLIC_SIGNALLING_SERVICE_URL="ws://localhost:4444/"
NEXT_PUBLIC_HISTORY_SERVICE_URL="http://localhost:8082/"
79 changes: 73 additions & 6 deletions apps/frontend/src/app/collaboration/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Col,
Input,
Layout,
message,
Row,
Select,
Tabs,
Expand All @@ -27,6 +28,8 @@ import {
} from "@ant-design/icons";
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions";
import CollaborativeEditor from "@/components/CollaborativeEditor/CollaborativeEditor";
import { CreateHistory, UpdateHistory } from "@/app/services/history";
import { Language } from "@codemirror/language";

interface CollaborationProps {}

Expand All @@ -36,20 +39,26 @@ export default function CollaborationPage(props: CollaborationProps) {
const [isLoading, setIsLoading] = useState<boolean>(false);

// Code Editor States
const [historyDocRefId, setHistoryDocRefId] = useState<string | undefined>(
undefined
);
const [code, setCode] = useState<string>("");
const [questionTitle, setQuestionTitle] = useState<string | undefined>(
undefined
);
const [questionDocRefId, setQuestionDocRefId] = useState<string | undefined>(undefined);
const [complexity, setComplexity] = useState<string | undefined>(undefined);
const [categories, setCategories] = useState<string[]>([]); // Store the selected filter categories
const [description, setDescription] = useState<string | undefined>(undefined);
const [selectedLanguage, setSelectedLanguage] = useState("javascript"); // State to hold the selected language item
const [selectedLanguage, setSelectedLanguage] = useState("Javascript"); // State to hold the selected language item

// Session states
const [collaborationId, setCollaborationId] = useState<string | undefined>(
undefined
);
const [currentUser, setCurrentUser] = useState<string | undefined>(undefined);
const [matchedUser, setMatchedUser] = useState<string | undefined>(undefined);
const [matchedTopics, setMatchedTopics] = useState<string[] | undefined>(undefined);

// Chat states
const [messageToSend, setMessageToSend] = useState<string | undefined>(
Expand All @@ -61,6 +70,54 @@ export default function CollaborationPage(props: CollaborationProps) {
undefined
);

// Message
const [messageApi, contextHolder] = message.useMessage();

const successMessage = (message: string) => {
messageApi.open({
type: "success",
content: message,
});
};

const handleSubmitCode = async () => {
if (!historyDocRefId) {
const data = await CreateHistory({
title: questionTitle ?? "",
code: code,
language: selectedLanguage,
user: currentUser ?? "",
matchedUser: matchedUser ?? "",
matchId: collaborationId ?? "",
matchedTopics: matchedTopics ?? [],
questionDocRefId: questionDocRefId ?? "",
questionDifficulty: complexity ?? "",
questionTopics: categories,
});
setHistoryDocRefId(data.docRefId);
successMessage("Code submitted successfully!");
return;
}

UpdateHistory({
title: questionTitle ?? "",
code: code,
language: selectedLanguage,
user: currentUser ?? "",
matchedUser: matchedUser ?? "",
matchId: collaborationId ?? "",
matchedTopics: matchedTopics ?? [],
questionDocRefId: questionDocRefId ?? "",
questionDifficulty: complexity ?? "",
questionTopics: categories,
}, historyDocRefId!);
successMessage("Code updated successfully!");
}

const handleCodeChange = (code: string) => {
setCode(code);
}

// Retrieve the docRefId from query params during page navigation
// const searchParams = useSearchParams();

Expand All @@ -71,17 +128,20 @@ export default function CollaborationPage(props: CollaborationProps) {
}

// Retrieve details from localstorage
const docRefId: string = localStorage.getItem("docRefId") ?? "";
const questionDocRefId: string = localStorage.getItem("questionDocRefId") ?? "";
const collabId: string = localStorage.getItem("collabId") ?? "";
const matchedUser: string = localStorage.getItem("matchedUser") ?? "";
const currentUser: string = localStorage.getItem("user") ?? "";
const matchedTopics: string[] = localStorage.getItem("matchedTopics")?.split(",") ?? [];

// Set states from localstorage
setCollaborationId(collabId);
setMatchedUser(matchedUser);
setCurrentUser(currentUser);
setMatchedTopics(matchedTopics);
setQuestionDocRefId(questionDocRefId);

GetSingleQuestion(docRefId).then((data: Question) => {
GetSingleQuestion(questionDocRefId).then((data: Question) => {
setQuestionTitle(`${data.id}. ${data.title}`);
setComplexity(data.complexity);
setCategories(data.categories);
Expand Down Expand Up @@ -121,15 +181,17 @@ export default function CollaborationPage(props: CollaborationProps) {
// Remove localstorage variables for collaboration
localStorage.removeItem("user");
localStorage.removeItem("matchedUser");
localStorage.removeItem("collaId");
localStorage.removeItem("docRefId");
localStorage.removeItem("collabId");
localStorage.removeItem("questionDocRefId");
localStorage.removeItem("matchedTopics");

// Redirect back to matching page
router.push("/matching");
};

return (
<Layout className="collaboration-layout">
{contextHolder}
<Header selectedKey={undefined} />
<Content className="collaboration-content">
<Row gutter={0} className="collab-row">
Expand Down Expand Up @@ -189,7 +251,11 @@ export default function CollaborationPage(props: CollaborationProps) {
Code
</div>
{/* TODO: Link to execution service for code submission */}
<Button icon={<SendOutlined />} iconPosition="end">
<Button
icon={<SendOutlined />}
iconPosition="end"
onClick={() => handleSubmitCode()}
>
Submit
</Button>
</div>
Expand All @@ -207,6 +273,7 @@ export default function CollaborationPage(props: CollaborationProps) {
user={currentUser}
collaborationId={collaborationId}
language={selectedLanguage}
onCodeChange={handleCodeChange}
/>
)}
</div>
Expand Down
15 changes: 8 additions & 7 deletions apps/frontend/src/app/matching/MatchingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ const MatchingModal: React.FC<MatchingModalProps> = ({
cancel={() => {
setClosedType("cancelled");
}}
name1={matchingState.info?.myName || ""}
name2={matchingState.info?.partnerName || ""}
name1={matchingState.info?.user || ""}
name2={matchingState.info?.matchedUser || ""}
/>
);
}
Expand Down Expand Up @@ -97,19 +97,20 @@ const MatchingModal: React.FC<MatchingModalProps> = ({
join={() => {
matchingState.ok();
setClosedType("joined");
localStorage.setItem("user", matchingState.info.myName);
localStorage.setItem("user", matchingState.info.user);
localStorage.setItem(
"matchedUser",
matchingState.info.partnerName
matchingState.info.matchedUser
);
localStorage.setItem("collabId", matchingState.info.matchId);
localStorage.setItem("docRefId", matchingState.info.docRefId);
localStorage.setItem("questionDocRefId", matchingState.info.questionDocRefId);
localStorage.setItem("matchedTopics", matchingState.info.matchedTopics.join(","));

// Redirect to collaboration page
router.push(`/collaboration/${matchingState.info.matchId}`);
}}
name1={matchingState.info.myName}
name2={matchingState.info.partnerName}
name1={matchingState.info.user}
name2={matchingState.info.matchedUser}
/>
);
case "timeout":
Expand Down
61 changes: 61 additions & 0 deletions apps/frontend/src/app/services/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const HISTORY_SERVICE_URL = process.env.NEXT_PUBLIC_HISTORY_SERVICE_URL;

export interface History {
title: string;
code: string;
language: string;
user: string;
matchedUser: string;
matchId: string;
matchedTopics: string[];
questionDocRefId: string;
questionDifficulty: string;
questionTopics: string[];
createdAt?: string;
updatedAt?: string;
docRefId?: string;
}

export const CreateHistory = async (
history: History
): Promise<History> => {
const response = await fetch(`${HISTORY_SERVICE_URL}histories`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(history),
});

if (response.status === 200) {
return response.json();
} else {
throw new Error(
`Error creating history: ${response.status} ${response.statusText}`
);
}
};

export const UpdateHistory = async (
history: History,
historyDocRefId: string
): Promise<History> => {
const response = await fetch(
`${HISTORY_SERVICE_URL}histories/${historyDocRefId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(history),
}
);

if (response.status === 200) {
return response.json();
} else {
throw new Error(
`Error updating history: ${response.status} ${response.statusText}`
);
}
}
7 changes: 4 additions & 3 deletions apps/frontend/src/app/services/use-matching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ export default function useMatching(): MatchState {
function parseInfoFromResponse(responseJson: MatchFoundResponse): MatchInfo {
return {
matchId: responseJson.match_id?.toString() ?? "unknown",
partnerName: responseJson.matched_user ?? "unknown",
myName: responseJson.user ?? "unknown",
docRefId: responseJson.question_doc_ref_id ?? "unknown",
matchedUser: responseJson.matched_user ?? "unknown",
user: responseJson.user ?? "unknown",
questionDocRefId: responseJson.question_doc_ref_id ?? "unknown",
matchedTopics: responseJson.matched_topics ?? [],
};
}
Loading