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

Migrate FeedbackSessionAttributes time fields from Date to Instant #8628 #8629

Merged

Conversation

whipermr5
Copy link
Member

@whipermr5 whipermr5 commented Mar 12, 2018

Fixes #8628

Script used to migrate timestamps in JSON data:

import json
import os, io
from collections import OrderedDict
from datetime import datetime, timedelta

P_FORMAT = '%Y-%m-%d %I:%M %p UTC'

filenames = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.json')]

for filename in filenames:
    print(filename)
    with open(filename) as file:
        data = json.loads(file.read(), object_pairs_hook=OrderedDict)

    try:
        if not data.get('feedbackSessions'):
            continue
    except:
        continue

    for fsKey, fs in data.get('feedbackSessions').iteritems():
        for key, val in fs.iteritems():
            if key in ['createdTime', 'startTime', 'endTime', 'sessionVisibleFromTime', 'resultsVisibleFromTime']:
                timestamp = datetime.strptime(fs[key], P_FORMAT)
                fs[key] = timestamp.isoformat() + 'Z'

    with io.open(filename, 'w', encoding='utf8') as file:
        file.write(json.dumps(data, indent=2, separators=(',', ': '), ensure_ascii=False) + '\n')

@whipermr5 whipermr5 added the s.Ongoing The PR is being worked on by the author(s) label Mar 12, 2018
@whipermr5 whipermr5 force-pushed the 8628-migrate-fs-time-fields-instant branch from 2c0d62e to 1b103eb Compare March 12, 2018 07:18
@whipermr5 whipermr5 added s.ToReview The PR is waiting for review(s) and removed s.Ongoing The PR is being worked on by the author(s) labels Mar 12, 2018
Copy link
Member

@darrenwee darrenwee left a comment

Choose a reason for hiding this comment

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

Most of it looks great to me 👍 Not sure if you wanna add javadocs for the boolean methods in FeedbackSessionAttributes because I think some of their behavior is not super intuitive.


return now.after(startTime) && differenceBetweenDeadlineAndNow < hours;
public boolean isClosedAfter(long hours) {
return Instant.now().plus(Duration.ofHours(hours)).isAfter(endTime);
}

