Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Measure things that increment or decrement
*/
const counter = io.counter({
name: 'Downloads'
})
/**
* Now let's create some remote action
* And act on the Counter probe we just created
*/
io.action('decrement', { comment: 'Increment downloads' }, (cb) => {
counter.dec()
cb({ success: true })
})
io.action('increment', { comment : 'Decrement downloads' }, (cb) => {
// Increment the previous counter
counter.inc()
cb({ success: true })
})
io.action('throw error', { comment: 'Throw a random error ' }, (cb) => {
// Increment the previous counter
throw new Error('This error will be caught!')
})
io.action('send event', { comment: 'Sends an event' }, (cb) => {
io.emit('event:sent', {
msg: 'You sent a custom event!'
})
cb('Sent event!')
})
if (err) {
console.error('failed to remove from cache');
return rej(err);
}
res(quantity);
});
});
}
function clearCache(key) {
return removeFromCache('*');
}
let cacheSize = 0;
// action to manually clear the ad cache
if (process.env.NODE_ENV !== 'development') {
io.action('cache:clear', async cb => {
console.log('clearing cache');
try {
await clearCache();
cb({ success: true });
} catch (err) {
console.error(err);
cb({ success: false, err: err.message });
}
});
// setInterval(() => {
// cache.size((err, size) => {
// if (err) {
// console.error(err);
// return
// }
// Increment the previous counter
throw new Error('This error will be caught!')
})
io.action('send event', { comment: 'Sends an event' }, (cb) => {
io.emit('event:sent', {
msg: 'You sent a custom event!'
})
cb('Sent event!')
})
io.action('get env', (cb) => {
cb(process.env)
})
io.action('modules version', { comment: 'Get modules version' }, (cb) => {
cb(process.versions)
})
io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
// Replies the received data
cb(`Data received: ${JSON.stringify(data)}`)
})
/**
* Create an action that hit the HTTP server we just created
* So we can see how the meter probe behaves
*/
io.action('do:http:query', (cb) => {
const options = {
hostname: '127.0.0.1',
port: 5005,
})
io.action('modules version', { comment: 'Get modules version' }, (cb) => {
cb(process.versions)
})
io.action('Action with params', { comment: 'Returns sent params' }, (data, cb) => {
// Replies the received data
cb(`Data received: ${JSON.stringify(data)}`)
})
/**
* Create an action that hit the HTTP server we just created
* So we can see how the meter probe behaves
*/
io.action('do:http:query', (cb) => {
const options = {
hostname: '127.0.0.1',
port: 5005,
path: '/users',
method: 'GET',
headers: { 'Content-Type': 'application/json' }
}
const req = http.request(options, (res) => {
res.setEncoding('utf8')
res.on('data', (data) => {
console.log(data)
})
})
req.on('error', (e) => {
this.es.cluster.health({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('get unassigned shards', (reply) => {
this.es.cat.shards({
h: [ 'index', 'state', 'unassigned.reason' ]
}, (err, data) => {
return err
? reply(`An error has occured, \n ${err}`)
: reply(data.split('\n').filter(line => line.indexOf('UNASSIGNED') !== -1).join('\n'))
})
})
io.action('get biggers indices', (reply) => {
this.es.cat.indices({
s: [ 'docs.count:desc' ]
}, (err, data) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('get biggers indices stats', async reply => {
const final = {}
const stats = await this.es.indices.stats()
await Promise.all(Object.keys(stats.indices)
.map(name => {
stats.indices[name].name = name
return stats.indices[name]
})
}, (err, data) => {
return err
? reply(`An error has occured, \n ${err}`)
: reply(data.split('\n').filter(line => line.indexOf('UNASSIGNED') !== -1).join('\n'))
})
})
io.action('get biggers indices', (reply) => {
this.es.cat.indices({
s: [ 'docs.count:desc' ]
}, (err, data) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('get biggers indices stats', async reply => {
const final = {}
const stats = await this.es.indices.stats()
await Promise.all(Object.keys(stats.indices)
.map(name => {
stats.indices[name].name = name
return stats.indices[name]
})
.sort((a, b) => {
return b.total.docs.count - a.total.docs.count
})
.slice(0, 10)
.map(async indice => {
final[indice.name] = {
docs: indice.total.docs.count,
size: filesize(indice.total.store.size_in_bytes),
register () {
io.action('indices stats', (reply) => {
this.es.cat.indices({ bytes: 'k' }, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('shards stats', (reply) => {
this.es.cat.shards({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster infos', (reply) => {
this.es.nodes.info({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster stats', (reply) => {
this.es.nodes.stats({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
})
})
io.action('shards stats', (reply) => {
this.es.cat.shards({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster infos', (reply) => {
this.es.nodes.info({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster stats', (reply) => {
this.es.nodes.stats({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster health', (reply) => {
this.es.cluster.health({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('get unassigned shards', (reply) => {
this.es.cat.shards({
h: [ 'index', 'state', 'unassigned.reason' ]
}, (err, data) => {
return err
register () {
io.action('indices stats', (reply) => {
this.es.cat.indices({ bytes: 'k' }, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('shards stats', (reply) => {
this.es.cat.shards({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster infos', (reply) => {
this.es.nodes.info({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
register () {
io.action('indices stats', (reply) => {
this.es.cat.indices({ bytes: 'k' }, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('shards stats', (reply) => {
this.es.cat.shards({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster infos', (reply) => {
this.es.nodes.info({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster stats', (reply) => {
this.es.nodes.stats({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})
io.action('cluster health', (reply) => {
this.es.cluster.health({}, (err, data, code) => {
return err ? reply(`An error has occured, \n ${err}`) : reply(data)
})
})