Edit in JSFiddle

YUI().use("yql", "template-base", "handlebars", "node-base", "base", function(Y){
	var container = Y.one('#results');
	var templateLoading = "<div id='current'><img src='http://www.craigslist.org/images/map/animated-spinny.gif'></div>";
	var params = {
		'queryParams': {
			'diagnostics': 'true'
		},
		'errorTemplate': '<p class="error">Ooops, błąd: {{error}}',
		'table': 'http://strimoid-strah.rhcloud.com/opentable/strimoid.xml',
		'endDate': '2114-01-01',
		'startDate': '2010-01-01',
		'from': 0,
		'limit': 2000
	};
	var queries = [{
			'query': 'USE "{table}" AS strimoid; SELECT user_id, frontpage_at FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" | sort(field="user_id");',
			'postProcess': getGroupsStats,
			'templateHeader': 'Skuteczność użytkowników',
			'template': '<ul>{{#each data}}<li><strong>{{name}}</strong>: {{items}} treści {{#if frontpage}}/ {{frontpage}} na głównej (skuteczność: {{ratio frontpage items}}){{/if}}<li>{{/each}}</ul>'
		}/*,
		{
			'query': 'USE "{table}" AS strimoid; SELECT * FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" | unique(field="user_id");',
			'templateHeader': '',
			'template': '<h2>Treści: {{sum data "yahoo:repeatcount"}} dodanych przez {{length data}} użytkowników</h2>'
		},
		{
			'query': 'USE "{table}" AS strimoid; SELECT * FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" | sort(field="uv", descending="true") | truncate(5);',
			'templateHeader': '<h2>Najpopularniejsze treści</h2>',
			'template': '<ul>{{#each data}}<li><a href="http://strimoid.pl/c/{{_id}}">{{title}}</a> ({{user_id}}): <strong>{{uv}}</strong></li>{{/each}}</ul>'
		},
		{
			'query': 'USE "{table}" AS strimoid; SELECT user_id FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" | unique(field="user_id") | sort(field="yahoo:repeatcount", descending="true") | truncate(10);',
			'templateHeader': '<h2>Top 10 użytkowników (dodane treści)</h2>',
			'template': '<ul>{{#each data}}<li>{{user_id}}: {{yahoo:repeatcount}}</li>{{/each}}</ul>'
		},
		{
			'query': 'USE "{table}" AS strimoid; SELECT votes FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" and votes.up="true" | unique(field="votes.user_id") | sort(field="yahoo:repeatcount", descending="true") | truncate(10);',
			'templateHeader': '<h2>Top 10 użytkowników (głosujący UV)</h2>',
			'template': '<ul>{{#each data}}<li>{{votes.user_id}}: {{yahoo:repeatcount}}</li>{{/each}}</ul>'
		},
		{
			'query': 'USE "{table}" AS strimoid; SELECT votes FROM strimoid({from},{limit}) where created_at<"{endDate}" and created_at>"{startDate}" and votes.up="false" | unique(field="votes.user_id") | sort(field="yahoo:repeatcount", descending="true") | truncate(10);',
			'templateHeader': '<h2>Top 5 użytkowników (głosujący DV)</h2>',
			'template': '<ul>{{#each data}}<li>{{votes.user_id}}: {{yahoo:repeatcount}}</li>{{/each}}</ul>'
		}*/
	];

	doQuery(procResults);

	function doQuery(callback) {
		var query;
		if (queries.length > 0) {
			console.log('Query start');
			query = queries[0].query;
			query = Y.Lang.sub(query, params);
			console.log(query);
			container.append(queries[0].templateHeader + templateLoading);
			Y.YQL(query, callback, params.queryParams);
		} else {
			console.log('No more queries');
		}
	}

	function procResults(res) {
		var html = "";
		var handlebars = new Y.Template(Y.Handlebars);
		var queryObj = queries.shift();
		if (res.query.diagnostics.error === undefined) {
			if (res.query.results) {
				if (queryObj.postProcess) {
					res.query.results.data = queryObj.postProcess(res.query.results.data);
				}
				//TODO: if 'data' contains just one element 'data' is NOT an array - template breaks
				html = handlebars.render(queryObj.template, res.query.results);
			} else {
				html = "<p>N/A";
			}
		} else {
			html = handlebars.render(params.errorTemplate, res.query.diagnostics);
		}
		Y.one('#current').remove(true);
		container.append(html);
		doQuery(procResults);
	}

	function formatDate(date, offset) {
		var now = new Date();
		var timestamp = now.getTime();
	}

	function getGroupsStats(data) {
		var result ={};
		data.forEach(function(el, ind, arr) {
			if (!result[el.user_id]){
				result[el.user_id] = {
					items: 0,
					frontpage: 0
				}
			}
			result[el.user_id].items += 1;
			if (el.frontpage_at) {
				result[el.user_id].frontpage += 1;
			}
		});
		return obj2arr(result);
	}

	function obj2arr(obj) {
		var arr = [];
		Y.Object.each(obj, function (val, key, o) {
			val.name = key;
			arr.push(val)
		});
		return arr;
	}

	Y.Handlebars.registerHelper('length', function(context) {
		return context.length;
	});
	Y.Handlebars.registerHelper('ratio', function(x, y) {
		return parseInt(x/y * 100) + "%"
	})	
	Y.Handlebars.registerHelper('sum', function(context, property) {
		var sum = 0;
		for (var i = 0; i < context.length; i++) {
			sum += parseInt(context[i][property]);
		};
		return sum;
	});
});