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

fix: improve live session to live quiz migration script #4368

Merged
merged 10 commits into from
Nov 10, 2024

Conversation

rschlaefli
Copy link
Member

@rschlaefli rschlaefli commented Nov 10, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a migration process for converting live sessions to a new live quiz format.
    • Added functionalities for data transformation and cache management during migration.
  • Bug Fixes

    • Improved handling of leaderboard and instance data to ensure compatibility with the new quiz structure.
  • Documentation

    • Enhanced documentation with detailed comments on functions related to migration logic and data handling.

Copy link

coderabbitai bot commented Nov 10, 2024

Warning

Rate limit exceeded

@rschlaefli has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 1 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d8c5934 and 4d990c6.

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant updates to the migration script for transitioning live sessions to a new live quiz format. It adds new imports and several functions to facilitate data transformation, cache management, and database updates. Key functions include applyCacheUpdatesForQuiz for Redis cache updates, applyDBUpdatesForQuiz for migrating database entries, and a restructured run function to orchestrate the overall migration process, ensuring compatibility with the new quiz structure.

Changes

File Path Change Summary
packages/graphql/src/scripts/2024-11-07_migrate_live_session_to_live_quiz.ts - Added functions: applyCacheUpdatesForQuiz, applyDBUpdatesForQuiz, run.
- Updated method signatures for computeElementType, fakeElementFromQuestionData, questionDataToElementData, convertOldResults.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
packages/graphql/src/scripts/2024-11-07_migrate_live_session_to_live_quiz.ts (1)

611-611: Remove unnecessary console.log statement

The console.log statement at line 611 outputs the name of the new live quiz. Unless required for debugging, consider removing it or controlling it with a logging flag for cleaner console output.

Apply this diff to remove the statement:

-     console.log(newLiveQuizData.data.name)
🛑 Comments failed to post (3)
packages/graphql/src/scripts/2024-11-07_migrate_live_session_to_live_quiz.ts (3)

704-706: ⚠️ Potential issue

Handle the case when newLiveQuiz is undefined to prevent runtime errors

If applyDBUpdatesForQuiz returns undefined (e.g., if the live session has already been migrated), accessing newLiveQuiz.status will cause a runtime error. Add a check to ensure newLiveQuiz is defined before accessing its properties.

Apply this diff to fix the issue:

        const newLiveQuiz = await applyDBUpdatesForQuiz(prisma, liveSession)

