Skip to content

Commit

Permalink
fix: attempt to fix user tracker issues
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Oct 12, 2024
1 parent d74481e commit dfbd77c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 23 deletions.
19 changes: 14 additions & 5 deletions app/components/centrum/dispatches/DispatchAssignModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,17 +25,21 @@ const schema = z.object({
type Schema = z.output<typeof schema>;
const state = reactive<Schema>({
units: [...dispatch.value.units.map((du) => du.unitId)],
units: [],
});
async function assignDispatch(): Promise<void> {
if (dispatch === undefined) {

Check failure on line 32 in app/components/centrum/dispatches/DispatchAssignModal.vue

View workflow job for this annotation

GitHub Actions / node-tests

Must use `.value` to read or write the value wrapped by `computed()`
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);
Expand Down Expand Up @@ -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 () => {
Expand All @@ -128,7 +137,7 @@ const onSubmitThrottle = useThrottleFn(async () => {
<div class="flex items-center justify-between">
<h3 class="inline-flex items-center text-2xl font-semibold leading-6">
{{ $t('components.centrum.assign_dispatch.title') }}:
<IDCopyBadge :id="dispatch.id" class="ml-2" prefix="DSP" />
<IDCopyBadge :id="dispatch?.id ?? dispatchId" class="ml-2" prefix="DSP" />
</h3>
<UButton color="gray" variant="ghost" icon="i-mdi-window-close" class="-my-1" @click="isOpen = false" />
Expand Down
13 changes: 10 additions & 3 deletions app/components/centrum/dispatches/DispatchDetailsSlideover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
if (ownUnitId.value === undefined) {
Expand Down Expand Up @@ -69,12 +69,19 @@ async function deleteDispatch(id: string): Promise<void> {
}
}
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;
}
});
</script>

<template>
<USlideover :ui="{ width: 'w-screen max-w-xl' }" :overlay="false">
<UCard
v-if="dispatch"
:ui="{
body: {
base: 'flex-1 min-h-[calc(100dvh-(2*var(--header-height)))] max-h-[calc(100dvh-(2*var(--header-height)))] overflow-y-auto',
Expand All @@ -101,7 +108,7 @@ const dispatchStatusColors = computed(() => dispatchStatusToBGColor(dispatch.val
:title="$t('common.delete')"
@click="
modal.open(ConfirmModal, {
confirm: async () => deleteDispatch(dispatch.id),
confirm: async () => dispatch && deleteDispatch(dispatch.id),
})
"
/>
Expand Down
22 changes: 16 additions & 6 deletions app/components/centrum/livemap/CentrumSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ watchDebounced(getSortedOwnDispatches.value, () => ensureOwnDispatchSelected(),
onBeforeMount(async () => {
if (canStream.value) {
useIntervalFn(() => checkup(), 1 * 60 * 1000);
useTimeoutFn(async () => startStream(), 550);
useTimeoutFn(async () => {
try {
startStream();
} catch (e) {
logger.error('exception during centrum stream', e);
}
}, 550);
toggleSidebarBasedOnUnit();
}
});
Expand Down Expand Up @@ -562,13 +568,17 @@ defineShortcuts({
</UButton>
</li>
<OwnDispatchEntry
v-for="id in getSortedOwnDispatches.slice().reverse()"
<template
v-else
v-for="id in getSortedOwnDispatches.slice().reverse()"

Check warning on line 573 in app/components/centrum/livemap/CentrumSidebar.vue

View workflow job for this annotation

GitHub Actions / node-tests

Attribute "v-for" should go before "v-else"
:key="id"
v-model:selected-dispatch="selectedDispatch"
:dispatch="dispatches.get(id)!"
/>
>
<OwnDispatchEntry
v-if="dispatches.get(id) !== undefined"
v-model:selected-dispatch="selectedDispatch"
:dispatch="dispatches.get(id)!"
/>
</template>
</ul>
</li>
Expand Down
8 changes: 7 additions & 1 deletion app/components/livemap/MapUsersLayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const { startStream, stopStream } = notificatorStore;
async function toggleStream(): Promise<void> {
// 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();
Expand Down
9 changes: 7 additions & 2 deletions pkg/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/tracker/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -106,6 +107,7 @@ func New(p Params) (ITracker, error) {

return nil
}

return nil
})
if err != nil {
Expand All @@ -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
Expand Down

0 comments on commit dfbd77c

Please sign in to comment.