Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
[TEAMMATES#11585] Remove unnecessary data read (TEAMMATES#11646)
Browse files Browse the repository at this point in the history
  • Loading branch information
moziliar authored Mar 13, 2022
1 parent a3c4ba0 commit 0642966
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src/main/java/teammates/logic/core/FeedbackQuestionsLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ private boolean areQuestionNumbersConsistent(List<FeedbackQuestionAttributes> qu
return true;
}

/**
* Checks if there are any questions for the given session that instructors can view/submit.
*/
public boolean hasFeedbackQuestionsForInstructors(
String feedbackSessionName, String courseId, String userEmail) {
boolean hasQuestions = fqDb.hasFeedbackQuestionsForGiverType(
feedbackSessionName, courseId, FeedbackParticipantType.INSTRUCTORS);
if (hasQuestions) {
return true;
}

if (userEmail != null && fsLogic.isCreatorOfSession(feedbackSessionName, courseId, userEmail)) {
hasQuestions = fqDb.hasFeedbackQuestionsForGiverType(
feedbackSessionName, courseId, FeedbackParticipantType.SELF);
}

return hasQuestions;
}

/**
* Gets a {@code List} of all questions for the given session that instructors can view/submit.
*/
Expand Down Expand Up @@ -180,6 +199,15 @@ public List<FeedbackQuestionAttributes> getFeedbackQuestionsForInstructors(
return questions;
}

/**
* Checks if there are any questions for the given session that students can view/submit.
*/
public boolean hasFeedbackQuestionsForStudents(
String feedbackSessionName, String courseId) {
return fqDb.hasFeedbackQuestionsForGiverType(feedbackSessionName, courseId, FeedbackParticipantType.STUDENTS)
|| fqDb.hasFeedbackQuestionsForGiverType(feedbackSessionName, courseId, FeedbackParticipantType.TEAMS);
}

/**
* Gets a {@code List} of all questions for the given session that students can view/submit.
*/
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/teammates/logic/core/FeedbackSessionsLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public List<FeedbackSessionAttributes> getFeedbackSessionsWhichNeedOpenEmailsToB
*/
public boolean isCreatorOfSession(String feedbackSessionName, String courseId, String userEmail) {
FeedbackSessionAttributes fs = getFeedbackSession(feedbackSessionName, courseId);
if (fs == null) {
return false;
}
return fs.getCreatorEmail().equals(userEmail);
}

Expand Down Expand Up @@ -558,11 +561,13 @@ public boolean isFeedbackSessionViewableToUserType(FeedbackSessionAttributes ses
* Returns true if there are any questions for the specified user type (students/instructors) to answer.
*/
public boolean isFeedbackSessionForUserTypeToAnswer(FeedbackSessionAttributes session, boolean isInstructor) {
List<FeedbackQuestionAttributes> questionsToAnswer = isInstructor
? fqLogic.getFeedbackQuestionsForInstructors(session.getFeedbackSessionName(), session.getCourseId(), null)
: fqLogic.getFeedbackQuestionsForStudents(session.getFeedbackSessionName(), session.getCourseId());
if (!session.isVisible()) {
return false;
}

return session.isVisible() && !questionsToAnswer.isEmpty();
return isInstructor
? fqLogic.hasFeedbackQuestionsForInstructors(session.getFeedbackSessionName(), session.getCourseId(), null)
: fqLogic.hasFeedbackQuestionsForStudents(session.getFeedbackSessionName(), session.getCourseId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ boolean hasExistingEntities(FeedbackResponseAttributes entityToCreate) {
.filterKey(Key.create(FeedbackResponse.class,
FeedbackResponse.generateId(entityToCreate.getFeedbackQuestionId(),
entityToCreate.getGiver(), entityToCreate.getRecipient())))
.keys()
.list()
.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ boolean hasExistingEntities(FeedbackSessionAttributes entityToCreate) {
return !load()
.filterKey(Key.create(FeedbackSession.class,
FeedbackSession.generateId(entityToCreate.getFeedbackSessionName(), entityToCreate.getCourseId())))
.keys()
.list()
.isEmpty();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/teammates/storage/api/StudentsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ boolean hasExistingEntities(StudentAttributes entityToCreate) {
return !load()
.filterKey(Key.create(CourseStudent.class,
CourseStudent.generateId(entityToCreate.getEmail(), entityToCreate.getCourse())))
.keys()
.list()
.isEmpty();
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/teammates/logic/core/FeedbackQuestionsLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public void testDeleteFeedbackQuestions_byCourseIdAndSessionName_shouldDeleteQue

@Test
public void allTests() throws Exception {
testHasFeedbackQuestionsForInstructor();
testGetFeedbackQuestionsForInstructor();
testHasFeedbackQuestionsForStudents();
testGetFeedbackQuestionsForStudents();
testAddQuestion();
}
Expand Down Expand Up @@ -905,6 +907,20 @@ public void testBuildCompleteGiverRecipientMap_teamQuestion_shouldBuildMapCorrec
assertTrue(completeGiverRecipientMap.get("Team 1.2").contains("Team 1.1</td></div>'\""));
}

private void testHasFeedbackQuestionsForInstructor() {
______TS("Valid session valid instructor should have questions");
assertTrue(fqLogic.hasFeedbackQuestionsForInstructors(
"First feedback session", "idOfTypicalCourse1", "[email protected]"));

______TS("Valid session without questions for instructor should return false");
assertFalse(fqLogic.hasFeedbackQuestionsForInstructors(
"session without instructor questions", "idOfTestingSanitizationCourse", "[email protected]"));

______TS("Invalid session should not have questions");
assertFalse(fqLogic.hasFeedbackQuestionsForInstructors(
"Zeroth feedback session", "idOfTypicalCourse1", "[email protected]"));
}

private void testGetFeedbackQuestionsForInstructor() {
List<FeedbackQuestionAttributes> expectedQuestions;
List<FeedbackQuestionAttributes> actualQuestions;
Expand Down Expand Up @@ -963,6 +979,20 @@ private void testGetFeedbackQuestionsForInstructor() {
assertEquals(actualQuestions, expectedQuestions);
}

private void testHasFeedbackQuestionsForStudents() {
______TS("Valid session should have questions");
assertTrue(fqLogic.hasFeedbackQuestionsForStudents(
"First feedback session", "idOfTypicalCourse1"));

______TS("Valid session without questions for students should return false");
assertFalse(fqLogic.hasFeedbackQuestionsForStudents(
"session without student questions", "idOfArchivedCourse"));

______TS("Invalid session should not have questions");
assertFalse(fqLogic.hasFeedbackQuestionsForStudents(
"Zeroth feedback session", "idOfTypicalCourse1"));
}

private void testGetFeedbackQuestionsForStudents() {
List<FeedbackQuestionAttributes> expectedQuestions;
List<FeedbackQuestionAttributes> actualQuestions;
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/teammates/logic/core/FeedbackSessionsLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public void testAll() throws Exception {

testCreateAndDeleteFeedbackSession();

testIsFeedbackSessionForUserTypeToAnswer();

testUpdateFeedbackSession();
testPublishUnpublishFeedbackSession();

Expand Down Expand Up @@ -518,6 +520,28 @@ private void testIsFeedbackSessionViewableToUserType() {
assertFalse(fsLogic.isFeedbackSessionViewableToUserType(session, true));
}

private void testIsFeedbackSessionForUserTypeToAnswer() {
______TS("Non-visible session should not be for any types of user to answer");
FeedbackSessionAttributes session = dataBundle.feedbackSessions.get("awaiting.session");
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, false));
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, true));

______TS("Empty session should not be for any types of user to answer");
session = dataBundle.feedbackSessions.get("empty.session");
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, false));
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, true));

______TS("Session without student question should not be for students to answer");
session = dataBundle.feedbackSessions.get("archiveCourse.session1");
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, false));
assertTrue(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, true));

