-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor spoon/qodana mining to event based strategy (#829)
- Loading branch information
1 parent
e0a3031
commit b776ccc
Showing
15 changed files
with
295 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
.../src/main/java/io/github/martinwitt/laughing_train/mining/AnalyzerResultsPersistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.github.martinwitt.laughing_train.mining; | ||
|
||
import com.google.common.flogger.FluentLogger; | ||
import io.github.martinwitt.laughing_train.data.Project; | ||
import io.github.martinwitt.laughing_train.data.result.CodeAnalyzerResult; | ||
import io.github.martinwitt.laughing_train.domain.entity.AnalyzerStatus; | ||
import io.github.martinwitt.laughing_train.domain.entity.GitHubCommit; | ||
import io.github.martinwitt.laughing_train.mining.requests.MineNextProject; | ||
import io.github.martinwitt.laughing_train.mining.requests.StoreResults; | ||
import io.github.martinwitt.laughing_train.persistence.repository.ProjectRepository; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.vertx.core.eventbus.EventBus; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AnalyzerResultsPersistence { | ||
|
||
public static final String SERVICE_NAME = "analyzerResultsPersistence"; | ||
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); | ||
ProjectRepository projectRepository; | ||
EventBus eventBus; | ||
|
||
public AnalyzerResultsPersistence(ProjectRepository projectRepository, EventBus eventBus) { | ||
this.projectRepository = projectRepository; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
@ConsumeEvent(value = SERVICE_NAME, blocking = true) | ||
void persistResults(StoreResults storeResults) { | ||
Project project = storeResults.project(); | ||
CodeAnalyzerResult result = storeResults.result(); | ||
addOrUpdateCommitHash(project, result, SERVICE_NAME); | ||
if (result instanceof CodeAnalyzerResult.Failure failure) { | ||
logger.atInfo().log("Analyzer %s failed for project %s", SERVICE_NAME, project.name()); | ||
|
||
} else if (result instanceof CodeAnalyzerResult.Success success) { | ||
logger.atInfo().log("Analyzer %s succeeded for project %s", SERVICE_NAME, project.name()); | ||
} | ||
eventBus.publish("miner", new MineNextProject(storeResults.analyzerName())); | ||
} | ||
|
||
private AnalyzerStatus getAnalyzerStatus(CodeAnalyzerResult spoonResult, String name) { | ||
AnalyzerStatus analyzerStatus = null; | ||
if (spoonResult instanceof CodeAnalyzerResult.Success success) { | ||
analyzerStatus = AnalyzerStatus.success(name, success.results().size()); | ||
} else if (spoonResult instanceof CodeAnalyzerResult.Failure failure) { | ||
analyzerStatus = AnalyzerStatus.failure(name, 0); | ||
} | ||
return analyzerStatus; | ||
} | ||
|
||
private void addOrUpdateCommitHash(Project project, CodeAnalyzerResult spoonResult, String analyzerName) { | ||
String name = project.name(); | ||
String commitHash = project.commitHash(); | ||
List<io.github.martinwitt.laughing_train.domain.entity.Project> list = | ||
projectRepository.findByProjectUrl(project.url()); | ||
AnalyzerStatus analyzerStatus = getAnalyzerStatus(spoonResult, analyzerName); | ||
if (list.isEmpty()) { | ||
io.github.martinwitt.laughing_train.domain.entity.Project newProject = | ||
new io.github.martinwitt.laughing_train.domain.entity.Project(name, project.url()); | ||
newProject.addCommitHash(commitHash); | ||
var commits = newProject.getCommits(); | ||
commits.stream() | ||
.filter(v -> v.getCommitHash().equals(commitHash)) | ||
.findFirst() | ||
.ifPresent(v -> { | ||
v.addAnalyzerStatus(analyzerStatus); | ||
}); | ||
projectRepository.create(newProject); | ||
} else { | ||
logger.atInfo().log("Updating commit hash for %s", name); | ||
var oldProject = list.get(0); | ||
oldProject.addCommitHash(commitHash); | ||
var commits = oldProject.getCommits(); | ||
GitHubCommit gitHubCommit = new GitHubCommit(commitHash, new ArrayList<>()); | ||
commits.add(gitHubCommit); | ||
gitHubCommit.addAnalyzerStatus(analyzerStatus); | ||
oldProject.addCommitHash(gitHubCommit); | ||
projectRepository.save(oldProject); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/CodeMiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.martinwitt.laughing_train.mining; | ||
|
||
public interface CodeMiner { | ||
|
||
String getAnalyzerName(); | ||
} |
20 changes: 20 additions & 0 deletions
20
github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.github.martinwitt.laughing_train.mining; | ||
|
||
import io.github.martinwitt.laughing_train.mining.requests.MineNextProject; | ||
import io.quarkus.runtime.StartupEvent; | ||
import io.vertx.mutiny.core.Vertx; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
|
||
@ApplicationScoped | ||
public class MiningStartup { | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
void startup(@Observes StartupEvent event) { | ||
vertx.eventBus().send("miner", new MineNextProject(QodanaPeriodicMiner.ANALYZER_NAME)); | ||
vertx.eventBus().send("miner", new MineNextProject(SpoonPeriodicMiner.ANALYZER_NAME)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/ProjectSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.github.martinwitt.laughing_train.mining; | ||
|
||
import io.github.martinwitt.laughing_train.data.ProjectRequest; | ||
import io.github.martinwitt.laughing_train.data.ProjectResult; | ||
import io.github.martinwitt.laughing_train.domain.entity.Project; | ||
import io.github.martinwitt.laughing_train.mining.requests.GetProject; | ||
import io.github.martinwitt.laughing_train.persistence.repository.ProjectRepository; | ||
import io.github.martinwitt.laughing_train.services.ProjectService; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import java.io.IOException; | ||
import java.util.Random; | ||
|
||
public class ProjectSupplier { | ||
|
||
public static final String SERVICE_NAME = "projectSupplier"; | ||
|
||
final SearchProjectService searchProjectService; | ||
final ProjectRepository projectRepository; | ||
final ProjectService projectService; | ||
private static final Random random = new Random(); | ||
|
||
ProjectSupplier( | ||
SearchProjectService searchProjectService, | ||
ProjectRepository projectRepository, | ||
ProjectService projectService) { | ||
this.searchProjectService = searchProjectService; | ||
this.projectRepository = projectRepository; | ||
this.projectService = projectService; | ||
} | ||
|
||
@ConsumeEvent(value = SERVICE_NAME, blocking = true) | ||
ProjectResult supplyProject(GetProject getProject) { | ||
try { | ||
Project project = getRandomProject(); | ||
return checkoutProject(project); | ||
} catch (IOException e) { | ||
return new ProjectResult.Error(e.getMessage()); | ||
} | ||
} | ||
|
||
private ProjectResult checkoutProject(Project project) throws IOException { | ||
return projectService.handleProjectRequest(new ProjectRequest.WithUrl(project.getProjectUrl())); | ||
} | ||
|
||
private Project getRandomProject() throws IOException { | ||
if (random.nextBoolean()) { | ||
return searchProjectService.searchProjectOnGithub(); | ||
} else { | ||
return getKnownProject(); | ||
} | ||
} | ||
|
||
private Project getKnownProject() { | ||
var list = projectRepository.getAll(); | ||
return list.get(random.nextInt(list.size())); | ||
} | ||
} |
Oops, something went wrong.