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

High volume data generation on top of MongoDB #105

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open

High volume data generation on top of MongoDB #105

wants to merge 37 commits into from

Conversation

MattG57
Copy link
Collaborator

@MattG57 MattG57 commented Jan 11, 2025

The calendarClockServiceTests.ts file simulates a calendar-clock function that runs various data generation tasks on an hourly basis. It connects to a MongoDB database and performs the following tasks:

Survey Generation: Randomly generates surveys 20% of the time during weekdays (Monday to Friday) between 6 AM and 11 PM.
Seats Generation: Generates seat data for each member of the team every hour.
Metrics Generation: Generates metrics data daily at 11 PM.
The script loops through each hour within a specified date range, incrementing the datetime parameter each cycle, and calls the respective data generation functions. It also retrieves all team members from the database to use in the seats generation process.

austenstone and others added 8 commits December 23, 2024 06:34
…erfile to expose port 8080, and add MongoDB service to Docker Compose
…ations, update setup logic, and remove unused models
…pdate method signatures, and enhance test coverage
…Tests.ts run via "npx tsx src/__tests__/services/calendarClockServiceTests.ts"
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

ESLint found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link

github-actions bot commented Jan 11, 2025

Dependency Review

The following issues were found:

  • ❌ 1 vulnerable package(s)
  • ✅ 0 package(s) with incompatible licenses
  • ✅ 0 package(s) with invalid SPDX license definitions
  • ✅ 0 package(s) with unknown licenses.
  • ⚠️ 5 packages with OpenSSF Scorecard issues.

View full job summary

const newData = JSON.parse(JSON.stringify(this.baseData));

