X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FJSON.java;h=13509ed05b7da9c2b420f8768ba5cb218271cbb6;hb=6f411daf8bfd05580744e3c46a8c1497e8cd7898;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..13509ed 100644 --- a/war/src/main/java/pnews/servlet/JSON.java +++ b/war/src/main/java/pnews/servlet/JSON.java @@ -1,28 +1,58 @@ package pnews.servlet; +import java.io.IOException; +import java.util.List; +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; - Article[] articles; + private static final Logger LOG = Logger.getLogger(JSON.class.getName()); + + public static String getStats(ArticleProvider provider, Config config) { + JsonObject jstats, jreadcounts, jcategories, jmemory; + Runtime runtime; + List
articles; + Article[] allArticles; - 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) + allArticles = ArticleStore.singleton.getArticles(); + for (Article a: allArticles) 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: config.getCategories()) + try { + articles = provider.getArticles(cat); + jcategories.addProperty(cat.getLabel(), + articles.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); } }