James Moger
2011-12-22 e6935876b97a63bae2ec087b4fc390c832aef155
commit | author | age
db91a3 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.models;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.eclipse.jgit.lib.PersonIdent;
26 import org.eclipse.jgit.revwalk.RevCommit;
27
28 import com.gitblit.utils.TimeUtils;
29
30 /**
31  * Model class to represent the commit activity across many repositories. This
32  * class is used by the Activity page.
33  * 
34  * @author James Moger
35  */
36 public class Activity implements Serializable, Comparable<Activity> {
37
38     private static final long serialVersionUID = 1L;
39
40     public final Date startDate;
41
42     public final Date endDate;
43
44     public final List<RepositoryCommit> commits;
45
46     private final Map<String, Metric> authorMetrics;
47
48     private final Map<String, Metric> repositoryMetrics;
49
50     /**
51      * Constructor for one day of activity.
52      * 
53      * @param date
54      */
55     public Activity(Date date) {
56         this(date, TimeUtils.ONEDAY - 1);
57     }
58
59     /**
60      * Constructor for specified duration of activity from start date.
61      * 
62      * @param date
63      *            the start date of the activity
64      * @param duration
65      *            the duration of the period in milliseconds
66      */
67     public Activity(Date date, long duration) {
68         startDate = date;
69         endDate = new Date(date.getTime() + duration);
70         commits = new ArrayList<RepositoryCommit>();
71         authorMetrics = new HashMap<String, Metric>();
72         repositoryMetrics = new HashMap<String, Metric>();
73     }
74
75     public RepositoryCommit addCommit(String repository, String branch, RevCommit commit) {
76         RepositoryCommit commitModel = new RepositoryCommit(repository, branch, commit);
77         commits.add(commitModel);
78
79         if (!repositoryMetrics.containsKey(repository)) {
80             repositoryMetrics.put(repository, new Metric(repository));
81         }
82         repositoryMetrics.get(repository).count++;
83
84         String author = commit.getAuthorIdent().getEmailAddress().toLowerCase();
85         if (!authorMetrics.containsKey(author)) {
86             authorMetrics.put(author, new Metric(author));
87         }
88         authorMetrics.get(author).count++;
89         return commitModel;
90     }
91
92     public Map<String, Metric> getAuthorMetrics() {
93         return authorMetrics;
94     }
95
96     public Map<String, Metric> getRepositoryMetrics() {
97         return repositoryMetrics;
98     }
99
100     @Override
101     public int compareTo(Activity o) {
102         // reverse chronological order
103         return o.startDate.compareTo(startDate);
104     }
105
106     /**
107      * Model class to represent a RevCommit, it's source repository, and the
108      * branch. This class is used by the activity page.
109      * 
110      * @author James Moger
111      */
112     public static class RepositoryCommit implements Serializable, Comparable<RepositoryCommit> {
113
114         private static final long serialVersionUID = 1L;
115
116         public final String repository;
117
118         public final String branch;
119
120         private final RevCommit commit;
121
122         private List<RefModel> refs;
123
124         public RepositoryCommit(String repository, String branch, RevCommit commit) {
125             this.repository = repository;
126             this.branch = branch;
127             this.commit = commit;
128         }
129
130         public void setRefs(List<RefModel> refs) {
131             this.refs = refs;
132         }
133
134         public List<RefModel> getRefs() {
135             return refs;
136         }
137
138         public String getName() {
139             return commit.getName();
140         }
141
142         public String getShortName() {
143             return commit.getName().substring(0, 8);
144         }
145
146         public String getShortMessage() {
147             return commit.getShortMessage();
148         }
149
150         public int getParentCount() {
151             return commit.getParentCount();
152         }
153
154         public PersonIdent getAuthorIdent() {
155             return commit.getAuthorIdent();
156         }
157
158         @Override
159         public int compareTo(RepositoryCommit o) {
160             // reverse-chronological order
161             if (commit.getCommitTime() > o.commit.getCommitTime()) {
162                 return -1;
163             } else if (commit.getCommitTime() < o.commit.getCommitTime()) {
164                 return 1;
165             }
166             return 0;
167         }
168     }
169 }