+       if (!newLiveQuiz) {
+         continue
+       }

        if (newLiveQuiz.status === PublicationStatus.PUBLISHED) {
          await applyCacheUpdatesForQuiz(redisExec, newLiveQuiz)
        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    const newLiveQuiz = await applyDBUpdatesForQuiz(prisma, liveSession)

    if (!newLiveQuiz) {
      continue
    }

    if (newLiveQuiz.status === PublicationStatus.PUBLISHED) {

94-97: ⚠️ Potential issue

Add validation for questionData.id format to prevent runtime errors

The current code assumes that questionData.id follows the format 'elementId-version' (e.g., '12345-v1'). If questionData.id doesn't conform to this format, splitting and parsing might throw errors. To enhance robustness, add validation to ensure the ID is correctly formatted before parsing.

Apply this diff to add validation:

+     const idParts = questionData.id.split('-')
+     if (idParts.length < 2) {
+       throw new Error(`Invalid questionData.id format: ${questionData.id}`)
+     }
+     const elementId = idParts[0]
+     const versionPart = idParts[1]
+     const elementVersion = Number(versionPart.startsWith('v') ? versionPart.slice(1) : versionPart)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

  const idParts = questionData.id.split('-')
  if (idParts.length < 2) {
    throw new Error(`Invalid questionData.id format: ${questionData.id}`)
  }
  const elementId = idParts[0]
  const versionPart = idParts[1]
  const elementVersion = Number(versionPart.startsWith('v') ? versionPart.slice(1) : versionPart)
  const elementType = computeElementType({ questionData })

373-408: ⚠️ Potential issue

Fix the improper use of async functions inside forEach

Using forEach with an async callback does not wait for the promises to resolve, which may lead to incomplete Redis pipeline operations before pipeline.exec() is called. Replace forEach with a for...of loop to ensure asynchronous operations complete as intended.

Apply the following changes to fix the issue:

-        activeBlock.elements.forEach(async (instance) => {
+        for (const instance of activeBlock.elements) {

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

@sjschlapbach sjschlapbach left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
packages/graphql/src/scripts/2024-11-07_migrate_live_session_to_live_quiz.ts (3)

Line range hint 42-82: Consider adding type safety for questionData parameter.

The function uses any type for questionData which could lead to runtime errors. Consider creating an interface to define the expected shape of the question data.

interface QuestionData {
  type: string;
  id: string;
  name: string;
  content: string;
  explanation?: string;
  pointsMultiplier?: number;
  options?: Record<string, unknown>;
}

function computeElementType({ questionData }: { questionData: QuestionData }): ElementType {
  // ... rest of the function
}

Line range hint 74-126: Add validation for required questionData fields.

The function checks for required fields but logs and throws a generic error. Consider adding specific error messages for each missing field and validating the data structure upfront.

- if (!questionData.id || !questionData.name || !questionData.content) {
-   console.log('QUESTION DATA')
-   console.log(questionData)
-   throw new Error('Missing required question data properties')
- }
+ const requiredFields = ['id', 'name', 'content'] as const;
+ const missingFields = requiredFields.filter(field => !questionData[field]);
+ if (missingFields.length > 0) {
+   throw new Error(`Missing required question data properties: ${missingFields.join(', ')}`);
+ }

Line range hint 401-626: Enhance data integrity with explicit transaction handling.

The function performs multiple database operations without explicit transaction handling, which could lead to partial migrations if errors occur.

Consider wrapping the database operations in an explicit transaction:

 async function applyDBUpdatesForQuiz(
   prisma: PrismaClient,
   liveSession: LiveSession & {
     activeBlock?: (SessionBlock & { instances: QuestionInstance[] }) | null
     blocks: (SessionBlock & { instances: QuestionInstance[] })[]
   }
 ) {
+  return await prisma.$transaction(async (tx) => {
     // check if the considered live session has already been migrated
-    const existingLiveQuiz = await prisma.liveQuiz.findFirst({
+    const existingLiveQuiz = await tx.liveQuiz.findFirst({
       where: { originalId: liveSession.id },
     });

     if (existingLiveQuiz) {
       console.log(`DB: Live session ${liveSession.id} has already been migrated`);
       return existingLiveQuiz;
     }

     // ... rest of the function ...

-    let newLiveQuiz = await prisma.liveQuiz.create(newLiveQuizData);
+    let newLiveQuiz = await tx.liveQuiz.create(newLiveQuizData);

     if (liveSession.activeBlock) {
-      newLiveQuiz = await prisma.liveQuiz.update({
+      newLiveQuiz = await tx.liveQuiz.update({
         // ... update data ...
       });
     }

     return newLiveQuiz;
+  }, {
+    timeout: 10000, // Set an appropriate timeout
+    maxWait: 5000,  // Maximum time to wait for transaction
+  });
 }
🛑 Comments failed to post (2)
packages/graphql/src/scripts/2024-11-07_migrate_live_session_to_live_quiz.ts (2)

314-399: ⚠️ Potential issue

Add error handling and optimize Redis operations.

The function has several potential issues:

  1. No error handling for Redis operations
  2. Potential race condition between cache check and update
  3. Pipeline execution inside forEach loop is inefficient

Consider applying these improvements:

 async function applyCacheUpdatesForQuiz(
   redisExec: Redis,
   newLiveQuiz: LiveQuiz & {
     activeBlock?: ElementBlock & { elements: Element[] }
   }
 ) {
+  const key = `lq:${newLiveQuiz.id}:meta`;
+  
+  // Use multi instead of pipeline for atomic operations
+  const multi = redisExec.multi();
+
+  try {
-    if (await redisExec.exists(`lq:${newLiveQuiz.id}:meta`)) {
-      console.log(`Cache: Live quiz ${newLiveQuiz.id} already migrated`)
-      return
-    }
+    // Use SETNX for atomic check-and-set
+    multi.hsetnx(key, 'namespace', newLiveQuiz.namespace);
+    const results = await multi.exec();
+    
+    if (!results?.[0]?.[1]) {
+      console.log(`Cache: Live quiz ${newLiveQuiz.id} already migrated`);
+      return;
+    }

-    const pipeline = redisExec.pipeline();
+    const multi = redisExec.multi();

     // ... rest of the cache updates ...

-    activeBlock.elements.forEach(async (instance) => {
+    for (const instance of activeBlock.elements) {
       // ... instance updates ...
-    })
+    }

-    await pipeline.exec()
+    await multi.exec();
+  } catch (error) {
+    console.error(`Failed to update cache for quiz ${newLiveQuiz.id}:`, error);
+    throw error;
+  }
 }

Committable suggestion skipped: line range outside the PR's diff.


628-701: 🛠️ Refactor suggestion

Improve robustness and performance of the migration process.

The main function could benefit from several improvements:

  1. Batch processing for better performance
  2. Proper cleanup on errors
  3. Configuration management

Consider these improvements:

 async function run() {
+  let redisExec: Redis | null = null;
+  let prisma: PrismaClient | null = null;
+
+  try {
-    const redisExec = new Redis({
+    redisExec = new Redis({
       family: 4,
-      host: process.env.REDIS_HOST ?? 'localhost',
-      password: process.env.REDIS_PASS ?? '',
-      port: Number(process.env.REDIS_PORT) ?? 6379,
-      tls: process.env.REDIS_TLS ? {} : undefined,
+      host: process.env.REDIS_HOST || 'localhost',
+      password: process.env.REDIS_PASS || '',
+      port: parseInt(process.env.REDIS_PORT || '6379', 10),
+      tls: process.env.REDIS_TLS === 'true' ? {} : undefined,
     });

-    const prisma = new PrismaClient();
+    prisma = new PrismaClient();

     const liveSessions = await prisma.liveSession.findMany({
       // ... query options ...
     });

+    // Process in batches for better performance
+    const BATCH_SIZE = 10;
+    for (let i = 0; i < liveSessions.length; i += BATCH_SIZE) {
+      const batch = liveSessions.slice(i, i + BATCH_SIZE);
+      await Promise.all(
+        batch.map(async (liveSession) => {
+          try {
+            const newLiveQuiz = await applyDBUpdatesForQuiz(prisma!, liveSession);
+            if (newLiveQuiz.status === PublicationStatus.PUBLISHED) {
+              await applyCacheUpdatesForQuiz(redisExec!, newLiveQuiz);
+            }
+            console.log(
+              `Migrated live session ${liveSession.id} to live quiz ${newLiveQuiz.id}`
+            );
+          } catch (error) {
+            console.error(
+              `Failed to migrate session ${liveSession.id}:`,
+              error
+            );
+          }
+        })
+      );
+    }
+  } catch (error) {
+    console.error('Migration failed:', error);
+    throw error;
+  } finally {
+    await prisma?.$disconnect();
+    await redisExec?.quit();
+  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

/**
 * Migrates existing live sessions to live quizzes in the database and updates corresponding Redis cache entries
 *
 * This function:
 * 1. Connects to Redis using environment variables or default values
 * 2. Creates a Prisma client instance
 * 3. Fetches all live sessions with their associated blocks and question instances
 * 4. For each live session:
 *    - Creates a new live quiz entry in the database
 *    - Updates Redis cache if the quiz is published
 * 5. Disconnects from Prisma and Redis after completion
 *
 * @requires REDIS_HOST - Redis host (defaults to 'localhost')
 * @requires REDIS_PASS - Redis password (defaults to '')
 * @requires REDIS_PORT - Redis port (defaults to 6379)
 * @requires REDIS_TLS - Redis TLS configuration (optional)
 *
 * @throws Will throw an error if database operations fail
 * @throws Will throw an error if Redis operations fail
 *
 * @async
 */
async function run() {
  let redisExec: Redis | null = null;
  let prisma: PrismaClient | null = null;

  try {
    redisExec = new Redis({
      family: 4,
      host: process.env.REDIS_HOST || 'localhost',
      password: process.env.REDIS_PASS || '',
      port: parseInt(process.env.REDIS_PORT || '6379', 10),
      tls: process.env.REDIS_TLS === 'true' ? {} : undefined,
    });

    prisma = new PrismaClient();

    const liveSessions = await prisma.liveSession.findMany({
      include: {
        blocks: {
          orderBy: {
            order: 'asc',
          },
          include: {
            instances: {
              orderBy: {
                order: 'asc',
              },
            },
          },
        },
        activeBlock: {
          include: {
            instances: {
              orderBy: {
                order: 'asc',
              },
            },
          },
        },
      },
    });

    // Process in batches for better performance
    const BATCH_SIZE = 10;
    for (let i = 0; i < liveSessions.length; i += BATCH_SIZE) {
      const batch = liveSessions.slice(i, i + BATCH_SIZE);
      await Promise.all(
        batch.map(async (liveSession) => {
          try {
            const newLiveQuiz = await applyDBUpdatesForQuiz(prisma!, liveSession);
            if (newLiveQuiz.status === PublicationStatus.PUBLISHED) {
              await applyCacheUpdatesForQuiz(redisExec!, newLiveQuiz);
            }
            console.log(
              `Migrated live session ${liveSession.id} to live quiz ${newLiveQuiz.id}`
            );
          } catch (error) {
            console.error(
              `Failed to migrate session ${liveSession.id}:`,
              error
            );
          }
        })
      );
    }
  } catch (error) {
    console.error('Migration failed:', error);
    throw error;
  } finally {
    await prisma?.$disconnect();
    await redisExec?.quit();
  }
}

Copy link

cypress bot commented Nov 10, 2024

klicker-uzh    Run #3633

Run Properties:  status check passed Passed #3633  •  git commit 0771cbb3f5 ℹ️: Merge 4d990c63a11e930cc2a0c9019cac65dc18304c73 into 3cab4a2aa3f23f01f4dd867fb22f...
Project klicker-uzh
Branch Review LiveQuizScriptImprovements
Run status status check passed Passed #3633
Run duration 11m 09s
Commit git commit 0771cbb3f5 ℹ️: Merge 4d990c63a11e930cc2a0c9019cac65dc18304c73 into 3cab4a2aa3f23f01f4dd867fb22f...
Committer Roland Schläfli
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 140
View all changes introduced in this branch ↗︎

@rschlaefli rschlaefli merged commit 48b5675 into v3-new-live-quiz Nov 10, 2024
10 checks passed
@rschlaefli rschlaefli deleted the LiveQuizScriptImprovements branch November 10, 2024 22:31
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants