YUI().use("yql", "template-base", "handlebars", "node-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': '2014-03-20', 'startDate': '2014-03-12', 'from': 0, 'limit': 2000 }; var queries = [{ '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) { console.log(res); var html = ""; var handlebars = new Y.Template(Y.Handlebars); var queryObj = queries.shift(); if (res.query.diagnostics.error === undefined) { if (res.query.results) { //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); } Y.Handlebars.registerHelper('length', function(context) { return context.length; }); 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; }); });