Skip to content

Commit

Permalink
various: Rename APIStats collection to api.stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Apr 5, 2018
1 parent 591f34e commit 88d7ab9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
50 changes: 25 additions & 25 deletions libs/api.js
Expand Up @@ -71,11 +71,11 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Authorization', 'OAuth ' + config.settings.bot_oauth.split(':')[1])
.set('Client-ID', config.settings.client_id)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelID', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelID', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelID', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelID', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
} finally {
if (timeout === 1000) setTimeout(() => this.getChannelID(), timeout)
Expand Down Expand Up @@ -113,7 +113,7 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Authorization', 'OAuth ' + config.settings.broadcaster_oauth.split(':')[1])
.set('Client-ID', config.settings.client_id)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
if (e.message === '422 Unprocessable Entity') {
timeout = 0
Expand All @@ -129,7 +129,7 @@ class API {
} else {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
}
return
} finally {
Expand Down Expand Up @@ -167,11 +167,11 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Authorization', 'OAuth ' + config.settings.bot_oauth.split(':')[1])
.set('Client-ID', config.settings.client_id)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelDataOldAPI', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelDataOldAPI', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelDataOldAPI', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelDataOldAPI', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
} finally {
setTimeout(() => this.getChannelDataOldAPI(), timeout)
Expand Down Expand Up @@ -222,11 +222,11 @@ class API {
let timeout = 30000
try {
request = await snekfetch.get(url)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelHosts', api: 'tmi', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelHosts', api: 'tmi', endpoint: url, code: request.status })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getChannelHosts', api: 'tmi', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelHosts', api: 'tmi', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
} finally {
setTimeout(() => this.getChannelHosts(), timeout)
Expand Down Expand Up @@ -261,11 +261,11 @@ class API {
request = await snekfetch.get(url)
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'Bearer ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'updateChannelViews', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'updateChannelViews', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'updateChannelViews', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'updateChannelViews', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
return
} finally {
setTimeout(() => this.updateChannelViews(), timeout)
Expand Down Expand Up @@ -299,12 +299,12 @@ class API {
request = await snekfetch.get(url)
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'Bearer ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
quiet = false
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
return
} finally {
setTimeout(() => this.getLatest100Followers(quiet), timeout)
Expand Down Expand Up @@ -340,7 +340,7 @@ class API {
this.remainingAPICalls = usersFromApi.headers['ratelimit-remaining']
this.refreshAPICalls = usersFromApi.headers['ratelimit-reset']

global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: `https://api.twitch.tv/helix/users?${fids.join('&')}`, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: `https://api.twitch.tv/helix/users?${fids.join('&')}`, code: request.status, remaining: this.remainingAPICalls })
let toAwait = []
for (let follower of usersFromApi.body.data) {
followersUsername.push(follower.login.toLowerCase())
Expand Down Expand Up @@ -394,7 +394,7 @@ class API {
request = await snekfetch.get(url)
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'Bearer ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getGameFromId', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getGameFromId', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })

// add id->game to cache
gids[gid] = request.body.data[0].name
Expand All @@ -404,7 +404,7 @@ class API {
} catch (e) {
global.log.warning(`Couldn't find name of game for gid ${gid} - fallback to ${this.current.game}`)
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getGameFromId', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getGameFromId', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
return this.current.game
}
}
Expand All @@ -428,11 +428,11 @@ class API {
request = await snekfetch.get(url)
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'Bearer ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getCurrentStreamData', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getCurrentStreamData', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getCurrentStreamData', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getCurrentStreamData', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: this.remainingAPICalls })
return
} finally {
if (opts.interval) setTimeout(() => this.getCurrentStreamData(opts), timeout)
Expand Down Expand Up @@ -601,10 +601,10 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'OAuth ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
}
debug('api:setTitleAndGame')(request.body)
Expand Down Expand Up @@ -652,10 +652,10 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'OAuth ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
}
d(request.body.games)
Expand All @@ -677,10 +677,10 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Client-ID', config.settings.client_id)
.set('Authorization', 'OAuth ' + config.settings.bot_oauth.split(':')[1])
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
}
d(request.body)
Expand Down Expand Up @@ -715,11 +715,11 @@ class API {
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Authorization', 'Bearer ' + config.settings.bot_oauth.split(':')[1])
.set('Client-ID', config.settings.client_id)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'isFollowerUpdate', api: 'helix', endpoint: url, code: request.status, remaining: global.twitch.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'isFollowerUpdate', api: 'helix', endpoint: url, code: request.status, remaining: global.twitch.remainingAPICalls })
debug('api:isFollowerUpdate')('Request done: %j', request.body)
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'isFollowerUpdate', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: global.twitch.remainingAPICalls })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'isFollowerUpdate', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}`, remaining: global.twitch.remainingAPICalls })
return
}

Expand Down
2 changes: 1 addition & 1 deletion libs/configuration.js
Expand Up @@ -25,7 +25,7 @@ Configuration.prototype.commands = function () {

Configuration.prototype.debug = async function (self, sender) {
let [api, widgets] = await Promise.all([
global.db.engine.find('APIStats'),
global.db.engine.find('api.stats'),
global.db.engine.find('widgets')
])

Expand Down
4 changes: 2 additions & 2 deletions libs/overlays/credits.js
Expand Up @@ -157,10 +157,10 @@ class Credits {
request = await snekfetch.get(url)
.set('Accept', 'application/vnd.twitchtv.v5+json')
.set('Client-ID', config.settings.client_id)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getTopClips', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getTopClips', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'getTopClips', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getTopClips', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
}
return request.body.clips
}
Expand Down
8 changes: 4 additions & 4 deletions libs/stats.js
Expand Up @@ -16,12 +16,12 @@ Stats.prototype.sockets = function () {

Stats.prototype.clearAPIStats = async function (self) {
const d = debug('stats:clearAPIStats')
let stats = await global.db.engine.find('APIStats')
let stats = await global.db.engine.find('api.stats')

// remove data older than 24h
stats = _.filter(stats, (o) => _.now() - o.timestamp >= 1000 * 60 * 60 * 24)
d('Stats to delete: %j', stats)
for (let s of stats) await global.db.engine.remove('APIStats', { _id: s._id.toString() })
for (let s of stats) await global.db.engine.remove('api.stats', { _id: s._id.toString() })

setTimeout(() => self.clearAPIStats(self), 1000 * 60 * 60)
}
Expand Down Expand Up @@ -89,9 +89,9 @@ Stats.prototype.getLatestStats = async function (self, socket) {
Stats.prototype.getApiStats = async function (self, socket, options) {
const [from, to] = [_.get(options, 'from', _.now() - 1000 * 60 * 60), _.get(options, 'to', _.now())]

let stats = await global.db.engine.find('APIStats')
let stats = await global.db.engine.find('api.stats')
// return hour of data
socket.emit('APIStats', _.filter(stats, (o) => from < o.timestamp && to >= o.timestamp))
socket.emit('api.stats', _.filter(stats, (o) => from < o.timestamp && to >= o.timestamp))
}

module.exports = Stats
2 changes: 1 addition & 1 deletion libs/systems/commercial.js
Expand Up @@ -59,7 +59,7 @@ class Commercial {
if (!_.isNil(commercial.message)) global.commons.sendMessage(commercial.message, sender)
} catch (e) {
global.log.error(`API: ${url} - ${e.status} ${_.get(e, 'body.message', e.message)}`)
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'commercial', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'commercial', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
}
} else {
global.commons.sendMessage('$sender, available commercial duration are: 30, 60, 90, 120, 150 and 180', sender)
Expand Down
4 changes: 2 additions & 2 deletions libs/systems/highlights.js
Expand Up @@ -77,10 +77,10 @@ class Highlights {
global.client.api(options, function (err, res, body) {
if (err) {
global.log.error(err, { fnc: 'Highlights#1' })
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'highlight', api: 'kraken', endpoint: url, code: err })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'highlight', api: 'kraken', endpoint: url, code: err })
return
}
global.db.engine.insert('APIStats', { timestamp: _.now(), call: 'highlight', api: 'kraken', endpoint: url, code: 200 })
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'highlight', api: 'kraken', endpoint: url, code: 200 })
const video = body.videos[0]
self.cached.id = video._id
self.cached.created_at = when.online
Expand Down

0 comments on commit 88d7ab9

Please sign in to comment.