This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.repository.scanner;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.repository.ManagedRepository;
023import org.apache.commons.collections4.CollectionUtils;
024
025import javax.xml.bind.annotation.XmlRootElement;
026import java.text.SimpleDateFormat;
027import java.util.Date;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * RepositoryScanStatistics - extension to the RepositoryContentStatistics model.
033 *
034 *
035 */
036@XmlRootElement( name = "repositoryScanStatistics" )
037public class RepositoryScanStatistics
038{
039    private transient List<String> knownConsumers;
040
041    private transient List<String> invalidConsumers;
042
043    private transient long startTimestamp;
044
045    private SimpleDateFormat df = new SimpleDateFormat();
046
047    /**
048     * Field repositoryId
049     */
050    private String repositoryId;
051
052    /**
053     * Field whenGathered
054     */
055    private Date whenGathered;
056
057    /**
058     * Field duration
059     */
060    private long duration = 0;
061
062    /**
063     * Field totalFileCount
064     */
065    private long totalFileCount = 0;
066
067    /**
068     * Field newFileCount
069     */
070    private long newFileCount = 0;
071
072    /**
073     * Field totalSize
074     */
075    private long totalSize = 0;
076
077    private Map<String, Long> consumerCounts;
078
079    private Map<String, Long> consumerTimings;
080
081    public void triggerStart()
082    {
083        startTimestamp = System.currentTimeMillis();
084    }
085
086    public java.util.Date getWhenGathered()
087    {
088        return whenGathered;
089    }
090
091    public void triggerFinished()
092    {
093        long finished = System.currentTimeMillis();
094        this.duration = finished - startTimestamp;
095        this.whenGathered = new java.util.Date( finished );
096    }
097
098    public void increaseFileCount()
099    {
100        this.totalFileCount += 1;
101    }
102
103    public void increaseNewFileCount()
104    {
105        this.newFileCount += 1;
106    }
107
108    public void setKnownConsumers( List<String> consumers )
109    {
110        knownConsumers = consumers;
111    }
112
113    public void setInvalidConsumers( List<String> consumers )
114    {
115        invalidConsumers = consumers;
116    }
117
118    public String toDump( ManagedRepository repo )
119    {
120        StringBuilder buf = new StringBuilder();
121
122        buf.append( "\n.\\ Scan of " ).append( this.getRepositoryId() );
123        buf.append( " \\.__________________________________________" );
124
125        buf.append( "\n  Repository Dir    : " ).append( repo.getLocation() );
126        buf.append( "\n  Repository Name   : " ).append( repo.getName() );
127        buf.append( "\n  Repository Layout : " ).append( repo.getLayout() );
128
129        buf.append( "\n  Known Consumers   : " );
130        if ( CollectionUtils.isNotEmpty( knownConsumers ) )
131        {
132            buf.append( "(" ).append( knownConsumers.size() ).append( " configured)" );
133            for ( String id : knownConsumers )
134            {
135                buf.append( "\n                      " ).append( id );
136                if ( consumerTimings.containsKey( id ) )
137                {
138                    long time = consumerTimings.get( id );
139                    buf.append( " (Total: " ).append( time ).append( "ms" );
140                    if ( consumerCounts.containsKey( id ) )
141                    {
142                        long total = consumerCounts.get( id );
143                        buf.append( "; Avg.: " + ( time / total ) + "; Count: " + total );
144                    }
145                    buf.append( ")" );
146                }
147            }
148        }
149        else
150        {
151            buf.append( "<none>" );
152        }
153
154        buf.append( "\n  Invalid Consumers : " );
155        if ( CollectionUtils.isNotEmpty( invalidConsumers ) )
156        {
157            buf.append( "(" ).append( invalidConsumers.size() ).append( " configured)" );
158            for ( String id : invalidConsumers )
159            {
160                buf.append( "\n                      " ).append( id );
161                if ( consumerTimings.containsKey( id ) )
162                {
163                    long time = consumerTimings.get( id );
164                    buf.append( " (Total: " ).append( time ).append( "ms" );
165                    if ( consumerCounts.containsKey( id ) )
166                    {
167                        long total = consumerCounts.get( id );
168                        buf.append( "; Avg.: " + ( time / total ) + "ms; Count: " + total );
169                    }
170                    buf.append( ")" );
171                }
172            }
173        }
174        else
175        {
176            buf.append( "<none>" );
177        }
178
179        buf.append( "\n  Duration          : " );
180        buf.append( org.apache.archiva.common.utils.DateUtil.getDuration( this.getDuration() ) );
181        buf.append( "\n  When Gathered     : " );
182        if ( this.getWhenGathered() == null )
183        {
184            buf.append( "<null>" );
185        }
186        else
187        {
188            buf.append( df.format( this.getWhenGathered() ) );
189        }
190
191        buf.append( "\n  Total File Count  : " ).append( this.getTotalFileCount() );
192
193        long averageMsPerFile = 0;
194
195        if ( getTotalFileCount() != 0 )
196        {
197            averageMsPerFile = ( this.getDuration() / this.getTotalFileCount() );
198        }
199
200        buf.append( "\n  Avg Time Per File : " );
201        buf.append( org.apache.archiva.common.utils.DateUtil.getDuration( averageMsPerFile ) );
202        buf.append( "\n______________________________________________________________" );
203
204        return buf.toString();
205    }
206
207    public void setRepositoryId( String repositoryId )
208    {
209        this.repositoryId = repositoryId;
210    }
211
212    public String getRepositoryId()
213    {
214        return repositoryId;
215    }
216
217    public long getDuration()
218    {
219        return duration;
220    }
221
222    public long getTotalFileCount()
223    {
224        return totalFileCount;
225    }
226
227    public long getNewFileCount()
228    {
229        return newFileCount;
230    }
231
232    public long getTotalSize()
233    {
234        return totalSize;
235    }
236
237    public void setConsumerCounts( Map<String, Long> consumerCounts )
238    {
239        this.consumerCounts = consumerCounts;
240    }
241
242    public void setConsumerTimings( Map<String, Long> consumerTimings )
243    {
244        this.consumerTimings = consumerTimings;
245    }
246}