This project has retired. For details please refer to its Attic page.
RepositoryScanStatistics xref
View Javadoc
1   package org.apache.archiva.repository.scanner;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.repository.ManagedRepository;
23  import org.apache.commons.collections4.CollectionUtils;
24  
25  import javax.xml.bind.annotation.XmlRootElement;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * RepositoryScanStatistics - extension to the RepositoryContentStatistics model.
33   *
34   *
35   */
36  @XmlRootElement( name = "repositoryScanStatistics" )
37  public class RepositoryScanStatistics
38  {
39      private transient List<String> knownConsumers;
40  
41      private transient List<String> invalidConsumers;
42  
43      private transient long startTimestamp;
44  
45      private SimpleDateFormat df = new SimpleDateFormat();
46  
47      /**
48       * Field repositoryId
49       */
50      private String repositoryId;
51  
52      /**
53       * Field whenGathered
54       */
55      private Date whenGathered;
56  
57      /**
58       * Field duration
59       */
60      private long duration = 0;
61  
62      /**
63       * Field totalFileCount
64       */
65      private long totalFileCount = 0;
66  
67      /**
68       * Field newFileCount
69       */
70      private long newFileCount = 0;
71  
72      /**
73       * Field totalSize
74       */
75      private long totalSize = 0;
76  
77      private Map<String, Long> consumerCounts;
78  
79      private Map<String, Long> consumerTimings;
80  
81      public void triggerStart()
82      {
83          startTimestamp = System.currentTimeMillis();
84      }
85  
86      public java.util.Date getWhenGathered()
87      {
88          return whenGathered;
89      }
90  
91      public void triggerFinished()
92      {
93          long finished = System.currentTimeMillis();
94          this.duration = finished - startTimestamp;
95          this.whenGathered = new java.util.Date( finished );
96      }
97  
98      public void increaseFileCount()
99      {
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 }