【发布时间】:2015-05-16 01:36:04
【问题描述】:
我有一个运行并从 httpprovider 获取统计信息的小节点应用程序
它每秒以这种格式返回值:
{ WOWZA_CONNECTIONS_CURRENT: 21,
WOWZA_CONNECTIONS_TOTAL: 4879,
WOWZA_CONNECTIONS_BYTES_IN: 303242,
WOWZA_CONNECTIONS_BYTES_OUT: 96372 }
但我需要它以不同的格式返回,就像这样:
WOWZA_CONNECTIONS_CURRENT 21 Rem-East-v4-Edge-1
WOWZA_CONNECTIONS_TOTAL 4879 Rem-East-v4-Edge-1
WOWZA_CONNECTIONS_BYTES_IN 303242 Rem-East-v4-Edge-1
WOWZA_CONNECTIONS_BYTES_OUT 96372 Rem-East-v4-Edge-1
我将如何做到这一点?
这是我的代码:
#!/usr/bin/env node
// Requires.
// request, jsdom, optimist, all of which can be install with npm.
// NOTE: -----------------------------------------------------------------------
// Uses jsdom 0.2.13, 0.2.14 has a bug
// https://github.com/tmpvar/jsdom/issues/436
// Install w/ npm via `npm install jsdom@0.2.13`
// -----------------------------------------------------------------------------
// Require modules.
var request = require('request'),
jsdom = require('jsdom'),
argv = require('optimist').argv;
// Make sure that at least the --uri argument was passed.
if (argv.uri.length == 0) {
console.log('URI Required! Script should be called with one argument which is the URI of the connectioncounts HTTP provider to query.');
return;
}
/**
*
*/
var Collector = {
stats: {},
get_stats: function(uri, callback) {
request({ uri: uri }, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error when contacting ' + uri);
}
jsdom.env({
html: body,
scripts: [
'http://code.jquery.com/jquery.min.js'
]
}, function (err, window) {
// User jQuery to Gather some stats from the connectioncounts HTTP
// provider.
var $ = window.jQuery;
Collector.stats['WOWZA_CONNECTIONS_CURRENT'] = parseInt($('ConnectionsCurrent').html());
Collector.stats['WOWZA_CONNECTIONS_TOTAL'] = parseInt($('ConnectionsTotal').html());
Collector.stats['WOWZA_CONNECTIONS_BYTES_IN'] = parseFloat($('MessagesInBytesRate').html());
Collector.stats['WOWZA_CONNECTIONS_BYTES_OUT'] = parseFloat($('MessagesOutBytesRate').html());
callback(window);
});
});
},
}
/**
* Wrap Collector.get_stats call in a closure it works better with setInterval.
*/
var callDelay = function() {
Collector.get_stats(argv.uri, function(response) {
// Print out collected stats.
console.log(Collector.stats);
});
}
// Get stats once right away.
callDelay();
// Allo repeating at specified interval if --repeat is set.
if (argv.repeat != undefined) {
// Default to every 30 seconds if no delay is specified.
if (argv.delay == undefined) {
argv.delay = 30000;
}
setInterval(callDelay, argv.delay);
}
编辑: 通过更改代码链接的输出部分,我实现了所需的输出。
// Print out collected stats.
//console.log(Collector.stats);
console.log('WOWZA_CONNECTIONS_CURRENT ' + Collector.stats.WOWZA_CONNECTIONS_CURRENT + ' Rem-East-v4-Edge-1');
console.log('WOWZA_CONNECTIONS_TOTAL ' + Collector.stats.WOWZA_CONNECTIONS_TOTAL + ' Rem-East-v4-Edge-1')
console.log('WOWZA_CONNECTIONS_BYTES_IN ' + Collector.stats.WOWZA_CONNECTIONS_BYTES_IN + ' Rem-East-v4-Edge-1')
console.log('WOWZA_CONNECTIONS_BYTES_OUT ' + Collector.stats.WOWZA_CONNECTIONS_BYTES_OUT + ' Rem-East-v4-Edge-1')
【问题讨论】:
-
我很惊讶您设法编写了上面的代码,但不知道如何格式化您的输出。
-
这不是我的代码。该代码来自其他人从事的项目,但不再在这里工作。我不精通这种代码语言,这就是我寻求帮助的原因。
-
我会扔给你一块骨头 - 你需要看看评论
// Print out collected stats.的行。 Javascript 并不复杂,我将把它留作练习,让您了解如何与变量交互并将数据输出为格式正确的字符串。 -
我喜欢这里的社区如何投票,因为想要学习的新手问的是一个基本的或格式不正确的问题。我认为这个社区促进了学习和发展编程技能。