BrightSide Workbench Full Report + Source Code
ActivitySummary.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.activity;
20 
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.elephant.db.ElephantPU;
26 import org.turro.jpa.Dao;
27 import org.turro.marker.ElephantMarker;
28 import org.turro.sql.SqlClause;
29 import org.turro.util.Cached;
30 import org.turro.util.Converter;
31 
36 public class ActivitySummary {
37 
38  public static void render(IConstructor constructor) {
39  ElephantMarker marker = new ElephantMarker(constructor);
40  marker.put("activities", activities());
41  marker.process("summary", "activity");
42  }
43 
44  public static Map<String, Long> activities() {
45  return new ActivitySummary().getActivities();
46  }
47 
48  /* Process */
49 
50  private Map<String, Long> getActivities() {
51  Map<String, Long> activities = new TreeMap<>();
52  SqlClause.select("x.reason, count(x)").from("Activity x")
53  .where().in("x.reason", List.of("REASON_SEEN", "Related polls"))
54  .groupBy("x.reason")
55  .dao(dao.get())
56  .forEach(Object[].class, sv -> {
57  activities.put(
58  translate(Converter.STANDARD.convert(sv[0], String.class)),
59  Converter.STANDARD.convert(sv[1], Long.class));
60  });
61  SqlClause.select("x.entityPath, count(x)").from("SystemLog x")
62  .where().in("x.entityPath", List.of("/log/in", "/log/profile"))
63  .groupBy("x.entityPath")
64  .dao(dao.get())
65  .forEach(Object[].class, sv -> {
66  activities.put(
67  translate(Converter.STANDARD.convert(sv[0], String.class)),
68  Converter.STANDARD.convert(sv[1], Long.class));
69  });
70  SqlClause.select("x.comment, count(x)").from("SystemLog x")
71  .where().in("x.comment", List.of("downloaded", "uploaded"))
72  .groupBy("x.comment")
73  .dao(dao.get())
74  .forEach(Object[].class, sv -> {
75  activities.put(
76  translate(Converter.STANDARD.convert(sv[0], String.class)),
77  Converter.STANDARD.convert(sv[1], Long.class));
78  });
79  SqlClause.select("count(distinct x.contactId), count(x)").from("ActivityAssistant x")
80  .dao(dao.get())
81  .forEach(Object[].class, sv -> {
82  activities.put(
83  "Active participants",
84  Converter.STANDARD.convert(sv[0], Long.class));
85  activities.put(
86  "Total participation",
87  Converter.STANDARD.convert(sv[1], Long.class));
88  });
89  return activities;
90  }
91 
92  /* User friendly */
93 
94  private static Map<String, String> friendly = Map.of(
95  "REASON_SEEN", "Visualizations",
96  "Related polls", "Polls",
97  "/log/in", "Logins",
98  "/log/profile", "Profile updates",
99  "uploaded", "Documents uploaded",
100  "downloaded", "Documents downloaded"
101  );
102 
103  private String translate(String label) {
104  return friendly.getOrDefault(label, label);
105  }
106 
107  /* Dao */
108 
109  protected final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
110 
111 }
static Map< String, Long > activities()
static void render(IConstructor constructor)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)