Search in sources :

Example 1 with Activity

use of com.gitblit.models.Activity in project gitblit by gitblit.

the class ActivityUtils method getRecentActivity.

/**
 * Gets the recent activity from the repositories for the last daysBack days
 * on the specified branch.
 *
 * @param settings
 *            the runtime settings
 * @param repositoryManager
 *            the repository manager
 * @param models
 *            the list of repositories to query
 * @param daysBack
 *            the number of days back from Now to collect
 * @param objectId
 *            the branch to retrieve. If this value is null or empty all
 *            branches are queried.
 * @param timezone
 *            the timezone for aggregating commits
 * @return
 */
public static List<Activity> getRecentActivity(IStoredSettings settings, IRepositoryManager repositoryManager, List<RepositoryModel> models, int daysBack, String objectId, TimeZone timezone) {
    // Activity panel shows last daysBack of activity across all
    // repositories.
    Date thresholdDate = new Date(System.currentTimeMillis() - daysBack * TimeUtils.ONEDAY);
    // Build a map of DailyActivity from the available repositories for the
    // specified threshold date.
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setTimeZone(timezone);
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(timezone);
    // aggregate author exclusions
    Set<String> authorExclusions = new TreeSet<String>();
    authorExclusions.addAll(settings.getStrings(Keys.web.metricAuthorExclusions));
    for (RepositoryModel model : models) {
        if (!ArrayUtils.isEmpty(model.metricAuthorExclusions)) {
            authorExclusions.addAll(model.metricAuthorExclusions);
        }
    }
    Map<String, Activity> activity = new HashMap<String, Activity>();
    for (RepositoryModel model : models) {
        if (!model.isShowActivity()) {
            // skip this repository
            continue;
        }
        if (model.hasCommits && model.lastChange.after(thresholdDate)) {
            if (model.isCollectingGarbage) {
                continue;
            }
            Repository repository = repositoryManager.getRepository(model.name);
            List<String> branches = new ArrayList<String>();
            if (StringUtils.isEmpty(objectId)) {
                for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
                    if (!local.getDate().after(thresholdDate)) {
                        // branch not recently updated
                        continue;
                    }
                    branches.add(local.getName());
                }
            } else {
                branches.add(objectId);
            }
            for (String branch : branches) {
                String shortName = branch;
                if (shortName.startsWith(Constants.R_HEADS)) {
                    shortName = shortName.substring(Constants.R_HEADS.length());
                }
                List<RepositoryCommit> commits = CommitCache.instance().getCommits(model.name, repository, branch, thresholdDate);
                if (model.maxActivityCommits > 0 && commits.size() > model.maxActivityCommits) {
                    // trim commits to maximum count
                    commits = commits.subList(0, model.maxActivityCommits);
                }
                for (RepositoryCommit commit : commits) {
                    Date date = commit.getCommitDate();
                    String dateStr = df.format(date);
                    if (!activity.containsKey(dateStr)) {
                        // Normalize the date to midnight
                        cal.setTime(date);
                        cal.set(Calendar.HOUR_OF_DAY, 0);
                        cal.set(Calendar.MINUTE, 0);
                        cal.set(Calendar.SECOND, 0);
                        cal.set(Calendar.MILLISECOND, 0);
                        Activity a = new Activity(cal.getTime());
                        a.excludeAuthors(authorExclusions);
                        activity.put(dateStr, a);
                    }
                    activity.get(dateStr).addCommit(commit);
                }
            }
            // close the repository
            repository.close();
        }
    }
    List<Activity> recentActivity = new ArrayList<Activity>(activity.values());
    return recentActivity;
}
Also used : RefModel(com.gitblit.models.RefModel) HashMap(java.util.HashMap) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Activity(com.gitblit.models.Activity) RepositoryModel(com.gitblit.models.RepositoryModel) RepositoryCommit(com.gitblit.models.RepositoryCommit) Date(java.util.Date) Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Activity

use of com.gitblit.models.Activity in project gitblit by gitblit.

the class ActivityPage method createCharts.

/**
 * Creates the daily activity line chart, the active repositories pie chart,
 * and the active authors pie chart
 *
 * @param recentActivity
 * @return
 */
private Charts createCharts(List<Activity> recentActivity) {
    // activity metrics
    Map<String, Metric> repositoryMetrics = new HashMap<String, Metric>();
    Map<String, Metric> authorMetrics = new HashMap<String, Metric>();
    // aggregate repository and author metrics
    for (Activity activity : recentActivity) {
        // aggregate author metrics
        for (Map.Entry<String, Metric> entry : activity.getAuthorMetrics().entrySet()) {
            String author = entry.getKey();
            if (!authorMetrics.containsKey(author)) {
                authorMetrics.put(author, new Metric(author));
            }
            authorMetrics.get(author).count += entry.getValue().count;
        }
        // aggregate repository metrics
        for (Map.Entry<String, Metric> entry : activity.getRepositoryMetrics().entrySet()) {
            String repository = StringUtils.stripDotGit(entry.getKey());
            if (!repositoryMetrics.containsKey(repository)) {
                repositoryMetrics.put(repository, new Metric(repository));
            }
            repositoryMetrics.get(repository).count += entry.getValue().count;
        }
    }
    // build charts
    Charts charts = new Flotr2Charts();
    // sort in reverse-chronological order and then reverse that
    Collections.sort(recentActivity);
    Collections.reverse(recentActivity);
    // daily line chart
    Chart chart = charts.createLineChart("chartDaily", getString("gb.dailyActivity"), "day", getString("gb.commits"));
    SimpleDateFormat df = new SimpleDateFormat("MMM dd");
    df.setTimeZone(getTimeZone());
    for (Activity metric : recentActivity) {
        chart.addValue(metric.startDate, metric.getCommitCount());
    }
    charts.addChart(chart);
    // active repositories pie chart
    chart = charts.createPieChart("chartRepositories", getString("gb.activeRepositories"), getString("gb.repository"), getString("gb.commits"));
    for (Metric metric : repositoryMetrics.values()) {
        chart.addValue(metric.name, metric.count);
    }
    chart.setShowLegend(false);
    String url = urlFor(SummaryPage.class, null).toString() + "?r=";
    chart.setClickUrl(url);
    charts.addChart(chart);
    // active authors pie chart
    chart = charts.createPieChart("chartAuthors", getString("gb.activeAuthors"), getString("gb.author"), getString("gb.commits"));
    for (Metric metric : authorMetrics.values()) {
        chart.addValue(metric.name, metric.count);
    }
    chart.setShowLegend(false);
    charts.addChart(chart);
    return charts;
}
Also used : Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) HashMap(java.util.HashMap) Activity(com.gitblit.models.Activity) Charts(com.gitblit.wicket.charting.Charts) Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) Metric(com.gitblit.models.Metric) HashMap(java.util.HashMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat) Chart(com.gitblit.wicket.charting.Chart)

Aggregations

Activity (com.gitblit.models.Activity)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Metric (com.gitblit.models.Metric)1 RefModel (com.gitblit.models.RefModel)1 RepositoryCommit (com.gitblit.models.RepositoryCommit)1 RepositoryModel (com.gitblit.models.RepositoryModel)1 Chart (com.gitblit.wicket.charting.Chart)1 Charts (com.gitblit.wicket.charting.Charts)1 Flotr2Charts (com.gitblit.wicket.charting.Flotr2Charts)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Repository (org.eclipse.jgit.lib.Repository)1