Skip to content

Commit

Permalink
libs/api: Fix incorrect account created_at fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Apr 7, 2018
1 parent 7f10277 commit a232820
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
34 changes: 17 additions & 17 deletions libs/api.js
Expand Up @@ -73,7 +73,7 @@ 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('api.stats', { timestamp: _.now(), call: 'getChannelID', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, 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}`)
Expand All @@ -99,7 +99,7 @@ class API {
var request
try {
request = await snekfetch.get(url)
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelChattersUnofficialAPI', api: 'unofficial', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, timestamp: _.now(), call: 'getChannelChattersUnofficialAPI', api: 'unofficial', endpoint: url, code: request.status })
} catch (e) {
timeout = e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT' ? 1000 : timeout
global.log.error(`${url} - ${e.message}`)
Expand Down Expand Up @@ -158,7 +158,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('api.stats', { timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, timestamp: _.now(), call: 'getChannelSubscribersOldAPI', api: 'kraken', endpoint: url, code: request.status })
} catch (e) {
if (e.message === '422 Unprocessable Entity') {
timeout = 0
Expand Down Expand Up @@ -212,7 +212,7 @@ 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('api.stats', { timestamp: _.now(), call: 'getChannelDataOldAPI', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, 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}`)
Expand Down Expand Up @@ -267,7 +267,7 @@ class API {
let timeout = 30000
try {
request = await snekfetch.get(url)
global.db.engine.insert('api.stats', { timestamp: _.now(), call: 'getChannelHosts', api: 'tmi', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, 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}`)
Expand Down Expand Up @@ -306,7 +306,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('api.stats', { timestamp: _.now(), call: 'updateChannelViews', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { data: request.body.data, 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}`)
Expand Down Expand Up @@ -344,7 +344,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('api.stats', { timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { data: request.body.data, 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
Expand Down Expand Up @@ -385,7 +385,7 @@ class API {
this.remainingAPICalls = usersFromApi.headers['ratelimit-remaining']
this.refreshAPICalls = usersFromApi.headers['ratelimit-reset']

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 })
global.db.engine.insert('api.stats', { data: request.body.data, timestamp: _.now(), call: 'getLatest100Followers', api: 'helix', endpoint: `https://api.twitch.tv/helix/users?${fids.join('&')}`, code: request.status, remaining: this.remainingAPICalls })
for (let follower of usersFromApi.body.data) {
followersUsername.push(follower.login.toLowerCase())
debug('api:getLatest100Followers:users')('Saving user %s id %s', follower.login.toLowerCase(), follower.id)
Expand Down Expand Up @@ -446,7 +446,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('api.stats', { timestamp: _.now(), call: 'getGameFromId', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { data: request.body.data, 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 @@ -456,7 +456,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('api.stats', { 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', { data: request.body.data, 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 @@ -480,7 +480,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('api.stats', { timestamp: _.now(), call: 'getCurrentStreamData', api: 'helix', endpoint: url, code: request.status, remaining: this.remainingAPICalls })
global.db.engine.insert('api.stats', { data: request.body.data, 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}`)
Expand Down Expand Up @@ -653,7 +653,7 @@ 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('api.stats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, 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('api.stats', { timestamp: _.now(), call: 'setTitleAndGame', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
Expand Down Expand Up @@ -704,7 +704,7 @@ 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('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body.data, 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('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
Expand All @@ -729,14 +729,14 @@ 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('api.stats', { timestamp: _.now(), call: 'sendGameFromTwitch', api: 'kraken', endpoint: url, code: request.status })
global.db.engine.insert('api.stats', { data: request.body, timestamp: _.now(), call: 'fetchAccountAge', 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('api.stats', { 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: 'fetchAccountAge', api: 'kraken', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.message)}` })
return
}
d(request.body)
global.db.engine.update('users', { username: username }, { created_at: moment(request.body.created_at).format('x') })
await global.db.engine.update('users', { username: username }, { time: { created_at: moment(request.body.created_at).format('x') } })
}

async isFollower (username) {
Expand Down Expand Up @@ -767,7 +767,7 @@ 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('api.stats', { timestamp: _.now(), call: 'isFollowerUpdate', api: 'helix', endpoint: url, code: request.status, remaining: global.twitch.remainingAPICalls })
global.db.engine.insert('api.stats', { data: request.body.data, 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)}`)
Expand Down
18 changes: 14 additions & 4 deletions public/pages/apistats.html
Expand Up @@ -20,14 +20,15 @@
<div role="tabpanel" class="tab-pane active" id="helix">
<div class="widget">
<canvas id="helixChart" width="400" height="100"></canvas>
<table class="table table-responsive">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">time</th>
<th scope="col">call</th>
<th scope="col">endpoint</th>
<th scope="col">status</th>
<th scope="col">remaining API calls</th>
<th scope="col">data</th>
</tr>
</thead>
<tbody class="helixData"></tbody>
Expand All @@ -44,6 +45,8 @@
<th scope="col">call</th>
<th scope="col">endpoint</th>
<th scope="col">status</th>
<th scope="col">remaining API calls</th>
<th scope="col">data</th>
</tr>
</thead>
<tbody class="krakenData"></tbody>
Expand All @@ -60,6 +63,8 @@
<th scope="col">call</th>
<th scope="col">endpoint</th>
<th scope="col">status</th>
<th scope="col">remaining API calls</th>
<th scope="col">data</th>
</tr>
</thead>
<tbody class="tmiData"></tbody>
Expand All @@ -76,6 +81,8 @@
<th scope="col">call</th>
<th scope="col">endpoint</th>
<th scope="col">status</th>
<th scope="col">remaining API calls</th>
<th scope="col">data</th>
</tr>
</thead>
<tbody class="unofficialData"></tbody>
Expand Down Expand Up @@ -108,19 +115,22 @@
socket.on('api.stats', (data) => {
data = _.orderBy(data, 'timestamp', 'asc')
lastTimestampFetched = _.get(data[data.length - 1], 'timestamp', 0)
for (let c of data) {

for (c of data) {
let tBody = `.${c.api}Data`
c.code = _.get(c, 'code', 200) // set default to 200
c.data = !_.isNil(c.data) ? JSON.stringify(c.data) : 'n/a'
c.remaining = !_.isNil(c.remaining) ? c.remaining : 'n/a'

$(tBody).prepend(
`
<tr class="${c.code.toString().startsWith('2') ? '' : 'font-weight-bold text-danger'}">
<th scope="row">${moment(c.timestamp).format('LTS')}</th>
<td>${c.call}</td>
<td><div style="word-wrap: break-word; font-family: Monospace">${c.endpoint}</div></td>
<td><div style="word-wrap: break-word; font-family: Monospace; width: 200px;">${c.endpoint}</div></td>
<td>${c.code}</td>
${c.remaining ? `<td>${c.remaining}</td>` : ''}
<td>${c.remaining}</td>
<td><div style="word-wrap: break-word; font-family: Monospace; width: 100%;">${c.data}</div></td>
</tr>
`
)
Expand Down

0 comments on commit a232820

Please sign in to comment.