Skip to content

Commit

Permalink
SAK-40430: Allow SiteStats aggregator job to compensate for sakai_eve…
Browse files Browse the repository at this point in the history
…nt data stored in a different timezone (sakaiproject#5866)

* SAK-40430: Allow SiteStats aggregator job to compensate for sakai_event data stored in a different timezone

* SAK-40430: replacing TimeService with UserTimeService and standardizing display of dates within the tool

* SAK-40430: calendar mock fixup
  • Loading branch information
plukasew authored and ern committed Aug 13, 2018
1 parent 0462d02 commit 3b21698
Show file tree
Hide file tree
Showing 36 changed files with 486 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.Instant;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -439,7 +441,21 @@ public String dateFormatLong(Date date, Locale locale) {
public String dateTimeFormatLong(Date date, Locale locale) {
return null;
}


@Override
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale) {
return null;
}

@Override
public String shortLocalizedTimestamp(Instant instant, Locale locale) {
return null;
}

@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
return null;
}
};
services = new HashMap<String,Object>();
services.put("sqlservice", sqlService);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.sakaiproject.time.api;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -42,5 +44,33 @@ public interface UserTimeService {
*/
public String dateTimeFormatLong(Date date, Locale locale);


/**
* Formats a point in time, in the given time zone, for display to the user in a concise way that still presents all relevant information
* including date, time, and time zone.
*
* @param instant the instant in time
* @param timezone the time zone to use when displaying the date
* @param locale the locale to use when formatting the date for display
* @return a formatted date/time for presentation to the user
*/
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale);

/**
* Formats a point in time, in the user's time zone, for display to the user in a concise way that still presents all relevant information
* including date, time, and time zone.
*
* @param instant the instant in time
* @param locale the locale to use when formatting the date for display
* @return a formatted date/time for presentation to the user
*/
public String shortLocalizedTimestamp(Instant instant, Locale locale);

/**
* Formats a date (month/day/year) in a concise but easily understood format for the given locale.
* Typically presents unambiguous month/day/year values as opposed to a purely 2-digit value for each.
* @param date month/day/year value
* @param locale the locale for use when formatting the date for display
* @return a formatted date for presentation to the user
*/
public String shortLocalizedDate(LocalDate date, Locale locale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package org.sakaiproject.content.impl.serialize.impl.conversion;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
Expand Down Expand Up @@ -197,4 +199,18 @@ public String dateTimeFormatLong(Date date, Locale locale) {
throw new UnsupportedOperationException("This class is only to be used for conversion purposes");
}

@Override
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale) {
throw new UnsupportedOperationException("This class is only to be used for conversion purposes");
}

@Override
public String shortLocalizedTimestamp(Instant instant, Locale locale) {
throw new UnsupportedOperationException("This class is only to be used for conversion purposes");
}

@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
throw new UnsupportedOperationException("This class is only to be used for conversion purposes");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
Expand Down Expand Up @@ -1021,4 +1023,18 @@ public String dateTimeFormatLong(Date date, Locale locale) {
return userTimeService.dateTimeFormatLong(date, locale);
}

@Override
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale) {
return userTimeService.shortLocalizedTimestamp(instant, timezone, locale);
}

@Override
public String shortLocalizedTimestamp(Instant instant, Locale locale) {
return userTimeService.shortLocalizedTimestamp(instant, locale);
}

@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
return userTimeService.shortLocalizedDate(date, locale);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.sakaiproject.time.impl;

import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.format.TextStyle;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
Expand Down Expand Up @@ -116,4 +123,24 @@ public String dateTimeFormatLong(Date date, Locale locale) {
return d;
}

@Override
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale) {
ZonedDateTime userDate = ZonedDateTime.ofInstant(instant, timezone.toZoneId());
DateTimeFormatter userFormatter = new DateTimeFormatterBuilder()
.appendLocalized(FormatStyle.MEDIUM, FormatStyle.SHORT)
.appendLiteral(" ").appendZoneText(TextStyle.SHORT)
.toFormatter(locale);
return userDate.format(userFormatter);
}

@Override
public String shortLocalizedTimestamp(Instant instant, Locale locale) {
return shortLocalizedTimestamp(instant, getLocalTimeZone(), locale);
}

@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale);
return date.format(df);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package org.sakaiproject.content.impl.serialize.impl.test;

import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
Expand Down Expand Up @@ -216,4 +218,18 @@ public String dateTimeFormatLong(Date date, Locale locale) {
return null;
}

@Override
public String shortLocalizedTimestamp(Instant instant, TimeZone timezone, Locale locale) {
return null;
}

@Override
public String shortLocalizedTimestamp(Instant instant, Locale locale) {
return null;
}