public boolean isClosingWithinTimeLimit(int hours) {
Copy link
Member

Choose a reason for hiding this comment

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

Change param type from int to long

@@ -354,7 +333,7 @@ public boolean isPublished() {
} else if (publishTime.equals(Const.TIME_REPRESENTS_NOW)) {
return true;
} else {
return publishTime.before(now);
return now.isAfter(publishTime) || now.equals(publishTime);
Copy link
Member

Choose a reason for hiding this comment

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

Use early returns instead for these if/elses?

@@ -1126,7 +1128,8 @@ protected void testAccessControl() throws Exception {

private void testGracePeriodAccessControlForStudents() {
FeedbackSessionAttributes fs = typicalBundle.feedbackSessions.get("gracePeriodSession");
fs.setEndTime(TimeHelper.convertLocalDateToUtc(TimeHelper.getDateOffsetToCurrentTime(0), fs.getTimeZone()));
fs.setEndTime(TimeHelper.convertLocalDateTimeToInstant(
TimeHelperExtension.now(), TimeHelper.convertToZoneId(fs.getTimeZone())));
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic seems long winded, probably a result of the fact that these times used to be in local time instead of UTC. Just fs.setEndTime(Instant.now()) would be sufficient here. Personally, I find TimeHelperExtension.now very awkward, i.e. it shouldn't be there. Wherever it's used, I feel like it's because of the long-winded logic such as this.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right; just tried and realised it isn't necessary for this test. However it's still needed in StudentHomePageUiTest due to a problem with the test itself. In that test, the end time of Graced Feedback Session is set to x hours before the current time where x is the zone offset for the session, such that when displayed in the user's time zone, it is printed as the current time in UTC. This is necessary for the test to pass due to broken time zone detection in HtmlHelper, where it searches for the string UTC+xxxx to detect the time zone. However, that particular page has no such string, so the zone is detected as UTC, and the search and replace for datetime.now only matches the current time in UTC. Since this is a problem with the test, I will explicitly set it as such, and it can be fixed in a future PR. Will remove TimeHelperExtension#now.

TIME_REPRESENTS_NOW = TimeHelper.parseInstant("1970-02-14 12:00 AM +0000");
TIME_REPRESENTS_DEFAULT_TIMESTAMP = TimeHelper.parseInstant("2011-01-01 12:00 AM +0000");

TIME_REPRESENTS_DEFAULT_TIMESTAMP_DATE = TimeHelper.convertInstantToDate(TIME_REPRESENTS_DEFAULT_TIMESTAMP);
Copy link
Contributor

@tran-tien-dat tran-tien-dat Mar 12, 2018

Choose a reason for hiding this comment

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

Mark @Deprecated?

Copy link
Member Author

Choose a reason for hiding this comment

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

The field is marked deprecated a few lines up, during declaration.

@whipermr5 whipermr5 force-pushed the 8628-migrate-fs-time-fields-instant branch from e9a5a69 to 5a8931e Compare March 14, 2018 06:10
Deprecate TimeHelper.isSpecialTime(Date) in favour of new method isSpecialTime(Instant)
Remove now unused method isTimeWithinPeriod
AdminInstructorAccountAddAction: remove "update feedback session time" replace as it was not working anyway (no such string "2013-04-01 11:59 PM UTC" in the template InstructorSampleData.json)
Add TimeHelperExtension.now() utility method
Update TimeHelperExtension methods:
Remove getInstantMillisOffsetFromNow in favour of new method getInstantMinutesOffsetFromNow
Remove now unused convertToOptionValueInTimeDropDown(Date)
@whipermr5 whipermr5 force-pushed the 8628-migrate-fs-time-fields-instant branch from 5a8931e to 3c4eb3a Compare March 14, 2018 06:21
Copy link
Contributor

@tran-tien-dat tran-tien-dat 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
Member

@darrenwee darrenwee left a comment

Choose a reason for hiding this comment

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

🥇 LGTM

@whipermr5 whipermr5 requested a review from damithc March 14, 2018 07:26
@whipermr5 whipermr5 added s.FinalReview The PR is ready for final review and removed s.ToReview The PR is waiting for review(s) labels Mar 14, 2018
Copy link
Contributor

@damithc damithc left a comment

Choose a reason for hiding this comment

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

Just a question.

+ "<span class=\"bold\">From:</span> 2012-01-31T16:00:00Z"
+ "<span class=\"bold\"> to</span> 2014-12-31T16:00:00Z<br>"
+ "<span class=\"bold\">Session visible from:</span> 2011-12-31T16:00:00Z<br>"
+ "<span class=\"bold\">Results visible from:</span> 1970-06-22T00:00:00Z<br>"
Copy link
Contributor

Choose a reason for hiding this comment

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

I remember a recent PR changed our date display format to a 'universally understood' format. Are we reversing that here?

Copy link
Member Author

@whipermr5 whipermr5 Mar 14, 2018

Choose a reason for hiding this comment

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

No; this is just the admin logging system. It uses toString on the timestamps. The default toString implementation for Instant results in this format.

statusToAdmin =
"New Feedback Session <span class=\"bold\">(" + fs.getFeedbackSessionName() + ")</span> for Course "
+ "<span class=\"bold\">[" + fs.getCourseId() + "]</span> created.<br>"
+ "<span class=\"bold\">From:</span> " + fs.getStartTime()
+ "<span class=\"bold\"> to</span> " + fs.getEndTime() + "<br>"
+ "<span class=\"bold\">Session visible from:</span> " + fs.getSessionVisibleFromTime() + "<br>"
+ "<span class=\"bold\">Results visible from:</span> " + fs.getResultsVisibleFromTime() + "<br><br>"
+ "<span class=\"bold\">Instructions:</span> " + fs.getInstructions();

@whipermr5 whipermr5 added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging and removed s.FinalReview The PR is ready for final review labels Mar 14, 2018
@whipermr5 whipermr5 added this to the V6.4.0 milestone Mar 14, 2018
@whipermr5 whipermr5 added the e.2 label Mar 14, 2018
@whipermr5 whipermr5 merged commit 30f0012 into TEAMMATES:master Mar 14, 2018
@whipermr5 whipermr5 deleted the 8628-migrate-fs-time-fields-instant branch March 14, 2018 15:00
@whipermr5 whipermr5 added the c.Task Other non-user-facing works, e.g. refactoring, adding tests label Mar 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c.Task Other non-user-facing works, e.g. refactoring, adding tests s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants