1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.activeinstalls.internal.client.data; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.activeinstalls.internal.JestClientManager; |
33 |
|
import org.xwiki.activeinstalls.internal.client.PingDataProvider; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.instance.InstanceIdManager; |
36 |
|
|
37 |
|
import io.searchbox.client.JestResult; |
38 |
|
import io.searchbox.core.Search; |
39 |
|
import io.searchbox.params.SearchType; |
40 |
|
import net.sf.json.JSONObject; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
50 |
|
@Component |
51 |
|
@Named("date") |
52 |
|
@Singleton |
|
|
| 89.7% |
Uncovered Elements: 4 (39) |
Complexity: 7 |
Complexity Density: 0.22 |
|
53 |
|
public class DatePingDataProvider implements PingDataProvider |
54 |
|
{ |
55 |
|
private static final String PROPERTY_FIRST_PING_DATE = "firstPingDate"; |
56 |
|
|
57 |
|
private static final String PROPERTY_SINCE_DAYS = "sinceDays"; |
58 |
|
|
59 |
|
private static final String PROPERTY_SERVER_TIME = "serverTime"; |
60 |
|
|
61 |
|
private static final String PROPERTY_VALUE = "value"; |
62 |
|
|
63 |
|
private static final String PROPERTY_TYPE = "type"; |
64 |
|
|
65 |
|
private static final String PROPERTY_MIN = "min"; |
66 |
|
|
67 |
|
private static final String ERROR_MESSAGE = "Failed to compute the first ping date and the number of elapsed days " |
68 |
|
+ "since the first ping. This information has not been added to the Active Installs ping data. Reason [{}]"; |
69 |
|
|
70 |
|
@Inject |
71 |
|
private JestClientManager jestClientManager; |
72 |
|
|
73 |
|
@Inject |
74 |
|
private InstanceIdManager instanceIdManager; |
75 |
|
|
76 |
|
@Inject |
77 |
|
private Logger logger; |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
79 |
2 |
@Override... |
80 |
|
public Map<String, Object> provideMapping() |
81 |
|
{ |
82 |
2 |
Map<String, Object> propertiesMap = new HashMap<>(); |
83 |
2 |
propertiesMap.put(PROPERTY_FIRST_PING_DATE, Collections.singletonMap(PROPERTY_TYPE, "date")); |
84 |
2 |
propertiesMap.put(PROPERTY_SINCE_DAYS, Collections.singletonMap(PROPERTY_TYPE, "long")); |
85 |
2 |
return propertiesMap; |
86 |
|
} |
87 |
|
|
|
|
| 84% |
Uncovered Elements: 4 (25) |
Complexity: 5 |
Complexity Density: 0.24 |
|
88 |
2 |
@Override... |
89 |
|
public Map<String, Object> provideData() |
90 |
|
{ |
91 |
2 |
Map<String, Object> jsonMap = new HashMap<>(); |
92 |
2 |
try { |
93 |
2 |
String instanceId = this.instanceIdManager.getInstanceId().toString(); |
94 |
2 |
Search search = new Search.Builder(constructSearchJSON(instanceId)) |
95 |
|
.addIndex(JestClientManager.INDEX) |
96 |
|
.addType(JestClientManager.TYPE) |
97 |
|
.setSearchType(SearchType.COUNT) |
98 |
|
.build(); |
99 |
2 |
JestResult result = this.jestClientManager.getClient().execute(search); |
100 |
|
|
101 |
2 |
if (!result.isSucceeded()) { |
102 |
0 |
this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); |
103 |
0 |
return jsonMap; |
104 |
|
} |
105 |
|
|
106 |
2 |
@SuppressWarnings("unchecked") |
107 |
|
Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
2 |
@SuppressWarnings("unchecked") |
112 |
|
Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); |
113 |
2 |
Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); |
114 |
2 |
@SuppressWarnings("unchecked") |
115 |
|
Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); |
116 |
2 |
Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); |
117 |
|
|
118 |
2 |
if (serverTimeObject != null && firstPingDateObject != null) { |
119 |
1 |
long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); |
120 |
1 |
jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); |
121 |
1 |
long firstPingDate = Math.round((double) firstPingDateObject); |
122 |
1 |
jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); |
123 |
|
} else { |
124 |
|
|
125 |
|
|
126 |
1 |
jsonMap.put(PROPERTY_SINCE_DAYS, 0); |
127 |
|
} |
128 |
|
} catch (Exception e) { |
129 |
|
|
130 |
|
|
131 |
0 |
this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); |
132 |
|
} |
133 |
2 |
return jsonMap; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
136 |
2 |
private String constructSearchJSON(String instanceId)... |
137 |
|
{ |
138 |
2 |
Map<String, Object> jsonMap = new HashMap<>(); |
139 |
|
|
140 |
2 |
jsonMap.put("query", Collections.singletonMap("term", Collections.singletonMap("instanceId", instanceId))); |
141 |
|
|
142 |
2 |
Map<String, Object> aggsMap = new HashMap<>(); |
143 |
2 |
aggsMap.put(PROPERTY_SERVER_TIME, Collections.singletonMap(PROPERTY_MIN, |
144 |
|
Collections.singletonMap("script", "time()"))); |
145 |
2 |
aggsMap.put(PROPERTY_FIRST_PING_DATE, Collections.singletonMap(PROPERTY_MIN, |
146 |
|
Collections.singletonMap("field", "_timestamp"))); |
147 |
|
|
148 |
2 |
jsonMap.put("aggs", aggsMap); |
149 |
|
|
150 |
2 |
return JSONObject.fromObject(jsonMap).toString(); |
151 |
|
} |
152 |
|
} |