001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.admin.repo;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027import java.util.Map;
028import java.util.concurrent.ConcurrentHashMap;
029
030import org.nuxeo.common.utils.Path;
031
032/**
033 * Statistics collector class
034 *
035 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
036 */
037public class RepoStatInfo {
038
039    private final Map<String, Long> docsPerTypes;
040
041    private long totalBlobSize = 0;
042
043    private long totalBlobNb = 0;
044
045    private long lastTotalNbDocs = 0;
046
047    private long lastTotalBlobSize = 0;
048
049    private long lastTotalBlobNb = 0;
050
051    private long maxDepth = 0;
052
053    private long maxChildren;
054
055    private long maxBlobSize;
056
057    private long versions = 0;
058
059    private final long t1;
060
061    protected float speed;
062
063    public RepoStatInfo() {
064        docsPerTypes = new ConcurrentHashMap<String, Long>();
065        t1 = System.currentTimeMillis();
066    }
067
068    public String toString() {
069        StringBuilder sb = new StringBuilder();
070
071        sb.append("\nNumber of documents    :");
072        sb.append(getTotalNbDocs());
073
074        for (String dtype : docsPerTypes.keySet()) {
075            sb.append("\n   ");
076            sb.append(dtype);
077            sb.append(" :");
078            sb.append(docsPerTypes.get(dtype));
079        }
080        sb.append("\nNumber of Blobs        :");
081        sb.append(totalBlobNb);
082        sb.append("\nSize of Blobs          :");
083        sb.append(totalBlobSize);
084
085        sb.append("\n");
086        sb.append("\nMax tree depth         :");
087        sb.append(maxDepth);
088        sb.append("\nBiggest Folder         :");
089        sb.append(maxChildren);
090        sb.append(" children");
091        sb.append("\nBiggest Blob           :");
092        sb.append(maxBlobSize);
093
094        return sb.toString();
095    }
096
097    public Long getDocTypeCount(String dType) {
098        return docsPerTypes.get(dType);
099    }
100
101    public List<String> getDocTypes() {
102        List<String> types = new ArrayList<String>();
103        types.addAll(docsPerTypes.keySet());
104        Collections.sort(types);
105        return types;
106    }
107
108    public long getVersions() {
109        return versions;
110    }
111
112    public long getMaxDepth() {
113        return maxDepth;
114    }
115
116    public long getMaxChildren() {
117        return maxChildren;
118    }
119
120    public long getMaxBlobSize() {
121        return maxBlobSize;
122    }
123
124    public Map<String, Long> getDocsPerType() {
125        return docsPerTypes;
126    }
127
128    public float getSpeed() {
129        return speed;
130    }
131
132    public synchronized void addDoc(String type, Path path) {
133        addDoc(type, path, false);
134    }
135
136    public synchronized void addDoc(String type, Path path, boolean isVersion) {
137        Long counter = docsPerTypes.get(type);
138        if (path.segmentCount() > maxDepth) {
139            maxDepth = path.segmentCount();
140        }
141        if (counter == null) {
142            counter = 1L;
143        } else {
144            counter += 1;
145        }
146        docsPerTypes.put(type, counter);
147
148        if (isVersion) {
149            versions += 1;
150        }
151        long t2 = System.currentTimeMillis();
152        long delta = t2 - t1;
153        if (delta == 0)
154            delta = 1;
155        speed = 1000 * getTotalNbDocs() / (float) delta;
156    }
157
158    public synchronized void addBlob(long size, Path path) {
159        totalBlobSize += size;
160        totalBlobNb += 1;
161        if (size > maxBlobSize) {
162            maxBlobSize = size;
163        }
164    }
165
166    public synchronized void childrenCount(long children, Path path) {
167        if (children > maxChildren) {
168            maxChildren = children;
169        }
170    }
171
172    public long getTotalNbDocs() {
173        long total = 0;
174        for (String k : docsPerTypes.keySet()) {
175            total += docsPerTypes.get(k);
176        }
177        lastTotalNbDocs = total;
178        return total;
179    }
180
181    public long getTotalBlobSize() {
182        lastTotalBlobSize = totalBlobSize;
183        return totalBlobSize;
184    }
185
186    public long getTotalBlobNumber() {
187        lastTotalBlobNb = totalBlobNb;
188        return totalBlobNb;
189    }
190
191    public long getLastTotalNbDocs() {
192        return lastTotalNbDocs;
193    }
194
195    public long getLastTotalBlobSize() {
196        return lastTotalBlobSize;
197    }
198
199    public long getLastTotalBlobNumber() {
200        return lastTotalBlobNb;
201    }
202
203}