Replace Map with MetricsRecord to group metrics
Added a MetricsRecord class to group the metrics collected
by the File System collector and to improve code readability.
Change-Id: I9a0528c8a90e4ccbeb0cfde4db452b410f4879ae
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/collectors/FSMetricsCollector.java b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/collectors/FSMetricsCollector.java
index b2a66c7..d9f352f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/collectors/FSMetricsCollector.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/collectors/FSMetricsCollector.java
@@ -30,6 +30,45 @@
public class FSMetricsCollector implements MetricsCollector {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+ static class MetricsRecord {
+ private long numberOfKeepFilesCount = 0L;
+ private long numberOfEmptyDirectoriesCount = 0L;
+ private long numberOfDirectoriesCount = 0L;
+ private long numberOfFilesCount = 0L;
+
+ void foundKeepFile() {
+ numberOfKeepFilesCount++;
+ }
+
+ void foundEmptyDirectory() {
+ numberOfEmptyDirectoriesCount++;
+ }
+
+ void foundDirectory() {
+ numberOfDirectoriesCount++;
+ }
+
+ void foundFile() {
+ numberOfFilesCount++;
+ }
+
+ void incrementMetrics(MetricsRecord metricsRecordInc) {
+ numberOfKeepFilesCount += metricsRecordInc.numberOfKeepFilesCount;
+ numberOfEmptyDirectoriesCount += metricsRecordInc.numberOfEmptyDirectoriesCount;
+ numberOfDirectoriesCount += metricsRecordInc.numberOfDirectoriesCount;
+ numberOfFilesCount += metricsRecordInc.numberOfFilesCount;
+ }
+
+ HashMap<GitRepoMetric, Long> toMap() {
+ HashMap<GitRepoMetric, Long> metrics = new HashMap<>(4);
+ metrics.put(numberOfFiles, numberOfFilesCount);
+ metrics.put(numberOfDirectories, numberOfDirectoriesCount);
+ metrics.put(numberOfEmptyDirectories, numberOfEmptyDirectoriesCount);
+ metrics.put(numberOfKeepFiles, numberOfKeepFilesCount);
+ return metrics;
+ }
+ }
+
protected static final GitRepoMetric numberOfKeepFiles =
new GitRepoMetric("numberOfKeepFiles", "Number of keep files on filesystem", "Count");
protected static final GitRepoMetric numberOfEmptyDirectories =
@@ -66,59 +105,40 @@
private HashMap<GitRepoMetric, Long> filesAndDirectoriesCount(
FileRepository repository, String projectName) {
- HashMap<GitRepoMetric, Long> metrics = new HashMap<GitRepoMetric, Long>();
try {
- metrics =
+ MetricsRecord metricsRecord =
Files.walk(repository.getObjectsDirectory().toPath())
.map(
path -> {
File f = path.toFile();
- HashMap<GitRepoMetric, Long> m = getInitializedMetrics();
+ MetricsRecord mr = new MetricsRecord();
+ ;
if (f.isFile()) {
- m.put(numberOfFiles, 1L);
+ mr.foundFile();
if (f.getName().endsWith(".keep")) {
- m.put(numberOfKeepFiles, 1L);
+ mr.foundKeepFile();
}
} else {
- m.put(numberOfDirectories, 1L);
+ mr.foundDirectory();
if (Objects.requireNonNull(f.listFiles()).length == 0) {
- m.put(numberOfEmptyDirectories, 1L);
+ mr.foundEmptyDirectory();
}
}
- return m;
+ return mr;
})
.reduce(
- getInitializedMetrics(),
- (acc, lastMetric) ->
- new HashMap<GitRepoMetric, Long>() {
- {
- putMetric(numberOfFiles);
- putMetric(numberOfDirectories);
- putMetric(numberOfEmptyDirectories);
- putMetric(numberOfKeepFiles);
- }
-
- private void putMetric(GitRepoMetric metric) {
- put(metric, acc.get(metric) + lastMetric.get(metric));
- }
- });
+ new MetricsRecord(),
+ (acc, lastMetric) -> {
+ acc.incrementMetrics(lastMetric);
+ return acc;
+ });
+ return metricsRecord.toMap();
} catch (IOException e) {
logger.atSevere().withCause(e).log(
"Error reading from file system for project " + projectName);
}
- return metrics;
- }
-
- private HashMap<GitRepoMetric, Long> getInitializedMetrics() {
- return new HashMap<GitRepoMetric, Long>() {
- {
- put(numberOfFiles, 0L);
- put(numberOfDirectories, 0L);
- put(numberOfEmptyDirectories, 0L);
- put(numberOfKeepFiles, 0L);
- }
- };
+ return new MetricsRecord().toMap();
}
@Override