fixed date formatting with CSV Export

This commit is contained in:
anhefti 2022-01-06 16:11:59 +01:00
parent ec4938d57e
commit f0afa473fd
5 changed files with 112 additions and 87 deletions

View file

@ -83,6 +83,7 @@ public final class Constants {
public static final String DYN_HTML_ATTR_OPEN = "%%_"; public static final String DYN_HTML_ATTR_OPEN = "%%_";
public static final String DYN_HTML_ATTR_CLOSE = "_%%"; public static final String DYN_HTML_ATTR_CLOSE = "_%%";
public static final String DEFAULT_DATE_TIME_MILLIS_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final String TIME_ZONE_OFFSET_TAIL_FORMAT = "|ZZ"; public static final String TIME_ZONE_OFFSET_TAIL_FORMAT = "|ZZ";

View file

@ -243,6 +243,10 @@ public final class Utils {
.getMillis()); .getMillis());
} }
public static String formatDate(final DateTime dateTime) {
return dateTime.toString(Constants.DEFAULT_DATE_TIME_MILLIS_FORMAT);
}
public static Long dateTimeStringToTimestamp(final String startTime, final Long defaultValue) { public static Long dateTimeStringToTimestamp(final String startTime, final Long defaultValue) {
return dateTimeStringToTimestamp(startTime) return dateTimeStringToTimestamp(startTime)
.getOr(defaultValue); .getOr(defaultValue);

View file

@ -47,40 +47,40 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
builder builder
.append("Event Type") .append("Event Type")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Message") .append("Message")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Value") .append("Value")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Client Time (UTC)") .append("Client Time (UTC)")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Server Time (UTC)"); .append("Server Time (UTC)");
if (includeConnectionDetails) { if (includeConnectionDetails) {
builder builder
.append(Constants.COMMA) .append(Constants.COMMA)
.append("User Session-ID") .append("User Session-ID")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Client Machine") .append("Client Machine")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Connection Status") .append("Connection Status")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Connection Token"); .append("Connection Token");
} }
if (includeExamDetails) { if (includeExamDetails) {
builder builder
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Exam Name") .append("Exam Name")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Exam Description") .append("Exam Description")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Exam Type") .append("Exam Type")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("Start Time (LMS)") .append("Start Time (LMS)")
.append(Constants.COMMA) .append(Constants.COMMA)
.append("End Time (LMS)"); .append("End Time (LMS)");
} }
builder.append(Constants.CARRIAGE_RETURN); builder.append(Constants.CARRIAGE_RETURN);
@ -114,9 +114,9 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter {
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(eventData.getNumericValue() != null ? eventData.getNumericValue() : ""); builder.append(eventData.getNumericValue() != null ? eventData.getNumericValue() : "");
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(Utils.toDateTimeUTC(eventData.getClientTime())); builder.append(Utils.formatDate(Utils.toDateTimeUTC(eventData.getClientTime())));
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(Utils.toDateTimeUTC(eventData.getServerTime())); builder.append(Utils.formatDate(Utils.toDateTimeUTC(eventData.getServerTime())));
if (connectionData != null) { if (connectionData != null) {
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
@ -137,9 +137,9 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter {
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(examData.getType().name()); builder.append(examData.getType().name());
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(examData.getStartTime()); builder.append(Utils.formatDate(examData.getStartTime()));
builder.append(Constants.COMMA); builder.append(Constants.COMMA);
builder.append(examData.getEndTime()); builder.append(Utils.formatDate(examData.getEndTime()));
} }
builder.append(Constants.CARRIAGE_RETURN); builder.append(Constants.CARRIAGE_RETURN);

View file

@ -293,6 +293,7 @@ public class ExamMonitoringController {
checkPrivileges(institutionId, examId); checkPrivileges(institutionId, examId);
// TODO do this async like registerInstruction
if (connectionToken.contains(Constants.LIST_SEPARATOR)) { if (connectionToken.contains(Constants.LIST_SEPARATOR)) {
final String[] tokens = StringUtils.split(connectionToken, Constants.LIST_SEPARATOR); final String[] tokens = StringUtils.split(connectionToken, Constants.LIST_SEPARATOR);
for (int i = 0; i < tokens.length; i++) { for (int i = 0; i < tokens.length; i++) {

View file

@ -8,122 +8,141 @@
package ch.ethz.seb.sebserver.webservice.servicelayer.exam.impl; package ch.ethz.seb.sebserver.webservice.servicelayer.exam.impl;
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
import ch.ethz.seb.sebserver.gbl.util.Utils;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ClientConnectionRecord;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ClientEventRecord;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
import ch.ethz.seb.sebserver.gbl.model.exam.Exam;
import ch.ethz.seb.sebserver.gbl.util.Utils;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ClientConnectionRecord;
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ClientEventRecord;
public class SEBClientEventCSVExporterTest { public class SEBClientEventCSVExporterTest {
@Test @Test
public void streamHeaderTestWithoutConnectionAndExamDetails() { public void streamHeaderTestWithoutConnectionAndExamDetails() {
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final ByteArrayOutputStream stream = new ByteArrayOutputStream();
BufferedOutputStream output = new BufferedOutputStream(stream); final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamHeader(output, false, false); exporter.streamHeader(output, false, false);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC)\n", string); Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC)\n", string);
} }
@Test @Test
public void streamHeaderTestWithConnectionDetails() { public void streamHeaderTestWithConnectionDetails() {
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final ByteArrayOutputStream stream = new ByteArrayOutputStream();
BufferedOutputStream output = new BufferedOutputStream(stream); final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamHeader(output, true, false); exporter.streamHeader(output, true, false);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC),User Session-ID,Client Machine,Connection Status,Connection Token\n", string); Assert.assertEquals(
"Event Type,Message,Value,Client Time (UTC),Server Time (UTC),User Session-ID,Client Machine,Connection Status,Connection Token\n",
string);
} }
@Test @Test
public void streamHeaderTestWithExamDetails() { public void streamHeaderTestWithExamDetails() {
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final ByteArrayOutputStream stream = new ByteArrayOutputStream();
BufferedOutputStream output = new BufferedOutputStream(stream); final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamHeader(output, false, true); exporter.streamHeader(output, false, true);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC),Exam Name,Exam Description,Exam Type,Start Time (LMS),End Time (LMS)\n", string); Assert.assertEquals(
"Event Type,Message,Value,Client Time (UTC),Server Time (UTC),Exam Name,Exam Description,Exam Type,Start Time (LMS),End Time (LMS)\n",
string);
} }
@Test @Test
public void streamHeaderTestWithConnectionAndExamDetails() { public void streamHeaderTestWithConnectionAndExamDetails() {
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final ByteArrayOutputStream stream = new ByteArrayOutputStream();
BufferedOutputStream output = new BufferedOutputStream(stream); final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamHeader(output, true, true); exporter.streamHeader(output, true, true);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC),User Session-ID,Client Machine,Connection Status,Connection Token,Exam Name,Exam Description,Exam Type,Start Time (LMS),End Time (LMS)\n", string); Assert.assertEquals(
"Event Type,Message,Value,Client Time (UTC),Server Time (UTC),User Session-ID,Client Machine,Connection Status,Connection Token,Exam Name,Exam Description,Exam Type,Start Time (LMS),End Time (LMS)\n",
string);
} }
@Test @Test
public void streamDataTestWithConnection() { public void streamDataTestWithConnection() {
ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); final ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid",
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7);
ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text");
BufferedOutputStream output = new BufferedOutputStream(stream); final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamData(output, event, connection, null); exporter.streamData(output, event, connection, null);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("INFO_LOG,\"text\",5,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.004Z,\"sessionid\",\"clientaddress\",status,token\n", string); Assert.assertEquals(
"INFO_LOG,\"text\",5,1970-01-01T00:00:00.003,1970-01-01T00:00:00.004,\"sessionid\",\"clientaddress\",status,token\n",
string);
} }
@Test @Test
public void streamDataTestWithExam() { public void streamDataTestWithExam() {
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text");
Exam exam = new Exam(0L, 1L, 3L, "externalid", "name", "description", new DateTime(1L), new DateTime(1L), "startURL", Exam.ExamType.BYOD, "owner", new ArrayList<>(), Exam.ExamStatus.RUNNING, false, "bek", true, "lastUpdate", 4L); final Exam exam = new Exam(0L, 1L, 3L, "externalid", "name", "description", new DateTime(1L), new DateTime(1L),
ByteArrayOutputStream stream = new ByteArrayOutputStream(); "startURL", Exam.ExamType.BYOD, "owner", new ArrayList<>(), Exam.ExamStatus.RUNNING, false, "bek", true,
BufferedOutputStream output = new BufferedOutputStream(stream); "lastUpdate", 4L);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamData(output, event, null, exam); exporter.streamData(output, event, null, exam);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("INFO_LOG,\"text\",5,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.004Z,\"name\",\"description\",BYOD,1970-01-01T01:00:00.001+01:00,1970-01-01T01:00:00.001+01:00\n", string); Assert.assertEquals(
"INFO_LOG,\"text\",5,1970-01-01T00:00:00.003,1970-01-01T00:00:00.004,\"name\",\"description\",BYOD,1970-01-01T01:00:00.001,1970-01-01T01:00:00.001\n",
string);
} }
@Test @Test
public void streamDataTestWithConnectionAndExam() { public void streamDataTestWithConnectionAndExam() {
ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); final ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid",
SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7);
ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter();
Exam exam = new Exam(0L, 1L, 3L, "externalid", "name", "description", new DateTime(1L), new DateTime(1L), "startURL", Exam.ExamType.BYOD, "owner", new ArrayList<>(), Exam.ExamStatus.RUNNING, false, "bek", true, "lastUpdate", 4L); final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text");
ByteArrayOutputStream stream = new ByteArrayOutputStream(); final Exam exam = new Exam(0L, 1L, 3L, "externalid", "name", "description", new DateTime(1L), new DateTime(1L),
BufferedOutputStream output = new BufferedOutputStream(stream); "startURL", Exam.ExamType.BYOD, "owner", new ArrayList<>(), Exam.ExamStatus.RUNNING, false, "bek", true,
"lastUpdate", 4L);
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final BufferedOutputStream output = new BufferedOutputStream(stream);
exporter.streamData(output, event, connection, exam); exporter.streamData(output, event, connection, exam);
byte[] array = stream.toByteArray(); final byte[] array = stream.toByteArray();
String string = Utils.toString(array); final String string = Utils.toString(array);
Assert.assertEquals("INFO_LOG,\"text\",5,1970-01-01T00:00:00.003Z,1970-01-01T00:00:00.004Z,\"sessionid\",\"clientaddress\",status,token,\"name\",\"description\",BYOD,1970-01-01T01:00:00.001+01:00,1970-01-01T01:00:00.001+01:00\n", string); Assert.assertEquals(
"INFO_LOG,\"text\",5,1970-01-01T00:00:00.003,1970-01-01T00:00:00.004,\"sessionid\",\"clientaddress\",status,token,\"name\",\"description\",BYOD,1970-01-01T01:00:00.001,1970-01-01T01:00:00.001\n",
string);
} }
} }