Skip to content

Performance fix#17

Open
adham667 wants to merge 7 commits intomasterfrom
performance-fix
Open

Performance fix#17
adham667 wants to merge 7 commits intomasterfrom
performance-fix

Conversation

@adham667
Copy link
Copy Markdown
Member

@adham667 adham667 commented Oct 27, 2025

Key Changes Summary

Authentication & Session Management

  • Use puppeteer-real-browser for interactive login and capture session data (cookies, localStorage, sessionStorage).
  • Restore saved session into regular puppeteer instances so we can spawn multiple pages/tabs for concurrent fetching.
  • Persist sessions to disk for reliability across restarts.
  • Note: this improves throughput and reliability, but we'll keep monitoring for edge cases.

Caching & Performance

  • Added file-based caching of submissions to avoid re-fetching on subsequent runs
  • Implemented retry logic for failed submission fetches

UI/UX Improvements

  • Added validation for matching percentage threshold input
  • Implemented Excel export functionality for results
  • Updated links with target="_blank" for better user experience

Code Quality & Edge Cases

  • Added edge case handling for empty string comparisons in code comparison
  • Commented out request timeout for better debugging

Dependencies & Configuration

  • Added dependencies for Excel generation and advanced Puppeteer capabilities
  • Updated axios and react-scripts to newer versions
  • Updated default API port from 3000 to 3030
  • Updated ESLint auto-fix setting to explicit mode in VS Code configuration
  • Expanded environment configuration with updated port and default values

Documentation

  • Expanded README from minimal to comprehensive setup and troubleshooting guide

File-by-File Changes

src/schedule-jobs.ts

  • Added typed error handling with failure callback support and result filtering

src/index.ts

  • Added EventEmitter configuration
  • Added Excel export capability
  • Commented out request timeout

src/compare-code/compare-code.ts

  • Added edge case handling for empty string comparisons

src/cheating-detector.ts

  • Major refactor: Switched to persistent browser sessions
  • Added file-based submission caching
  • Implemented retry logic and session management

package.json

  • Added dependencies for Excel generation and advanced Puppeteer capabilities

client/src/InputForm.js

  • Added validation for matching percentage threshold input

client/src/CheatersTable.js

  • Added target="_blank" to links for better UX

client/src/App.js

  • Updated default API port from 3000 to 3030

client/package.json

  • Updated axios and react-scripts to newer versions

README.md

  • Expanded from minimal to comprehensive setup and troubleshooting guide

.vscode/settings.json

  • Updated ESLint auto-fix setting to explicit mode

.env.example

  • Updated port and added default values for configuration parameters

@bolesawny7 bolesawny7 requested a review from Copilot October 28, 2025 12:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a comprehensive performance optimization and robustness improvement for the Codeforces cheating detector, transitioning from cookie-based authentication to a persistent browser session approach with file-based submission caching.

Key changes:

  • Replaced cookie-based authentication with puppeteer-real-browser for better session management and anti-bot detection bypass
  • Implemented file-based caching of submissions to avoid re-fetching on subsequent runs
  • Added error handling and retry logic for failed submission fetches
  • Enhanced the UI with validation, Excel export functionality, and improved accessibility

Reviewed Changes

Copilot reviewed 12 out of 15 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
src/schedule-jobs.ts Added typed error handling with failure callback support and result filtering
src/index.ts Added EventEmitter configuration, Excel export capability, and commented out request timeout
src/compare-code/compare-code.ts Added edge case handling for empty string comparisons
src/cheating-detector.ts Major refactor: switched to persistent browser sessions, added file-based submission caching, implemented retry logic, and session management
package.json Added dependencies for Excel generation and advanced Puppeteer capabilities
client/src/InputForm.js Added validation for matching percentage threshold input
client/src/CheatersTable.js Added target="_blank" to links for better UX
client/src/App.js Updated default API port from 3000 to 3030
client/package.json Updated axios and react-scripts to newer versions
README.md Expanded from minimal to comprehensive setup and troubleshooting guide
.vscode/settings.json Updated ESLint auto-fix setting to explicit mode
.env.example Updated port and added default values for configuration parameters

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/schedule-jobs.ts
Comment on lines +25 to +26
// Ensure result is of type T
return { status: 'fulfilled', value: result as T };
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type assertion as T is unnecessary since result is already inferred as type T from the job's return type. The comment on line 25 also mentions ensuring the type, but this is redundant. Remove both the comment and the type assertion.

Suggested change
// Ensure result is of type T
return { status: 'fulfilled', value: result as T };
return { status: 'fulfilled', value: result };

