Skip to content

Commit

Permalink
Improve percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-carter-at-sf committed Dec 10, 2024
1 parent 39ada2b commit c17c95d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class CpdRunner {
public Map<String, CpdLanguageRunResults> run(CpdRunInputData runInputData) throws IOException {
validateRunInputData(runInputData);

List<String> languagesToProcess = new ArrayList<>(runInputData.runDataPerLanguage.keySet());
progressReporter.initialize(languagesToProcess);
Map<String, Integer> languageFileCounts = runInputData.runDataPerLanguage.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().filesToScan.size()));
progressReporter.initialize(languageFileCounts);

Map<String, CpdLanguageRunResults> results = new HashMap<>();
for (String language : languagesToProcess) {
for (String language : runInputData.runDataPerLanguage.keySet()) {
CpdRunInputData.LanguageSpecificRunData languageSpecificRunData = runInputData.runDataPerLanguage.get(language);
List<Path> pathsToScan = languageSpecificRunData.filesToScan.stream().map(Paths::get).collect(Collectors.toList());
CpdLanguageRunResults languageRunResults = runLanguage(
Expand All @@ -49,6 +50,9 @@ public Map<String, CpdLanguageRunResults> run(CpdRunInputData runInputData) thro
}

private CpdLanguageRunResults runLanguage(String language, List<Path> pathsToScan, int minimumTokens, boolean skipDuplicateFiles) throws IOException {
System.out.println("Running CPD for language '" + language + "' with " + pathsToScan.size() +
" file(s) to scan using minimumTokens=" + minimumTokens + " and skipDuplicateFiles=" + skipDuplicateFiles + ".");

// Note that the name "minimumTokens" comes from the public facing documentation and the cli but
// behind the scenes, it maps to MinimumTileSize. To learn more about the mappings to the config, see:
// https://github.com/pmd/pmd/blob/main/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ public class PmdRunner {
PmdRunResults run(PmdRunInputData inputData) {
validateRunInputData(inputData);

List<String> languagesToProcess = new ArrayList<>(inputData.runDataPerLanguage.keySet());
progressReporter.initialize(languagesToProcess);
Map<String, Integer> languageFileCounts = inputData.runDataPerLanguage.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().filesToScan.size()));
progressReporter.initialize(languageFileCounts);

PmdRunResults runResults = new PmdRunResults();
for (String language: languagesToProcess) {
for (String language: inputData.runDataPerLanguage.keySet()) {
runLanguage(language, inputData, runResults);
}
return runResults;
Expand All @@ -37,6 +38,8 @@ PmdRunResults run(PmdRunInputData inputData) {
private void runLanguage(String language, PmdRunInputData inputData, PmdRunResults runResults) {
PmdRunInputData.LanguageSpecificRunData languageSpecificRunData = inputData.runDataPerLanguage.get(language);

System.out.println("Running PMD rules for language '" + language + "' with " + languageSpecificRunData.filesToScan.size() + " file(s) to scan.");

PMDConfiguration config = new PMDConfiguration();
config.addRuleSet(inputData.ruleSetInputFile);
List<Path> inputPathList = languageSpecificRunData.filesToScan.stream().map(Paths::get).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

// This class helps us track the overall progress of all language runs
public class ProgressReporter {
private Map<String, Float> languageWeight = new HashMap<>();
private Map<String, Float> progressPerLanguage = new HashMap<>();
private float lastReportedProgress = 0.0f;

public void initialize(List<String> languages) {
public void initialize(Map<String, Integer> languageFileCounts) {
progressPerLanguage = new HashMap<>();
languages.forEach(l -> this.updateProgressForLanguage(l, 0.0f));
languageWeight = new HashMap<>();
int totalNumFilesToScan = languageFileCounts.values().stream().mapToInt(Integer::intValue).sum();
for (Map.Entry<String,Integer> entry : languageFileCounts.entrySet()) {
String language = entry.getKey();
Integer numFiles = entry.getValue();
languageWeight.put(language, (float) numFiles / totalNumFilesToScan);
this.updateProgressForLanguage(language, 0.0f);

}
}

public void updateProgressForLanguage(String language, float percComplete) {
Expand All @@ -28,10 +37,10 @@ public void reportOverallProgress() {
}

private float calculateOverallPercentage() {
float sum = 0.0f;
for (float progress : progressPerLanguage.values()) {
sum += progress;
float overallPerc = 0.0f;
for (String language : progressPerLanguage.keySet()) {
overallPerc += (progressPerLanguage.get(language) * languageWeight.get(language));
}
return sum / progressPerLanguage.size();
return overallPerc;
}
}
3 changes: 2 additions & 1 deletion packages/code-analyzer-pmd-engine/src/cpd-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export class CpdWrapperInvoker {
if (stdOutMsg.startsWith(STDOUT_PROGRESS_MARKER)) {
const cpdWrapperProgress: number = parseFloat(stdOutMsg.slice(STDOUT_PROGRESS_MARKER.length));
emitProgress(10 + 80*(cpdWrapperProgress/100)); // 10 to 90%
} else {
this.emitLogEvent(LogLevel.Fine, `[JAVA StdOut]: ${stdOutMsg}`);
}
this.emitLogEvent(LogLevel.Fine, `[JAVA StdOut]: ${stdOutMsg}`);
});

try {
Expand Down
3 changes: 2 additions & 1 deletion packages/code-analyzer-pmd-engine/src/pmd-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ function toSeverityLevel(pmdRulePriority: string): SeverityLevel {

function toViolation(pmdViolation: PmdViolation): Violation {
return {
ruleName: toUniqueRuleName(pmdViolation.rule, extensionToLanguageId[path.extname(pmdViolation.codeLocation.file)]),
ruleName: toUniqueRuleName(pmdViolation.rule,
extensionToLanguageId[path.extname(pmdViolation.codeLocation.file).toLowerCase()]),
message: pmdViolation.message,
codeLocations: [{
file: pmdViolation.codeLocation.file,
Expand Down
2 changes: 1 addition & 1 deletion packages/code-analyzer-pmd-engine/src/pmd-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class PmdWrapperInvoker {
await this.javaCommandExecutor.exec(javaCmdArgs, javaClassPaths, (stdOutMsg: string) => {
if (stdOutMsg.startsWith(STDOUT_PROGRESS_MARKER)) {
const pmdWrapperProgress: number = parseFloat(stdOutMsg.slice(STDOUT_PROGRESS_MARKER.length));
emitProgress(10 + (80 * pmdWrapperProgress / 100)); // 10 to 90%
emitProgress(10 + 80*(pmdWrapperProgress/100)); // 10 to 90%
} else {
this.emitLogEvent(LogLevel.Fine, `[JAVA StdOut]: ${stdOutMsg}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/code-analyzer-pmd-engine/test/pmd-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe('Tests for the runRules method of PmdEngine', () => {

// Also check that we have all the correct progress events
expect(progressEvents.map(e => e.percentComplete)).toEqual(
[2, 2.3, 4.4, 4.7, 5, 6.86, 10.58, 14.3, 23.6, 32.9, 42.2, 51.5, 70.1, 88.7, 93.35, 98, 100]);
[2, 2.3, 4.4, 4.7, 5, 6.86, 10.58, 14.3, 26.7, 39.1, 51.5, 63.9, 76.3, 88.7, 93.35, 98, 100]);
});

it('When a single rule is selected, then return only violations for that rule', async () => {
Expand Down

0 comments on commit c17c95d

Please sign in to comment.