Skip to content

Commit

Permalink
SONARPY-2457 Collect data for the Python version (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghislainpiot authored Dec 17, 2024
1 parent a22456e commit fc085d2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private List<PythonInputFile> parseNotebooks(List<PythonInputFile> pythonFiles,

for (PythonInputFile inputFile : pythonFiles) {
try {
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOTEBOOK_PRESENT_KEY, "1");
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOTEBOOK_PRESENT_KEY, true);
var result = IpynbNotebookParser.parseNotebook(inputFile);
result.ifPresent(generatedIPythonFiles::add);
} catch (Exception e) {
Expand All @@ -114,7 +114,7 @@ private List<PythonInputFile> parseNotebooks(List<PythonInputFile> pythonFiles,
}
}

sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOTEBOOK_EXCEPTION_KEY, String.valueOf(numberOfExceptions));
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.NOTEBOOK_EXCEPTION_KEY, numberOfExceptions);
return generatedIPythonFiles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public final class PythonSensor implements Sensor {
static final String UNSET_VERSION_WARNING = "Your code is analyzed as compatible with all Python 3 versions by default." +
" You can get a more precise analysis by setting the exact Python version in your configuration via the parameter \"sonar.python.version\"";

private final SensorTelemetryStorage sensorTelemetryStorage;

/**
* Constructor to be used by pico if neither PythonCustomRuleRepository nor PythonIndexer are to be found and injected.
*/
Expand Down Expand Up @@ -100,6 +102,7 @@ public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactor
this.indexer = indexer;
this.sonarLintCache = sonarLintCache;
this.analysisWarnings = analysisWarnings;
this.sensorTelemetryStorage = new SensorTelemetryStorage();
}

@Override
Expand All @@ -121,15 +124,27 @@ public void execute(SensorContext context) {
if (pythonVersionParameter.length != 0){
ProjectPythonVersion.setCurrentVersions(PythonVersionUtils.fromStringArray(pythonVersionParameter));
}
updatePythonVersionTelemetry(context, pythonVersionParameter);
CacheContext cacheContext = CacheContextImpl.of(context);
PythonIndexer pythonIndexer = this.indexer != null ? this.indexer : new SonarQubePythonIndexer(pythonFiles, cacheContext, context);
pythonIndexer.setSonarLintCache(sonarLintCache);
TypeShed.setProjectLevelSymbolTable(pythonIndexer.projectLevelSymbolTable());
PythonScanner scanner = new PythonScanner(context, checks, fileLinesContextFactory, noSonarFilter, PythonParser.create(), pythonIndexer);
scanner.execute(pythonFiles, context);
sensorTelemetryStorage.send(context);
durationReport.stop();
}

private void updatePythonVersionTelemetry(SensorContext context, String[] pythonVersionParameter) {
if (context.runtime().getProduct() == SonarProduct.SONARLINT) {
return;
}
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.PYTHON_VERSION_SET_KEY, pythonVersionParameter.length != 0);
if (pythonVersionParameter.length != 0) {
sensorTelemetryStorage.updateMetric(TelemetryMetricKey.PYTHON_VERSION_KEY, String.join(",", pythonVersionParameter));
}
}

private static List<PythonInputFile> getInputFiles(SensorContext context) {
FilePredicates p = context.fileSystem().predicates();
Iterable<InputFile> it = context.fileSystem().inputFiles(p.and(p.hasLanguage(Python.KEY)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public void updateMetric(TelemetryMetricKey key, int value) {
data.put(key, String.valueOf(value));
}

public void updateMetric(TelemetryMetricKey key, boolean value) {
data.put(key, boolToString(value));
}

private static String boolToString(boolean value) {
return value ? "1" : "0";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public enum TelemetryMetricKey {
NOTEBOOK_PRESENT_KEY("python.notebook.present"),
NOTEBOOK_TOTAL_KEY("python.notebook.total"),
NOTEBOOK_RECOGNITION_ERROR_KEY("python.notebook.recognition_error"),
NOTEBOOK_EXCEPTION_KEY("python.notebook.exceptions");
NOTEBOOK_EXCEPTION_KEY("python.notebook.exceptions"),
PYTHON_VERSION_SET_KEY("python.version.set"),
PYTHON_VERSION_KEY("python.version");

private final String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,36 @@ void test_scanner_isNotebook() {
assertThat(PythonScanner.isNotebook(notebookPythonFile)).isTrue();
}

@Test
void send_telemetry_with_version() {
activeRules = new ActiveRulesBuilder()
.addRule(new NewActiveRule.Builder()
.setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "S930"))
.build())
.build();

context.setSettings(new MapSettings().setProperty("sonar.python.version", "3.10,3.13"));
var contextSpy = spy(context);
PythonSensor sensor = sensor();
sensor.execute(contextSpy);
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_KEY.key(), "3.10,3.13");
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_SET_KEY.key(), "1");
}

@Test
void send_telemetry_no_version() {
activeRules = new ActiveRulesBuilder()
.addRule(new NewActiveRule.Builder()
.setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "S930"))
.build())
.build();

PythonSensor sensor = sensor();
var contextSpy = spy(context);
sensor.execute(contextSpy);
verify(contextSpy, times(1)).addTelemetryProperty(TelemetryMetricKey.PYTHON_VERSION_SET_KEY.key(), "0");
}

private com.sonar.sslr.api.Token passToken(URI uri) {
return com.sonar.sslr.api.Token.builder()
.setType(PythonKeyword.PASS)
Expand Down

0 comments on commit fc085d2

Please sign in to comment.