Skip to content

Commit

Permalink
Support component name for general release metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Aug 27, 2024
1 parent 8f33f15 commit 310431b
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 71 deletions.
37 changes: 20 additions & 17 deletions src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,19 @@ public void generateLabelMetrics(List<String> repositories) {
throw new RuntimeException(e);
}
return labelInfo.entrySet().stream().flatMap(entry -> {
String labelname = entry.getKey();
String labelName = entry.getKey();
List<Long> values = entry.getValue();
LabelData labelData = new LabelData();
try {
labelData.setId(String.valueOf(UUID.nameUUIDFromBytes(MessageDigest.getInstance("SHA-1")
.digest(("label-metrics-" + labelname + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo)
.digest(("label-metrics-" + labelName + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo)
.getBytes()))));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
labelData.setRepository(repo);
labelData.setCurrentDate(currentDate.toString());
labelData.setLabelName(labelname);
labelData.setLabelName(labelName);
labelData.setLabelPullCount(values.get(1));
labelData.setLabelIssueCount(values.get(0));
return Stream.of(labelData);
Expand All @@ -155,35 +155,38 @@ public void generateReleaseMetrics() {
Map<String, String> metricFinalData =
Arrays.stream(releaseInputs)
.filter(ReleaseInputs::getTrack)
.flatMap(releaseInput -> releaseMetrics.getReleaseRepos(releaseInput.getVersion()).stream()
.flatMap(repo -> {
.flatMap(releaseInput -> releaseMetrics.getReleaseRepos(releaseInput.getVersion()).entrySet().stream()
.flatMap(entry -> {
String repoName = entry.getKey();
String componentName = entry.getValue();

Check warning on line 161 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L160-L161

Added lines #L160 - L161 were not covered by tests
ReleaseMetricsData releaseMetricsData = new ReleaseMetricsData();
releaseMetricsData.setRepository(repo);
releaseMetricsData.setRepository(repoName);
releaseMetricsData.setComponent(componentName);

Check warning on line 164 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L163-L164

Added lines #L163 - L164 were not covered by tests
releaseMetricsData.setCurrentDate(currentDate.toString());
try {
releaseMetricsData.setId(String.valueOf(UUID.nameUUIDFromBytes(MessageDigest.getInstance("SHA-1")
.digest(("release-metrics-" + releaseInput.getVersion() + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repo)
.digest(("release-metrics-" + releaseInput.getVersion() + "-" + currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "-" + repoName)

Check warning on line 168 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L168

Added line #L168 was not covered by tests
.getBytes()))));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
releaseMetricsData.setReleaseVersion(releaseInput.getVersion());
releaseMetricsData.setVersion(releaseInput.getVersion());
releaseMetricsData.setReleaseState(releaseInput.getState());
releaseMetricsData.setIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", false));
releaseMetricsData.setAutocutIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "open", true));
releaseMetricsData.setIssuesClosed(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repo, "closed", false));
releaseMetricsData.setPullsOpen(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repo, "open"));
releaseMetricsData.setPullsClosed(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repo, "closed"));
releaseMetricsData.setVersionIncrement(releaseMetrics.getReleaseVersionIncrement(releaseInput.getVersion(), repo, releaseInput.getBranch()));
releaseMetricsData.setReleaseNotes(releaseMetrics.getReleaseNotes(releaseInput.getVersion(), repo, releaseInput.getBranch()));
releaseMetricsData.setReleaseBranch(releaseMetrics.getReleaseBranch(releaseInput.getVersion(), repo));
String[] releaseOwners = releaseMetrics.getReleaseOwners(releaseInput.getVersion(), repo);
releaseMetricsData.setIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "open", false));
releaseMetricsData.setAutocutIssuesOpen(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "open", true));
releaseMetricsData.setIssuesClosed(releaseMetrics.getReleaseLabelIssues(releaseInput.getVersion(), repoName, "closed", false));
releaseMetricsData.setPullsOpen(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repoName, "open"));
releaseMetricsData.setPullsClosed(releaseMetrics.getReleaseLabelPulls(releaseInput.getVersion(), repoName, "closed"));
releaseMetricsData.setVersionIncrement(releaseMetrics.getReleaseVersionIncrement(releaseInput.getVersion(), repoName, releaseInput.getBranch()));
releaseMetricsData.setReleaseNotes(releaseMetrics.getReleaseNotes(releaseInput.getVersion(), repoName, releaseInput.getBranch()));
releaseMetricsData.setReleaseBranch(releaseMetrics.getReleaseBranch(releaseInput.getVersion(), repoName));
String[] releaseOwners = releaseMetrics.getReleaseOwners(releaseInput.getVersion(), repoName);

Check warning on line 184 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L176-L184

Added lines #L176 - L184 were not covered by tests
releaseMetricsData.setReleaseOwners(releaseOwners);
releaseMetricsData.setReleaseOwnerExists(Optional.ofNullable(releaseOwners)
.map(owners -> owners.length > 0)
.orElse(false));
String releaseIssue = releaseMetrics.getReleaseIssue(releaseInput.getVersion(), repo);
String releaseIssue = releaseMetrics.getReleaseIssue(releaseInput.getVersion(), repoName);

Check warning on line 189 in src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearchmetrics/metrics/MetricsCalculation.java#L189

Added line #L189 was not covered by tests
releaseMetricsData.setReleaseIssue(releaseIssue);
releaseMetricsData.setReleaseIssueExists(Optional.ofNullable(releaseIssue)
.map(str -> !str.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.inject.Inject;
import java.util.List;
import java.util.Map;

public class ReleaseMetrics {

Expand Down Expand Up @@ -39,10 +40,11 @@ public ReleaseMetrics(OpenSearchUtil openSearchUtil, ObjectMapper objectMapper,
this.releaseIssueChecker = releaseIssueChecker;
}

public List<String> getReleaseRepos(String releaseVersion) {
public Map<String, String> getReleaseRepos(String releaseVersion) {
return releaseRepoFetcher.getReleaseRepos(releaseVersion);
}


public Long getReleaseLabelIssues(String releaseVersion, String repo, String issueState, boolean autoCut) {
return releaseLabelIssuesFetcher.releaseLabelIssues(releaseVersion, repo, issueState, autoCut, openSearchUtil);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ReleaseRepoFetcher {

Expand All @@ -20,32 +19,28 @@ public class ReleaseRepoFetcher {
public ReleaseRepoFetcher() {
}

public List<String> getReleaseRepos(String releaseVersion) {
List<String> repoNames = new ArrayList<>();
repoNames.add("opensearch-build");
repoNames.add("performance-analyzer-rca");
repoNames.add("project-website");
public Map<String, String> getReleaseRepos(String releaseVersion) {
Map<String, String> repoMap = new HashMap<>();
String[] urls = {
String.format("https://raw.githubusercontent.com/opensearch-project/opensearch-build/main/manifests/%s/opensearch-%s.yml", releaseVersion, releaseVersion),
String.format("https://raw.githubusercontent.com/opensearch-project/opensearch-build/main/manifests/%s/opensearch-dashboards-%s.yml", releaseVersion, releaseVersion)
};
for (String url : urls) {
String responseBody = readUrl(url);
parseYaml(responseBody, repoNames);
parseYaml(responseBody, repoMap);
}
repoNames.addAll(releaseRepoExceptionList());
return repoNames.stream()
.distinct()
.collect(Collectors.toList());
repoMap.putAll(releaseRepoExceptionMap());
return repoMap;
}

public List<String> releaseRepoExceptionList() {
List<String> repoExceptionList = new ArrayList<>();
repoExceptionList.add("opensearch-build");
repoExceptionList.add("performance-analyzer-rca");
repoExceptionList.add("project-website");
repoExceptionList.add("documentation-website");
return repoExceptionList;

public Map<String, String> releaseRepoExceptionMap() {
Map<String, String> repoExceptionMap = new HashMap<>();
repoExceptionMap.put("opensearch-build", "opensearch-build");
repoExceptionMap.put("performance-analyzer-rca", "performance-analyzer-rca");
repoExceptionMap.put("project-website", "project-website");
repoExceptionMap.put("documentation-website", "documentation-website");
return repoExceptionMap;
};

public String readUrl(String url) {
Expand All @@ -70,7 +65,7 @@ public URL createURL(String url){
}


public void parseYaml(String responseBody, List<String> repoNames) {
public void parseYaml(String responseBody, Map<String, String> repoMap) {
new Yaml().loadAll(responseBody).forEach(document -> {
if (document instanceof Map) {
Map<?, ?> map = (Map<?, ?>) document;
Expand All @@ -79,21 +74,16 @@ public void parseYaml(String responseBody, List<String> repoNames) {
.flatMap(value -> ((List<?>) value).stream())
.filter(component -> component instanceof Map)
.map(component -> (Map<?, ?>) component)
.filter(componentMap -> {
Object repository = componentMap.get("repository");
return repository != null && repository.toString().contains("github.com");
})
.map(componentMap -> {
.forEach(componentMap -> {
String repoUrl = componentMap.get("repository").toString();
String componentName = componentMap.get("name").toString();

int startIndex = repoUrl.lastIndexOf('/') + 1;
int endIndex = repoUrl.lastIndexOf(".git");
if (repoUrl != null && endIndex != -1) {
return repoUrl.substring(startIndex, endIndex);
} else {
return repoUrl.substring(startIndex);
}
})
.forEach(repoNames::add);
String repoName = (endIndex != -1) ? repoUrl.substring(startIndex, endIndex) : repoUrl.substring(startIndex);

repoMap.put(repoName, componentName);
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ReleaseMetricsData {
@JsonProperty("repository")
private String repository;

@JsonProperty("component")
private String component;

@JsonProperty("release_version")
private String releaseVersion;

Expand Down Expand Up @@ -70,6 +73,7 @@ public String toJson(ObjectMapper mapper) throws JsonProcessingException {
data.put("id", id);
data.put("current_date", currentDate);
data.put("repository", repository);
data.put("component", component);
data.put("release_version", releaseVersion);
data.put("version", version);
data.put("release_state", releaseState);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.opensearchmetrics.metrics;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.index.query.BoolQueryBuilder;
import org.opensearchmetrics.metrics.general.*;
import org.opensearchmetrics.metrics.label.LabelMetrics;
import org.opensearchmetrics.metrics.release.ReleaseInputs;
import org.opensearchmetrics.metrics.release.ReleaseMetrics;
import org.opensearchmetrics.model.label.LabelData;
import org.opensearchmetrics.model.general.MetricsData;
import org.opensearchmetrics.model.release.ReleaseMetricsData;
import org.opensearchmetrics.util.OpenSearchUtil;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class MetricsCalculationTest {
@Mock
private OpenSearchUtil openSearchUtil;

@Mock
private ObjectMapper objectMapper;

@Mock
private UntriagedIssues untriagedIssues;
@Mock
private UncommentedPullRequests uncommentedPullRequests;
@Mock
private UnlabelledPullRequests unlabelledPullRequests;
@Mock
private UnlabelledIssues unlabelledIssues;
@Mock
private MergedPullRequests mergedPullRequests;
@Mock
private OpenPullRequests openPullRequests;
@Mock
private OpenIssues openIssues;
@Mock
private ClosedIssues closedIssues;
@Mock
private CreatedIssues createdIssues;
@Mock
private IssueComments issueComments;
@Mock
private PullComments pullComments;
@Mock
private IssuePositiveReactions issuePositiveReactions;
@Mock
private IssueNegativeReactions issueNegativeReactions;
@Mock
private LabelMetrics labelMetrics;
@Mock
private ReleaseMetrics releaseMetrics;

@InjectMocks
private MetricsCalculation metricsCalculation;


@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
metricsCalculation = new MetricsCalculation(openSearchUtil, objectMapper,
untriagedIssues, uncommentedPullRequests, unlabelledPullRequests, unlabelledIssues,
mergedPullRequests, openPullRequests, openIssues, closedIssues, createdIssues,
issueComments, pullComments, issuePositiveReactions, issueNegativeReactions,
labelMetrics, releaseMetrics);
}

@Test
void testGenerateGeneralMetrics() throws IOException {
List<String> repositories = Arrays.asList("repo1", "repo2");
MetricsData metricsData = new MetricsData();
metricsData.setId("id");
metricsData.setRepository("repo1");
metricsData.setCurrentDate(LocalDateTime.now(ZoneId.of("UTC")).toString());
metricsData.setMetricName("metric");
metricsData.setMetricCount(10L);
when(untriagedIssues.getBoolQueryBuilder(any())).thenReturn(new BoolQueryBuilder());
when(untriagedIssues.createSearchRequest(any(), any())).thenReturn(new SearchRequest());
when(untriagedIssues.performSearch(any(), any())).thenReturn(10L);
when(objectMapper.writeValueAsString(any())).thenReturn("json");
metricsCalculation.generateGeneralMetrics(repositories);
verify(openSearchUtil).createIndexIfNotExists("opensearch_general_metrics");
verify(openSearchUtil).bulkIndex(eq("opensearch_general_metrics"), any(Map.class));
}

@Test
void testGenerateLabelMetrics() throws IOException {
List<String> repositories = Arrays.asList("repo1", "repo2");
Map<String, List<Long>> labelInfo = new HashMap<>();
labelInfo.put("label1", Arrays.asList(5L, 10L));
LabelData labelData = new LabelData();
labelData.setId("id");
labelData.setRepository("repo1");
labelData.setCurrentDate(LocalDateTime.now(ZoneId.of("UTC")).toString());
labelData.setLabelName("label1");
labelData.setLabelPullCount(10L);
labelData.setLabelIssueCount(5L);
when(labelMetrics.getLabelInfo(any(), any())).thenReturn(labelInfo);
when(objectMapper.writeValueAsString(any())).thenReturn("json");
metricsCalculation.generateLabelMetrics(repositories);
verify(openSearchUtil).createIndexIfNotExists("opensearch_label_metrics");
verify(openSearchUtil).bulkIndex(eq("opensearch_label_metrics"), any(Map.class));
}

@Test
void testGenerateReleaseMetrics() {
Map<String, String> releaseRepos = new HashMap<>();
releaseRepos.put("repo1", "component1");
releaseRepos.put("repo2", "component2");
when(releaseMetrics.getReleaseRepos("2.13.0")).thenReturn(releaseRepos);
when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open", false)).thenReturn(10L);
when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open", true)).thenReturn(5L);
when(releaseMetrics.getReleaseLabelIssues(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "closed", false)).thenReturn(20L);
when(releaseMetrics.getReleaseLabelPulls(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "open")).thenReturn(3L);
when(releaseMetrics.getReleaseLabelPulls(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "closed")).thenReturn(8L);
when(releaseMetrics.getReleaseVersionIncrement(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "main")).thenReturn(true);
when(releaseMetrics.getReleaseNotes(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1", "main")).thenReturn(true);
when(releaseMetrics.getReleaseBranch(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn(true);
when(releaseMetrics.getReleaseOwners(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn(new String[]{"owner1", "owner2"});
when(releaseMetrics.getReleaseIssue(ReleaseInputs.VERSION_2_13_0.getVersion(), "repo1")).thenReturn("release-123");
metricsCalculation.generateReleaseMetrics();
verify(openSearchUtil).createIndexIfNotExists("opensearch_release_metrics");
verify(openSearchUtil).bulkIndex(eq("opensearch_release_metrics"), ArgumentMatchers.anyMap());
verify(openSearchUtil, times(1)).createIndexIfNotExists("opensearch_release_metrics");
}
}
Loading

0 comments on commit 310431b

Please sign in to comment.