This project has retired. For details please refer to its Attic page.
RepositoryStatistics xref
View Javadoc
1   package org.apache.archiva.metadata.repository.stats;
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.metadata.model.MetadataFacet;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.TimeZone;
29  
30  public class RepositoryStatistics
31      implements MetadataFacet
32  {
33      private Date scanEndTime;
34  
35      private Date scanStartTime;
36  
37      private long totalArtifactCount;
38  
39      private long totalArtifactFileSize;
40  
41      private long totalFileCount;
42  
43      private long totalGroupCount;
44  
45      private long totalProjectCount;
46  
47      private long newFileCount;
48  
49      public static String FACET_ID = "org.apache.archiva.metadata.repository.stats";
50  
51      static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
52  
53      private Map<String, Long> totalCountForType = new ZeroForNullHashMap<String, Long>();
54  
55      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
56  
57      private String repositoryId;
58  
59      public Date getScanEndTime()
60      {
61          return scanEndTime;
62      }
63  
64      public void setScanEndTime( Date scanEndTime )
65      {
66          this.scanEndTime = scanEndTime;
67      }
68  
69      public Date getScanStartTime()
70      {
71          return scanStartTime;
72      }
73  
74      public void setScanStartTime( Date scanStartTime )
75      {
76          this.scanStartTime = scanStartTime;
77      }
78  
79      public long getTotalArtifactCount()
80      {
81          return totalArtifactCount;
82      }
83  
84      public void setTotalArtifactCount( long totalArtifactCount )
85      {
86          this.totalArtifactCount = totalArtifactCount;
87      }
88  
89      public long getTotalArtifactFileSize()
90      {
91          return totalArtifactFileSize;
92      }
93  
94      public void setTotalArtifactFileSize( long totalArtifactFileSize )
95      {
96          this.totalArtifactFileSize = totalArtifactFileSize;
97      }
98  
99      public long getTotalFileCount()
100     {
101         return totalFileCount;
102     }
103 
104     public void setTotalFileCount( long totalFileCount )
105     {
106         this.totalFileCount = totalFileCount;
107     }
108 
109     public long getTotalGroupCount()
110     {
111         return totalGroupCount;
112     }
113 
114     public void setTotalGroupCount( long totalGroupCount )
115     {
116         this.totalGroupCount = totalGroupCount;
117     }
118 
119     public long getTotalProjectCount()
120     {
121         return totalProjectCount;
122     }
123 
124     public void setTotalProjectCount( long totalProjectCount )
125     {
126         this.totalProjectCount = totalProjectCount;
127     }
128 
129     public void setNewFileCount( long newFileCount )
130     {
131         this.newFileCount = newFileCount;
132     }
133 
134     public long getNewFileCount()
135     {
136         return newFileCount;
137     }
138 
139     public long getDuration()
140     {
141         return scanEndTime.getTime() - scanStartTime.getTime();
142     }
143 
144     public String getRepositoryId()
145     {
146         return repositoryId;
147     }
148 
149     public void setRepositoryId( String repositoryId )
150     {
151         this.repositoryId = repositoryId;
152     }
153 
154     @Override
155     public String getFacetId()
156     {
157         return FACET_ID;
158     }
159 
160     @Override
161     public String getName()
162     {
163         return createNameFormat().format( scanStartTime );
164     }
165 
166     private static SimpleDateFormat createNameFormat()
167     {
168         SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
169         fmt.setTimeZone( UTC_TIME_ZONE );
170         return fmt;
171     }
172 
173     @Override
174     public Map<String, String> toProperties()
175     {
176         Map<String, String> properties = new HashMap<>();
177         properties.put( "scanEndTime", String.valueOf( scanEndTime.getTime() ) );
178         properties.put( "scanStartTime", String.valueOf( scanStartTime.getTime() ) );
179         properties.put( "totalArtifactCount", String.valueOf( totalArtifactCount ) );
180         properties.put( "totalArtifactFileSize", String.valueOf( totalArtifactFileSize ) );
181         properties.put( "totalFileCount", String.valueOf( totalFileCount ) );
182         properties.put( "totalGroupCount", String.valueOf( totalGroupCount ) );
183         properties.put( "totalProjectCount", String.valueOf( totalProjectCount ) );
184         properties.put( "newFileCount", String.valueOf( newFileCount ) );
185         properties.put( "repositoryId", repositoryId );
186         for ( Map.Entry<String, Long> entry : totalCountForType.entrySet() )
187         {
188             properties.put( "count-" + entry.getKey(), String.valueOf( entry.getValue() ) );
189         }
190         return properties;
191     }
192 
193     @Override
194     public void fromProperties( Map<String, String> properties )
195     {
196         scanEndTime = new Date( Long.parseLong( properties.get( "scanEndTime" ) ) );
197         scanStartTime = new Date( Long.parseLong( properties.get( "scanStartTime" ) ) );
198         totalArtifactCount = Long.parseLong( properties.get( "totalArtifactCount" ) );
199         totalArtifactFileSize = Long.parseLong( properties.get( "totalArtifactFileSize" ) );
200         totalFileCount = Long.parseLong( properties.get( "totalFileCount" ) );
201         totalGroupCount = Long.parseLong( properties.get( "totalGroupCount" ) );
202         totalProjectCount = Long.parseLong( properties.get( "totalProjectCount" ) );
203         newFileCount = Long.parseLong( properties.get( "newFileCount" ) );
204         repositoryId = properties.get( "repositoryId" );
205         totalCountForType.clear();
206         for ( Map.Entry<String, String> entry : properties.entrySet() )
207         {
208             if ( entry.getKey().startsWith( "count-" ) )
209             {
210                 totalCountForType.put( entry.getKey().substring( 6 ), Long.valueOf( entry.getValue() ) );
211             }
212         }
213     }
214 
215     @Override
216     public boolean equals( Object o )
217     {
218         if ( this == o )
219         {
220             return true;
221         }
222         if ( o == null || getClass() != o.getClass() )
223         {
224             return false;
225         }
226 
227         RepositoryStatistics that = (RepositoryStatistics) o;
228 
229         if ( newFileCount != that.newFileCount )
230         {
231             return false;
232         }
233         if ( totalArtifactCount != that.totalArtifactCount )
234         {
235             return false;
236         }
237         if ( totalArtifactFileSize != that.totalArtifactFileSize )
238         {
239             return false;
240         }
241         if ( totalFileCount != that.totalFileCount )
242         {
243             return false;
244         }
245         if ( totalGroupCount != that.totalGroupCount )
246         {
247             return false;
248         }
249         if ( totalProjectCount != that.totalProjectCount )
250         {
251             return false;
252         }
253         if ( !scanEndTime.equals( that.scanEndTime ) )
254         {
255             return false;
256         }
257         if ( !scanStartTime.equals( that.scanStartTime ) )
258         {
259             return false;
260         }
261         if ( !totalCountForType.equals( that.totalCountForType ) )
262         {
263             return false;
264         }
265         if ( !repositoryId.equals( that.repositoryId ) )
266         {
267             return false;
268         }
269 
270         return true;
271     }
272 
273     @Override
274     public int hashCode()
275     {
276         int result = scanEndTime.hashCode();
277         result = 31 * result + scanStartTime.hashCode();
278         result = 31 * result + (int) ( totalArtifactCount ^ ( totalArtifactCount >>> 32 ) );
279         result = 31 * result + (int) ( totalArtifactFileSize ^ ( totalArtifactFileSize >>> 32 ) );
280         result = 31 * result + (int) ( totalFileCount ^ ( totalFileCount >>> 32 ) );
281         result = 31 * result + (int) ( totalGroupCount ^ ( totalGroupCount >>> 32 ) );
282         result = 31 * result + (int) ( totalProjectCount ^ ( totalProjectCount >>> 32 ) );
283         result = 31 * result + (int) ( newFileCount ^ ( newFileCount >>> 32 ) );
284         result = 31 * result + totalCountForType.hashCode();
285         result = 31 * result + repositoryId.hashCode();
286         return result;
287     }
288 
289     @Override
290     public String toString()
291     {
292         return "RepositoryStatistics{" + "scanEndTime=" + scanEndTime + ", scanStartTime=" + scanStartTime +
293             ", totalArtifactCount=" + totalArtifactCount + ", totalArtifactFileSize=" + totalArtifactFileSize +
294             ", totalFileCount=" + totalFileCount + ", totalGroupCount=" + totalGroupCount + ", totalProjectCount=" +
295             totalProjectCount + ", newFileCount=" + newFileCount + ", totalCountForType=" + totalCountForType + ", " +
296             "repositoryId=" + repositoryId + '}';
297     }
298 
299     public Map<String, Long> getTotalCountForType()
300     {
301         return totalCountForType;
302     }
303 
304     public long getTotalCountForType( String type )
305     {
306         return totalCountForType.get( type );
307     }
308 
309     public void setTotalCountForType( String type, long count )
310     {
311         totalCountForType.put( type.replaceAll( "-", "_" ).replaceAll( "\\.", "_" ), count );
312     }
313     
314     private static final class ZeroForNullHashMap<K, V extends Long> extends HashMap<K, V>
315     {   
316         @Override
317         public V get(Object key) {
318             V value = super.get( key );
319             
320             return value != null ? value : ( V ) Long.valueOf( 0L );
321         }
322     }
323 }