mirror of
https://github.com/openstf/stf
synced 2025-10-04 10:19:30 +02:00
Add more advanced filtering to the device tracker. Make the group tracker work properly.
This commit is contained in:
parent
334490d878
commit
fa7437a445
3 changed files with 65 additions and 23 deletions
|
@ -34,7 +34,7 @@ dbapi.loadUser = function(email) {
|
|||
|
||||
dbapi.loadGroup = function(email) {
|
||||
return db.run(r.table('devices').getAll(email, {
|
||||
index: 'ownerEmail'
|
||||
index: 'owner'
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
var r = require('rethinkdb')
|
||||
|
||||
module.exports = {
|
||||
users: {
|
||||
primaryKey: 'email'
|
||||
|
@ -5,8 +7,8 @@ module.exports = {
|
|||
, devices: {
|
||||
primaryKey: 'serial'
|
||||
, indexes: {
|
||||
ownerEmail: function(device) {
|
||||
return device('owner')('email')
|
||||
owner: function(device) {
|
||||
return r.branch(device('present'), device('owner')('email'), r.literal())
|
||||
}
|
||||
, lastHeartbeatAt: null
|
||||
}
|
||||
|
|
|
@ -88,34 +88,59 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
|||
}
|
||||
}
|
||||
|
||||
scopedSocket.on('device.add', function (data) {
|
||||
function fetch(data) {
|
||||
deviceService.load(data.serial)
|
||||
.then(changeListener)
|
||||
.catch(function() {})
|
||||
}
|
||||
|
||||
function addListener(data) {
|
||||
var device = get(data)
|
||||
if (device) {
|
||||
modify(device, data)
|
||||
}
|
||||
else if (options.insertPresent) {
|
||||
else if (options.filter(data)) {
|
||||
insert(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
scopedSocket.on('device.remove', function (data) {
|
||||
function removeListener(data) {
|
||||
var device = get(data)
|
||||
if (device) {
|
||||
modify(device, data)
|
||||
}
|
||||
})
|
||||
|
||||
scopedSocket.on('device.change', function (data) {
|
||||
var device = get(data)
|
||||
if (device) {
|
||||
if (options.removeAbsent) {
|
||||
if (!options.filter(device)) {
|
||||
remove(device)
|
||||
}
|
||||
else {
|
||||
modify(device, data)
|
||||
}
|
||||
else {
|
||||
if (options.filter(data)) {
|
||||
insert(data)
|
||||
// We've only got partial data
|
||||
fetch(data)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function changeListener(data) {
|
||||
var device = get(data)
|
||||
if (device) {
|
||||
modify(device, data)
|
||||
if (!options.filter(device)) {
|
||||
remove(device)
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (options.filter(data)) {
|
||||
insert(data)
|
||||
// We've only got partial data
|
||||
fetch(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scopedSocket.on('device.add', addListener)
|
||||
scopedSocket.on('device.remove', removeListener)
|
||||
scopedSocket.on('device.change', changeListener)
|
||||
|
||||
this.add = function(device) {
|
||||
remove(device)
|
||||
|
@ -127,7 +152,9 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
|||
|
||||
deviceService.trackAll = function ($scope) {
|
||||
var tracker = new Tracker($scope, {
|
||||
insertPresent: true
|
||||
filter: function(device) {
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
oboe('/api/v1/devices')
|
||||
|
@ -140,7 +167,9 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
|||
|
||||
deviceService.trackGroup = function ($scope) {
|
||||
var tracker = new Tracker($scope, {
|
||||
removeAbsent: true
|
||||
filter: function(device) {
|
||||
return device.using
|
||||
}
|
||||
})
|
||||
|
||||
oboe('/api/v1/group')
|
||||
|
@ -151,15 +180,26 @@ module.exports = function DeviceServiceFactory($rootScope, $http, socket) {
|
|||
return tracker
|
||||
}
|
||||
|
||||
deviceService.get = function (serial, $scope) {
|
||||
var tracker = new Tracker($scope, {})
|
||||
|
||||
deviceService.load = function(serial) {
|
||||
return $http.get('/api/v1/devices/' + serial)
|
||||
.then(function (response) {
|
||||
tracker.add(response.data.device)
|
||||
return response.data.device
|
||||
})
|
||||
}
|
||||
|
||||
deviceService.get = function (serial, $scope) {
|
||||
var tracker = new Tracker($scope, {
|
||||
filter: function(device) {
|
||||
return device.serial === serial
|
||||
}
|
||||
})
|
||||
|
||||
return deviceService.load(serial)
|
||||
.then(function(device) {
|
||||
tracker.add(device)
|
||||
return device
|
||||
})
|
||||
}
|
||||
|
||||
return deviceService
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue