001    package org.apache.archiva.metadata.repository.stats;
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    
022    import org.apache.archiva.metadata.model.MetadataFacet;
023    
024    import java.text.SimpleDateFormat;
025    import java.util.Date;
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.TimeZone;
029    
030    public class RepositoryStatistics
031        implements MetadataFacet
032    {
033        private Date scanEndTime;
034    
035        private Date scanStartTime;
036    
037        private long totalArtifactCount;
038    
039        private long totalArtifactFileSize;
040    
041        private long totalFileCount;
042    
043        private long totalGroupCount;
044    
045        private long totalProjectCount;
046    
047        private long newFileCount;
048    
049        public static String FACET_ID = "org.apache.archiva.metadata.repository.stats";
050    
051        static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
052    
053        private Map<String, Long> totalCountForType = new ZeroForNullHashMap<String, Long>();
054    
055        private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
056    
057        private String repositoryId;
058    
059        public Date getScanEndTime()
060        {
061            return scanEndTime;
062        }
063    
064        public void setScanEndTime( Date scanEndTime )
065        {
066            this.scanEndTime = scanEndTime;
067        }
068    
069        public Date getScanStartTime()
070        {
071            return scanStartTime;
072        }
073    
074        public void setScanStartTime( Date scanStartTime )
075        {
076            this.scanStartTime = scanStartTime;
077        }
078    
079        public long getTotalArtifactCount()
080        {
081            return totalArtifactCount;
082        }
083    
084        public void setTotalArtifactCount( long totalArtifactCount )
085        {
086            this.totalArtifactCount = totalArtifactCount;
087        }
088    
089        public long getTotalArtifactFileSize()
090        {
091            return totalArtifactFileSize;
092        }
093    
094        public void setTotalArtifactFileSize( long totalArtifactFileSize )
095        {
096            this.totalArtifactFileSize = totalArtifactFileSize;
097        }
098    
099        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        public String getFacetId()
155        {
156            return FACET_ID;
157        }
158    
159        public String getName()
160        {
161            return createNameFormat().format( scanStartTime );
162        }
163    
164        private static SimpleDateFormat createNameFormat()
165        {
166            SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
167            fmt.setTimeZone( UTC_TIME_ZONE );
168            return fmt;
169        }
170    
171        public Map<String, String> toProperties()
172        {
173            Map<String, String> properties = new HashMap<String, String>();
174            properties.put( "scanEndTime", String.valueOf( scanEndTime.getTime() ) );
175            properties.put( "scanStartTime", String.valueOf( scanStartTime.getTime() ) );
176            properties.put( "totalArtifactCount", String.valueOf( totalArtifactCount ) );
177            properties.put( "totalArtifactFileSize", String.valueOf( totalArtifactFileSize ) );
178            properties.put( "totalFileCount", String.valueOf( totalFileCount ) );
179            properties.put( "totalGroupCount", String.valueOf( totalGroupCount ) );
180            properties.put( "totalProjectCount", String.valueOf( totalProjectCount ) );
181            properties.put( "newFileCount", String.valueOf( newFileCount ) );
182            properties.put( "repositoryId", repositoryId );
183            for ( Map.Entry<String, Long> entry : totalCountForType.entrySet() )
184            {
185                properties.put( "count-" + entry.getKey(), String.valueOf( entry.getValue() ) );
186            }
187            return properties;
188        }
189    
190        public void fromProperties( Map<String, String> properties )
191        {
192            scanEndTime = new Date( Long.parseLong( properties.get( "scanEndTime" ) ) );
193            scanStartTime = new Date( Long.parseLong( properties.get( "scanStartTime" ) ) );
194            totalArtifactCount = Long.parseLong( properties.get( "totalArtifactCount" ) );
195            totalArtifactFileSize = Long.parseLong( properties.get( "totalArtifactFileSize" ) );
196            totalFileCount = Long.parseLong( properties.get( "totalFileCount" ) );
197            totalGroupCount = Long.parseLong( properties.get( "totalGroupCount" ) );
198            totalProjectCount = Long.parseLong( properties.get( "totalProjectCount" ) );
199            newFileCount = Long.parseLong( properties.get( "newFileCount" ) );
200            repositoryId = properties.get( "repositoryId" );
201            totalCountForType.clear();
202            for ( Map.Entry<String, String> entry : properties.entrySet() )
203            {
204                if ( entry.getKey().startsWith( "count-" ) )
205                {
206                    totalCountForType.put( entry.getKey().substring( 6 ), Long.valueOf( entry.getValue() ) );
207                }
208            }
209        }
210    
211        @Override
212        public boolean equals( Object o )
213        {
214            if ( this == o )
215            {
216                return true;
217            }
218            if ( o == null || getClass() != o.getClass() )
219            {
220                return false;
221            }
222    
223            RepositoryStatistics that = (RepositoryStatistics) o;
224    
225            if ( newFileCount != that.newFileCount )
226            {
227                return false;
228            }
229            if ( totalArtifactCount != that.totalArtifactCount )
230            {
231                return false;
232            }
233            if ( totalArtifactFileSize != that.totalArtifactFileSize )
234            {
235                return false;
236            }
237            if ( totalFileCount != that.totalFileCount )
238            {
239                return false;
240            }
241            if ( totalGroupCount != that.totalGroupCount )
242            {
243                return false;
244            }
245            if ( totalProjectCount != that.totalProjectCount )
246            {
247                return false;
248            }
249            if ( !scanEndTime.equals( that.scanEndTime ) )
250            {
251                return false;
252            }
253            if ( !scanStartTime.equals( that.scanStartTime ) )
254            {
255                return false;
256            }
257            if ( !totalCountForType.equals( that.totalCountForType ) )
258            {
259                return false;
260            }
261            if ( !repositoryId.equals( that.repositoryId ) )
262            {
263                return false;
264            }
265    
266            return true;
267        }
268    
269        @Override
270        public int hashCode()
271        {
272            int result = scanEndTime.hashCode();
273            result = 31 * result + scanStartTime.hashCode();
274            result = 31 * result + (int) ( totalArtifactCount ^ ( totalArtifactCount >>> 32 ) );
275            result = 31 * result + (int) ( totalArtifactFileSize ^ ( totalArtifactFileSize >>> 32 ) );
276            result = 31 * result + (int) ( totalFileCount ^ ( totalFileCount >>> 32 ) );
277            result = 31 * result + (int) ( totalGroupCount ^ ( totalGroupCount >>> 32 ) );
278            result = 31 * result + (int) ( totalProjectCount ^ ( totalProjectCount >>> 32 ) );
279            result = 31 * result + (int) ( newFileCount ^ ( newFileCount >>> 32 ) );
280            result = 31 * result + totalCountForType.hashCode();
281            result = 31 * result + repositoryId.hashCode();
282            return result;
283        }
284    
285        @Override
286        public String toString()
287        {
288            return "RepositoryStatistics{" + "scanEndTime=" + scanEndTime + ", scanStartTime=" + scanStartTime +
289                ", totalArtifactCount=" + totalArtifactCount + ", totalArtifactFileSize=" + totalArtifactFileSize +
290                ", totalFileCount=" + totalFileCount + ", totalGroupCount=" + totalGroupCount + ", totalProjectCount=" +
291                totalProjectCount + ", newFileCount=" + newFileCount + ", totalCountForType=" + totalCountForType + ", " +
292                "repositoryId=" + repositoryId + '}';
293        }
294    
295        public Map<String, Long> getTotalCountForType()
296        {
297            return totalCountForType;
298        }
299    
300        public long getTotalCountForType( String type )
301        {
302            return totalCountForType.get( type );
303        }
304    
305        public void setTotalCountForType( String type, long count )
306        {
307            totalCountForType.put( type.replaceAll( "-", "_" ).replaceAll( "\\.", "_" ), count );
308        }
309        
310        private static final class ZeroForNullHashMap<K, V extends Long> extends HashMap<K, V>
311        {   
312            @Override
313            public V get(Object key) {
314                V value = super.get( key );
315                
316                return value != null ? value : ( V ) Long.valueOf( 0L );
317            }
318        }
319    }