|
| 1 | +// Copyright (C) 2022 The Android Open Source Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.googlesource.gerrit.plugins.gitrepometrics; |
| 16 | + |
| 17 | +import com.google.common.base.Supplier; |
| 18 | +import com.google.gerrit.metrics.Description; |
| 19 | +import com.google.gerrit.metrics.MetricMaker; |
| 20 | +import com.google.inject.Inject; |
| 21 | +import com.google.inject.Singleton; |
| 22 | +import com.googlesource.gerrit.plugins.gitrepometrics.collectors.GitRepoMetric; |
| 23 | +import com.googlesource.gerrit.plugins.gitrepometrics.collectors.GitStats; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +@Singleton |
| 32 | +public class GitRepoMetricsCacheModule { |
| 33 | + public static List<GitRepoMetric> metricsNames = new ArrayList<>(GitStats.availableMetrics()); |
| 34 | + public List<String> projects; |
| 35 | + |
| 36 | + public static Map<String, Long> metrics = new HashMap<>(Collections.emptyMap());; |
| 37 | + |
| 38 | + public final MetricMaker metricMaker; |
| 39 | + public final GitRepoMetricsConfig config; |
| 40 | + |
| 41 | + @Inject |
| 42 | + GitRepoMetricsCacheModule(MetricMaker metricMaker, GitRepoMetricsConfig config) { |
| 43 | + this.metricMaker = metricMaker; |
| 44 | + this.config = config; |
| 45 | + this.projects = config.getRepositoryNames(); |
| 46 | + } |
| 47 | + |
| 48 | + public void initCache() { |
| 49 | + metricsNames.forEach( |
| 50 | + gitRepoMetric -> { |
| 51 | + projects.forEach( |
| 52 | + projectName -> { |
| 53 | + String name = |
| 54 | + GitRepoMetricsCacheModule.getMetricName(gitRepoMetric.getName(), projectName); |
| 55 | + Supplier<Long> supplier = |
| 56 | + new Supplier<Long>() { |
| 57 | + public Long get() { |
| 58 | + // TODO Blaah! Initializing all the values to zero!? Would be better |
| 59 | + // registering |
| 60 | + // dynamically the metrics |
| 61 | + // TODO add grace period!! |
| 62 | + return GitRepoMetricsCacheModule.metrics.getOrDefault(name, 0L); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + metricMaker.newCallbackMetric( |
| 67 | + name, |
| 68 | + Long.class, |
| 69 | + new Description(gitRepoMetric.getDescription()) |
| 70 | + .setRate() |
| 71 | + .setUnit(gitRepoMetric.getUnit()), |
| 72 | + supplier); |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + public static String getMetricName(String metricName, String projectName) { |
| 78 | + return String.format("%s_%s", metricName, projectName).toLowerCase(Locale.ROOT); |
| 79 | + } |
| 80 | +} |
0 commit comments