-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathlogging.ts
More file actions
196 lines (172 loc) Β· 5.21 KB
/
logging.ts
File metadata and controls
196 lines (172 loc) Β· 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* SPDX-FileCopyrightText: 2023 Nextcloud Gmbh and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { AxiosError } from 'axios'
import type { ILogEntry } from '../interfaces'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { getLog, pollLog } from '../api'
import { POLLING_INTERVAL } from '../constants'
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { useSettingsStore } from './settings'
import { parseLogFile, parseLogString, parseRawLogEntry } from '../utils/logfile'
import { logger } from '../utils/logger'
/**
* Store for handling log entries
*/
export const useLogStore = defineStore('logreader-logs', () => {
const _settings = useSettingsStore()
/**
* List of all log entries
*/
const allEntries = ref<ILogEntry[]>([])
/**
* The current query to filter logs
*/
const query = ref<string>('')
/**
* List of filtered log entries (search query)
*/
const entries = computed(() => {
if (query.value) {
const text = query.value.toLowerCase()
return allEntries.value.filter((entry) => JSON.stringify(entry).toLowerCase().includes(text))
}
return allEntries.value
})
/**
* Whether there are more remaining (older) log entries on the server
*/
const hasRemainingEntries = ref(true)
/**
* Whether polling service is currently running
*/
const _polling = ref(false)
/**
* Whether we are currently loading, used to prevent multiple loading requests at the same time
*/
const _loading = ref(false)
/**
* Load more entries from server
*
* @param older Load older entries (default: true)
*/
async function loadMore(older = true) {
// Nothing to do if server logging is disabled
if (!_settings.isEnabled) return
// Only load any entries if there is no previous unfinished request
if (!(_loading.value = !_loading.value)) return
try {
if (older) {
const { data } = await getLog({ offset: allEntries.value.length, query: query.value })
allEntries.value.push(...data.data.map(parseRawLogEntry))
hasRemainingEntries.value = data.remain
} else {
const { data } = await pollLog({ lastReqId: allEntries.value[0]?.reqId || '' })
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
}
} catch (e) {
logger.debug(e as Error)
showError(t('logreader', 'Could not load log entries'))
} finally {
// Handle any error to prevent a dead lock of the _loading property
_loading.value = false
}
}
/**
* Load entries from log file
*/
async function loadFile() {
if (!_settings.localFile) {
logger.debug('Can not read file, no file was uploaded')
return
}
allEntries.value = await parseLogFile(_settings.localFile)
hasRemainingEntries.value = false
}
/**
* Load entries from string
* @param text clipboard text content
*/
async function loadText(text: string) {
// Skip if aborted
if (text === '') {
return
}
try {
allEntries.value = await parseLogString(text)
// TRANSLATORS The clipboard used to paste stuff
_settings.localFile = new File([], t('logreader', 'Clipboard'))
// From clipboard so no more entries
hasRemainingEntries.value = false
} catch (e) {
// TRANSLATORS Error when the pasted content from the clipboard could not be parsed
showError(t('logreader', 'Could not parse clipboard content'))
logger.error(e as Error)
}
}
/**
* Stop polling entries
*/
function stopPolling() {
_polling.value = false
}
/**
* Start polling new entries from server
*/
function startPolling() {
if (_polling.value) {
// Already polling, nothing to do
return
}
const doPolling = async () => {
try {
// Only poll if not using a local file
if (_settings.isEnabled && query.value === '') {
const { data } = await pollLog({ lastReqId: allEntries.value[0]?.reqId || '' })
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
}
} catch (e) {
logger.warn('Unexpected error while polling for new log entries', { error: e })
const error = e as AxiosError
if ((error.status || 0) >= 500) {
showError(t('logreader', 'Could not fetch new log entries (server unavailable)'))
} else {
showError(t('logreader', 'Could not fetch new entries'))
}
} finally {
if (_polling.value) {
window.setTimeout(doPolling, POLLING_INTERVAL)
}
}
}
_polling.value = true
window.setTimeout(doPolling, POLLING_INTERVAL)
}
/**
* Search the logs for a string
*
* First it sets the query string so the filtered entries are updated,
* then it searched on the server for other logs
*
* @param search The query string
*/
async function searchLogs(search = '') {
const oldQuery = query.value
query.value = search
// if query changed and server logging is enabled, request new entries
if (search !== oldQuery && _settings.isEnabled) {
_loading.value = true
try {
const { data } = await getLog({ offset: 0, query: search })
allEntries.value = [...data.data.map(parseRawLogEntry)]
hasRemainingEntries.value = data.remain
} finally {
_loading.value = false
}
}
}
return { allEntries, entries, hasRemainingEntries, query, loadMore, loadText, loadFile, startPolling, stopPolling, searchLogs }
})