001package org.apache.archiva.metadata.repository.stats.model; 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 022import java.text.SimpleDateFormat; 023import java.util.Date; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.TimeZone; 027import java.util.stream.Collectors; 028 029/** 030 * Default statistics implementation 031 */ 032public class DefaultRepositoryStatistics 033 implements RepositoryStatistics 034{ 035 private Date scanEndTime; 036 037 private Date scanStartTime; 038 039 private long totalArtifactCount; 040 041 private long totalArtifactFileSize; 042 043 private long totalFileCount; 044 045 private long totalGroupCount; 046 047 private long totalProjectCount; 048 049 private long newFileCount; 050 051 public static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS"; 052 053 private Map<String, Long> totalCountForType = new ZeroForNullHashMap<>(); 054 055 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); 056 057 private String repositoryId; 058 059 private Map<String, Long> customValues; 060 061 public static final String TYPE_PREFIX = "count-type-"; 062 public static final String CUSTOM_PREFIX = "count-custom-"; 063 064 @Override 065 public Date getScanEndTime( ) 066 { 067 return scanEndTime; 068 } 069 070 public void setScanEndTime( Date scanEndTime ) 071 { 072 this.scanEndTime = scanEndTime; 073 } 074 075 @Override 076 public Date getScanStartTime( ) 077 { 078 return scanStartTime; 079 } 080 081 public void setScanStartTime( Date scanStartTime ) 082 { 083 this.scanStartTime = scanStartTime; 084 } 085 086 @Override 087 public long getTotalArtifactCount( ) 088 { 089 return totalArtifactCount; 090 } 091 092 @Override 093 public void setTotalArtifactCount( long totalArtifactCount ) 094 { 095 this.totalArtifactCount = totalArtifactCount; 096 } 097 098 @Override 099 public long getTotalArtifactFileSize( ) 100 { 101 return totalArtifactFileSize; 102 } 103 104 @Override 105 public void setTotalArtifactFileSize( long totalArtifactFileSize ) 106 { 107 this.totalArtifactFileSize = totalArtifactFileSize; 108 } 109 110 @Override 111 public long getTotalFileCount( ) 112 { 113 return totalFileCount; 114 } 115 116 @Override 117 public void setTotalFileCount( long totalFileCount ) 118 { 119 this.totalFileCount = totalFileCount; 120 } 121 122 @Override 123 public long getTotalGroupCount( ) 124 { 125 return totalGroupCount; 126 } 127 128 @Override 129 public void setTotalGroupCount( long totalGroupCount ) 130 { 131 this.totalGroupCount = totalGroupCount; 132 } 133 134 @Override 135 public long getTotalProjectCount( ) 136 { 137 return totalProjectCount; 138 } 139 140 @Override 141 public void setTotalProjectCount( long totalProjectCount ) 142 { 143 this.totalProjectCount = totalProjectCount; 144 } 145 146 @Override 147 public void setNewFileCount( long newFileCount ) 148 { 149 this.newFileCount = newFileCount; 150 } 151 152 @Override 153 public long getNewFileCount( ) 154 { 155 return newFileCount; 156 } 157 158 @Override 159 public long getDuration( ) 160 { 161 return scanEndTime.getTime() - scanStartTime.getTime(); 162 } 163 164 @Override 165 public String getRepositoryId( ) 166 { 167 return repositoryId; 168 } 169 170 public void setRepositoryId( String repositoryId ) 171 { 172 this.repositoryId = repositoryId; 173 } 174 175 @Override 176 public String getFacetId() 177 { 178 return FACET_ID; 179 } 180 181 @Override 182 public String getName() 183 { 184 return createNameFormat().format( scanStartTime ); 185 } 186 187 private static SimpleDateFormat createNameFormat() 188 { 189 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT ); 190 fmt.setTimeZone( UTC_TIME_ZONE ); 191 return fmt; 192 } 193 194 @Override 195 public Map<String, String> toProperties() 196 { 197 Map<String, String> properties = new HashMap<>(); 198 if (scanEndTime==null) { 199 properties.put("scanEndTime", "0"); 200 } else 201 { 202 properties.put( "scanEndTime", String.valueOf( scanEndTime.getTime( ) ) ); 203 } 204 if (scanStartTime==null) { 205 properties.put("scanStartTime","0"); 206 } else 207 { 208 properties.put( "scanStartTime", String.valueOf( scanStartTime.getTime( ) ) ); 209 } 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() ) 218 { 219 properties.put( TYPE_PREFIX + entry.getKey(), String.valueOf( entry.getValue() ) ); 220 } 221 if (customValues!=null) { 222 for (Map.Entry<String, Long> entry : customValues.entrySet()) { 223 properties.put(CUSTOM_PREFIX+entry.getKey(), String.valueOf(entry.getValue())); 224 } 225 } 226 return properties; 227 } 228 229 @Override 230 public void fromProperties( Map<String, String> properties ) 231 { 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() ) 243 { 244 if ( entry.getKey().startsWith( TYPE_PREFIX ) ) 245 { 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(); 250 } 251 customValues.put(entry.getKey().substring( CUSTOM_PREFIX.length() ), Long.valueOf(entry.getValue())); 252 } 253 } 254 } 255 256 257 @Override 258 public boolean equals( Object o ) 259 { 260 if ( this == o ) 261 { 262 return true; 263 } 264 if ( o == null || getClass() != o.getClass() ) 265 { 266 return false; 267 } 268 269 DefaultRepositoryStatistics that = (DefaultRepositoryStatistics) o; 270 271 if ( newFileCount != that.newFileCount ) 272 { 273 return false; 274 } 275 if ( totalArtifactCount != that.totalArtifactCount ) 276 { 277 return false; 278 } 279 if ( totalArtifactFileSize != that.totalArtifactFileSize ) 280 { 281 return false; 282 } 283 if ( totalFileCount != that.totalFileCount ) 284 { 285 return false; 286 } 287 if ( totalGroupCount != that.totalGroupCount ) 288 { 289 return false; 290 } 291 if ( totalProjectCount != that.totalProjectCount ) 292 { 293 return false; 294 } 295 if ( !scanEndTime.equals( that.scanEndTime ) ) 296 { 297 return false; 298 } 299 if ( !scanStartTime.equals( that.scanStartTime ) ) 300 { 301 return false; 302 } 303 if ( !totalCountForType.equals( that.totalCountForType ) ) 304 { 305 return false; 306 } 307 if ( customValues==null && that.customValues!=null) { 308 return false; 309 } 310 if ( customValues!=null && that.customValues==null) { 311 return false; 312 } 313 if (customValues!=null && !customValues.equals(that.customValues)) { 314 return false; 315 } 316 return repositoryId.equals( that.repositoryId ); 317 } 318 319 @Override 320 public int hashCode() 321 { 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(); 334 return result; 335 } 336 337 @Override 338 public String toString() 339 { 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() + 346 '}'; 347 } 348 349 private String getCustomValueString() { 350 if (customValues==null) { 351 return ""; 352 } else { 353 return customValues.entrySet().stream().map(entry -> entry.getKey()+"="+entry.getValue()).collect( 354 Collectors.joining( ",") 355 ); 356 } 357 } 358 359 @Override 360 public Map<String, Long> getTotalCountForType( ) 361 { 362 return totalCountForType; 363 } 364 365 @Override 366 public long getTotalCountForType( String type ) 367 { 368 return totalCountForType.get( type ); 369 } 370 371 @Override 372 public void setTotalCountForType( String type, long count ) 373 { 374 totalCountForType.put( type, count ); 375 } 376 377 @Override 378 public long getCustomValue( String fieldName ) 379 { 380 // Lazy evaluation, because it may not be used very often. 381 if (customValues==null) { 382 createCustomValueMap(); 383 } 384 return customValues.get(fieldName); 385 } 386 387 @Override 388 public void setCustomValue( String fieldName, long count ) 389 { 390 // Lazy evaluation, because it may not be used very often. 391 if (customValues==null) { 392 createCustomValueMap(); 393 } 394 customValues.put(fieldName, count); 395 } 396 397 private void createCustomValueMap( ) 398 { 399 customValues = new ZeroForNullHashMap<>(); 400 } 401 402 403 private static final class ZeroForNullHashMap<K> extends HashMap<K, Long> 404 { 405 @Override 406 public Long get(Object key) { 407 Long value = super.get( key ); 408 409 return ( value != null ) ? value : Long.valueOf( 0L ); 410 } 411 } 412}