mirror of
https://github.com/openstf/stf
synced 2025-10-05 02:29:26 +02:00
Save device logs to rethinkdb.
This commit is contained in:
parent
39989c6e7a
commit
389db73f08
8 changed files with 140 additions and 41 deletions
|
@ -13,6 +13,17 @@ module.exports.saveUserAfterLogin = function(user) {
|
|||
}))
|
||||
}
|
||||
|
||||
module.exports.saveDeviceLog = function(serial, entry) {
|
||||
return db.run(r.table('logs').insert({
|
||||
serial: entry.serial
|
||||
, timestamp: r.epochTime(entry.timestamp)
|
||||
, priority: entry.priority
|
||||
, tag: entry.tag
|
||||
, pid: entry.pid
|
||||
, message: entry.message
|
||||
}))
|
||||
}
|
||||
|
||||
module.exports.saveDeviceStatus = function(serial, status) {
|
||||
return db.run(r.table('devices').get(serial).update({
|
||||
status: status
|
||||
|
|
|
@ -11,28 +11,64 @@ var tables = require('./tables')
|
|||
module.exports = function(conn) {
|
||||
var log = logger.createLogger('db:setup')
|
||||
|
||||
function alreadyExistsError(err) {
|
||||
return err.msg && err.msg.indexOf('already exists') !== -1
|
||||
}
|
||||
|
||||
function noMasterAvailableError(err) {
|
||||
return err.msg && err.msg.indexOf('No master available') !== -1
|
||||
}
|
||||
|
||||
function createDatabase() {
|
||||
return rutil.run(conn, r.dbCreate(conn.db))
|
||||
.then(function() {
|
||||
log.info('Database "%s" created', conn.db)
|
||||
})
|
||||
.catch(rutil.errors.RqlRuntimeError, function(err) {
|
||||
var expected = util.format('Database `%s` already exists.', conn.db)
|
||||
assert.equal(expected, err.msg)
|
||||
.catch(alreadyExistsError, function(err) {
|
||||
log.info('Database "%s" already exists', conn.db)
|
||||
return Promise.resolve()
|
||||
})
|
||||
}
|
||||
|
||||
function createTable(table, options) {
|
||||
return rutil.run(conn, r.tableCreate(table, options))
|
||||
var tableOptions = {
|
||||
primaryKey: options.primaryKey
|
||||
}
|
||||
return rutil.run(conn, r.tableCreate(table, tableOptions))
|
||||
.then(function() {
|
||||
log.info('Table "%s" created', table)
|
||||
})
|
||||
.catch(alreadyExistsError, function(err) {
|
||||
log.info('Table "%s" already exists', table)
|
||||
return Promise.resolve()
|
||||
})
|
||||
.catch(rutil.errors.RqlRuntimeError, function(err) {
|
||||
var expected = util.format('Table `%s` already exists.', table)
|
||||
assert.equal(expected, err.msg)
|
||||
log.info('Table "%s" already exists', table)
|
||||
.catch(noMasterAvailableError, function(err) {
|
||||
return Promise.delay(1000).then(function() {
|
||||
return createTable(table, options)
|
||||
})
|
||||
})
|
||||
.then(function() {
|
||||
if (options.indexes) {
|
||||
return Promise.all(Object.keys(options.indexes).map(function(index) {
|
||||
return createIndex(table, index, options.indexes[index])
|
||||
}))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function createIndex(table, index, fn) {
|
||||
return rutil.run(conn, r.table(table).indexCreate(index, fn))
|
||||
.then(function() {
|
||||
log.info('Index "%s"."%s" created', table, index)
|
||||
})
|
||||
.catch(alreadyExistsError, function(err) {
|
||||
log.info('Index "%s"."%s" already exists', table, index)
|
||||
return Promise.resolve()
|
||||
})
|
||||
.catch(noMasterAvailableError, function(err) {
|
||||
return Promise.delay(1000).then(function() {
|
||||
return createIndex(table, index, fn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -5,4 +5,11 @@ module.exports = {
|
|||
, devices: {
|
||||
primaryKey: 'serial'
|
||||
}
|
||||
, logs: {
|
||||
primaryKey: 'id'
|
||||
, indexes: {
|
||||
serial: null
|
||||
, timestamp: null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue