Skip to content

Commit

Permalink
add search field by grep
Browse files Browse the repository at this point in the history
  • Loading branch information
imankarimi committed Jul 31, 2023
1 parent 810ef0a commit df04efd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion log_reader/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def get_urls(self):

def changelist_view(self, request, extra_context=None):
filename = request.GET.get('file_name', settings.LOG_READER_DEFAULT_FILE)
is_valid, file_contents = read_file_lines(file_name=filename)
search = request.GET.get('q', None) or None
is_valid, file_contents = read_file_lines(file_name=filename, search=search)
if not is_valid:
self.message_user(request, file_contents, level=messages.ERROR)
log_files = get_log_files(settings.LOG_READER_DIR_PATH)
Expand Down
3 changes: 3 additions & 0 deletions log_reader/static/log_reader/css/log_reader.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
color: #ffffff;
cursor: pointer;
border: 1px solid #d0d0d0;
position: fixed;
right: 315px;
top: 156px;
}
7 changes: 7 additions & 0 deletions log_reader/templates/log_reader/admin/change_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ <h1>File Logs</h1>
<div class="changelist-form-container">{% endif %}
{% if file_contents %}
<div id="toolbar">
<form id="changelist-search" method="get">
<div><!-- DIV needed for valid HTML -->
<label for="searchbar"><img src="{% static "admin/img/search.svg" %}" alt="Search"></label>
<input type="text" size="40" name="q" value="{{ request.GET.q }}" id="searchbar" autofocus="">
<input type="submit" value="Search">
</div>
</form>
<button id="DownloadLogFile" type="submit">Download</button>
</div>
{% endif %}
Expand Down
23 changes: 16 additions & 7 deletions log_reader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ def get_log_files(directory):
return []


def read_file_lines(file_name):
def read_file_lines(file_name, search=None):
if file_name not in get_log_files(settings.LOG_READER_DIR_PATH):
return False, _("%s file, not found. Please try again." % file_name)

try:
file_path = '%s/%s' % (settings.LOG_READER_DIR_PATH, file_name)
result = subprocess.run(
['tail', '-%s' % settings.LOG_READER_MAX_READ_LINES, file_path],
stdout=PIPE,
stderr=PIPE,
encoding="utf8",
)

if search:
result = subprocess.run(
['grep', '-m %s' % settings.LOG_READER_MAX_READ_LINES, search, file_path],
stdout=PIPE,
stderr=PIPE,
encoding="utf8",
)
else:
result = subprocess.run(
['tail', '-%s' % settings.LOG_READER_MAX_READ_LINES, file_path],
stdout=PIPE,
stderr=PIPE,
encoding="utf8",
)
content = repr(result.stdout) if result.stdout else None
except Exception as e:
return False, str(e)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-log-reader',
version='1.1.6',
version='1.1.7',
zip_safe=False,
packages=find_packages(),
include_package_data=True,
Expand Down

0 comments on commit df04efd

Please sign in to comment.