1 package net.wpitchoune.pnews.servlet;
3 import java.io.IOException;
5 import java.util.logging.Level;
6 import java.util.logging.Logger;
8 import com.google.gson.Gson;
9 import com.google.gson.JsonObject;
10 import com.rometools.rome.io.FeedException;
12 import net.wpitchoune.pnews.Article;
13 import net.wpitchoune.pnews.ArticleStore;
14 import net.wpitchoune.pnews.Category;
15 import net.wpitchoune.pnews.Config;
18 private static final Logger LOG = Logger.getLogger(JSON.class.getName());
20 public static String getStats(ArticleProvider provider, Config config) {
21 JsonObject jstats, jreadcounts, jcategories, jmemory;
23 List<Article> articles;
24 Article[] allArticles;
26 jstats = new JsonObject();
28 jstats.addProperty("articles-count", ArticleStore.singleton.size());
30 jreadcounts = new JsonObject();
31 jstats.add("read-counts", jreadcounts);
33 allArticles = ArticleStore.singleton.getArticles();
34 for (Article a: allArticles)
35 if (a.getReadCount() > 0)
36 jreadcounts.addProperty(a.getLink(), a.getReadCount());
38 jcategories = new JsonObject();
39 jstats.add("categories", jcategories);
41 for (Category cat: config.getCategories())
43 articles = provider.getArticles(cat, null);
44 jcategories.addProperty(cat.getLabel(),
46 } catch (IllegalArgumentException | FeedException | IOException e) {
47 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
50 jmemory = new JsonObject();
51 jstats.add("memory", jmemory);
53 runtime = Runtime.getRuntime();
54 jmemory.addProperty("total", runtime.totalMemory());
55 jmemory.addProperty("max", runtime.maxMemory());
56 jmemory.addProperty("free", runtime.freeMemory());
58 return new Gson().toJson(jstats);