From 4e2f57c6c5a2d60a09b0b47c1a3bdd470a8ea54f Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sat, 21 Oct 2017 00:07:29 +0200 Subject: [PATCH] added new stats --- war/src/main/java/pnews/servlet/JSON.java | 36 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/war/src/main/java/pnews/servlet/JSON.java b/war/src/main/java/pnews/servlet/JSON.java index 5167186..9d331f2 100644 --- a/war/src/main/java/pnews/servlet/JSON.java +++ b/war/src/main/java/pnews/servlet/JSON.java @@ -1,28 +1,54 @@ package pnews.servlet; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + import com.google.gson.Gson; import com.google.gson.JsonObject; +import com.rometools.rome.io.FeedException; import pnews.Article; +import pnews.Category; public class JSON { + private static final Logger LOG = Logger.getLogger(JSON.class.getName()); + public static String getStats() { - JsonObject jarticles, jreadcounts; + JsonObject jstats, jreadcounts, jcategories, jcategory, jmemory; Article[] articles; + Runtime runtime; - jarticles = new JsonObject(); + jstats = new JsonObject(); - jarticles.addProperty("articles-count", ArticleStore.singleton.size()); + jstats.addProperty("articles-count", ArticleStore.singleton.size()); jreadcounts = new JsonObject(); - jarticles.add("read-counts", jreadcounts); + jstats.add("read-counts", jreadcounts); articles = ArticleStore.singleton.getArticles(); for (Article a: articles) if (a.readCount.get() > 0) jreadcounts.addProperty(a.link, a.readCount); - return new Gson().toJson(jarticles); + jcategories = new JsonObject(); + jstats.add("categories", jcategories); + + for (Category cat: Category.values()) + try { + jcategories.addProperty(cat.getId(), ArticleProvider.singleton.getArticles(cat).size()); + } catch (IllegalArgumentException | FeedException | IOException e) { + LOG.log(Level.SEVERE, "Fail to retrieve articles", e); + } + + jmemory = new JsonObject(); + jstats.add("memory", jmemory); + + runtime = Runtime.getRuntime(); + jmemory.addProperty("total", runtime.totalMemory()); + jmemory.addProperty("max", runtime.maxMemory()); + jmemory.addProperty("free", runtime.freeMemory()); + return new Gson().toJson(jstats); } } -- 2.7.4