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;
13 import pnews.Category;
16 private static final Logger LOG = Logger.getLogger(JSON.class.getName());
18 public static String getStats(ArticleProvider provider, Config config) {
19 JsonObject jstats, jreadcounts, jcategories, jmemory;
21 List<Article> articles;
22 Article[] allArticles;
24 jstats = new JsonObject();
26 jstats.addProperty("articles-count", ArticleStore.singleton.size());
28 jreadcounts = new JsonObject();
29 jstats.add("read-counts", jreadcounts);
31 allArticles = ArticleStore.singleton.getArticles();
32 for (Article a: allArticles)
33 if (a.readCount.get() > 0)
34 jreadcounts.addProperty(a.link, a.readCount);
36 jcategories = new JsonObject();
37 jstats.add("categories", jcategories);
39 for (Category cat: config.getCategories())
41 articles = provider.getArticles(cat, null);
42 jcategories.addProperty(cat.getLabel(),
44 } catch (IllegalArgumentException | FeedException | IOException e) {
45 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
48 jmemory = new JsonObject();
49 jstats.add("memory", jmemory);
51 runtime = Runtime.getRuntime();
52 jmemory.addProperty("total", runtime.totalMemory());
53 jmemory.addProperty("max", runtime.maxMemory());
54 jmemory.addProperty("free", runtime.freeMemory());
56 return new Gson().toJson(jstats);