forked from MayGo/tockler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from GitStartHQ/gitstart-error-logs
Show errors in the Main Window
- Loading branch information
Showing
10 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { Alert } from 'antd'; | ||
import { findAllLogs } from '../../services/logs-api'; | ||
import moment from 'moment'; | ||
|
||
type Log = { | ||
id: number; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
type: 'ERROR'; | ||
message?: string; | ||
jsonData?: string; | ||
}; | ||
|
||
export const LogList = () => { | ||
const [logs, setLogs] = useState<Log[]>([]); | ||
|
||
const getAllLogs = async () => { | ||
const newLogs = await findAllLogs( | ||
moment() | ||
.subtract(1, 'day') | ||
.toDate(), | ||
); | ||
setLogs(newLogs); | ||
}; | ||
|
||
const handleDismiss = () => { | ||
// TODO: handle dismissing of logs | ||
}; | ||
|
||
useEffect(() => { | ||
if (logs.length === 0) { | ||
getAllLogs(); | ||
} | ||
const toCancel = setTimeout(() => { | ||
getAllLogs(); | ||
}, 10000); | ||
|
||
return () => clearTimeout(toCancel); | ||
// eslint-disable-next-line | ||
}, []); | ||
|
||
if (logs.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{logs.map(log => ( | ||
<Alert | ||
type={log.type === 'ERROR' ? 'error' : 'warning'} | ||
message={`"${log.message}" ${moment(log.updatedAt).fromNow()}`} | ||
description={`${JSON.stringify(log.jsonData)} | Created at: ${moment( | ||
log.createdAt, | ||
).format()}`} | ||
closeText="Dismiss" | ||
onClose={handleDismiss} | ||
style={{ marginBottom: '16px' }} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { emit } from 'eiphop'; | ||
|
||
export async function findAllLogs(from: Date, to?: Date) { | ||
const data = await emit('findAllLogs', { | ||
from, | ||
to, | ||
}); | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Model } from 'objection'; | ||
|
||
/** | ||
* This Log model will primarily be used to keep track of all errors in the DevTime app. | ||
* This table can be extended to also keep track of other kinds of logs. Things that don't need to be saved to GitStart's database. | ||
*/ | ||
export class Log extends Model { | ||
static tableName = 'Logs'; | ||
|
||
id!: number; | ||
createdAt!: Date; | ||
updatedAt!: Date; | ||
type!: 'ERROR'; | ||
message?: string; | ||
jsonData?: any; | ||
|
||
static get jsonAttributes() { | ||
return ['jsonData']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { logManager } from '../log-manager'; | ||
import { Log } from '../models/Log'; | ||
|
||
export class LogService { | ||
logger = logManager.getLogger('LogService'); | ||
lastLog: null | Log = null; | ||
|
||
async createOrUpdateLog(logAttributes: Partial<Log>): Promise<Log> { | ||
let log: Log = null; | ||
if ( | ||
this.lastLog && | ||
logAttributes.type === this.lastLog.type && | ||
logAttributes.message === this.lastLog.message && | ||
logAttributes.jsonData === this.lastLog.jsonData | ||
) { | ||
const now = new Date(); | ||
await Log.query().findById(this.lastLog.id).patch({ | ||
updatedAt: now, | ||
}); | ||
log = this.lastLog; | ||
log.updatedAt = now; | ||
} else { | ||
logAttributes.createdAt = new Date(); | ||
logAttributes.updatedAt = new Date(); | ||
log = await Log.query().insert(logAttributes); | ||
} | ||
this.lastLog = log; | ||
return log; | ||
} | ||
|
||
async findAllLogs(from: Date, to?: Date) { | ||
if (!to) { | ||
to = new Date(); | ||
} | ||
return Log.query() | ||
.where('updatedAt', '>=', from) | ||
.where('updatedAt', '<=', to) | ||
.skipUndefined() | ||
.orderBy('updatedAt', 'DESC'); | ||
} | ||
} | ||
|
||
export const logService = new LogService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
exports.up = function (knex) { | ||
return knex.schema.createTable('Logs', (table) => { | ||
table.increments('id').notNullable(); | ||
table.dateTime('createdAt').notNullable(); | ||
table.dateTime('updatedAt').notNullable(); | ||
table.string('type').notNullable(); | ||
table.string('message'); | ||
table.string('jsonData'); | ||
}); | ||
}; | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.dropTable('Logs'); | ||
}; |