Search in sources :

Example 6 with Metric

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

the class DashboardPage method addCharts.

/**
	 * Creates the daily activity line chart, the active repositories pie chart,
	 * and the active authors pie chart
	 *
	 * @param recentChanges
	 * @param authorExclusions
	 * @param daysBack
	 */
protected void addCharts(Fragment frag, List<DailyLogEntry> recentChanges, Set<String> authorExclusions, int daysBack) {
    // activity metrics
    Map<String, Metric> repositoryMetrics = new HashMap<String, Metric>();
    Map<String, Metric> authorMetrics = new HashMap<String, Metric>();
    // aggregate repository and author metrics
    int totalCommits = 0;
    for (RefLogEntry change : recentChanges) {
        // aggregate repository metrics
        String repository = StringUtils.stripDotGit(change.repository);
        if (!repositoryMetrics.containsKey(repository)) {
            repositoryMetrics.put(repository, new Metric(repository));
        }
        repositoryMetrics.get(repository).count += 1;
        for (RepositoryCommit commit : change.getCommits()) {
            totalCommits++;
            String author = StringUtils.removeNewlines(commit.getAuthorIdent().getName());
            String authorName = author.toLowerCase();
            String authorEmail = StringUtils.removeNewlines(commit.getAuthorIdent().getEmailAddress()).toLowerCase();
            if (!authorExclusions.contains(authorName) && !authorExclusions.contains(authorEmail)) {
                if (!authorMetrics.containsKey(author)) {
                    authorMetrics.put(author, new Metric(author));
                }
                authorMetrics.get(author).count += 1;
            }
        }
    }
    String headerPattern;
    if (daysBack == 1) {
        // today
        if (totalCommits == 0) {
            headerPattern = getString("gb.todaysActivityNone");
        } else {
            headerPattern = getString("gb.todaysActivityStats");
        }
    } else {
        // multiple days
        if (totalCommits == 0) {
            headerPattern = getString("gb.recentActivityNone");
        } else {
            headerPattern = getString("gb.recentActivityStats");
        }
    }
    frag.add(new Label("feedheader", MessageFormat.format(headerPattern, daysBack, totalCommits, authorMetrics.size())));
    if (app().settings().getBoolean(Keys.web.generateActivityGraph, true)) {
        // build google charts
        Charts charts = new Flotr2Charts();
        // active repositories pie chart
        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);
        add(new HeaderContributor(charts));
        frag.add(new Fragment("charts", "chartsFragment", this));
    } else {
        frag.add(new Label("charts").setVisible(false));
    }
}
Also used : Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) HashMap(java.util.HashMap) Label(org.apache.wicket.markup.html.basic.Label) Charts(com.gitblit.wicket.charting.Charts) Flotr2Charts(com.gitblit.wicket.charting.Flotr2Charts) RepositoryCommit(com.gitblit.models.RepositoryCommit) Fragment(org.apache.wicket.markup.html.panel.Fragment) RefLogEntry(com.gitblit.models.RefLogEntry) HeaderContributor(org.apache.wicket.behavior.HeaderContributor) Metric(com.gitblit.models.Metric) Chart(com.gitblit.wicket.charting.Chart)

Example 7 with Metric

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

the class MetricsPage method createLineChart.

