-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.ts
266 lines (233 loc) · 8.22 KB
/
index.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Log, AsyncStatus, APIError } from '../../types';
import { DataModel, defaultError } from '../../hooks/useResource';
import { apiHttp } from '../../constants';
export type LogItem = Log | 'Loading' | 'Error' | undefined;
export type LogData = {
logs: Array<LogItem>;
preloadStatus: AsyncStatus;
status: AsyncStatus;
loadMore: (index: number) => void;
error: APIError | null;
localSearch: LocalSearchType;
};
export type LogDataSettings = {
preload: boolean;
paused: boolean;
url: string;
pagesize?: number;
};
type LogSearchResult = {
line: number;
char: [number, number];
};
export type SearchState = { active: boolean; result: LogSearchResult[]; current: number; query: string };
export type LocalSearchType = {
search: (str: string) => void;
result: SearchState;
nextResult: () => void;
};
const DEFAULT_PAGE_SIZE = 500;
const PRELOAD_POLL_INTERVALL = 20000;
const POSTLOAD_POLL_INTERVAL = 10000;
const emptyArray: LogItem[] = [];
const emptySearchResult = { active: false, result: [], current: 0, query: '' };
function isOkResult(param: DataModel<Log[]> | APIError): param is DataModel<Log[]> {
return 'data' in param;
}
/**
* Currently useLogData does not include websocket support.
*
* Log amounts might be massive so we want to load them paginated and on demand depending on user
* actions. When preload is active (task is on running status) we first load what ever we get and after
* task completes we need to fetch again.
*/
const useLogData = ({ preload, paused, url, pagesize }: LogDataSettings): LogData => {
const [status, setStatus] = useState<AsyncStatus>('NotAsked');
const [preloadStatus, setPreloadStatus] = useState<AsyncStatus>('NotAsked');
const [error, setError] = useState<APIError | null>(null);
const [postPoll, setPostPoll] = useState<boolean>(false);
// Datastore
const [logs, setLogs] = useState<LogItem[]>(emptyArray);
const PAGE_SIZE = pagesize || DEFAULT_PAGE_SIZE;
// generic log fetcher
const fetchLogs = useCallback(
(
page: number,
order: '+' | '-' = '+',
isPostPoll = false,
): Promise<{ type: 'error'; error: APIError } | { type: 'ok'; data: Log[] }> => {
const requestUrl = url;
const fullUrl = `${requestUrl}${requestUrl.indexOf('?') > -1 ? '&' : '?'}_limit=${PAGE_SIZE}${
page ? `&_page=${page}` : ''
}&_order=${order}row`;
return fetch(apiHttp(fullUrl))
.then((response) => response.json())
.then((result: DataModel<Log[]> | APIError) => {
if (isOkResult(result)) {
// Check if there was any new lines. If there wasn't, let's cancel post finish polling.
// Or if was postpoll and we didnt get any results
if (
(result.data.length > 0 && logs.length > 0 && result.data[0].row === logs.length - 1) ||
(isPostPoll && result.data.length === 0)
) {
setPostPoll(false);
}
setLogs((array) => {
const newarr = [...array];
for (const item of result.data) {
newarr[item.row] = item;
}
return newarr;
});
return { type: 'ok' as const, data: result.data };
} else {
return { type: 'error' as const, error: result };
}
})
.catch((e) => {
if (e instanceof DOMException) {
return { type: 'error', error: { ...defaultError, id: 'user-aborted' } };
}
return { type: 'error', error: defaultError };
});
},
[PAGE_SIZE, logs.length, url],
);
const fetchPreload = useCallback(() => {
setPreloadStatus('Loading');
fetchLogs(1, '-').then((result) => {
if (result.type === 'error') {
if (result.error.id === 'user-aborted') {
setPreloadStatus('NotAsked');
return;
}
setPreloadStatus('Error');
return;
}
setPreloadStatus('Ok');
});
}, [fetchLogs]);
// Fetch logs when task gets completed
useEffect(() => {
if (status === 'NotAsked' && !paused) {
setStatus('Loading');
fetchLogs(1, '-').then((result) => {
setPostPoll(true);
if (result.type === 'error') {
if (result.error.id === 'user-aborted') {
return;
}
setStatus('Error');
setError(result.error);
return;
}
setStatus('Ok');
});
}
}, [paused, url, status, fetchLogs]);
useEffect(() => {
// For preload to happen following rules have to be matched
// paused -> Run has to be on running state
// status -> This should always be NotAsked if paused is on. Check just in case
// preload -> Run has to be runnign
// preloadStatus -> We havent triggered this yet.
if (paused && status === 'NotAsked' && preload && preloadStatus === 'NotAsked') {
fetchPreload();
}
}, [paused, preload, preloadStatus, status, url, fetchPreload]);
// Poller for auto updates when task is running
useEffect(() => {
let t: number;
if (['Ok', 'Error'].includes(preloadStatus) && paused) {
t = window.setTimeout(() => {
fetchPreload();
}, PRELOAD_POLL_INTERVALL);
}
return () => {
clearTimeout(t);
};
}, [preloadStatus, paused, fetchPreload]);
// Post finish polling
// In some cases all logs might not be there after task finishes. For this, lets poll new logs every 10sec until
// there are no new lines
useEffect(() => {
let t: number;
if (status === 'Ok' && postPoll) {
t = window.setTimeout(() => {
fetchLogs(1, '-', true);
}, POSTLOAD_POLL_INTERVAL);
}
return () => {
clearTimeout(t);
};
}, [status, postPoll, logs, fetchLogs]);
// loadMore gets triggered on all scrolling events on list.
function loadMore(index: number) {
// Get page number. Add one to correct lines starting from index 0
const page = Math.ceil((index + 1) / PAGE_SIZE);
// Need to have initial page before any other request.
if ((status === 'Ok' || preloadStatus === 'Ok') && !logs[index]) {
setLogs((arr) => {
// Since we are fetching stuff as pages, find start of page where current line is
// and fill them as loading so we dont try to fetch same page again.
const startOfPage = (page - 1) * PAGE_SIZE;
const endOfPage = startOfPage + PAGE_SIZE;
for (let i = startOfPage; i < endOfPage; i++) {
arr[i] = arr[i] || 'Loading';
}
return arr;
});
fetchLogs(page, '+').then((result) => {
if (result.type === 'error') {
return;
}
});
}
}
const [searchResult, setSearchResult] = useState<SearchState>(emptySearchResult);
const search = useCallback(
(str: string) => {
if (!str) {
return setSearchResult(emptySearchResult);
}
const query = str.toLowerCase();
const results = logs
.filter(filterbySearchTerm)
.filter((line) => line.line.toLowerCase().indexOf(query) > -1)
.map((item) => {
const index = item.line.toLowerCase().indexOf(query);
return {
line: item.row,
char: [index, index + str.length] as [number, number],
};
});
setSearchResult({ active: true, result: results, current: 0, query: str });
},
[logs],
);
const nextResult = useCallback(() => {
if (searchResult.current === searchResult.result.length - 1) {
setSearchResult((cur) => ({ ...cur, current: 0 }));
} else {
setSearchResult((cur) => ({ ...cur, current: cur.current + 1 }));
}
}, [searchResult]);
// Clean up on url change
useEffect(() => {
return () => {
setStatus('NotAsked');
setPreloadStatus('NotAsked');
setLogs(emptyArray);
setError(null);
setPostPoll(false);
setSearchResult(emptySearchResult);
};
}, [url]);
const localSearch = useMemo(() => ({ search, nextResult, result: searchResult }), [nextResult, search, searchResult]);
return { logs, status, preloadStatus, error, loadMore, localSearch };
};
function filterbySearchTerm(item: LogItem): item is Log {
return typeof item === 'object';
}
export default useLogData;