Skip to content

Commit

Permalink
Combine non-course and course seed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCwy committed Apr 23, 2024
1 parent 46f669c commit cc493e8
Showing 1 changed file with 43 additions and 47 deletions.
90 changes: 43 additions & 47 deletions src/client/java/teammates/client/scripts/sql/SeedDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> notificationsUuidSeen = new HashSet<String>();
private ArrayList<String> notificationUuids = new ArrayList<>();
private Map<String, Instant> notificationEndTimes = new HashMap<>();
private Map<String, Account> googleIdToAccountForStudentsMap = new HashMap<>();

/**
* Sets up the dependencies needed for the DB layer.
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -146,6 +153,8 @@ private void seedCourseAndRelatedEntities() {
log(e.toString());
}
}

GenerateUsageStatisticsObjects.main(args);
}

private void seedCourseWithCourseId(int i, String courseId) {
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<String> notificationsUuidSeen = new HashSet<String>();
ArrayList<String> notificationUuids = new ArrayList<>();
Map<String, Instant> notificationEndTimes = new HashMap<>();

for (int j = 0; j < constNotificationSize; j++) {
private void seedNotifications(ArrayList<String> notificationUuids,
Set<String> notificationsUuidSeen, Map<String, Instant> notificationEndTimes) {
for (int j = 0; j < NOTIFICATION_SIZE; j++) {
UUID notificationUuid = UUID.randomUUID();
while (notificationsUuidSeen.contains(notificationUuid.toString())) {
notificationUuid = UUID.randomUUID();
Expand Down Expand Up @@ -369,22 +378,21 @@ 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);
String accountRequestInstitute = String.format("Account Institute %s", i);
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) {
Expand All @@ -394,35 +402,21 @@ private void seedNotificationAndAccountRequest(int constReadNotificationSize, in
}


private void seedAccount(int constReadNotificationSize, int constNotificationSize,
ArrayList<String> notificationUuids, Map<String, Instant> 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<String, Instant> 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<String, Instant> 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) {
Expand All @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit cc493e8

Please sign in to comment.