Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String Implementation - StatisticsAggregator #678

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions detect_secrets/audit/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ def _get_plugin_counter(self, secret_type: str) -> 'StatisticsCounter':
return cast(StatisticsCounter, self.data[secret_type]['stats'])

def __str__(self) -> str:
raise NotImplementedError
output = ''

for secret_type, framework in self.data.items():
output += f'Plugin: {get_mapping_from_secret_type_to_class()[secret_type].__name__}\n'
for value in framework.values():
output += f'Statistics: {value}\n\n'

return output

def json(self) -> Dict[str, Any]:
output = {}
Expand All @@ -77,19 +84,36 @@ def __init__(self) -> None:
self.incorrect: int = 0
self.unknown: int = 0

def __repr__(self) -> str:
def __str__(self) -> str:
return (
f'{self.__class__.__name__}(correct={self.correct}, '
'incorrect={self.incorrect}, unknown={self.unknown},)'
f'True Positives: {self.correct}, False Positives: {self.incorrect}, '
f'Unknown: {self.unknown}, Precision: {self.calculate_precision()}, '
f'Recall: {self.calculate_recall()}'
)

def json(self) -> Dict[str, Any]:
return {
'raw': {
'true-positives': self.correct,
'false-positives': self.incorrect,
'unknown': self.unknown,
},
'score': {
'precision': self.calculate_precision(),
'recall': self.calculate_recall(),
},
}

def calculate_precision(self):
precision = (
round(float(self.correct) / (self.correct + self.incorrect), 4)
if (self.correct and self.incorrect)
else 0.0
)

return precision

def calculate_recall(self):
# NOTE(2020-11-08|domanchi): This isn't the formal definition of `recall`, however,
# this is the definition that we're going to attribute to it.
#
Expand Down Expand Up @@ -124,14 +148,4 @@ def json(self) -> Dict[str, Any]:
else 0.0
)

return {
'raw': {
'true-positives': self.correct,
'false-positives': self.incorrect,
'unknown': self.unknown,
},
'score': {
'precision': precision,
'recall': recall,
},
}
return recall
11 changes: 8 additions & 3 deletions tests/audit/analytics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ def test_no_divide_by_zero(secret):
main(['audit', f.name, '--stats', '--json'])


@pytest.mark.skip(reason='TODO')
def test_basic_statistics_str():
pass
def test_basic_statistics_str(printer):
with labelled_secrets() as filename:
main(['audit', filename, '--stats'])

assert printer.message == (
'Plugin: BasicAuthDetector\nStatistics: True Positives: 1, ' +
'False Positives: 2, Unknown: 1, Precision: 0.3333, Recall: 0.5\n\n\n'
)


@contextmanager
Expand Down