ImpressionsEntry.java
4.22 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.crx.statistics.Entry
* com.day.crx.statistics.PathBuilder
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFormatException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst;
import com.day.cq.analytics.sitecatalyst.ImpressionsPathBuilder;
import com.day.crx.statistics.Entry;
import com.day.crx.statistics.PathBuilder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Deprecated
public class ImpressionsEntry
extends Entry {
private final Logger log;
public static final String VIEWS = "views";
public static final String ROLLING_WEEK_COUNT = "rollingWeekViews";
public static final String ROLLING_MONTH_COUNT = "rollingMonthViews";
private final String pagePath;
private final long count;
public ImpressionsEntry(String pathPrefix, String pagePath, String date, long count) {
super(pathPrefix);
this.log = LoggerFactory.getLogger(this.getClass());
this.pagePath = pagePath;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
this.setTimestamp(format.parse(date).getTime());
}
catch (ParseException e) {
this.log.error("error while parsing date for impressionsentry", (Throwable)e);
}
this.count = count;
}
protected PathBuilder getPathBuilder() {
return new ImpressionsPathBuilder(this.pagePath);
}
public void write(Node node) throws RepositoryException {
this.log.info("writing impressions node " + node.getPath());
node.setProperty("views", this.count);
Node month = node.getParent();
NodeIterator dayIter = month.getNodes();
long monthCount = 0;
while (dayIter.hasNext()) {
Node tmp = dayIter.nextNode();
if (!tmp.hasProperty("views")) continue;
monthCount += tmp.getProperty("views").getLong();
}
month.setProperty("views", monthCount);
Node year = month.getParent();
NodeIterator monthIter = year.getNodes();
long yearCount = 0;
while (monthIter.hasNext()) {
Node tmp = monthIter.nextNode();
if (!tmp.hasProperty("views")) continue;
yearCount += tmp.getProperty("views").getLong();
}
year.setProperty("views", yearCount);
node.setProperty("rollingWeekViews", this.getCumulativeCount(node, 7, "views"));
node.setProperty("rollingMonthViews", this.getCumulativeCount(node, 30, "views"));
}
private long getCumulativeCount(Node node, int numDays, String propName) throws RepositoryException, ValueFormatException {
long viewCount = 0;
Session session = node.getSession();
PathBuilder builder = this.getPathBuilder();
Calendar date = Calendar.getInstance();
date.setTimeInMillis(this.getTimestamp());
ImpressionsEntry entry = new ImpressionsEntry(this.getPathPrefix(), this.pagePath, "1970-01-01", 0);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < numDays; ++i) {
buffer.setLength(0);
entry.setTimestamp(date.getTimeInMillis());
builder.formatPath((Entry)entry, buffer);
String path = buffer.toString();
try {
Node n;
Item item = session.getItem(path);
if (item.isNode() && (n = (Node)item).hasProperty(propName)) {
viewCount += n.getProperty(propName).getLong();
}
}
catch (PathNotFoundException item) {
// empty catch block
}
date.add(5, -1);
}
return viewCount;
}
}