ImpressionsEntry.java 4.22 KB
/*
 * 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;
    }
}