From f0afa473fd66af0c57bbaa2bff347ca737eeb21c Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 6 Jan 2022 16:11:59 +0100 Subject: [PATCH] fixed date formatting with CSV Export --- .../ch/ethz/seb/sebserver/gbl/Constants.java | 1 + .../ch/ethz/seb/sebserver/gbl/util/Utils.java | 4 + .../exam/impl/SEBClientEventCSVExporter.java | 62 ++++----- .../api/ExamMonitoringController.java | 1 + .../impl/SEBClientEventCSVExporterTest.java | 131 ++++++++++-------- 5 files changed, 112 insertions(+), 87 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java index 1c5c958c..5320c4b2 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java @@ -83,6 +83,7 @@ public final class Constants { public static final String DYN_HTML_ATTR_OPEN = "%%_"; 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 TIME_ZONE_OFFSET_TAIL_FORMAT = "|ZZ"; diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java index db64032e..0061aeaf 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java @@ -243,6 +243,10 @@ public final class Utils { .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) { return dateTimeStringToTimestamp(startTime) .getOr(defaultValue); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporter.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporter.java index a13d75b5..fecf7049 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporter.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporter.java @@ -47,40 +47,40 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter { final StringBuilder builder = new StringBuilder(); builder - .append("Event Type") - .append(Constants.COMMA) - .append("Message") - .append(Constants.COMMA) - .append("Value") - .append(Constants.COMMA) - .append("Client Time (UTC)") - .append(Constants.COMMA) - .append("Server Time (UTC)"); + .append("Event Type") + .append(Constants.COMMA) + .append("Message") + .append(Constants.COMMA) + .append("Value") + .append(Constants.COMMA) + .append("Client Time (UTC)") + .append(Constants.COMMA) + .append("Server Time (UTC)"); if (includeConnectionDetails) { builder - .append(Constants.COMMA) - .append("User Session-ID") - .append(Constants.COMMA) - .append("Client Machine") - .append(Constants.COMMA) - .append("Connection Status") - .append(Constants.COMMA) - .append("Connection Token"); + .append(Constants.COMMA) + .append("User Session-ID") + .append(Constants.COMMA) + .append("Client Machine") + .append(Constants.COMMA) + .append("Connection Status") + .append(Constants.COMMA) + .append("Connection Token"); } if (includeExamDetails) { builder - .append(Constants.COMMA) - .append("Exam Name") - .append(Constants.COMMA) - .append("Exam Description") - .append(Constants.COMMA) - .append("Exam Type") - .append(Constants.COMMA) - .append("Start Time (LMS)") - .append(Constants.COMMA) - .append("End Time (LMS)"); + .append(Constants.COMMA) + .append("Exam Name") + .append(Constants.COMMA) + .append("Exam Description") + .append(Constants.COMMA) + .append("Exam Type") + .append(Constants.COMMA) + .append("Start Time (LMS)") + .append(Constants.COMMA) + .append("End Time (LMS)"); } builder.append(Constants.CARRIAGE_RETURN); @@ -114,9 +114,9 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter { builder.append(Constants.COMMA); builder.append(eventData.getNumericValue() != null ? eventData.getNumericValue() : ""); builder.append(Constants.COMMA); - builder.append(Utils.toDateTimeUTC(eventData.getClientTime())); + builder.append(Utils.formatDate(Utils.toDateTimeUTC(eventData.getClientTime()))); builder.append(Constants.COMMA); - builder.append(Utils.toDateTimeUTC(eventData.getServerTime())); + builder.append(Utils.formatDate(Utils.toDateTimeUTC(eventData.getServerTime()))); if (connectionData != null) { builder.append(Constants.COMMA); @@ -137,9 +137,9 @@ public class SEBClientEventCSVExporter implements SEBClientEventExporter { builder.append(Constants.COMMA); builder.append(examData.getType().name()); builder.append(Constants.COMMA); - builder.append(examData.getStartTime()); + builder.append(Utils.formatDate(examData.getStartTime())); builder.append(Constants.COMMA); - builder.append(examData.getEndTime()); + builder.append(Utils.formatDate(examData.getEndTime())); } builder.append(Constants.CARRIAGE_RETURN); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java index 072c65a8..15203300 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java @@ -293,6 +293,7 @@ public class ExamMonitoringController { checkPrivileges(institutionId, examId); + // TODO do this async like registerInstruction if (connectionToken.contains(Constants.LIST_SEPARATOR)) { final String[] tokens = StringUtils.split(connectionToken, Constants.LIST_SEPARATOR); for (int i = 0; i < tokens.length; i++) { diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporterTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporterTest.java index 5bb5f878..2797d490 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporterTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/SEBClientEventCSVExporterTest.java @@ -8,122 +8,141 @@ 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.ByteArrayOutputStream; import java.math.BigDecimal; 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 { @Test public void streamHeaderTestWithoutConnectionAndExamDetails() { - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamHeader(output, false, false); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + final String string = Utils.toString(array); Assert.assertEquals("Event Type,Message,Value,Client Time (UTC),Server Time (UTC)\n", string); } @Test public void streamHeaderTestWithConnectionDetails() { - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamHeader(output, true, false); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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 public void streamHeaderTestWithExamDetails() { - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamHeader(output, false, true); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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 public void streamHeaderTestWithConnectionAndExamDetails() { - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamHeader(output, true, true); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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 public void streamDataTestWithConnection() { - ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", + "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamData(output, event, connection, null); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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 public void streamDataTestWithExam() { - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - 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); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); + final 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 ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamData(output, event, null, exam); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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 public void streamDataTestWithConnectionAndExam() { - ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); - SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); - 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); - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - BufferedOutputStream output = new BufferedOutputStream(stream); + final ClientConnectionRecord connection = new ClientConnectionRecord(0L, 1L, 2L, "status", "token", "sessionid", + "clientaddress", "virtualaddress", 3, "vdi", 4L, 5L, 6L, 7); + final SEBClientEventCSVExporter exporter = new SEBClientEventCSVExporter(); + final ClientEventRecord event = new ClientEventRecord(0L, 1L, 2, 3L, 4L, new BigDecimal(5), "text"); + final 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 ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final BufferedOutputStream output = new BufferedOutputStream(stream); exporter.streamData(output, event, connection, exam); - byte[] array = stream.toByteArray(); - String string = Utils.toString(array); + final byte[] array = stream.toByteArray(); + 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); } }