Skip to content

Commit 035bf86

Browse files
committed
Add logs api functions
1 parent 10c3139 commit 035bf86

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

urbackupserver/www2/src/api/urbackupserver.ts

+113
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,73 @@ export interface UsageGraphData {
357357
xlabel: string; // ISO Date of the data (YYYY-MM-DD)
358358
}
359359

360+
export type LogIdType = number;
361+
362+
export interface LogClient {
363+
id: ClientIdType; // Id of the client
364+
name: string; // Name of the client
365+
}
366+
367+
export interface LogInfo {
368+
name: string; // Name of the client
369+
id: LogIdType; // Id of the log
370+
time: number; // Unix timestamp of when the log was created
371+
errors: number; // Number of errors logged
372+
warnings: number; // Number of warnings logged
373+
image: number; // !=0 if this is a log of an image backup
374+
incremental: number; // !=0 if this is a log of an incremental backup
375+
resumed: number; // !=0 if this is a log of a resumed backup
376+
restore: number; // !=0 if this is a log of a restore
377+
}
378+
379+
export interface LogsResp {
380+
clients: LogClient[]; // List of clients with logs (does not include clients that don't have logs)
381+
all_clients: boolean; // If true all clients are selected
382+
has_user: boolean; // Logged in via user. Reports can only be configured if logged in as user
383+
log_right_clients: LogClient[]; // List of clients the user has log rights for
384+
filter: string; // Filter used
385+
logs: LogInfo[]; // List of logs
386+
ll: number; // Loglevel filter
387+
report_mail: string[]; // List of email addresses to send the report to
388+
report_loglevel: "" | LogLevel; // Loglevel when to send a report
389+
report_sendonly: "" | SendOnly; // When to send a report
390+
can_report_script_edit: boolean | undefined; // If true the user can edit the report script
391+
}
392+
393+
export interface LogDataRow {
394+
level: LogLevel; // Log level
395+
message: string; // Log message
396+
time: number; // Unix timestamp of when the log entry was created
397+
}
398+
399+
export interface LogData {
400+
data: LogDataRow[]; // Log data
401+
time: number; // Unix timestamp of when the log was created
402+
clientname: string; // Name of the client
403+
}
404+
405+
export interface LogDataResp {
406+
clients: LogClient[]; // List of clients with logs (does not include clients that don't have logs)
407+
all_clients: boolean; // If true all clients are selected
408+
has_user: boolean; // If true this is non-admin user
409+
log_right_clients: LogClient[]; // List of clients the user has log rights for
410+
filter: string; // Filter used
411+
log: LogData; // Log data
412+
}
413+
414+
export enum LogLevel {
415+
Info = 0,
416+
Warning = 1,
417+
Error = 2
418+
}
419+
420+
export enum SendOnly {
421+
Always = 0,
422+
Failed = 1,
423+
Succeeded = 2,
424+
FailedExcludingTimeout = 3
425+
}
426+
360427
class UrBackupServer {
361428
private serverUrl: string;
362429
private session = "";
@@ -743,6 +810,52 @@ class UrBackupServer {
743810
const resp = await this.fetchData({ "recalculate": "true" }, "usage");
744811
return resp as UsageStats;
745812
}
813+
814+
// Get Logs
815+
// Use an empty filter and loglevel LogLevel.Info to get all logs
816+
getLogs = async (filter: ClientIdType[], logLevel: LogLevel) => {
817+
const resp = await this.fetchData({ "filter": filter.join(","), "ll": logLevel.toString() }, "logs");
818+
const ret = resp as LogsResp;
819+
ret.report_mail = resp.report_mail.length == 0 ? [] : resp.report_mail.split(/[;,]/);
820+
return ret;
821+
}
822+
823+
// Parse log data
824+
parseLog = (d: string) => {
825+
const msgs = d.split("\n");
826+
const rows: LogDataRow[] = [];
827+
for (const msg of msgs) {
828+
const level = parseInt(msg.substring(0, 1));
829+
let message: string;
830+
const idx = msg.indexOf("-", 2);
831+
let time = NaN;
832+
if (idx != -1) {
833+
time = parseInt(msg.substring(2, idx));
834+
message = msg.substring(idx + 1);
835+
} else {
836+
message = msg.substring(2);
837+
}
838+
839+
rows.push({ level: level, message: message, time: time });
840+
}
841+
return rows;
842+
}
843+
844+
// Get log data via log id
845+
getLog = async (logid: LogIdType) => {
846+
const resp = await this.fetchData({ "logid": logid.toString() }, "logs");
847+
const ret = resp as LogDataResp;
848+
ret.log.data = this.parseLog(resp.log.data);
849+
return ret;
850+
}
851+
852+
// Save reporting configuration of user
853+
saveLogReporting = async (mails: string[], logLevel: LogLevel, sendOnly: SendOnly) => {
854+
const resp = await this.fetchData({ "report_mail": mails.join(";"), "report_loglevel": logLevel.toString(), "report_sendonly": sendOnly.toString() }, "logs");
855+
const ret = resp as LogsResp;
856+
ret.report_mail = resp.report_mail.length == 0 ? [] : resp.report_mail.split(/[;,]/);
857+
return ret;
858+
}
746859
}
747860

748861
export default UrBackupServer;

0 commit comments

Comments
 (0)