From dfbd77c5041aaa2f1ed0d65eb56fbee9978bd0a9 Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Sat, 12 Oct 2024 13:11:55 +0200 Subject: [PATCH] fix: attempt to fix user tracker issues Signed-off-by: Alexander Trost --- .../dispatches/DispatchAssignModal.vue | 19 +++++++++++----- .../dispatches/DispatchDetailsSlideover.vue | 13 ++++++++--- .../centrum/livemap/CentrumSidebar.vue | 22 ++++++++++++++----- app/components/livemap/MapUsersLayer.vue | 8 ++++++- .../notification/NotificationProvider.vue | 6 ++++- pkg/server/http.go | 9 ++++++-- pkg/tracker/manager.go | 6 ++--- pkg/tracker/tracker.go | 6 +++-- 8 files changed, 66 insertions(+), 23 deletions(-) diff --git a/app/components/centrum/dispatches/DispatchAssignModal.vue b/app/components/centrum/dispatches/DispatchAssignModal.vue index 550b82c95..e673dba16 100644 --- a/app/components/centrum/dispatches/DispatchAssignModal.vue +++ b/app/components/centrum/dispatches/DispatchAssignModal.vue @@ -14,7 +14,7 @@ const props = defineProps<{ dispatchId: string; }>(); -const dispatch = computed(() => dispatches.value.get(props.dispatchId)!); +const dispatch = computed(() => dispatches.value.get(props.dispatchId)); const { isOpen } = useModal(); @@ -25,17 +25,21 @@ const schema = z.object({ type Schema = z.output; const state = reactive({ - units: [...dispatch.value.units.map((du) => du.unitId)], + units: [], }); async function assignDispatch(): Promise { + if (dispatch === undefined) { + return; + } + try { const toAdd: string[] = []; const toRemove: string[] = []; state.units.forEach((u) => { toAdd.push(u); }); - dispatch.value.units?.forEach((u) => { + dispatch.value?.units?.forEach((u) => { const idx = state.units.findIndex((su) => su === u.unitId); if (idx === -1) { toRemove.push(u.unitId); @@ -101,7 +105,12 @@ const grouped = computedAsync(async () => { return groups; }); -watch(dispatch.value, () => (state.units = [...dispatch.value.units.map((du) => du.unitId)])); +function updateDispatchUnits(): void { + state.units = [...(dispatch.value?.units?.map((du) => du.unitId) ?? [])]; +} + +watch(dispatch, () => updateDispatchUnits); +updateDispatchUnits(); const canSubmit = ref(true); const onSubmitThrottle = useThrottleFn(async () => { @@ -128,7 +137,7 @@ const onSubmitThrottle = useThrottleFn(async () => {

{{ $t('components.centrum.assign_dispatch.title') }}: - +

diff --git a/app/components/centrum/dispatches/DispatchDetailsSlideover.vue b/app/components/centrum/dispatches/DispatchDetailsSlideover.vue index a0428373a..954e71ca6 100644 --- a/app/components/centrum/dispatches/DispatchDetailsSlideover.vue +++ b/app/components/centrum/dispatches/DispatchDetailsSlideover.vue @@ -34,7 +34,7 @@ const { canDo } = centrumStore; const notifications = useNotificatorStore(); -const dispatch = computed(() => (props.dispatch ? props.dispatch : dispatches.value.get(props.dispatchId)!)); +const dispatch = computed(() => (props.dispatch ? props.dispatch : dispatches.value.get(props.dispatchId))); async function selfAssign(id: string): Promise { if (ownUnitId.value === undefined) { @@ -69,12 +69,19 @@ async function deleteDispatch(id: string): Promise { } } -const dispatchStatusColors = computed(() => dispatchStatusToBGColor(dispatch.value.status?.status)); +const dispatchStatusColors = computed(() => dispatchStatusToBGColor(dispatch.value?.status?.status)); + +watch(dispatch, () => { + if (dispatch.value === undefined) { + isOpen.value = false; + } +}); diff --git a/app/components/livemap/MapUsersLayer.vue b/app/components/livemap/MapUsersLayer.vue index aa042f4be..3422078be 100644 --- a/app/components/livemap/MapUsersLayer.vue +++ b/app/components/livemap/MapUsersLayer.vue @@ -31,7 +31,13 @@ const settingsStore = useSettingsStore(); const { livemap } = storeToRefs(settingsStore); onBeforeMount(async () => { - useTimeoutFn(async () => startStream(), 50); + useTimeoutFn(async () => { + try { + startStream(); + } catch (e) { + logger.error('exception during map users stream', e); + } + }, 50); }); onBeforeUnmount(async () => stopStream()); diff --git a/app/components/partials/notification/NotificationProvider.vue b/app/components/partials/notification/NotificationProvider.vue index 0f65e7fa7..8b8169470 100644 --- a/app/components/partials/notification/NotificationProvider.vue +++ b/app/components/partials/notification/NotificationProvider.vue @@ -18,7 +18,11 @@ const { startStream, stopStream } = notificatorStore; async function toggleStream(): Promise { // Only stream notifications when a user is logged in and has a character selected if (username.value !== null && activeChar.value !== null) { - startStream(); + try { + startStream(); + } catch (e) { + logger.error('exception during notification stream', e); + } } else if (abort.value !== undefined) { await stopStream(); notificatorStore.$reset(); diff --git a/pkg/server/http.go b/pkg/server/http.go index 5f1ae2547..1a4a59911 100644 --- a/pkg/server/http.go +++ b/pkg/server/http.go @@ -199,10 +199,15 @@ func NewEngine(p EngineParams) (*gin.Engine, error) { ) ginWrappedGrpc := func(c *gin.Context) { if cip := c.ClientIP(); cip != "" { + _, port, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)) + if err != nil || port == "" { + port = "1" + } + if strings.Count(cip, ":") > 1 { - c.Request.RemoteAddr = "[" + cip + "]:80" + c.Request.RemoteAddr = "[" + cip + "]:" + port } else { - c.Request.RemoteAddr = cip + ":80" + c.Request.RemoteAddr = cip + ":" + port } } diff --git a/pkg/tracker/manager.go b/pkg/tracker/manager.go index 4e06d4ebd..e96d9395b 100644 --- a/pkg/tracker/manager.go +++ b/pkg/tracker/manager.go @@ -122,9 +122,7 @@ func (m *Manager) refreshCache(ctx context.Context) { defer span.End() if err := m.refreshUserLocations(ctx); err != nil { - m.logger.Error("failed to refresh livemap users cache", zap.Error(err)) - // Return here so we don't taint the user "on-duty" cache/list - return + m.logger.Error("failed to refresh user tracker cache", zap.Error(err)) } } @@ -173,6 +171,8 @@ func (m *Manager) cleanupUserIDs(ctx context.Context, found map[int32]interface{ } func (m *Manager) refreshUserLocations(ctx context.Context) error { + m.logger.Debug("refreshing user tracker cache") + tLocs := tLocs.AS("markerInfo") stmt := tLocs. SELECT( diff --git a/pkg/tracker/tracker.go b/pkg/tracker/tracker.go index 4ba3968e9..c4683268b 100644 --- a/pkg/tracker/tracker.go +++ b/pkg/tracker/tracker.go @@ -2,6 +2,7 @@ package tracker import ( "context" + "fmt" "github.com/fivenet-app/fivenet/gen/go/proto/resources/livemap" "github.com/fivenet-app/fivenet/pkg/events" @@ -78,7 +79,7 @@ func New(p Params) (ITracker, error) { go t.broker.Start(ctx) - userIDs, err := store.NewWithLocks(ctx, p.Logger, p.JS, "tracker", nil, + userIDs, err := store.NewWithLocks(c, p.Logger, p.JS, "tracker", nil, func(s *store.Store[livemap.UserMarker, *livemap.UserMarker]) error { s.OnUpdate = func(um *livemap.UserMarker) (*livemap.UserMarker, error) { if um == nil || um.Info == nil { @@ -106,6 +107,7 @@ func New(p Params) (ITracker, error) { return nil } + return nil }) if err != nil { @@ -118,7 +120,7 @@ func New(p Params) (ITracker, error) { t.userStore = userIDs if err := t.registerSubscriptions(c); err != nil { - return err + return fmt.Errorf("failed to register tracker nats subscriptions. %w", err) } return nil