001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.admin.repo;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026import java.util.concurrent.ConcurrentHashMap;
027
028import org.nuxeo.common.utils.Path;
029
030/**
031 * Statistics collector class
032 *
033 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
034 */
035public class RepoStatInfo {
036
037    private final Map<String, Long> docsPerTypes;
038
039    private long totalBlobSize = 0;
040
041    private long totalBlobNb = 0;
042
043    private long lastTotalNbDocs = 0;
044
045    private long lastTotalBlobSize = 0;
046
047    private long lastTotalBlobNb = 0;
048
049    private long maxDepth = 0;
050
051    private long maxChildren;
052
053    private long maxBlobSize;
054
055    private long versions = 0;
056
057    private final long t1;
058
059    protected float speed;
060
061    public RepoStatInfo() {
062        docsPerTypes = new ConcurrentHashMap<String, Long>();
063        t1 = System.currentTimeMillis();
064    }
065
066    public String toString() {
067        StringBuilder sb = new StringBuilder();
068
069        sb.append("\nNumber of documents    :");
070        sb.append(getTotalNbDocs());
071
072        for (String dtype : docsPerTypes.keySet()) {
073            sb.append("\n   ");
074            sb.append(dtype);
075            sb.append(" :");
076            sb.append(docsPerTypes.get(dtype));
077        }
078        sb.append("\nNumber of Blobs        :");
079        sb.append(totalBlobNb);
080        sb.append("\nSize of Blobs          :");
081        sb.append(totalBlobSize);
082
083        sb.append("\n");
084        sb.append("\nMax tree depth         :");
085        sb.append(maxDepth);
086        sb.append("\nBiggest Folder         :");
087        sb.append(maxChildren);
088        sb.append(" children");
089        sb.append("\nBiggest Blob           :");
090        sb.append(maxBlobSize);
091
092        return sb.toString();
093    }
094
095    public Long getDocTypeCount(String dType) {
096        return docsPerTypes.get(dType);
097    }
098
099    public List<String> getDocTypes() {
100        List<String> types = new ArrayList<String>();
101        types.addAll(docsPerTypes.keySet());
102        Collections.sort(types);
103        return types;
104    }
105
106    public long getVersions() {
107        return versions;
108    }
109
110    public long getMaxDepth() {
111        return maxDepth;
112    }
113
114    public long getMaxChildren() {
115        return maxChildren;
116    }
117
118    public long getMaxBlobSize() {
119        return maxBlobSize;
120    }
121
122    public Map<String, Long> getDocsPerType() {
123        return docsPerTypes;
124    }
125
126    public float getSpeed() {
127        return speed;
128    }
129
130    public synchronized void addDoc(String type, Path path) {
131        addDoc(type, path, false);
132    }
133
134    public synchronized void addDoc(String type, Path path, boolean isVersion) {
135        Long counter = docsPerTypes.get(type);
136        if (path.segmentCount() > maxDepth) {
137            maxDepth = path.segmentCount();
138        }
139        if (counter == null) {
140            counter = 1L;
141        } else {
142            counter += 1;
143        }
144        docsPerTypes.put(type, counter);
145
146        if (isVersion) {
147            versions += 1;
148        }
149        long t2 = System.currentTimeMillis();
150        long delta = t2 - t1;
151        if (delta == 0)
152            delta = 1;
153        speed = 1000 * getTotalNbDocs() / (float) delta;
154    }
155
156    public synchronized void addBlob(long size, Path path) {
157        totalBlobSize += size;
158        totalBlobNb += 1;
159        if (size > maxBlobSize) {
160            maxBlobSize = size;
161        }
162    }
163
164    public synchronized void childrenCount(long children, Path path) {
165        if (children > maxChildren) {
166            maxChildren = children;
167        }
168    }
169
170    public long getTotalNbDocs() {
171        long total = 0;
172        for (String k : docsPerTypes.keySet()) {
173            total += docsPerTypes.get(k);
174        }
175        lastTotalNbDocs = total;
176        return total;
177    }
178
179    public long getTotalBlobSize() {
180        lastTotalBlobSize = totalBlobSize;
181        return totalBlobSize;
182    }
183
184    public long getTotalBlobNumber() {
185        lastTotalBlobNb = totalBlobNb;
186        return totalBlobNb;
187    }
188
189    public long getLastTotalNbDocs() {
190        return lastTotalNbDocs;
191    }
192
193    public long getLastTotalBlobSize() {
194        return lastTotalBlobSize;
195    }
196
197    public long getLastTotalBlobNumber() {
198        return lastTotalBlobNb;
199    }
200
201}