[tor-commits] [metrics-web/release] Adds generate-news-json Ant task

karsten at torproject.org karsten at torproject.org
Wed May 30 13:45:12 UTC 2018


commit f70408df045bd1343d300647a64bbe9dd71cdde7
Author: Iain R. Learmonth <irl at fsfe.org>
Date:   Tue Mar 20 14:23:01 2018 +0000

    Adds generate-news-json Ant task
    
    This commit updates UpdateNews to use the existing News class and to
    accept the output filename as an argument. It also enables pretty
    printing the output to remove the additional manual step of piping the
    output through jq. (See #23854 comment:9)
    
    The new Ant task requires that the JAR be built prior to running. It
    will update news.json in place.
---
 build.xml                                          | 14 +++++++
 src/main/java/org/torproject/metrics/web/News.java | 45 ++++++++++++++--------
 .../org/torproject/metrics/web/UpdateNews.java     | 19 +++------
 3 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/build.xml b/build.xml
index 59f3720..e98757e 100644
--- a/build.xml
+++ b/build.xml
@@ -235,6 +235,20 @@
     </apply>
   </target>
 
+  <target name="generate-news-json" >
+    <echo message="Running java module UpdateNews ... " />
+    <available file="${dist}/${jarfile}" property="have.jar"/>
+    <fail unless="have.jar" message="Please run 'ant jar' first."/>
+    <java classname="org.torproject.metrics.web.UpdateNews">
+      <arg value="${basedir}/src/main/resources/web/json/news.json" />
+      <classpath>
+        <pathelement location="${dist}/${jarfile}" />
+        <pathelement location="${resources}" />
+      </classpath>
+    </java>
+    <echo message="Java module UpdateNews finished. " />
+  </target>
+
   <!-- This can be adapted to point at the actual work directory. -->
   <property name="prepare.deployment"
             value="/srv/metrics.torproject.org/metrics/work" />
diff --git a/src/main/java/org/torproject/metrics/web/News.java b/src/main/java/org/torproject/metrics/web/News.java
index f21d4e3..ee1b09f 100644
--- a/src/main/java/org/torproject/metrics/web/News.java
+++ b/src/main/java/org/torproject/metrics/web/News.java
@@ -9,21 +9,21 @@ import java.util.TreeMap;
 
 public class News {
 
-  private String start;
+  String start;
 
-  private String end;
+  String end;
 
-  private boolean ongoing;
+  Boolean ongoing;
 
-  private List<String> places;
+  List<String> places;
 
-  private String[] protocols;
+  List<String> protocols;
 
-  private String description;
+  String description;
 
-  private String[] links;
+  List<String> links;
 
-  private boolean unknown;
+  Boolean unknown;
 
   String getStart() {
     return this.start;
@@ -33,8 +33,16 @@ public class News {
     return this.end;
   }
 
+  /**
+   * Returns whether or not the event is ongoing. If no value was set, it is
+   * assumed that the event is not ongoing.
+   */
   boolean getOngoing() {
-    return this.ongoing;
+    if (this.ongoing != null) {
+      return this.ongoing;
+    } else {
+      return false;
+    }
   }
 
   List<String> getPlaces() {
@@ -42,7 +50,7 @@ public class News {
   }
 
   String[] getProtocols() {
-    return this.protocols;
+    return (String[]) this.protocols.toArray();
   }
 
   String getDescription() {
@@ -50,11 +58,19 @@ public class News {
   }
 
   String[] getLinks() {
-    return this.links;
+    return (String[]) this.links.toArray();
   }
 
+  /**
+   * Returns whether or not the reason for an event is known. If no value was
+   * set, it is assumed that the reason is known.
+   */
   boolean isUnknown() {
-    return this.unknown;
+    if (this.unknown != null) {
+      return this.unknown;
+    } else {
+      return false;
+    }
   }
 
   static SortedMap<String, String> countries;
@@ -72,7 +88,7 @@ public class News {
     if (null == this.start) {
       /* Invalid event without start date. */
       sb.append("N/A");
-    } else if (this.ongoing) {
+    } else if (this.getOngoing()) {
       /* Ongoing event. */
       sb.append(this.start).append(" to present");
     } else if (null == this.end || this.start.equals(this.end)) {
@@ -117,7 +133,7 @@ public class News {
         }
       }
     }
-    if (this.unknown) {
+    if (this.isUnknown()) {
       sb.append(" <span class=\"label label-default\">Unknown</span>");
     }
     sb.append("</td><td>");
@@ -144,4 +160,3 @@ public class News {
     return sb.toString();
   }
 }
-
diff --git a/src/main/java/org/torproject/metrics/web/UpdateNews.java b/src/main/java/org/torproject/metrics/web/UpdateNews.java
index 527988f..3e23233 100644
--- a/src/main/java/org/torproject/metrics/web/UpdateNews.java
+++ b/src/main/java/org/torproject/metrics/web/UpdateNews.java
@@ -11,23 +11,15 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class UpdateNews {
-  private static class News {
-    String start;
-    String end;
-    Boolean ongoing;
-    List<String> places;
-    List<String> protocols;
-    String description;
-    List<String> links;
-    Boolean unknown;
-  }
-
   /** Update news. */
   public static void main(String[] args) throws Exception {
     URL textFile = new URL(
         "https://trac.torproject.org/projects/tor/wiki/doc/"
         + "MetricsTimeline?format=txt");
-    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
+    Gson gson = new GsonBuilder()
+        .disableHtmlEscaping()
+        .setPrettyPrinting()
+        .create();
     List<News> news = new ArrayList<>();
     try (BufferedReader br = new BufferedReader(new InputStreamReader(
         textFile.openStream()))) {
@@ -123,8 +115,7 @@ public class UpdateNews {
         news.add(entry);
       }
     }
-    try (FileWriter fw = new FileWriter(
-        "src/main/resources/web/json/news-raw.json")) {
+    try (FileWriter fw = new FileWriter(args[0])) {
       fw.write(gson.toJson(news));
     }
   }





More information about the tor-commits mailing list