3 import java.util.logging.Level;
4 import java.util.logging.Logger;
6 import com.google.gson.Gson;
7 import com.google.gson.JsonObject;
10 import pnews.Category;
13 private static final Logger LOG = Logger.getLogger(JSON.class.getName());
15 public static String getStats(Category[] cats) {
16 JsonObject jstats, jreadcounts, jcategories, jmemory;
20 jstats = new JsonObject();
22 jstats.addProperty("articles-count", ArticleStore.singleton.size());
24 jreadcounts = new JsonObject();
25 jstats.add("read-counts", jreadcounts);
27 articles = ArticleStore.singleton.getArticles();
28 for (Article a: articles)
29 if (a.readCount.get() > 0)
30 jreadcounts.addProperty(a.link, a.readCount);
32 jcategories = new JsonObject();
33 jstats.add("categories", jcategories);
35 for (Category cat: cats)
37 jcategories.addProperty(cat.getName(),
39 } catch (IllegalArgumentException e) {
40 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
43 jmemory = new JsonObject();
44 jstats.add("memory", jmemory);
46 runtime = Runtime.getRuntime();
47 jmemory.addProperty("total", runtime.totalMemory());
48 jmemory.addProperty("max", runtime.maxMemory());
49 jmemory.addProperty("free", runtime.freeMemory());
51 return new Gson().toJson(jstats);