newData.surveys = newData.surveys.map((survey: any) => {
survey.userId = this.getRandomUserId();

Check failure

Code scanning / CodeQL

Insecure randomness High test

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix AI 9 days ago

To fix the problem, we need to replace the use of Math.random() with a cryptographically secure random number generator. In Node.js, we can use the crypto module's randomInt function to generate secure random integers. This will ensure that the generated values are not predictable and enhance the security of the application.

We will:

  1. Import the crypto module.
  2. Replace all instances of Math.random() with crypto.randomInt.
Suggested changeset 1
backend/src/__tests__/__mock__/survey-gen/mockSurveyGenerator.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/src/__tests__/__mock__/survey-gen/mockSurveyGenerator.ts b/backend/src/__tests__/__mock__/survey-gen/mockSurveyGenerator.ts
--- a/backend/src/__tests__/__mock__/survey-gen/mockSurveyGenerator.ts
+++ b/backend/src/__tests__/__mock__/survey-gen/mockSurveyGenerator.ts
@@ -6,2 +6,3 @@
 import { SurveyType } from 'models/survey.model.js';
+import { randomInt } from 'crypto';
 
@@ -17,3 +18,3 @@
   private getRandomUserId(): string {
-    return this.config.userIds[Math.floor(Math.random() * this.config.userIds.length)];
+    return this.config.userIds[randomInt(this.config.userIds.length)];
   }
@@ -21,3 +22,3 @@
   private getRandomOrg(): string {
-    return this.config.orgs[Math.floor(Math.random() * this.config.orgs.length)];
+    return this.config.orgs[randomInt(this.config.orgs.length)];
   }
@@ -25,3 +26,3 @@
   private getRandomRepo(): string {
-    return this.config.repos[Math.floor(Math.random() * this.config.repos.length)];
+    return this.config.repos[randomInt(this.config.repos.length)];
   }
@@ -29,3 +30,3 @@
   private getRandomPrNumber(): number {
-    return Math.floor(Math.random() * 100);
+    return randomInt(100);
   }
@@ -33,3 +34,3 @@
   private getRandomPercentTimeSaved(): number {
-    return Math.floor(Math.random() * 100);
+    return randomInt(100);
   }
@@ -37,3 +38,3 @@
   private getRandomReason(): string {
-    return this.config.reasons[Math.floor(Math.random() * this.config.reasons.length)];
+    return this.config.reasons[randomInt(this.config.reasons.length)];
   }
@@ -41,3 +42,3 @@
   private getRandomTimeUsedFor(): string {
-    return this.config.timeUsedFors[Math.floor(Math.random() * this.config.timeUsedFors.length)];
+    return this.config.timeUsedFors[randomInt(this.config.timeUsedFors.length)];
   }
@@ -45,3 +46,3 @@
   private getRandomDate(): Date {
-    return addDays(this.config.startDate, Math.floor(Math.random() * (this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
+    return addDays(this.config.startDate, randomInt((this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
   }
@@ -57,3 +58,3 @@
       survey.prNumber = this.getRandomPrNumber();
-      survey.usedCopilot = Math.random() > 0.5;
+      survey.usedCopilot = randomInt(2) > 0;
       survey.percentTimeSaved = this.getRandomPercentTimeSaved();
EOF
@@ -6,2 +6,3 @@
import { SurveyType } from 'models/survey.model.js';
import { randomInt } from 'crypto';

@@ -17,3 +18,3 @@
private getRandomUserId(): string {
return this.config.userIds[Math.floor(Math.random() * this.config.userIds.length)];
return this.config.userIds[randomInt(this.config.userIds.length)];
}
@@ -21,3 +22,3 @@
private getRandomOrg(): string {
return this.config.orgs[Math.floor(Math.random() * this.config.orgs.length)];
return this.config.orgs[randomInt(this.config.orgs.length)];
}
@@ -25,3 +26,3 @@
private getRandomRepo(): string {
return this.config.repos[Math.floor(Math.random() * this.config.repos.length)];
return this.config.repos[randomInt(this.config.repos.length)];
}
@@ -29,3 +30,3 @@
private getRandomPrNumber(): number {
return Math.floor(Math.random() * 100);
return randomInt(100);
}
@@ -33,3 +34,3 @@
private getRandomPercentTimeSaved(): number {
return Math.floor(Math.random() * 100);
return randomInt(100);
}
@@ -37,3 +38,3 @@
private getRandomReason(): string {
return this.config.reasons[Math.floor(Math.random() * this.config.reasons.length)];
return this.config.reasons[randomInt(this.config.reasons.length)];
}
@@ -41,3 +42,3 @@
private getRandomTimeUsedFor(): string {
return this.config.timeUsedFors[Math.floor(Math.random() * this.config.timeUsedFors.length)];
return this.config.timeUsedFors[randomInt(this.config.timeUsedFors.length)];
}
@@ -45,3 +46,3 @@
private getRandomDate(): Date {
return addDays(this.config.startDate, Math.floor(Math.random() * (this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
return addDays(this.config.startDate, randomInt((this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
}
@@ -57,3 +58,3 @@
survey.prNumber = this.getRandomPrNumber();
survey.usedCopilot = Math.random() > 0.5;
survey.usedCopilot = randomInt(2) > 0;
survey.percentTimeSaved = this.getRandomPercentTimeSaved();
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
const [updated] = await Survey.update(req.body, {
where: { id }
});
const updated = await Survey.findByIdAndUpdate(id, req.body);

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
}],
attributes: ['name', 'org', 'slug', 'description', 'html_url']
const query = req.query.org ? { org: req.query.org as string } : {};
const teams = await Team.find(query)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix AI 11 days ago

To fix the problem, we need to ensure that the user input is interpreted as a literal value and not as a query object. This can be achieved by using the $eq operator in the MongoDB query. This approach ensures that the user input is treated as a literal value, preventing any potential NoSQL injection attacks.

Suggested changeset 1
backend/src/controllers/teams.controller.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/src/controllers/teams.controller.ts b/backend/src/controllers/teams.controller.ts
--- a/backend/src/controllers/teams.controller.ts
+++ b/backend/src/controllers/teams.controller.ts
@@ -10,3 +10,3 @@
     try {
-      const query = req.query.org ? { org: req.query.org as string } : {};
+      const query = req.query.org ? { org: { $eq: req.query.org as string } } : {};
       const teams = await Team.find(query)
EOF
@@ -10,3 +10,3 @@
try {
const query = req.query.org ? { org: req.query.org as string } : {};
const query = req.query.org ? { org: { $eq: req.query.org as string } } : {};
const teams = await Team.find(query)
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
});
return await Survey.findByPk(survey.id);
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix AI about 23 hours ago

To fix the problem, we need to ensure that the user input is properly sanitized before being used in the database query. We can use the $eq operator to ensure that the id is treated as a literal value and not as a query object. This will prevent any potential NoSQL injection attacks.

Suggested changeset 1
backend/src/services/survey.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/src/services/survey.service.ts b/backend/src/services/survey.service.ts
--- a/backend/src/services/survey.service.ts
+++ b/backend/src/services/survey.service.ts
@@ -11,3 +11,3 @@
     const Survey = mongoose.model('Survey');
-    await Survey.updateOne({ id: survey.id }, survey);
+    await Survey.updateOne({ id: { $eq: survey.id } }, survey);
     return await Survey.findOne({ id: survey.id });
EOF
@@ -11,3 +11,3 @@
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);
await Survey.updateOne({ id: { $eq: survey.id } }, survey);
return await Survey.findOne({ id: survey.id });
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
});
return await Survey.findByPk(survey.id);
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix AI about 23 hours ago

To fix the problem, we need to ensure that the user-provided data is properly sanitized before being used in a MongoDB query. We can use the $eq operator to ensure that the user input is interpreted as a literal value and not as a query object. This will prevent NoSQL injection attacks.

Suggested changeset 1
backend/src/services/survey.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/src/services/survey.service.ts b/backend/src/services/survey.service.ts
--- a/backend/src/services/survey.service.ts
+++ b/backend/src/services/survey.service.ts
@@ -11,3 +11,3 @@
     const Survey = mongoose.model('Survey');
-    await Survey.updateOne({ id: survey.id }, survey);
+    await Survey.updateOne({ id: { $eq: survey.id } }, survey);
     return await Survey.findOne({ id: survey.id });
EOF
@@ -11,3 +11,3 @@
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);
await Survey.updateOne({ id: { $eq: survey.id } }, survey);
return await Survey.findOne({ id: survey.id });
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
return await Survey.findByPk(survey.id);
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);
return await Survey.findOne({ id: survey.id });

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.

Copilot Autofix AI about 23 hours ago

To fix the problem, we need to ensure that the user-provided data is properly sanitized or validated before being used in a database query. For MongoDB, we can use the $eq operator to ensure that the user input is interpreted as a literal value and not as a query object. This will prevent any potential NoSQL injection attacks.

We will modify the updateSurvey method in backend/src/services/survey.service.ts to use the $eq operator for the id field in the query.

Suggested changeset 1
backend/src/services/survey.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/backend/src/services/survey.service.ts b/backend/src/services/survey.service.ts
--- a/backend/src/services/survey.service.ts
+++ b/backend/src/services/survey.service.ts
@@ -11,4 +11,4 @@
     const Survey = mongoose.model('Survey');
-    await Survey.updateOne({ id: survey.id }, survey);
-    return await Survey.findOne({ id: survey.id });
+    await Survey.updateOne({ id: { $eq: survey.id } }, survey);
+    return await Survey.findOne({ id: { $eq: survey.id } });
   }
EOF
@@ -11,4 +11,4 @@
const Survey = mongoose.model('Survey');
await Survey.updateOne({ id: survey.id }, survey);
return await Survey.findOne({ id: survey.id });
await Survey.updateOne({ id: { $eq: survey.id } }, survey);
return await Survey.findOne({ id: { $eq: survey.id } });
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@austenstone austenstone requested a review from Copilot January 15, 2025 14:59
Copy link

@Copilot 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.

Copilot reviewed 60 out of 75 changed files in this pull request and generated 1 comment.

Files not reviewed (15)
  • .vscode/tasks.json: Language not supported
  • Dockerfile: Language not supported
  • backend/github-manifest.json: Language not supported
  • backend/package.json: Language not supported
  • backend/src/tests/mock/metrics-gen/example.json: Language not supported
  • backend/src/tests/mock/seats-gen/seatsExampleTest.json: Language not supported
  • backend/src/tests/mock/survey-gen/exampleSurvey.json: Language not supported
  • README.md: Evaluated as low risk
  • backend/src/tests/mock/metrics-gen/runExampleMock.ts: Evaluated as low risk
  • backend/jest.config.ts: Evaluated as low risk
  • backend/src/tests/mock/metrics-gen/runMock.ts: Evaluated as low risk
  • backend/src/tests/mock/metrics-gen/mockGenerator.ts: Evaluated as low risk
  • backend/src/tests/mock/mock.mongo.ts: Evaluated as low risk
  • backend/src/tests/mock/seats-gen/runSeatsGenerator.ts: Evaluated as low risk
  • backend/src/tests/mock/seats-gen/mockSeatsGenerator.js: Evaluated as low risk
Comments suppressed due to low confidence (3)

backend/src/tests/mock/seats-gen/mockSeatsGenerator.ts:25

  • The variable 'lastActivityAt' should have a specific type instead of 'any'. Consider using 'string' or 'Date'.
const lastActivityAt : any = seat.last_activity_at;

backend/src/tests/mock/seats-gen/mockSeatsGenerator.ts:55

  • Use '===' instead of '==' for comparing dates.
if (newActivity == currentActivity ){

backend/src/tests/mock/seats-gen/mockSeatsGenerator.ts:74

  • The property 'specificUser' is not defined in the 'SeatsMockConfig' type. Ensure it is part of the configuration.
seat.specificUser = this.config.specificUser;

Tip: If you use Visual Studio Code, you can request a review from Copilot before you push from the "Source Control" tab. Learn more

}

private getRandomDate(): Date {
return addDays(this.config.startDate, Math.floor(Math.random() * (this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
Copy link
Preview

Copilot AI Jan 15, 2025

Choose a reason for hiding this comment

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

The division by (1000 * 60 * 60 * 24) should be inside the Math.floor function to ensure the correct number of days is calculated. The corrected line should be: return addDays(this.config.startDate, Math.floor(Math.random() * ((this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24))));

Suggested change
return addDays(this.config.startDate, Math.floor(Math.random() * (this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24)));
return addDays(this.config.startDate, Math.floor(Math.random() * ((this.config.endDate.getTime() - this.config.startDate.getTime()) / (1000 * 60 * 60 * 24))));

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
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