001    package 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    
022    import org.apache.archiva.admin.model.beans.ManagedRepository;
023    import org.apache.commons.collections.CollectionUtils;
024    
025    import javax.xml.bind.annotation.XmlRootElement;
026    import java.text.SimpleDateFormat;
027    import java.util.Date;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * RepositoryScanStatistics - extension to the RepositoryContentStatistics model.
033     *
034     *
035     */
036    @XmlRootElement( name = "repositoryScanStatistics" )
037    public 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            long count = getTotalFileCount();
101            this.totalFileCount = ++count;
102        }
103    
104        public void increaseNewFileCount()
105        {
106            long count = getNewFileCount();
107            this.newFileCount = ++count;
108        }
109    
110        public void setKnownConsumers( List<String> consumers )
111        {
112            knownConsumers = consumers;
113        }
114    
115        public void setInvalidConsumers( List<String> consumers )
116        {
117            invalidConsumers = consumers;
118        }
119    
120        public String toDump( ManagedRepository repo )
121        {
122            StringBuilder buf = new StringBuilder();
123    
124            buf.append( "\n.\\ Scan of " ).append( this.getRepositoryId() );
125            buf.append( " \\.__________________________________________" );
126    
127            buf.append( "\n  Repository Dir    : " ).append( repo.getLocation() );
128            buf.append( "\n  Repository Name   : " ).append( repo.getName() );
129            buf.append( "\n  Repository Layout : " ).append( repo.getLayout() );
130    
131            buf.append( "\n  Known Consumers   : " );
132            if ( CollectionUtils.isNotEmpty( knownConsumers ) )
133            {
134                buf.append( "(" ).append( knownConsumers.size() ).append( " configured)" );
135                for ( String id : knownConsumers )
136                {
137                    buf.append( "\n                      " ).append( id );
138                    if ( consumerTimings.containsKey( id ) )
139                    {
140                        long time = consumerTimings.get( id );
141                        buf.append( " (Total: " ).append( time ).append( "ms" );
142                        if ( consumerCounts.containsKey( id ) )
143                        {
144                            long total = consumerCounts.get( id );
145                            buf.append( "; Avg.: " + ( time / total ) + "; Count: " + total );
146                        }
147                        buf.append( ")" );
148                    }
149                }
150            }
151            else
152            {
153                buf.append( "<none>" );
154            }
155    
156            buf.append( "\n  Invalid Consumers : " );
157            if ( CollectionUtils.isNotEmpty( invalidConsumers ) )
158            {
159                buf.append( "(" ).append( invalidConsumers.size() ).append( " configured)" );
160                for ( String id : invalidConsumers )
161                {
162                    buf.append( "\n                      " ).append( id );
163                    if ( consumerTimings.containsKey( id ) )
164                    {
165                        long time = consumerTimings.get( id );
166                        buf.append( " (Total: " ).append( time ).append( "ms" );
167                        if ( consumerCounts.containsKey( id ) )
168                        {
169                            long total = consumerCounts.get( id );
170                            buf.append( "; Avg.: " + ( time / total ) + "ms; Count: " + total );
171                        }
172                        buf.append( ")" );
173                    }
174                }
175            }
176            else
177            {
178                buf.append( "<none>" );
179            }
180    
181            buf.append( "\n  Duration          : " );
182            buf.append( org.apache.archiva.common.utils.DateUtil.getDuration( this.getDuration() ) );
183            buf.append( "\n  When Gathered     : " );
184            if ( this.getWhenGathered() == null )
185            {
186                buf.append( "<null>" );
187            }
188            else
189            {
190                buf.append( df.format( this.getWhenGathered() ) );
191            }
192    
193            buf.append( "\n  Total File Count  : " ).append( this.getTotalFileCount() );
194    
195            long averageMsPerFile = 0;
196    
197            if ( getTotalFileCount() != 0 )
198            {
199                averageMsPerFile = ( this.getDuration() / this.getTotalFileCount() );
200            }
201    
202            buf.append( "\n  Avg Time Per File : " );
203            buf.append( org.apache.archiva.common.utils.DateUtil.getDuration( averageMsPerFile ) );
204            buf.append( "\n______________________________________________________________" );
205    
206            return buf.toString();
207        }
208    
209        public void setRepositoryId( String repositoryId )
210        {
211            this.repositoryId = repositoryId;
212        }
213    
214        public String getRepositoryId()
215        {
216            return repositoryId;
217        }
218    
219        public long getDuration()
220        {
221            return duration;
222        }
223    
224        public long getTotalFileCount()
225        {
226            return totalFileCount;
227        }
228    
229        public long getNewFileCount()
230        {
231            return newFileCount;
232        }
233    
234        public long getTotalSize()
235        {
236            return totalSize;
237        }
238    
239        public void setConsumerCounts( Map<String, Long> consumerCounts )
240        {
241            this.consumerCounts = consumerCounts;
242        }
243    
244        public void setConsumerTimings( Map<String, Long> consumerTimings )
245        {
246            this.consumerTimings = consumerTimings;
247        }
248    }