diff --git a/src/client/java/teammates/client/scripts/sql/SeedDb.java b/src/client/java/teammates/client/scripts/sql/SeedDb.java index b232cdb08b0..a80731e42d9 100644 --- a/src/client/java/teammates/client/scripts/sql/SeedDb.java +++ b/src/client/java/teammates/client/scripts/sql/SeedDb.java @@ -56,10 +56,18 @@ 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 static final int NOTIFICATION_SIZE = 1000; + private static final int READ_NOTIFICATION_SIZE = 5; + private static final double PERCENTAGE_STUDENTS_WITH_ACCOUNT = 0.5; private Random rand = new Random(); private final LogicExtension logic = new LogicExtension(); private Closeable closeable; + private Set notificationsUuidSeen = new HashSet(); + private ArrayList notificationUuids = new ArrayList<>(); + private Map notificationEndTimes = new HashMap<>(); + private Map googleIdToAccountForStudentsMap = new HashMap<>(); + /** * Sets up the dependencies needed for the DB layer. */ @@ -122,13 +130,12 @@ protected Instant getRandomInstant() { protected void persistAdditionalData() { String[] args = {}; // Each account will have this amount of read notifications - seedNotificationAndAccountRequest(5, 1000); - seedCourseAndRelatedEntities(); + assert NOTIFICATION_SIZE >= READ_NOTIFICATION_SIZE; + log("Seeding Notifications, Account and Account Request"); - GenerateUsageStatisticsObjects.main(args); - } + seedNotifications(notificationUuids, notificationsUuidSeen, notificationEndTimes); + seedAccountRequests(); - private void seedCourseAndRelatedEntities() { log("Seeding courses"); for (int i = 0; i < MAX_ENTITY_SIZE; i++) { if (i % (MAX_ENTITY_SIZE / 5) == 0) { @@ -146,6 +153,8 @@ private void seedCourseAndRelatedEntities() { log(e.toString()); } } + + GenerateUsageStatisticsObjects.main(args); } private void seedCourseWithCourseId(int i, String courseId) { @@ -163,8 +172,6 @@ private void seedCourseWithCourseId(int i, String courseId) { private void seedStudents(int courseNumber, String courseId) { assert MAX_SECTION_PER_COURSE <= MAX_STUDENT_PER_COURSE; - Random rand = new Random(); - log("Seeding students for course " + courseNumber); int currSection = -1; int currTeam = -1; @@ -196,6 +203,14 @@ private void seedStudents(int courseNumber, String courseId) { student.setLastUpdate(rand.nextInt(3) > 1 ? null : getRandomInstant()); student.setRegistrationKey(studentRegistrationKey); + if (rand.nextDouble() >= PERCENTAGE_STUDENTS_WITH_ACCOUNT || googleIdToAccountForStudentsMap.containsKey(studentGoogleId)) { + Account account = googleIdToAccountForStudentsMap.getOrDefault(studentGoogleId, null); + if (account == null) { + account = seedAccount(studentGoogleId, studentName, studentEmail); + googleIdToAccountForStudentsMap.put(studentGoogleId, account); + } + } + ofy().save().entities(student).now(); } catch (Exception e) { log(e.toString()); @@ -332,15 +347,9 @@ private void seedFeedbackResponseComments(int courseNumber, String courseId, Str } } - private void seedNotificationAndAccountRequest(int constReadNotificationSize, int constNotificationSize) { - assert constNotificationSize >= constReadNotificationSize; - log("Seeding Notifications, Account and Account Request"); - - Set notificationsUuidSeen = new HashSet(); - ArrayList notificationUuids = new ArrayList<>(); - Map notificationEndTimes = new HashMap<>(); - - for (int j = 0; j < constNotificationSize; j++) { + private void seedNotifications(ArrayList notificationUuids, + Set notificationsUuidSeen, Map notificationEndTimes) { + for (int j = 0; j < NOTIFICATION_SIZE; j++) { UUID notificationUuid = UUID.randomUUID(); while (notificationsUuidSeen.contains(notificationUuid.toString())) { notificationUuid = UUID.randomUUID(); @@ -369,14 +378,14 @@ private void seedNotificationAndAccountRequest(int constReadNotificationSize, in log(e.toString()); } } + } + private void seedAccountRequests() { 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", + log(String.format("Seeded %d %% of account requests", (int) (100 * ((float) i / (float) MAX_ENTITY_SIZE)))); } - try { String accountRequestName = String.format("Account Request %s", i); String accountRequestEmail = String.format("Account Email %s", i); @@ -384,7 +393,6 @@ private void seedNotificationAndAccountRequest(int constReadNotificationSize, in 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) { @@ -394,35 +402,21 @@ private void seedNotificationAndAccountRequest(int constReadNotificationSize, in } - 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); - Map readNotificationsToCreate = new HashMap<>(); - - for (int j = 0; j < constReadNotificationSize; j++) { - int randIndex = rand.nextInt(constNotificationSize); - String notificationUuid = notificationUuids.get(randIndex); - assert notificationEndTimes.get(notificationUuid) != null; - readNotificationsToCreate.put(notificationUuid, notificationEndTimes.get(notificationUuid)); - } + private Account seedAccount(String googleId, String accountName, String email) { + Map readNotificationsToCreate = new HashMap<>(); + + for (int j = 0; j < READ_NOTIFICATION_SIZE; j++) { + int randIndex = rand.nextInt(NOTIFICATION_SIZE); + String notificationUuid = notificationUuids.get(randIndex); + assert notificationEndTimes.get(notificationUuid) != null; + readNotificationsToCreate.put(notificationUuid, notificationEndTimes.get(notificationUuid)); + } - Account account = new Account(accountGoogleId, accountName, - accountEmail, readNotificationsToCreate, false); + Account account = new Account(googleId, accountName, + email, readNotificationsToCreate, false); - ofy().save().entities(account).now(); - } catch (Exception e) { - log(e.toString()); - } - } + ofy().save().entities(account).now(); + return account; } private void log(String logLine) { @@ -447,6 +441,7 @@ protected void persistData() { * Clears all entities in the data store if re-seeding is needed. */ private void clearDataStore() { + log("Starting datastore entity deletion"); ofy().delete().entities(ofy().load().type(Account.class).list()).now(); ofy().delete().entities(ofy().load().type(AccountRequest.class).list()).now(); ofy().delete().entities(ofy().load().type(Course.class).list()).now(); @@ -466,6 +461,7 @@ public static void main(String[] args) throws Exception { @Override protected void doOperation() { try { + clearDataStore(); // LogicStarter.initializeDependencies(); this.persistData(); } catch (Exception e) {