X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FJSON.java;h=0a9b8cec523804c3a46e0e8a219b373c17d5d25f;hb=386f46525e32212ac5f3653135a6539c1b2639eb;hp=51671860f28bbe6b556c8bc5e09f164c50f5ea01;hpb=7b8a970adb62ec5d1c5ca07b5bbbb5750f0cdd62;p=pnews.git diff --git a/war/src/main/java/pnews/servlet/JSON.java b/war/src/main/java/pnews/servlet/JSON.java index 5167186..0a9b8ce 100644 --- a/war/src/main/java/pnews/servlet/JSON.java +++ b/war/src/main/java/pnews/servlet/JSON.java @@ -1,28 +1,55 @@ 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 { - public static String getStats() { - JsonObject jarticles, jreadcounts; + private static final Logger LOG = Logger.getLogger(JSON.class.getName()); + + public static String getStats(Category[] cats) { + JsonObject jstats, jreadcounts, jcategories, jmemory; + Runtime runtime; Article[] articles; - 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: cats) + try { + jcategories.addProperty(cat.getName(), + articles.length); + } catch (IllegalArgumentException 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); } }