1 package org.apache.archiva.metadata.repository.stats;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.metadata.model.MetadataFacet;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.HashMap;
28 import java.util.TimeZone;
30 public class RepositoryStatistics
31 implements MetadataFacet
33 private Date scanEndTime;
35 private Date scanStartTime;
37 private long totalArtifactCount;
39 private long totalArtifactFileSize;
41 private long totalFileCount;
43 private long totalGroupCount;
45 private long totalProjectCount;
47 private long newFileCount;
49 public static String FACET_ID = "org.apache.archiva.metadata.repository.stats";
51 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
53 private Map<String, Long> totalCountForType = new ZeroForNullHashMap<String, Long>();
55 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
57 private String repositoryId;
59 public Date getScanEndTime()
64 public void setScanEndTime( Date scanEndTime )
66 this.scanEndTime = scanEndTime;
69 public Date getScanStartTime()
74 public void setScanStartTime( Date scanStartTime )
76 this.scanStartTime = scanStartTime;
79 public long getTotalArtifactCount()
81 return totalArtifactCount;
84 public void setTotalArtifactCount( long totalArtifactCount )
86 this.totalArtifactCount = totalArtifactCount;
89 public long getTotalArtifactFileSize()
91 return totalArtifactFileSize;
94 public void setTotalArtifactFileSize( long totalArtifactFileSize )
96 this.totalArtifactFileSize = totalArtifactFileSize;
99 public long getTotalFileCount()
101 return totalFileCount;
104 public void setTotalFileCount( long totalFileCount )
106 this.totalFileCount = totalFileCount;
109 public long getTotalGroupCount()
111 return totalGroupCount;
114 public void setTotalGroupCount( long totalGroupCount )
116 this.totalGroupCount = totalGroupCount;
119 public long getTotalProjectCount()
121 return totalProjectCount;
124 public void setTotalProjectCount( long totalProjectCount )
126 this.totalProjectCount = totalProjectCount;
129 public void setNewFileCount( long newFileCount )
131 this.newFileCount = newFileCount;
134 public long getNewFileCount()
139 public long getDuration()
141 return scanEndTime.getTime() - scanStartTime.getTime();
144 public String getRepositoryId()
149 public void setRepositoryId( String repositoryId )
151 this.repositoryId = repositoryId;
155 public String getFacetId()
161 public String getName()
163 return createNameFormat().format( scanStartTime );
166 private static SimpleDateFormat createNameFormat()
168 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
169 fmt.setTimeZone( UTC_TIME_ZONE );
174 public Map<String, String> toProperties()
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() )
188 properties.put( "count-" + entry.getKey(), String.valueOf( entry.getValue() ) );
194 public void fromProperties( Map<String, String> properties )
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() )
208 if ( entry.getKey().startsWith( "count-" ) )
210 totalCountForType.put( entry.getKey().substring( 6 ), Long.valueOf( entry.getValue() ) );
216 public boolean equals( Object o )
222 if ( o == null || getClass() != o.getClass() )
227 RepositoryStatistics that = (RepositoryStatistics) o;
229 if ( newFileCount != that.newFileCount )
233 if ( totalArtifactCount != that.totalArtifactCount )
237 if ( totalArtifactFileSize != that.totalArtifactFileSize )
241 if ( totalFileCount != that.totalFileCount )
245 if ( totalGroupCount != that.totalGroupCount )
249 if ( totalProjectCount != that.totalProjectCount )
253 if ( !scanEndTime.equals( that.scanEndTime ) )
257 if ( !scanStartTime.equals( that.scanStartTime ) )
261 if ( !totalCountForType.equals( that.totalCountForType ) )
265 if ( !repositoryId.equals( that.repositoryId ) )
274 public int hashCode()
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();
290 public String toString()
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 + '}';
299 public Map<String, Long> getTotalCountForType()
301 return totalCountForType;
304 public long getTotalCountForType( String type )
306 return totalCountForType.get( type );
309 public void setTotalCountForType( String type, long count )
311 totalCountForType.put( type.replaceAll( "-", "_" ).replaceAll( "\\.", "_" ), count );
314 private static final class ZeroForNullHashMap<K, V extends Long> extends HashMap<K, V>
317 public V get(Object key) {
318 V value = super.get( key );
320 return value != null ? value : ( V ) Long.valueOf( 0L );