@Override
public String shortLocalizedDate(LocalDate date, Locale locale) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,7 @@ public int getResourceStatsRowCount(
/** Logs an event using EventTrackingService. */
public void logEvent(Object object, String logAction, String siteId, boolean oncePerSession);

/** Get the local sakai name (from ui.service property) */
public String getLocalSakaiName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.sakaiproject.sitestats.api.EventStat;
import org.sakaiproject.sitestats.api.ResourceStat;
import org.sakaiproject.sitestats.api.Stat;
import org.sakaiproject.time.cover.TimeService;


public class Report implements Serializable {
Expand Down Expand Up @@ -59,10 +58,7 @@ public void setReportDefinition(ReportDef reportDef) {
public Date getReportGenerationDate() {
return reportGenerationDate;
}
/** Get the localized date the report was generated. */
public String getLocalizedReportGenerationDate() {
return TimeService.newTime(reportGenerationDate.getTime()).toStringLocalFull();
}

/** Set the localized date the report was generated. */
public void setReportGenerationDate(Date reportGenerationDate) {
this.reportGenerationDate = reportGenerationDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package org.sakaiproject.sitestats.api.report;

import java.io.Serializable;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -65,14 +67,12 @@ public ReportParams(){
public ReportParams(String siteId){
this.siteId = siteId;
whatToolIds.add(ReportManager.WHAT_EVENTS_ALLTOOLS);
whenFrom = new Date();
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, -1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
whenFrom = c.getTime();
whenTo = new Date();
// events are counted against a particular date using the server time zone, so initialize the date
// range based on the current date in that time zone
ZonedDateTime today = ZonedDateTime.now(ZoneId.systemDefault()).truncatedTo(ChronoUnit.DAYS);
ZonedDateTime yesterday = today.minusDays(1);
whenFrom = Date.from(yesterday.toInstant());
whenTo = Date.from(today.toInstant());
}

public ReportParams(String siteId, String what, List<String> whatToolIds, List<String> whatEventIds, String whatResourceAction, List<String> whatResourceIds, String when, Date whenFrom, Date whenTo, String who, String whoRoleId, String whoGroupId, List<String> whoUserIds) {
Expand Down
10 changes: 8 additions & 2 deletions sitestats/sitestats-bundle/src/resources/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ reportres_title=Report
reportres_title_detailed=Report: '${title}'
reportres_summ_description=Description:
reportres_summ_site=Site:
reportres_summ_generatedon=Report date:
reportres_summ_generatedon=Report generated:
reportres_summ_act_basedon=Activity type:
reportres_summ_act_tools_selected=Tools selected:
reportres_summ_act_events_selected=Events selected:
reportres_summ_act_rsrc_action=Resources action:
reportres_summ_act_rsrc_selected=Resources selected:
reportres_summ_timeperiod=Time period:
reportres_summ_timeperiod=Date range:
reportres_summ_usr_selectiontype=User selection type:
reportres_summ_usr_group_selected=Group selected:
reportres_summ_usr_role_selected=Role selected:
Expand Down Expand Up @@ -325,3 +325,9 @@ predefined_report5_description = Show users who have never visited the site.
#
predefined_report6_title = Users with no activity
predefined_report6_description = Show users with no activity in site.

# Dates
lastJobRun_server_time=({0} server time: {1})
widget_server_time_msg=All dates use the {0} server time zone.
report_server_time_zone=({0} server time zone)

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
Expand All @@ -47,6 +50,7 @@ public class StatsAggregateJobImpl implements StatefulJob {
private int sqlBlockSize = 1000;
private long startEventId = -1;
private long lastEventIdInTable = -1;
private String sakaiEventTimeZone = "";

private String driverClassName = null;
private String url = null;
Expand Down Expand Up @@ -281,7 +285,16 @@ private String startJob() throws SQLException {
String sessionId = null;
try{
//If an exception is launched, iteration is not aborted but no event is added to event queue
date = new Date(rs.getTimestamp("EVENT_DATE").getTime());

// Daily events can only be counted relative to a single time zone (server time). The sakai_event table
// may be storing dates in a time zone different than this. Adjust for the sakai_event time zone if provided.
if (StringUtils.isNotBlank(sakaiEventTimeZone)) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(sakaiEventTimeZone));
date = new Date(rs.getTimestamp("EVENT_DATE", calendar).getTime());
} else {
date = new Date(rs.getTimestamp("EVENT_DATE").getTime());
}

event = rs.getString("EVENT");
ref = rs.getString("REF");
sessionUser = rs.getString("SESSION_USER");
Expand Down Expand Up @@ -633,6 +646,14 @@ public void setStartEventId(long startEventId) {
this.startEventId = startEventId;
}

public String getSakaiEventTimeZone() {
return sakaiEventTimeZone;
}

public void setSakaiEventTimeZone(String timeZone) {
sakaiEventTimeZone = timeZone;
}

public String getDriverClassName() {
return driverClassName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3885,6 +3885,14 @@ public void logEvent(Object object, String logAction, String siteId, boolean onc
}
}

/* (non-Javadoc)
* @see org.sakaiproject.sitestats.api.StatsManager#getLocalSakaiName()
*/
@Override
public String getLocalSakaiName() {
return M_scs.getString("ui.service", "Sakai");
}

private void checkForEventContextSupport() {
try{
Event.class.getMethod("getContext", null);
Expand Down
Loading

0 comments on commit 3b21698

Please sign in to comment.