Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43ef95906a63b62042243d0e953b16b6ac5742fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/******************************************************************************
 *  Copyright (c) 2012, 2015 SAP AG and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Christian Halstrick (SAP AG) - initial implementation
 *****************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.text.NumberFormat;
import java.util.Properties;

import org.eclipse.egit.core.AdapterUtils;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jgit.api.GarbageCollectCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.SystemReader;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * Page exposing statistic data for elements that can adapt to a
 * {@link Repository} object.
 */
public class RepositoryStatisticsPage extends PropertyPage {
	private final NumberFormat bigFpFmt;

	private final NumberFormat bigIntFmt;

	/**
	 * Creates a new statistics page
	 */
	public RepositoryStatisticsPage() {
		bigFpFmt = NumberFormat.getInstance(SystemReader.getInstance()
				.getLocale());
		bigFpFmt.setMaximumFractionDigits(2);
		bigIntFmt = NumberFormat.getInstance(SystemReader.getInstance()
				.getLocale());
	}

	@Override
	protected Control createContents(Composite parent) {
		noDefaultAndApplyButton();
		Table table = new Table(parent, SWT.MULTI | SWT.BORDER
				| SWT.FULL_SELECTION);
		String[] titles = { UIText.RepositoryStatistics_Description,
				UIText.RepositoryStatistics_LooseObjects,
				UIText.RepositoryStatistics_PackedObjects };
		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(titles[i]);
		}

		Repository repo = AdapterUtils.adapt(getElement(), Repository.class);
		if (repo == null) {
			return table;
		}
		try (Git git = new Git(repo)) {
			GarbageCollectCommand gc = git.gc();
			Properties stats = gc.getStatistics();

			table.setLinesVisible(true);
			table.setHeaderVisible(true);
			GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
			data.heightHint = 200;
			table.setLayoutData(data);

			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, UIText.RepositoryStatistics_NrOfObjects);
			item.setText(1, getStatsAsString(stats, "numberOfLooseObjects")); //$NON-NLS-1$
			item.setText(2, getStatsAsString(stats, "numberOfPackedObjects")); //$NON-NLS-1$

			item = new TableItem(table, SWT.NONE);
			item.setText(0, UIText.RepositoryStatistics_NrOfPackfiles);
			item.setText(2, getStatsAsString(stats, "numberOfPackFiles")); //$NON-NLS-1$

			item = new TableItem(table, SWT.NONE);
			item.setText(0, UIText.RepositoryStatistics_NrOfRefs);
			item.setText(1, getStatsAsString(stats, "numberOfLooseRefs")); //$NON-NLS-1$
			item.setText(2, getStatsAsString(stats, "numberOfPackedRefs"));//$NON-NLS-1$

			item = new TableItem(table, SWT.NONE);
			item.setText(0, UIText.RepositoryStatistics_SpaceNeededOnFilesystem);
			item.setText(1,
					describeSize(getStatsAsLong(stats, "sizeOfLooseObjects"))); //$NON-NLS-1$
			item.setText(2,
					describeSize(getStatsAsLong(stats, "sizeOfPackedObjects"))); //$NON-NLS-1$

			for (int i = 0; i < titles.length; i++) {
				table.getColumn(i).pack();
			}
			parent.pack();
		} catch (GitAPIException e) {
			Activator.handleError(e.getMessage(), e, false);
		}
		return table;
	}

	private String getStatsAsString(Properties stats, String key) {
		return bigIntFmt.format(firstNonNull(stats.get(key), "")); //$NON-NLS-1$
	}

	private static long getStatsAsLong(Properties stats, String key) {
		Object value = stats.get(key);
		if (value instanceof Number)
			return ((Number) value).longValue();
		return 0L;
	}

	private static <T> T firstNonNull(T first, T second) {
		return first != null ? first : second;
	}

	private String describeSize(long nrOfBytes) {
		if (nrOfBytes < 1000L)
			return bigIntFmt.format(nrOfBytes) + " Bytes"; //$NON-NLS-1$
		if (nrOfBytes < 1000000L)
			return bigFpFmt.format(nrOfBytes / 1000.0)
					+ " kB (" + bigIntFmt.format(nrOfBytes) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		if (nrOfBytes < 1000000000L)
			return bigFpFmt.format(nrOfBytes / 1000000.0)
					+ " MB (" + bigIntFmt.format(nrOfBytes) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		return bigFpFmt.format(nrOfBytes / 1000000000.0)
				+ " GB (" + bigIntFmt.format(nrOfBytes) + ")"; //$NON-NLS-1$ //$NON-NLS-2$
	}
}

Back to the top