Skip to content

Commit

Permalink
Add router and interface poll feature (#88)
Browse files Browse the repository at this point in the history
* Add endpoint for singular event poll

* Enable singular event poll button in event details

* Drop redundant call to zinolib in singular poll

Poll in zinolib accepts both event ID and event

* Add endpoint for polling selected events in bulk

* Enable poll button in bulk updates menu

* Declare htmx indicator on bulk poll

* Add htmx indicator components
  • Loading branch information
podliashanyk authored Jun 12, 2024
1 parent 1cbd8b9 commit 3ac00e7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
45 changes: 44 additions & 1 deletion src/howitz/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from datetime import datetime, timezone

from werkzeug.exceptions import BadRequest, MethodNotAllowed
from werkzeug.exceptions import BadRequest, InternalServerError, MethodNotAllowed
from zinolib.controllers.zino1 import Zino1EventManager, RetryError, EventClosedError, UpdateHandler
from zinolib.event_types import Event, AdmState, PortState, BFDState, ReachabilityState, LogEntry, HistoryEntry
from zinolib.compat import StrEnum
Expand Down Expand Up @@ -499,6 +499,49 @@ def show_update_events_status_modal():
return render_template('/components/popups/modals/update-event-status-modal.html', current_state='open')


@main.route('/event/<i>/poll', methods=["POST"])
def poll(i):
selected_events = session.get("selected_events", {})
event_id = int(i)

poll_res = current_app.event_manager.poll(event_id)

if poll_res:
event_attr, event_logs, event_history, event_msgs = get_event_details(event_id)
event = create_table_event(current_app.event_manager.create_event_from_id(event_id))["event"]

return render_template('/responses/update-event-response.html', event=event, id=event_id, event_attr=event_attr,
event_logs=event_logs,
event_history=event_history, event_msgs=event_msgs,
is_selected=str(event_id) in selected_events)
else:
raise InternalServerError(description=f"Unexpected error when polling event #{event_id}")


@main.route('/event/bulk_poll', methods=['POST'])
def bulk_poll():
selected_events = session.get("selected_events", {})
expanded_events = session.get("expanded_events", {})
current_app.logger.debug('SELECTED EVENTS %s', selected_events)
current_app.logger.debug('EXPANDED EVENTS %s', expanded_events)

# Update each selected event with new values
for event_id in selected_events:
poll_res = current_app.event_manager.poll(int(event_id))

if not poll_res:
raise InternalServerError(description=f"Unexpected error when polling event #{event_id}")

# Clear selected events
session["selected_events"] = {}
session.modified = True # Necessary when modifying arrays/dicts/etc in flask session
current_app.logger.debug("SELECTED EVENTS %s", session["selected_events"])

# Rerender whole events table
event_list = get_current_events()
return render_template('/responses/bulk-update-events-status.html', event_list=event_list)


@main.route('/event/<event_id>/unselect', methods=["POST"])
def unselect_event(event_id):
session["selected_events"].pop(event_id, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
{% include "/components/accordion/update-state-btn.html" %}

<button
disabled
id="poll-event-{{ id }}"
class="text-white bg-blue-500 cursor-not-allowed font-medium rounded-lg text-sm px-3 py-2 mr-2 mb-2 text-center"
hx-post="/event/{{ id }}/poll"
hx-target="#event-accordion-row-{{ id }}"
hx-swap="outerHTML show:#event-msg-feed-{{ id }}:bottom"
hx-indicator="#row-{{ id }}-indicator"
class="text-white focus:ring-4 font-medium rounded-lg text-sm px-3 py-2 mr-2 mb-2 bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-blue-800"
>
Poll
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
</button>

<button
disabled
hx-post="/event/bulk_poll"
hx-target="#eventlist-list"
hx-swap="innerHTML"
hx-trigger="click"
id="bulk-poll-events"
class="text-white bg-blue-500 cursor-not-allowed font-medium rounded-lg text-sm px-3 py-2 mr-2 text-center"
>
hx-indicator="#bulk-poll-indicator"
class="text-white focus:ring-4 font-medium rounded-lg text-sm px-3 py-2 mr-2 bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-blue-800">
Poll
</button>

Expand Down

0 comments on commit 3ac00e7

Please sign in to comment.