From 46f669c561612b52252f03d61cf1d9fb9850fac8 Mon Sep 17 00:00:00 2001 From: nicolascwy <25302138+NicolasCwy@users.noreply.github.com> Date: Tue, 23 Apr 2024 22:54:23 +0800 Subject: [PATCH] Abstract seed account method --- .../teammates/client/scripts/sql/SeedDb.java | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/client/java/teammates/client/scripts/sql/SeedDb.java b/src/client/java/teammates/client/scripts/sql/SeedDb.java index c2e2de77dec..b232cdb08b0 100644 --- a/src/client/java/teammates/client/scripts/sql/SeedDb.java +++ b/src/client/java/teammates/client/scripts/sql/SeedDb.java @@ -56,6 +56,7 @@ public class SeedDb extends DatastoreClient { private static final int MAX_QUESTION_PER_COURSE = 6; private static final int MAX_RESPONSES_PER_QUESTION = 10; private static final int MAX_COMMENTS_PER_RESPONSE = 2; + private Random rand = new Random(); private final LogicExtension logic = new LogicExtension(); private Closeable closeable; @@ -121,7 +122,7 @@ protected Instant getRandomInstant() { protected void persistAdditionalData() { String[] args = {}; // Each account will have this amount of read notifications - seedNotificationAccountAndAccountRequest(5, 1000); + seedNotificationAndAccountRequest(5, 1000); seedCourseAndRelatedEntities(); GenerateUsageStatisticsObjects.main(args); @@ -305,8 +306,6 @@ private void seedFeedbackResponses(int courseNumber, String courseId, String fee private void seedFeedbackResponseComments(int courseNumber, String courseId, String feedbackQuestionId, String feedbackResponseId, String giverSection, String receiverSection) { - Random rand = new Random(); - int currGiverSection = -1; int currRecipientSection = 0; @@ -333,7 +332,7 @@ private void seedFeedbackResponseComments(int courseNumber, String courseId, Str } } - private void seedNotificationAccountAndAccountRequest(int constReadNotificationSize, int constNotificationSize) { + private void seedNotificationAndAccountRequest(int constReadNotificationSize, int constNotificationSize) { assert constNotificationSize >= constReadNotificationSize; log("Seeding Notifications, Account and Account Request"); @@ -341,8 +340,6 @@ private void seedNotificationAccountAndAccountRequest(int constReadNotificationS ArrayList notificationUuids = new ArrayList<>(); Map notificationEndTimes = new HashMap<>(); - Random rand = new Random(); - for (int j = 0; j < constNotificationSize; j++) { UUID notificationUuid = UUID.randomUUID(); while (notificationsUuidSeen.contains(notificationUuid.toString())) { @@ -387,7 +384,25 @@ private void seedNotificationAccountAndAccountRequest(int constReadNotificationS AccountRequest accountRequest = AccountRequestAttributes .builder(accountRequestEmail, accountRequestInstitute, accountRequestName) .withRegisteredAt(Instant.now()).build().toEntity(); + seedAccount(constReadNotificationSize, constNotificationSize, notificationUuids, notificationEndTimes); + ofy().save().entities(accountRequest).now(); + } catch (Exception e) { + log(e.toString()); + } + } + } + + + private void seedAccount(int constReadNotificationSize, int constNotificationSize, + ArrayList notificationUuids, Map notificationEndTimes) { + for (int i = 0; i < MAX_ENTITY_SIZE; i++) { + if (i % (MAX_ENTITY_SIZE / 5) == 0) { + log(String.format("Seeded %d %% of new sets of entities", + (int) (100 * ((float) i / (float) MAX_ENTITY_SIZE)))); + } + + try { String accountGoogleId = String.format("Account Google ID %s", i); String accountName = String.format("Account name %s", i); String accountEmail = String.format("Account email %s", i); @@ -404,7 +419,6 @@ private void seedNotificationAccountAndAccountRequest(int constReadNotificationS accountEmail, readNotificationsToCreate, false); ofy().save().entities(account).now(); - ofy().save().entities(accountRequest).now(); } catch (Exception e) { log(e.toString()); }