Skip to content

Commit

Permalink
Add String Implementation - StatisticsAggregator (#678)
Browse files Browse the repository at this point in the history
* Add string output for statistics due to not implementation error

* Add return type annotations

* Empty commit
  • Loading branch information
jpdakran authored Mar 29, 2023
1 parent c6e53b7 commit 8d3b21f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
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) -> float:
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) -> float:
# 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

0 comments on commit 8d3b21f

Please sign in to comment.