001package 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
022import org.apache.archiva.metadata.model.MetadataFacet;
023
024import java.text.SimpleDateFormat;
025import java.util.Date;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.TimeZone;
029
030public 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    @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}