commit efb86141eb58aafc652065faae1f6f870844ad15 Author: Karsten Loesing karsten.loesing@gmx.net Date: Wed Jan 15 14:52:17 2020 +0000
Set default time zone UTC. --- CHANGELOG.md | 1 + .../bwhist/RelayDescriptorDatabaseImporter.java | 13 ++++------- .../torproject/metrics/stats/clients/Database.java | 8 ++----- .../org/torproject/metrics/stats/main/Main.java | 2 ++ .../torproject/metrics/stats/onionperf/Main.java | 27 +++++++--------------- .../torproject/metrics/stats/servers/Database.java | 25 +++++++------------- .../torproject/metrics/stats/totalcw/Database.java | 10 +++----- .../torproject/metrics/stats/webstats/Main.java | 6 +---- 8 files changed, 29 insertions(+), 63 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index 57694f1..5731bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Configure a base URL in order to turn ExoneraTor's permanent links into https:// links. - Set default locale `US` at the beginning of the execution. + - Set default time zone `UTC` at the beginning of the execution.
# Changes in version 1.3.0 - 2019-11-09 diff --git a/src/main/java/org/torproject/metrics/stats/bwhist/RelayDescriptorDatabaseImporter.java b/src/main/java/org/torproject/metrics/stats/bwhist/RelayDescriptorDatabaseImporter.java index 2609188..2958bda 100644 --- a/src/main/java/org/torproject/metrics/stats/bwhist/RelayDescriptorDatabaseImporter.java +++ b/src/main/java/org/torproject/metrics/stats/bwhist/RelayDescriptorDatabaseImporter.java @@ -25,13 +25,11 @@ import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedSet; -import java.util.TimeZone; import java.util.TreeSet;
/** @@ -154,7 +152,6 @@ public final class RelayDescriptorDatabaseImporter {
/* Initialize date format, so that we can format timestamps. */ this.dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - this.dateTimeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); }
private void addDateToScheduledUpdates(long timestamp) @@ -189,11 +186,10 @@ public final class RelayDescriptorDatabaseImporter { if (this.importIntoDatabase) { try { this.addDateToScheduledUpdates(validAfter); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); Timestamp validAfterTimestamp = new Timestamp(validAfter); if (lastCheckedStatusEntries != validAfter) { insertedStatusEntries.clear(); - this.psSs.setTimestamp(1, validAfterTimestamp, cal); + this.psSs.setTimestamp(1, validAfterTimestamp); ResultSet rs = psSs.executeQuery(); while (rs.next()) { String insertedFingerprint = rs.getString(1); @@ -204,11 +200,11 @@ public final class RelayDescriptorDatabaseImporter { } if (!insertedStatusEntries.contains(fingerprint)) { this.psR.clearParameters(); - this.psR.setTimestamp(1, validAfterTimestamp, cal); + this.psR.setTimestamp(1, validAfterTimestamp); this.psR.setString(2, nickname); this.psR.setString(3, fingerprint); this.psR.setString(4, descriptor); - this.psR.setTimestamp(5, new Timestamp(published), cal); + this.psR.setTimestamp(5, new Timestamp(published)); this.psR.setString(6, address); this.psR.setLong(7, orPort); this.psR.setLong(8, dirPort); @@ -625,12 +621,11 @@ public final class RelayDescriptorDatabaseImporter { + "dirwrite"; statistics.add(columns.split(", ")); Statement st = this.conn.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM stats_bandwidth"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[7]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = getBooleanFromResultSet(rs, "isexit"); outputLine[2] = getBooleanFromResultSet(rs, "isguard"); outputLine[3] = getLongFromResultSet(rs, "bwread"); diff --git a/src/main/java/org/torproject/metrics/stats/clients/Database.java b/src/main/java/org/torproject/metrics/stats/clients/Database.java index ac715fd..aa69318 100644 --- a/src/main/java/org/torproject/metrics/stats/clients/Database.java +++ b/src/main/java/org/torproject/metrics/stats/clients/Database.java @@ -12,9 +12,7 @@ import java.sql.Statement; import java.sql.Timestamp; import java.time.Instant; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; -import java.util.TimeZone;
/** Database wrapper to connect to the database, insert data, run the stored * procedure for aggregating data, and query aggregated data as output. */ @@ -94,12 +92,11 @@ class Database implements AutoCloseable { String columns = "date, node, country, transport, version, frac, users"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM estimated"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[7]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("node"); outputLine[2] = rs.getString("country"); outputLine[3] = rs.getString("transport"); @@ -118,12 +115,11 @@ class Database implements AutoCloseable { String columns = "date, node, country, transport, version, frac, low, high"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM combined"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[8]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("node"); outputLine[2] = rs.getString("country"); outputLine[3] = rs.getString("transport"); diff --git a/src/main/java/org/torproject/metrics/stats/main/Main.java b/src/main/java/org/torproject/metrics/stats/main/Main.java index 3a4baf7..6badd96 100644 --- a/src/main/java/org/torproject/metrics/stats/main/Main.java +++ b/src/main/java/org/torproject/metrics/stats/main/Main.java @@ -11,6 +11,7 @@ import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.TimeZone;
public class Main {
@@ -31,6 +32,7 @@ public class Main { log.info("Starting metrics update run.");
Locale.setDefault(Locale.US); + TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
File[] outputDirs = new File[] { modulesDir, statsDir }; for (File outputDir : outputDirs) { diff --git a/src/main/java/org/torproject/metrics/stats/onionperf/Main.java b/src/main/java/org/torproject/metrics/stats/onionperf/Main.java index c8e3a90..4325c01 100644 --- a/src/main/java/org/torproject/metrics/stats/onionperf/Main.java +++ b/src/main/java/org/torproject/metrics/stats/onionperf/Main.java @@ -24,11 +24,9 @@ import java.sql.Types; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.TimeZone;
public class Main {
@@ -96,7 +94,6 @@ public class Main { "INSERT INTO buildtimes (measurement_id, position, buildtime, delta) " + "VALUES (?, ?, ?, ?)");
- Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); DescriptorReader dr = DescriptorSourceFactory.createDescriptorReader(); for (Descriptor d : dr.readDescriptors( new File(org.torproject.metrics.stats.main.Main.descriptorsDir, @@ -113,7 +110,7 @@ public class Main { psMeasurementsSelect.setString(1, truncatedSource); psMeasurementsSelect.setInt(2, tr.getFileSize()); psMeasurementsSelect.setTimestamp(3, - new Timestamp(tr.getStartMillis()), calendar); + new Timestamp(tr.getStartMillis())); try (ResultSet rs = psMeasurementsSelect.executeQuery()) { if (rs.next()) { measurementId = rs.getInt(1); @@ -124,7 +121,7 @@ public class Main { psMeasurementsInsert.setString(1, truncatedSource); psMeasurementsInsert.setInt(2, tr.getFileSize()); psMeasurementsInsert.setTimestamp(3, - new Timestamp(tr.getStartMillis()), calendar); + new Timestamp(tr.getStartMillis())); long[] timestamps = new long[] { tr.getSocketMillis(), tr.getConnectMillis(), tr.getNegotiateMillis(), tr.getRequestMillis(), tr.getResponseMillis(), @@ -158,13 +155,13 @@ public class Main { psMeasurementsInsert.setNull(26, Types.TIMESTAMP); } else { psMeasurementsInsert.setTimestamp(26, - new Timestamp(tr.getLaunchMillis()), calendar); + new Timestamp(tr.getLaunchMillis())); } if (tr.getUsedAtMillis() < 0L) { psMeasurementsInsert.setNull(27, Types.TIMESTAMP); } else { psMeasurementsInsert.setTimestamp(27, - new Timestamp(tr.getUsedAtMillis()), calendar); + new Timestamp(tr.getUsedAtMillis())); } if (tr.getTimeout() < 0L) { psMeasurementsInsert.setNull(28, Types.INTEGER); @@ -251,12 +248,10 @@ public class Main { String queryString = "SELECT date, filesize, source, server, q1, md, q3, " + "timeouts, failures, requests FROM onionperf"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { statistics.add(String.format("%s,%d,%s,%s,%.0f,%.0f,%.0f,%d,%d,%d", - dateFormat.format(rs.getDate("date", calendar)), + dateFormat.format(rs.getDate("date")), rs.getInt("filesize"), getStringFromResultSet(rs, "source"), getStringFromResultSet(rs, "server"), @@ -280,12 +275,10 @@ public class Main { String queryString = "SELECT date, source, position, q1, md, q3 " + "FROM buildtimes_stats"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { statistics.add(String.format("%s,%s,%d,%d,%d,%d", - dateFormat.format(rs.getDate("date", calendar)), + dateFormat.format(rs.getDate("date")), getStringFromResultSet(rs, "source"), rs.getInt("position"), rs.getInt("q1"), @@ -305,12 +298,10 @@ public class Main { String queryString = "SELECT date, source, server, low, q1, md, q3, high " + "FROM latencies_stats"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { statistics.add(String.format("%s,%s,%s,%d,%d,%d,%d,%d", - dateFormat.format(rs.getDate("date", calendar)), + dateFormat.format(rs.getDate("date")), getStringFromResultSet(rs, "source"), rs.getString("server"), rs.getInt("low"), @@ -332,12 +323,10 @@ public class Main { String queryString = "SELECT date, source, server, low, q1, md, q3, high " + "FROM throughput_stats"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { statistics.add(String.format("%s,%s,%s,%d,%d,%d,%d,%d", - dateFormat.format(rs.getDate("date", calendar)), + dateFormat.format(rs.getDate("date")), getStringFromResultSet(rs, "source"), rs.getString("server"), rs.getInt("low"), diff --git a/src/main/java/org/torproject/metrics/stats/servers/Database.java b/src/main/java/org/torproject/metrics/stats/servers/Database.java index 8e5e31d..834d004 100644 --- a/src/main/java/org/torproject/metrics/stats/servers/Database.java +++ b/src/main/java/org/torproject/metrics/stats/servers/Database.java @@ -14,14 +14,12 @@ import java.sql.Types; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; -import java.util.Calendar; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedSet; -import java.util.TimeZone;
/** Database wrapper to connect to the database, insert data, run the stored * procedure for aggregating data, and query aggregated data as output. */ @@ -266,10 +264,9 @@ class Database implements AutoCloseable { this.psStatusesSelect.clearParameters(); this.psStatusesSelect.setString(1, networkStatus.isRelay ? "relay" : "bridge"); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); this.psStatusesSelect.setTimestamp(2, Timestamp.from(ZonedDateTime.of(networkStatus.timestamp, - ZoneId.of("UTC")).toInstant()), calendar); + ZoneId.of("UTC")).toInstant())); try (ResultSet rs = this.psStatusesSelect.executeQuery()) { if (rs.next()) { if (rs.getBoolean(1)) { @@ -284,7 +281,7 @@ class Database implements AutoCloseable { networkStatus.isRelay ? "relay" : "bridge"); this.psStatusesInsert.setTimestamp(2, Timestamp.from(ZonedDateTime.of(networkStatus.timestamp, - ZoneId.of("UTC")).toInstant()), calendar); + ZoneId.of("UTC")).toInstant())); this.psStatusesInsert.setInt(3, networkStatus.running); if (null != networkStatus.totalConsensusWeight) { this.psStatusesInsert.setFloat(4, networkStatus.totalConsensusWeight); @@ -412,12 +409,11 @@ class Database implements AutoCloseable { + "server_count_sum_avg, advertised_bandwidth_bytes_sum_avg"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM ipv6servers"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[9]; - outputLine[0] = rs.getDate("valid_after_date", calendar) + outputLine[0] = rs.getDate("valid_after_date") .toLocalDate().toString(); outputLine[1] = rs.getString("server"); outputLine[2] = rs.getString("guard_relay"); @@ -440,12 +436,11 @@ class Database implements AutoCloseable { String columns = "date, isexit, isguard, advbw"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM bandwidth_advbw"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[4]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("isexit"); outputLine[2] = rs.getString("isguard"); outputLine[3] = getLongFromResultSet(rs, "advbw"); @@ -461,12 +456,11 @@ class Database implements AutoCloseable { String columns = "date, relays, bridges"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM servers_networksize"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[3]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = getLongFromResultSet(rs, "relays"); outputLine[2] = getLongFromResultSet(rs, "bridges"); statistics.add(outputLine); @@ -481,12 +475,11 @@ class Database implements AutoCloseable { String columns = "date, flag, relays"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM servers_relayflags"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[3]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("flag"); outputLine[2] = getLongFromResultSet(rs, "relays"); statistics.add(outputLine); @@ -501,12 +494,11 @@ class Database implements AutoCloseable { String columns = "date, version, relays"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM servers_versions"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[3]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("version"); outputLine[2] = getLongFromResultSet(rs, "relays"); statistics.add(outputLine); @@ -521,12 +513,11 @@ class Database implements AutoCloseable { String columns = "date, platform, relays"; statistics.add(columns.split(", ")); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + columns + " FROM servers_platforms"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { String[] outputLine = new String[3]; - outputLine[0] = rs.getDate("date", calendar).toLocalDate().toString(); + outputLine[0] = rs.getDate("date").toLocalDate().toString(); outputLine[1] = rs.getString("platform"); outputLine[2] = getLongFromResultSet(rs, "relays"); statistics.add(outputLine); diff --git a/src/main/java/org/torproject/metrics/stats/totalcw/Database.java b/src/main/java/org/torproject/metrics/stats/totalcw/Database.java index 3d51604..74b84d1 100644 --- a/src/main/java/org/torproject/metrics/stats/totalcw/Database.java +++ b/src/main/java/org/torproject/metrics/stats/totalcw/Database.java @@ -15,9 +15,7 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; -import java.util.TimeZone;
/** Database wrapper to connect to the database, insert data, run the stored * procedure for aggregating data, and query aggregated data as output. */ @@ -121,11 +119,10 @@ class Database implements AutoCloseable {
private void insertStatusIfAbsent(LocalDateTime validAfter, Integer authorityId, long[] measuredSums) throws SQLException { - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); this.psVoteSelect.clearParameters(); this.psVoteSelect.setTimestamp(1, Timestamp.from(ZonedDateTime.of(validAfter, - ZoneId.of("UTC")).toInstant()), calendar); + ZoneId.of("UTC")).toInstant())); if (null == authorityId) { this.psVoteSelect.setNull(2, Types.INTEGER); } else { @@ -144,7 +141,7 @@ class Database implements AutoCloseable { this.psVoteInsert.clearParameters(); this.psVoteInsert.setTimestamp(1, Timestamp.from(ZonedDateTime.of(validAfter, - ZoneId.of("UTC")).toInstant()), calendar); + ZoneId.of("UTC")).toInstant())); if (null == authorityId) { this.psVoteInsert.setNull(2, Types.INTEGER); } else { @@ -171,14 +168,13 @@ class Database implements AutoCloseable { Iterable<OutputLine> queryTotalcw() throws SQLException { List<OutputLine> statistics = new ArrayList<>(); Statement st = this.connection.createStatement(); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); String queryString = "SELECT " + OutputLine.columnHeadersDelimitedBy(", ") + " FROM totalcw"; try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { OutputLine outputLine = new OutputLine(); outputLine.validAfterDate = rs.getDate( - OutputLine.Column.VALID_AFTER_DATE.name(), calendar).toLocalDate(); + OutputLine.Column.VALID_AFTER_DATE.name()).toLocalDate(); outputLine.nickname = rs.getString(OutputLine.Column.NICKNAME.name()); outputLine.haveGuardFlag = rs.getBoolean( OutputLine.Column.HAVE_GUARD_FLAG.name()); diff --git a/src/main/java/org/torproject/metrics/stats/webstats/Main.java b/src/main/java/org/torproject/metrics/stats/webstats/Main.java index 17e21e9..bca86c5 100644 --- a/src/main/java/org/torproject/metrics/stats/webstats/Main.java +++ b/src/main/java/org/torproject/metrics/stats/webstats/Main.java @@ -31,11 +31,9 @@ import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; import java.util.Map; import java.util.SortedSet; -import java.util.TimeZone; import java.util.TreeSet;
/** Main class of the webstats module that downloads log files from the server, @@ -272,12 +270,10 @@ public class Main { Statement st = connection.createStatement(); String queryString = "SELECT " + ALL_COLUMNS + " FROM webstats"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); try (ResultSet rs = st.executeQuery(queryString)) { while (rs.next()) { statistics.add(String.format("%s,%s,%s,%s,%s,%s,%d", - dateFormat.format(rs.getDate(LOG_DATE, calendar)), + dateFormat.format(rs.getDate(LOG_DATE)), emptyNull(rs.getString(REQUEST_TYPE)), emptyNull(rs.getString(PLATFORM)), emptyNull(rs.getString(CHANNEL)),