______TS("Session without instructor question should not be for instructors to answer");
session = dataBundle.feedbackSessions.get("session2InCourse1");
assertFalse(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, true));
assertTrue(fsLogic.isFeedbackSessionForUserTypeToAnswer(session, false));
}

private void testUpdateFeedbackSession() throws Exception {

______TS("failure: non-existent session name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void testGetAllOngoingSessions_typicalCase_shouldQuerySuccessfullyWithout
Instant rangeStart = Instant.parse("2000-12-03T10:15:30.00Z");
Instant rangeEnd = Instant.parse("2050-04-30T21:59:00Z");
List<FeedbackSessionAttributes> actualAttributesList = fsDb.getAllOngoingSessions(rangeStart, rangeEnd);
assertEquals("should not return more than 13 sessions as there are only 13 distinct sessions in the range",
13, actualAttributesList.size());
assertEquals("should not return more than 14 sessions as there are only 14 distinct sessions in the range",
14, actualAttributesList.size());
}

@Test
Expand Down
42 changes: 42 additions & 0 deletions src/test/resources/data/typicalDataBundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,27 @@
"gracePeriod": 5,
"sentOpeningSoonEmail": true,
"sentOpenEmail": true,
"sentClosingEmail": true,
"sentClosedEmail": true,
"sentPublishedEmail": true,
"isOpeningEmailEnabled": true,
"isClosingEmailEnabled": true,
"isPublishedEmailEnabled": true
},
"archiveCourse.session2": {
"feedbackSessionName": "session without instructor questions",
"courseId": "idOfArchivedCourse",
"creatorEmail": "[email protected]",
"instructions": "Please please fill in the following questions.",
"createdTime": "2013-01-20T23:00:00Z",
"startTime": "2013-02-20T23:00:00Z",
"endTime": "2026-04-28T23:00:00Z",
"sessionVisibleFromTime": "2013-02-20T23:00:00Z",
"resultsVisibleFromTime": "2026-04-29T23:00:00Z",
"timeZone": "Africa/Johannesburg",
"gracePeriod": 5,
"sentOpeningSoonEmail": true,
"sentOpenEmail": true,
"sentClosingEmail": false,
"sentClosedEmail": false,
"sentPublishedEmail": false,
Expand Down Expand Up @@ -1356,6 +1377,27 @@
"RECEIVER"
]
},
"qn1InSession2InArchivedCourse": {
"feedbackSessionName": "session without instructor questions",
"courseId": "idOfArchivedCourse",
"questionDetails": {
"questionType": "TEXT",
"questionText": "Give feedback to each other"
},
"questionNumber": 1,
"giverType": "STUDENTS",
"recipientType": "STUDENTS",
"numberOfEntitiesToGiveFeedbackTo": 4,
"showResponsesTo": [
"RECEIVER"
],
"showGiverNameTo": [
"RECEIVER"
],
"showRecipientNameTo": [
"RECEIVER"
]
},
"qn1InSession1InCourse2": {
"feedbackSessionName": "Instructor feedback session",
"courseId": "idOfTypicalCourse2",
Expand Down

0 comments on commit 0642966

Please sign in to comment.