private void createLineChart(Charts charts, String id, List<Metric> metrics) {
    if ((metrics != null) && (metrics.size() > 0)) {
        Chart chart = charts.createLineChart(id, "", "day", getString("gb.commits"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String displayFormat = "MMM dd";
        if (metrics.size() > 0 && metrics.get(0).name.length() == 7) {
            df = new SimpleDateFormat("yyyy-MM");
            displayFormat = "yyyy MMM";
        }
        df.setTimeZone(getTimeZone());
        chart.setDateFormat(displayFormat);
        for (Metric metric : metrics) {
            Date date;
            try {
                date = df.parse(metric.name);
            } catch (ParseException e) {
                logger.error("Unable to parse date: " + metric.name);
                return;
            }
            chart.addValue(date, (int) metric.count);
            if (metric.tag > 0) {
                chart.addHighlight(date, (int) metric.count);
            }
        }
        charts.addChart(chart);
    }
}
Also used : Metric(com.gitblit.models.Metric) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Chart(com.gitblit.wicket.charting.Chart) Date(java.util.Date)

Example 8 with Metric

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

the class MetricsPage method getDayOfWeekMetrics.

private List<Metric> getDayOfWeekMetrics(Repository repository, String objectId) {
    List<Metric> list = MetricUtils.getDateMetrics(repository, objectId, false, "E", getTimeZone());
    SimpleDateFormat sdf = new SimpleDateFormat("E");
    Calendar cal = Calendar.getInstance();
    List<Metric> sorted = new ArrayList<Metric>();
    int firstDayOfWeek = cal.getFirstDayOfWeek();
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    // rewind date to first day of week
    cal.add(Calendar.DATE, firstDayOfWeek - dayOfWeek);
    for (int i = 0; i < 7; i++) {
        String day = sdf.format(cal.getTime());
        for (Metric metric : list) {
            if (metric.name.equals(day)) {
                sorted.add(metric);
                list.remove(metric);
                break;
            }
        }
        cal.add(Calendar.DATE, 1);
    }
    return sorted;
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Metric(com.gitblit.models.Metric) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with Metric

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

the class MetricsPage method createBarChart.

private void createBarChart(Charts charts, String id, List<Metric> metrics) {
    if ((metrics != null) && (metrics.size() > 0)) {
        Chart chart = charts.createBarChart(id, "", "day", getString("gb.commits"));
        for (Metric metric : metrics) {
            chart.addValue(metric.name, (int) metric.count);
        }
        charts.addChart(chart);
    }
}
Also used : Metric(com.gitblit.models.Metric) Chart(com.gitblit.wicket.charting.Chart)

Example 10 with Metric

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

the class MetricUtils method getDateMetrics.

/**
	 * Returns the list of metrics for the specified commit reference, branch,
	 * or tag within the repository. If includeTotal is true, the total of all
	 * the metrics will be included as the first element in the returned list.
	 *
	 * If the dateformat is unspecified an attempt is made to determine an
	 * appropriate date format by determining the time difference between the
	 * first commit on the branch and the most recent commit. This assumes that
	 * the commits are linear.
	 *
	 * @param repository
	 * @param objectId
	 *            if null or empty, HEAD is assumed.
	 * @param includeTotal
	 * @param dateFormat
	 * @param timezone
	 * @return list of metrics
	 */
public static List<Metric> getDateMetrics(Repository repository, String objectId, boolean includeTotal, String dateFormat, TimeZone timezone) {
    Metric total = new Metric("TOTAL");
    final Map<String, Metric> metricMap = new HashMap<String, Metric>();
    if (JGitUtils.hasCommits(repository)) {
        final List<RefModel> tags = JGitUtils.getTags(repository, true, -1);
        final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
        for (RefModel tag : tags) {
            tagMap.put(tag.getReferencedObjectId(), tag);
        }
        RevWalk revWalk = null;
        try {
            // resolve branch
            ObjectId branchObject;
            if (StringUtils.isEmpty(objectId)) {
                branchObject = JGitUtils.getDefaultBranch(repository);
            } else {
                branchObject = repository.resolve(objectId);
            }
            revWalk = new RevWalk(repository);
            RevCommit lastCommit = revWalk.parseCommit(branchObject);
            revWalk.markStart(lastCommit);
            DateFormat df;
            if (StringUtils.isEmpty(dateFormat)) {
                // dynamically determine date format
                RevCommit firstCommit = JGitUtils.getFirstCommit(repository, branchObject.getName());
                int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime()) / (60 * 60 * 24);
                total.duration = diffDays;
                if (diffDays <= 365) {
                    // Days
                    df = new SimpleDateFormat("yyyy-MM-dd");
                } else {
                    // Months
                    df = new SimpleDateFormat("yyyy-MM");
                }
            } else {
                // use specified date format
                df = new SimpleDateFormat(dateFormat);
            }
            df.setTimeZone(timezone);
            Iterable<RevCommit> revlog = revWalk;
            for (RevCommit rev : revlog) {
                Date d = JGitUtils.getAuthorDate(rev);
                String p = df.format(d);
                if (!metricMap.containsKey(p)) {
                    metricMap.put(p, new Metric(p));
                }
                Metric m = metricMap.get(p);
                m.count++;
                total.count++;
                if (tagMap.containsKey(rev.getId())) {
                    m.tag++;
                    total.tag++;
                }
            }
        } catch (Throwable t) {
            error(t, repository, "{0} failed to mine log history for date metrics of {1}", objectId);
        } finally {
            if (revWalk != null) {
                revWalk.dispose();
            }
        }
    }
    List<String> keys = new ArrayList<String>(metricMap.keySet());
    Collections.sort(keys);
    List<Metric> metrics = new ArrayList<Metric>();
    for (String key : keys) {
        metrics.add(metricMap.get(key));
    }
    if (includeTotal) {
        metrics.add(0, total);
    }
    return metrics;
}
Also used : RefModel(com.gitblit.models.RefModel) HashMap(java.util.HashMap) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Metric(com.gitblit.models.Metric) SimpleDateFormat(java.text.SimpleDateFormat) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Metric (com.gitblit.models.Metric)11 Chart (com.gitblit.wicket.charting.Chart)7 SimpleDateFormat (java.text.SimpleDateFormat)5 Charts (com.gitblit.wicket.charting.Charts)4 Flotr2Charts (com.gitblit.wicket.charting.Flotr2Charts)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 ParseException (java.text.ParseException)2 HeaderContributor (org.apache.wicket.behavior.HeaderContributor)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 Activity (com.gitblit.models.Activity)1 RefLogEntry (com.gitblit.models.RefLogEntry)1 RefModel (com.gitblit.models.RefModel)1 RepositoryCommit (com.gitblit.models.RepositoryCommit)1 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1