ReportEntry.java
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.crx.statistics.Entry
* com.day.crx.statistics.PathBuilder
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.impl.importer;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameContext;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameResolver;
import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.analytics.sitecatalyst.impl.util.SitecatalystPageJsonPropWriter;
import com.day.cq.analytics.sitecatalyst.util.SitecatalystJsonItemReader;
import com.day.crx.statistics.Entry;
import com.day.crx.statistics.PathBuilder;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReportEntry
extends Entry {
private final Logger log;
private JSONObject report;
private List<String> elementIds;
private List<String> metricIds;
private int elementsStartIndex;
private Framework framework;
private String pageChildName;
private AnalyticsPageNameContext pageNameContext;
private boolean resolveByPageName;
private AnalyticsPageNameResolver pageNameResolver;
public ReportEntry(String pathPrefix, JSONObject report, ResourceResolver resolver, Framework framework, int elementsStartIndex, String pageChildName, AnalyticsPageNameResolver pageNameResolver, AnalyticsPageNameContext pageNameContext, boolean resolveByPageName) throws JSONException {
super(pathPrefix);
this.log = LoggerFactory.getLogger(this.getClass());
this.elementIds = new ArrayList<String>();
this.metricIds = new ArrayList<String>();
this.framework = framework;
this.log.debug("scvars: " + framework.scVars().toString());
this.resolveByPageName = resolveByPageName;
this.pageNameContext = pageNameContext;
this.pageNameResolver = pageNameResolver;
this.report = this.prepareReport(report, resolver);
this.elementsStartIndex = elementsStartIndex;
this.pageChildName = pageChildName;
this.log.debug("Using elements start index: " + this.elementsStartIndex);
}
public ReportEntry(String pathPrefix, JSONObject report, ResourceResolver resolver, Framework framework, AnalyticsPageNameResolver pageNameResolver, AnalyticsPageNameContext context, boolean resolveByPageName) throws JSONException {
this(pathPrefix, report, resolver, framework, -1, null, pageNameResolver, context, resolveByPageName);
}
private JSONObject prepareReport(JSONObject report, ResourceResolver resolver) throws JSONException {
String id;
int i;
JSONArray elements;
String reportType = report.getString("type");
if ("trended".equals(reportType.toLowerCase()) && (elements = report.getJSONArray("elements")).optJSONObject(0) != null && !"datetime".equals(elements.optJSONObject(0).getString("id"))) {
JSONArray newElements = new JSONArray();
newElements.put((Object)new JSONObject("{\"id\":\"datetime\",\"name\":\"Date\"}"));
for (int i2 = 0; i2 < elements.length(); ++i2) {
newElements.put((Object)elements.getJSONObject(i2));
}
report.put("elements", (Object)newElements);
}
for (i = 0; i < report.getJSONArray("elements").length(); ++i) {
id = report.getJSONArray("elements").getJSONObject(i).getString("id");
if (this.framework.mapsSCVariable(id)) {
this.log.info("mapping element " + id + " to " + this.framework.getMapping(id));
id = this.framework.getMapping(id);
report.getJSONArray("elements").getJSONObject(i).put("mapping", (Object)id);
}
this.elementIds.add(i, id);
}
for (i = 0; i < report.getJSONArray("metrics").length(); ++i) {
id = report.getJSONArray("metrics").getJSONObject(i).getString("id");
if (this.framework.mapsSCVariable(id)) {
this.log.info("mapping metric " + id + " to " + this.framework.getMapping(id));
id = this.framework.getMapping(id);
report.getJSONArray("metrics").getJSONObject(i).put("mapping", (Object)id);
}
this.metricIds.add(i, id);
}
if (report.getJSONArray("elements").length() > 0) {
String elementId = this.elementIds.get(0);
JSONArray dataEntries = report.getJSONArray("data");
JSONArray preparedEntries = new JSONArray();
for (int i3 = 0; i3 < dataEntries.length(); ++i3) {
JSONObject data = dataEntries.getJSONObject(i3);
preparedEntries.put((Object)this.prepareElement(data, 0, resolver));
}
report.put(elementId, (Object)preparedEntries);
report.remove("data");
}
report = this.prepareMetrics(report, "totals");
this.log.info("preparing report type: " + reportType + " #elements: " + report.getJSONArray("elements").length() + " #metrics: " + report.getJSONArray("metrics").length());
return report;
}
private JSONObject prepareElement(JSONObject data, int level, ResourceResolver resolver) throws JSONException {
JSONArray dataEntries;
if (data.has("breakdown") && (dataEntries = data.getJSONArray("breakdown")).length() > 0) {
JSONArray preparedEntries = new JSONArray();
for (int i = 0; i < dataEntries.length(); ++i) {
JSONObject plain = dataEntries.getJSONObject(i);
JSONObject prepared = this.prepareElement(plain, level + 1, resolver);
preparedEntries.put((Object)prepared);
}
data.put(this.elementIds.get(level + 1), (Object)preparedEntries);
data.remove("breakdown");
}
if ("page".equals(this.elementIds.get(level))) {
Resource resource = null;
if (this.resolveByPageName) {
this.pageNameContext.setPageName(data.optString("name"));
resource = this.pageNameResolver.getResource(this.pageNameContext);
} else {
String url = data.optString("url");
try {
URI uri = new URI(url);
resource = resolver.getResource(uri.getPath().replace(".html", ""));
}
catch (Exception e) {
this.log.warn("Error parsing url " + url, (Throwable)e);
}
}
if (resource != null) {
data.put("cq:path", (Object)resource.getPath());
}
}
data = this.prepareMetrics(data, "counts");
return data;
}
private JSONObject prepareMetrics(JSONObject data, String key) throws JSONException {
Object countsObj = data.opt(key);
if (countsObj != null) {
if (countsObj instanceof JSONArray) {
JSONArray counts = (JSONArray)countsObj;
for (int i = 0; i < this.metricIds.size(); ++i) {
long longValue;
double dblValue = counts.optDouble(i);
if (dblValue > (double)(longValue = (long)dblValue)) {
data.put(this.metricIds.get(i), dblValue);
continue;
}
data.put(this.metricIds.get(i), longValue);
}
} else if (countsObj instanceof JSONObject) {
JSONObject counts = (JSONObject)countsObj;
for (int i = 0; i < this.metricIds.size(); ++i) {
long longValue;
double dblValue = counts.optDouble(String.valueOf(i));
if (dblValue > (double)(longValue = (long)dblValue)) {
data.put(this.metricIds.get(i), dblValue);
continue;
}
data.put(this.metricIds.get(i), longValue);
}
}
data.remove(key);
}
return data;
}
protected PathBuilder getPathBuilder() {
return new PathBuilder("yyyy/MM/dd"){
public void formatPath(Entry entry, StringBuffer buffer) {
buffer.append(entry.getPathPrefix());
}
};
}
public void write(Node node) throws RepositoryException {
this.log.info("writing report node " + node.getPath());
String incrementalIndexKey = null;
if (this.elementIds.size() > 0) {
incrementalIndexKey = this.elementIds.get(0);
}
if (StringUtils.isNotEmpty((String)this.pageChildName)) {
SitecatalystPageJsonPropWriter pagePropWriter = new SitecatalystPageJsonPropWriter(node.getSession(), this.pageChildName);
pagePropWriter.writeData(this.report);
} else {
SitecatalystJsonItemReader reader = null;
reader = this.elementsStartIndex >= 0 ? new SitecatalystJsonItemReader(incrementalIndexKey, this.elementsStartIndex) : new SitecatalystJsonItemReader();
try {
reader.readObject(node.getParent(), node.getName(), this.report);
}
catch (Exception e) {
this.log.error("error while storing report", (Throwable)e);
}
}
}
public JSONObject getReport() {
return this.report;
}
}