1 package org.apache.archiva.metadata.repository.stats.model;
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 java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.HashMap;
26 import java.util.TimeZone;
27 import java.util.stream.Collectors;
30 * Default statistics implementation
32 public class DefaultRepositoryStatistics
33 implements RepositoryStatistics
35 private Date scanEndTime;
37 private Date scanStartTime;
39 private long totalArtifactCount;
41 private long totalArtifactFileSize;
43 private long totalFileCount;
45 private long totalGroupCount;
47 private long totalProjectCount;
49 private long newFileCount;
51 public static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
53 private Map<String, Long> totalCountForType = new ZeroForNullHashMap<>();
55 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
57 private String repositoryId;
59 private Map<String, Long> customValues;
61 public static final String TYPE_PREFIX = "count-type-";
62 public static final String CUSTOM_PREFIX = "count-custom-";
65 public Date getScanEndTime( )
70 public void setScanEndTime( Date scanEndTime )
72 this.scanEndTime = scanEndTime;
76 public Date getScanStartTime( )
81 public void setScanStartTime( Date scanStartTime )
83 this.scanStartTime = scanStartTime;
87 public long getTotalArtifactCount( )
89 return totalArtifactCount;
93 public void setTotalArtifactCount( long totalArtifactCount )
95 this.totalArtifactCount = totalArtifactCount;
99 public long getTotalArtifactFileSize( )
101 return totalArtifactFileSize;
105 public void setTotalArtifactFileSize( long totalArtifactFileSize )
107 this.totalArtifactFileSize = totalArtifactFileSize;
111 public long getTotalFileCount( )
113 return totalFileCount;
117 public void setTotalFileCount( long totalFileCount )
119 this.totalFileCount = totalFileCount;
123 public long getTotalGroupCount( )
125 return totalGroupCount;
129 public void setTotalGroupCount( long totalGroupCount )
131 this.totalGroupCount = totalGroupCount;
135 public long getTotalProjectCount( )
137 return totalProjectCount;
141 public void setTotalProjectCount( long totalProjectCount )
143 this.totalProjectCount = totalProjectCount;
147 public void setNewFileCount( long newFileCount )
149 this.newFileCount = newFileCount;
153 public long getNewFileCount( )
159 public long getDuration( )
161 return scanEndTime.getTime() - scanStartTime.getTime();
165 public String getRepositoryId( )
170 public void setRepositoryId( String repositoryId )
172 this.repositoryId = repositoryId;
176 public String getFacetId()
182 public String getName()
184 return createNameFormat().format( scanStartTime );
187 private static SimpleDateFormat createNameFormat()
189 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
190 fmt.setTimeZone( UTC_TIME_ZONE );
195 public Map<String, String> toProperties()
197 Map<String, String> properties = new HashMap<>();
198 if (scanEndTime==null) {
199 properties.put("scanEndTime", "0");
202 properties.put( "scanEndTime", String.valueOf( scanEndTime.getTime( ) ) );
204 if (scanStartTime==null) {
205 properties.put("scanStartTime","0");
208 properties.put( "scanStartTime", String.valueOf( scanStartTime.getTime( ) ) );
210 properties.put( "totalArtifactCount", String.valueOf( totalArtifactCount ) );
211 properties.put( "totalArtifactFileSize", String.valueOf( totalArtifactFileSize ) );
212 properties.put( "totalFileCount", String.valueOf( totalFileCount ) );
213 properties.put( "totalGroupCount", String.valueOf( totalGroupCount ) );
214 properties.put( "totalProjectCount", String.valueOf( totalProjectCount ) );
215 properties.put( "newFileCount", String.valueOf( newFileCount ) );
216 properties.put( "repositoryId", repositoryId );
217 for ( Map.Entry<String, Long> entry : totalCountForType.entrySet() )
219 properties.put( TYPE_PREFIX + entry.getKey(), String.valueOf( entry.getValue() ) );
221 if (customValues!=null) {
222 for (Map.Entry<String, Long> entry : customValues.entrySet()) {
223 properties.put(CUSTOM_PREFIX+entry.getKey(), String.valueOf(entry.getValue()));
230 public void fromProperties( Map<String, String> properties )
232 scanEndTime = new Date( Long.parseLong( properties.get( "scanEndTime" ) ) );
233 scanStartTime = new Date( Long.parseLong( properties.get( "scanStartTime" ) ) );
234 totalArtifactCount = Long.parseLong( properties.get( "totalArtifactCount" ) );
235 totalArtifactFileSize = Long.parseLong( properties.get( "totalArtifactFileSize" ) );
236 totalFileCount = Long.parseLong( properties.get( "totalFileCount" ) );
237 totalGroupCount = Long.parseLong( properties.get( "totalGroupCount" ) );
238 totalProjectCount = Long.parseLong( properties.get( "totalProjectCount" ) );
239 newFileCount = Long.parseLong( properties.get( "newFileCount" ) );
240 repositoryId = properties.get( "repositoryId" );
241 totalCountForType.clear();
242 for ( Map.Entry<String, String> entry : properties.entrySet() )
244 if ( entry.getKey().startsWith( TYPE_PREFIX ) )
246 totalCountForType.put( entry.getKey().substring( TYPE_PREFIX.length() ), Long.valueOf( entry.getValue() ) );
247 } else if (entry.getKey().startsWith( CUSTOM_PREFIX )) {
248 if (customValues==null) {
249 createCustomValueMap();
251 customValues.put(entry.getKey().substring( CUSTOM_PREFIX.length() ), Long.valueOf(entry.getValue()));
258 public boolean equals( Object o )
264 if ( o == null || getClass() != o.getClass() )
269 DefaultRepositoryStatistics that = (DefaultRepositoryStatistics) o;
271 if ( newFileCount != that.newFileCount )
275 if ( totalArtifactCount != that.totalArtifactCount )
279 if ( totalArtifactFileSize != that.totalArtifactFileSize )
283 if ( totalFileCount != that.totalFileCount )
287 if ( totalGroupCount != that.totalGroupCount )
291 if ( totalProjectCount != that.totalProjectCount )
295 if ( !scanEndTime.equals( that.scanEndTime ) )
299 if ( !scanStartTime.equals( that.scanStartTime ) )
303 if ( !totalCountForType.equals( that.totalCountForType ) )
307 if ( customValues==null && that.customValues!=null) {
310 if ( customValues!=null && that.customValues==null) {
313 if (customValues!=null && !customValues.equals(that.customValues)) {
316 return repositoryId.equals( that.repositoryId );
320 public int hashCode()
322 int result = scanEndTime.hashCode();
323 result = 31 * result + scanStartTime.hashCode();
324 result = 31 * result + (int) ( totalArtifactCount ^ ( totalArtifactCount >>> 32 ) );
325 result = 31 * result + (int) ( totalArtifactFileSize ^ ( totalArtifactFileSize >>> 32 ) );
326 result = 31 * result + (int) ( totalFileCount ^ ( totalFileCount >>> 32 ) );
327 result = 31 * result + (int) ( totalGroupCount ^ ( totalGroupCount >>> 32 ) );
328 result = 31 * result + (int) ( totalProjectCount ^ ( totalProjectCount >>> 32 ) );
329 result = 31 * result + (int) ( newFileCount ^ ( newFileCount >>> 32 ) );
330 result = 31 * result + totalCountForType.hashCode();
331 result = 31 * result + repositoryId.hashCode();
332 if (customValues!=null)
333 result = 31 * result + customValues.hashCode();
338 public String toString()
340 return "RepositoryStatistics{" + "scanEndTime=" + scanEndTime + ", scanStartTime=" + scanStartTime +
341 ", totalArtifactCount=" + totalArtifactCount + ", totalArtifactFileSize=" + totalArtifactFileSize +
342 ", totalFileCount=" + totalFileCount + ", totalGroupCount=" + totalGroupCount + ", totalProjectCount=" +
343 totalProjectCount + ", newFileCount=" + newFileCount + ", totalCountForType=" + totalCountForType + ", " +
344 "repositoryId=" + repositoryId +
345 getCustomValueString() +
349 private String getCustomValueString() {
350 if (customValues==null) {
353 return customValues.entrySet().stream().map(entry -> entry.getKey()+"="+entry.getValue()).collect(
354 Collectors.joining( ",")
360 public Map<String, Long> getTotalCountForType( )
362 return totalCountForType;
366 public long getTotalCountForType( String type )
368 return totalCountForType.get( type );
372 public void setTotalCountForType( String type, long count )
374 totalCountForType.put( type, count );
378 public long getCustomValue( String fieldName )
380 // Lazy evaluation, because it may not be used very often.
381 if (customValues==null) {
382 createCustomValueMap();
384 return customValues.get(fieldName);
388 public void setCustomValue( String fieldName, long count )
390 // Lazy evaluation, because it may not be used very often.
391 if (customValues==null) {
392 createCustomValueMap();
394 customValues.put(fieldName, count);
397 private void createCustomValueMap( )
399 customValues = new ZeroForNullHashMap<>();
403 private static final class ZeroForNullHashMap<K> extends HashMap<K, Long>
406 public Long get(Object key) {
407 Long value = super.get( key );
409 return ( value != null ) ? value : Long.valueOf( 0L );