Copilot uses AI. Check for mistakes.
Comment thread src/schedule-jobs.ts

batchResults.forEach((result, idx) => {
if (result.status === 'fulfilled' && result.value !== undefined) {
results.push(result.value); // Make sure value is not undefined
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment 'Make sure value is not undefined' describes what the code is doing rather than why. Since the condition already checks for undefined, this comment is redundant and should be removed.

Suggested change
results.push(result.value); // Make sure value is not undefined
results.push(result.value);

Copilot uses AI. Check for mistakes.
Comment thread src/index.ts

app.post('/api/cheating-detection', async (req, res) => {
req.setTimeout(1000 * 60 * 5); // 5 Minutes
// req.setTimeout(1000 * 60 * 5); // 5 Minutes
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commented-out timeout configuration should either be removed entirely or replaced with a proper solution. Commented code creates confusion about whether this was intentionally disabled or forgotten.

Suggested change
// req.setTimeout(1000 * 60 * 5); // 5 Minutes
// Set a timeout for this request (default: 5 minutes, configurable via CHEATING_DETECTION_TIMEOUT_MS)
req.setTimeout(Number(process.env.CHEATING_DETECTION_TIMEOUT_MS) || 1000 * 60 * 5);

Copilot uses AI. Check for mistakes.
Comment thread src/index.ts
Comment on lines +91 to 93
XLSX.writeFile(workbook, 'possibleCheaters.xlsx');

res.json(result);
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Excel file is being written to disk on the server but never sent to the client. This creates a file in the server's working directory without delivering it to the user who made the request. Either remove this line or implement proper file download by using XLSX.write() with type 'buffer' and sending it in the response.

Suggested change
XLSX.writeFile(workbook, 'possibleCheaters.xlsx');
res.json(result);
const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename="possibleCheaters.xlsx"');
res.send(excelBuffer);

Copilot uses AI. Check for mistakes.
import check from 'string-similarity';

export default function compareCode(a: string, b: string) {
if(a===''&&b===''){
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing spaces around operators. Should be if (a === '' && b === '') { to follow standard JavaScript/TypeScript formatting conventions.

Suggested change
if(a===''&&b===''){
if (a === '' && b === '') {

Copilot uses AI. Check for mistakes.
Comment thread src/cheating-detector.ts
)
.map(submission => ({
id: submission.id,
id: submission.id.toString(),
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace should be removed from this line.

Suggested change
id: submission.id.toString(),
id: submission.id.toString(),

Copilot uses AI. Check for mistakes.
Comment thread client/src/InputForm.js
matchingPercentageThreshold,
setMatchingPercentageThreshold,
] = useState('');
const[error, setError] = useState('');
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after const. Should be const [error, setError] = useState(''); to follow standard JavaScript formatting.

Suggested change
const[error, setError] = useState('');
const [error, setError] = useState('');

Copilot uses AI. Check for mistakes.
Comment on lines +24 to +25
<td><a href={cheatingCase.first.url} target="_blank">Code</a></td>
<td><a href={cheatingCase.second.url} target="_blank">Code</a></td>
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Links with target=\"_blank\" should include rel=\"noopener noreferrer\" to prevent security vulnerabilities where the opened page can access the window.opener object.

Suggested change
<td><a href={cheatingCase.first.url} target="_blank">Code</a></td>
<td><a href={cheatingCase.second.url} target="_blank">Code</a></td>
<td><a href={cheatingCase.first.url} target="_blank" rel="noopener noreferrer">Code</a></td>
<td><a href={cheatingCase.second.url} target="_blank" rel="noopener noreferrer">Code</a></td>

Copilot uses AI. Check for mistakes.
Comment thread README.md
### Starting Fresh on a New Contest
**Important**: If this is not your first time running the detector and you're analyzing a new contest:

1. Navigate to the `submissions.json` file in the cd-cheating-detector directoy
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'directoy' to 'directory'.

Suggested change
1. Navigate to the `submissions.json` file in the cd-cheating-detector directoy
1. Navigate to the `submissions.json` file in the cd-cheating-detector directory

Copilot uses AI. Check for mistakes.
Comment thread README.md
### Starting Fresh on a New Contest
**Important**: If this is not your first time running the detector and you're analyzing a new contest:

1. Navigate to the `submissions.json` file in the cd-cheating-detector directoy
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repository name 'cd-cheating-detector' appears to be a typo. Based on the clone command on line 21, it should be 'cf-cheating-detector'.

Suggested change
1. Navigate to the `submissions.json` file in the cd-cheating-detector directoy
1. Navigate to the `submissions.json` file in the cf-cheating-detector directory

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants