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

Fixes: #18241 - Script results log_threshold should default to Default #18501

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions netbox/extras/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,22 @@ class JournalEntryKindChoices(ChoiceSet):
class LogLevelChoices(ChoiceSet):

LOG_DEBUG = 'debug'
LOG_DEFAULT = 'default'
LOG_INFO = 'info'
LOG_SUCCESS = 'success'
LOG_WARNING = 'warning'
LOG_FAILURE = 'failure'

CHOICES = (
(LOG_DEBUG, _('Debug'), 'teal'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed? I think the ordering is supposed to be of increasing severity. Also this should stay consistent with the order of the constants above and in LOG_LEVEL_RANK.

Is there something gained by changing the order here and the .first to .last in the template?

(LOG_DEFAULT, _('Default'), 'gray'),
(LOG_INFO, _('Info'), 'cyan'),
(LOG_SUCCESS, _('Success'), 'green'),
(LOG_WARNING, _('Warning'), 'yellow'),
(LOG_FAILURE, _('Failure'), 'red'),

)

SYSTEM_LEVELS = {
LOG_DEBUG: logging.DEBUG,
LOG_DEFAULT: logging.INFO,
LOG_INFO: logging.INFO,
LOG_SUCCESS: logging.INFO,
LOG_WARNING: logging.WARNING,
Expand Down
9 changes: 4 additions & 5 deletions netbox/extras/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@

LOG_LEVEL_RANK = {
LogLevelChoices.LOG_DEBUG: 0,
LogLevelChoices.LOG_DEFAULT: 1,
LogLevelChoices.LOG_INFO: 2,
LogLevelChoices.LOG_SUCCESS: 3,
LogLevelChoices.LOG_WARNING: 4,
LogLevelChoices.LOG_FAILURE: 5,
LogLevelChoices.LOG_INFO: 1,
LogLevelChoices.LOG_SUCCESS: 2,
LogLevelChoices.LOG_WARNING: 3,
LogLevelChoices.LOG_FAILURE: 4,
}
2 changes: 1 addition & 1 deletion netbox/extras/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Report(BaseScript):

# There is no generic log() equivalent on BaseScript
def log(self, message):
self._log(message, None, level=LogLevelChoices.LOG_DEFAULT)
self._log(message, None, level=LogLevelChoices.LOG_INFO)

def log_success(self, obj=None, message=None):
super().log_success(message, obj)
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def as_form(self, data=None, files=None, initial=None):
# Logging
#

def _log(self, message, obj=None, level=LogLevelChoices.LOG_DEFAULT):
def _log(self, message, obj=None, level=LogLevelChoices.LOG_INFO):
"""
Log a message. Do not call this method directly; use one of the log_* wrappers below.
"""
Expand Down
12 changes: 6 additions & 6 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,17 +1315,17 @@ def get_table(self, job, request, bulk_actions=True):
index = 0

try:
log_threshold = LOG_LEVEL_RANK[request.GET.get('log_threshold', LogLevelChoices.LOG_DEBUG)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these supposed to be changed?

log_threshold = LOG_LEVEL_RANK[request.GET.get('log_threshold', LogLevelChoices.LOG_INFO)]
except KeyError:
log_threshold = LOG_LEVEL_RANK[LogLevelChoices.LOG_DEBUG]
log_threshold = LOG_LEVEL_RANK[LogLevelChoices.LOG_INFO]
if job.data:

if 'log' in job.data:
if 'tests' in job.data:
tests = job.data['tests']

for log in job.data['log']:
log_level = LOG_LEVEL_RANK.get(log.get('status'), LogLevelChoices.LOG_DEFAULT)
log_level = LOG_LEVEL_RANK.get(log.get('status'), LogLevelChoices.LOG_INFO)
if log_level >= log_threshold:
index += 1
result = {
Expand All @@ -1348,7 +1348,7 @@ def get_table(self, job, request, bulk_actions=True):
for method, test_data in tests.items():
if 'log' in test_data:
for time, status, obj, url, message in test_data['log']:
log_level = LOG_LEVEL_RANK.get(status, LogLevelChoices.LOG_DEFAULT)
log_level = LOG_LEVEL_RANK.get(status, LogLevelChoices.LOG_INFO)
if log_level >= log_threshold:
index += 1
result = {
Expand All @@ -1374,9 +1374,9 @@ def get(self, request, **kwargs):
if job.completed:
table = self.get_table(job, request, bulk_actions=False)

log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_DEBUG)
log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO)
if log_threshold not in LOG_LEVEL_RANK:
log_threshold = LogLevelChoices.LOG_DEBUG
log_threshold = LogLevelChoices.LOG_INFO

context = {
'script': job.object,
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/extras/script_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<div class="dropdown-menu">
{% for level, name in log_levels.items %}
<a class="dropdown-item d-flex justify-content-between" href="{% url 'extras:script_result' job_pk=job.pk %}?log_threshold={{ level }}">
{{ name }}{% if forloop.first %} ({% trans "All" %}){% endif %}
{{ name }}{% if forloop.counter == 1 %} ({% trans "All" %}){% elif forloop.counter == 2 %} ({% trans "Default" %}){% endif %}
{% if level == log_threshold %}<span class="badge bg-green ms-auto"></span>{% endif %}
</a>
{% endfor %}
Expand Down