Skip to content
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
46 changes: 23 additions & 23 deletions js/logreader-main.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/logreader-main.mjs.map

Large diffs are not rendered by default.

125 changes: 124 additions & 1 deletion src/store/logging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { IAppSettings, ILogEntry } from '../interfaces'

import { createTestingPinia } from '@pinia/testing'
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { computed, isProxy, toRaw } from 'vue'
import { POLLING_INTERVAL } from '../constants'
import { useSettingsStore } from '../store/settings'
import { useLogStore } from './logging'
Expand All @@ -23,6 +24,7 @@ const mocks = vi.hoisted(() => {
},
getLog: vi.fn(),
pollLog: vi.fn(),
parseRawLogEntry: vi.fn((v) => v),
showError: vi.fn(),
}
})
Expand All @@ -35,7 +37,7 @@ vi.mock('../utils/logfile.ts', () => {
return {
parseLogFile: mocks.parseLogFile,
parseLogString: mocks.parseLogString,
parseRawLogEntry: vi.fn((v) => v),
parseRawLogEntry: mocks.parseRawLogEntry,
}
})

Expand Down Expand Up @@ -77,6 +79,7 @@ describe('store:logging', () => {
afterEach(() => {
vi.restoreAllMocks()
vi.clearAllMocks()
mocks.parseRawLogEntry.mockReset()
vi.clearAllTimers()
})

Expand Down Expand Up @@ -314,6 +317,19 @@ describe('store:logging', () => {
expect(store.allEntries).toEqual([{ message: 'hello' }, { reqId: '1234' }, { reqId: '5678' }])
})

it('loads more newer entries than fit into one function call', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: Array(500000).fill({ reqId: '2' }),
}))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]

await store.loadMore(false)
expect(mocks.showError).not.toBeCalled()
expect(store.allEntries).toHaveLength(500001)
})

it('does not loads more from server when local file is used', async () => {
const store = useLogStore()
const settings = useSettingsStore()
Expand Down Expand Up @@ -392,6 +408,113 @@ describe('store:logging', () => {
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
})

it('can poll more new entries than fit into one function call', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: Array(500000).fill({ reqId: '2' }),
}))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
store.stopPolling()

expect(mocks.logger.warn).not.toBeCalled()
expect(store.allEntries).toHaveLength(500001)
})

it('keeps the list when there are no new entries', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: [],
}))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]
const knownEntries = store.allEntries
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
store.stopPolling()

expect(mocks.pollLog).toBeCalledTimes(1)
expect(mocks.logger.warn).not.toBeCalled()
expect(store.allEntries).toBe(knownEntries)
})

it('handles new entries that are not a list', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: {},
}))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
store.stopPolling()

expect(mocks.showError).toBeCalledWith('Could not fetch new entries')
expect(store.allEntries).toEqual([{ reqId: '1' }])
})

it('adds the parsed new entries in front of the known ones', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: [{ reqId: '3' }, { reqId: '2' }],
}))
mocks.parseRawLogEntry.mockImplementation((entry) => ({ reqId: `parsed ${entry.reqId}` }))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]
const reqIds = computed(() => store.allEntries.map(({ reqId }) => reqId))
expect(reqIds.value).toEqual(['1'])
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
store.stopPolling()
expect(reqIds.value).toEqual(['parsed 3', 'parsed 2', '1'])

// The new list is reactive as well
store.allEntries.push({ reqId: '0' } as ILogEntry)
expect(reqIds.value).toEqual(['parsed 3', 'parsed 2', '1', '0'])
})

it('keeps the known entries plain objects when new ones are added', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: [{ reqId: '2' }],
}))

const store = useLogStore()
store.allEntries = [{ reqId: '1' }] as ILogEntry[]
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
store.stopPolling()

expect(toRaw(store.allEntries).map(isProxy)).toEqual([false, false])
})

it('keeps older entries that are loaded while polling', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(async () => {
await new Promise((resolve) => window.setTimeout(resolve, 100))

return {
data: [{ reqId: '3' }],
}
})
vi.mocked(mocks.getLog).mockImplementationOnce(() => ({
data: {
data: [{ reqId: '1' }],
remain: false,
},
}))

const store = useLogStore()
store.allEntries = [{ reqId: '2' }] as ILogEntry[]
store.startPolling()
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
await store.loadMore()
await vi.advanceTimersByTimeAsync(100)
store.stopPolling()

expect(store.allEntries).toEqual([{ reqId: '3' }, { reqId: '2' }, { reqId: '1' }])
})

it('only starts one polling timer', async () => {
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
data: [{ reqId: '456' }],
Expand Down
25 changes: 21 additions & 4 deletions src/store/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*/

import type { AxiosError } from '@nextcloud/axios'
import type { ILogEntry } from '../interfaces'
import type { ILogEntry, IRawLogEntry } from '../interfaces'

import { showError } from '@nextcloud/dialogs/toast'
import { translate as t } from '@nextcloud/l10n'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { computed, ref, toRaw } from 'vue'
import { getLog, pollLog } from '../api'
import { POLLING_INTERVAL } from '../constants'
import { parseLogFile, parseLogString, parseRawLogEntry } from '../utils/logfile'
Expand Down Expand Up @@ -58,6 +58,23 @@ export const useLogStore = defineStore('logreader-logs', () => {
*/
const _loading = ref(false)

/**
* Add new entries in front of the known ones
*
* @param newEntries The new entries, newest first
*/
function prependEntries(newEntries: readonly IRawLogEntry[]) {
const parsedEntries = newEntries.map(parseRawLogEntry)

// Do not trigger any updates if there is nothing new
if (!parsedEntries.length) {
return
}

// concat() gets the entries as one list, tens of thousands of arguments would overflow the stack
allEntries.value = parsedEntries.concat(toRaw(allEntries.value))
}

/**
* Load more entries from server
*
Expand All @@ -81,7 +98,7 @@ export const useLogStore = defineStore('logreader-logs', () => {
hasRemainingEntries.value = data.remain
} else {
const { data } = await pollLog({ lastReqId: allEntries.value[0]!.reqId })
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
prependEntries(data)
}
} catch (e) {
logger.debug(e as Error)
Expand Down Expand Up @@ -150,7 +167,7 @@ export const useLogStore = defineStore('logreader-logs', () => {
// Only poll if not using a local file and store already has some known entries
if (_settings.isEnabled && query.value === '' && allEntries.value.length) {
const { data } = await pollLog({ lastReqId: allEntries.value[0]!.reqId })
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
prependEntries(data)
}
} catch (e) {
logger.warn('Unexpected error while polling for new log entries', { error: e })
Expand Down
Loading