Commit f8998e0e authored by Marco Flowers's avatar Marco Flowers

Caching at the api call level and updated method names

Caching was moved from the website request methods to the json
api methods. The api methods arguments were expanded.
parent 739bd866
......@@ -16,12 +16,13 @@ var config = conf('config');
/* Controllers */
var streamApi = require('./controllers/stream-api.js')(query, cache);
var index = require('./controllers/index.js')(query, cache, streamApi);
var genres = require('./controllers/genres.js')(query, cache, streamApi);
var formats = require('./controllers/formats.js')(query, cache, streamApi);
var streamsFindBy = require('./controllers/stream-api.js')(query, cache);
var streamFindById = require('./controllers/stream-by-id.js')(query, cache);
var index = require('./controllers/index.js')(query, cache, streamsFindBy);
var genres = require('./controllers/genres.js')(query, cache, streamsFindBy);
var formats = require('./controllers/formats.js')(query, cache, streamsFindBy);
var yp_cgi = require('./controllers/yp-cgi.js')(query, qs, validator, config);
var listen = require('./controllers/listen.js')(query, qs, xmlbuilder, streamApi);
var listen = require('./controllers/listen.js')(query, qs, xmlbuilder, streamFindById);
......@@ -57,18 +58,22 @@ function respond(res, err, rows, result) {
}
app.get('/streams/', function(req,res){
res.set('Content-Type', 'application/json');
streamApi(req.query, function(err, rows,result){
respond(res, err, rows, result);
var params = req.query;
streamsFindBy(params.format, params.genre, params.q, params.order, params.limit, 1, function(err, rows){
if(err || rows[0].array_to_json == null) {
res.send([]);
} else {
res.send(rows[0].array_to_json);
}
});
});
app.get('/streams/:streamId', function(req,res){
res.set('Content-Type', 'application/json');
params = {'id':req.params.streamId};
streamApi(params, function(err,rows,result){
if(err || result.rowCount != 1) {
res.send([]);
streamFindById(req.params.streamId, 1, function(err,rows,result){
if(err || rows[0].array_to_json == null) {
res.send({});
} else {
res.send(rows[0]);
res.send(rows[0].array_to_json[0]);
}
});
});
......
var query, cache, streamApi;
var query, cache, streamsFindBy;
function init(q, c, s) {
query = q;
cache = c;
streamApi = s;
streamsFindBy = s;
return byFormat;
}
function byFormat(req, res) {
getCachedFormatStreams(req.param("format"), function(err, result) {
var format = req.param("format");
streamsFindBy(format, undefined, undefined, 1, 10, 0, function(err, rows) {
if (err) {
res.send(503);
} else {
var error;
if (result.rowCount === 0) {
error = "No streams for this format.";
if (rows.length === 0) {
error = "No streams for this genre.";
} else {
error = false;
}
res.render("by_xx", {
title: req.param("format"),
servers: result.rows,
title: format,
servers: rows,
error: error
});
}
});
}
function getCachedFormatStreams(format, cb) {
cache.wrap(format, function (_cb) {
getFormatStreams(format, _cb);
}, 5, cb);
}
function getFormatStreams(format, cb) {
var params = {'format':format};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
module.exports = init;
var query, cache, streamApi;
var query, cache, streamsFindBy;
function init(q, c, s) {
query = q;
cache = c;
streamApi = s;
streamsFindBy = s;
return byGenre;
}
function byGenre(req, res) {
getCachedGenreStreams(req.param("genre"), function(err, result) {
var genre = req.param("genre");
streamsFindBy(undefined, genre, undefined, 1, 10, 0, function(err, rows) {
if (err) {
res.send(503);
} else {
var error;
if (result.rowCount === 0) {
if (rows.length === 0) {
error = "No streams for this genre.";
} else {
error = false;
}
res.render("by_xx", {
title: req.param("genre"),
servers: result.rows,
title: genre,
servers: rows,
error: error
});
}
});
}
function getCachedGenreStreams(genre, cb) {
cache.wrap(genre, function (_cb) {
getGenreStreams(genre, _cb);
}, 5, cb);
}
function getGenreStreams(genre, cb) {
var params = {'genre':genre};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
module.exports = init;
var query, cache, streamApi;
var query, cache, streamsFindBy;
function init(q, c, s) {
query = q;
cache = c;
streamApi = s;
streamsFindBy = s;
return index;
}
function index(req, res) {
getCachedRandomStreams(20, function(err, result) {
streamsFindBy(undefined, undefined, undefined, 0, 20, 0, function(err, rows) {
if (err) {
res.send(503);
} else {
var error;
if (result.rowCount === 0) {
if (rows.length === 0) {
error = "No streams found.";
} else {
error = false;
}
console.log(result.rows);
res.render("index", {
title: '',
servers: result.rows,
servers: rows,
error: error
});
}
});
}
function getCachedRandomStreams(count, cb) {
cache.wrap(count, function (_cb) {
getRandomStreams(count, _cb);
}, 5, cb);
}
function getRandomStreams(count, cb) {
var params = {'limit':count,'order':0};
streamApi(params, function(err, rows, result) {
cb(err, result);
});
}
module.exports = init;
var query, cache, xmlbuilder, streamApi;
var query, cache, xmlbuilder, streamFindById;
function init(q, c, x, s) {
query = q;
cache = c;
streamApi = s;
streamFindById = s;
xmlbuilder = x;
return getListen;
}
function getListen(req, res) {
params = {'id':req.params.streamId};
console.log(req.get('User-Agent'));
var userAgent = req.get('User-Agent');
filename = req.params.filename;
streamApi(params,function(err, rows, result) {
if(err || result.rowCount != 1) {
streamFindById(req.params.streamId, 0,function(err, rows) {
if(err || rows.length != 1) {
res.status(404);
res.send('Stream not in the Database');
return;
......
var query, cache;
function init(q, c) {
query = q;
cache = c;
return streamApi;
return getCachedStreams;
}
function getCachedStreams(format, genre, q, order, limit, json, cb) {
var cacheString = JSON.stringify({"format":format,"genre":genre,"q":q,"order":order,"limit":limit,"json":json});
console.log(cacheString);
cache.wrap(cacheString, function (_cb) {
var params = JSON.parse(cacheString);
findBy(params.format, params.genre, params.q, params.order, params.limit, params.json, _cb);
}, 5, cb);
}
/*
Takes in paramters {}
q = Search string
id, limit, offset, genre, format, order(0 Random, 1 Listeners Desc, 2 Listeners Asc)
callback is called during the resulting query, should be
function(err, rows, result){}
function(err, rows){}
Turns parameters into a sql query on streams
*/
function streamApi(params,callback)
function findBy(format, genre, q, order, limit, json, resultCallback)
{
var allStreamsBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, s.cm, s.samplerate, s.channels, s.quality, s.genres, array_agg(sm.listenurl) AS listenurls, COUNT(sm.listeners) AS listeners, COUNT(sm.max_listeners) AS max_listeners FROM streams s INNER JOIN server_mounts AS sm ON s.id = sm.stream_id ';
var allStreamsGroup = 'GROUP BY s.id ';
console.log(format);
console.log(genre);
var queryStringJsonBeg = 'SELECT array_to_json(array_agg(row_to_json(t))) FROM (';
var queryStringJsonEnd = ' ) t';
var queryStringBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description \
,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, \
s.cm, s.samplerate, s.channels, s.quality, s.genres, array_agg(sm.listenurl) AS listenurls, \
COUNT(sm.listeners) AS listeners, COUNT(sm.max_listeners) AS max_listeners \
FROM streams s \
INNER JOIN server_mounts AS sm ON s.id = sm.stream_id ';
var queryStringGroup = 'GROUP BY s.id ';
var queryString;
var countItems = 1;
var items = [];
var queryString = allStreamsBeg;
var genre, format, streamId, search, order, limit, offset;
if(params.genre) {
genre = params.genre;
if(Array.isArray(genre)) {
for(var i =0; i <genre.length;i++)
{
queryString += genreFormatModifier(countItems, 's.genres');
countItems++;
items.push(genre[i]);
}
} else {
queryString += genreFormatModifier(countItems, 's.genres');
countItems++;
items.push(genre);
}
// if json add the json conversion to the query
if(json) {
queryString = queryStringJsonBeg+queryStringBeg;
} else {
queryString = queryStringBeg;
}
if(params.format) {
format = params.format;
if(Array.isArray(format)) {
for(var i =0; i <genre.length;i++)
{
queryString += genreFormatModifier(countItems, 's.codec_sub_types');
countItems++;
items.push(format[i]);
}
} else {
queryString += genreFormatModifier(countItems, 's.codec_sub_types');
countItems++;
items.push(format);
}
var search;
if(genre) {
queryString += genreFormatModifier(countItems, 's.genres');
countItems++;
items.push(genre);
}
if(params.id) {
streamId = params.id;
if(countItems > 1) {
queryString += 'AND $'+countItems+' = s.id ';
} else {
queryString += 'WHERE $'+countItems+' = s.id ';
}
if(format) {
queryString += genreFormatModifier(countItems, 's.codec_sub_types');
countItems++;
items.push(streamId);
items.push(format);
}
if(params.q) {
search = params.q;
if(q) {
search = q;
var add = false;
var searchWord = '';
var searchArray ='(';
......@@ -105,46 +100,33 @@ function streamApi(params,callback)
queryString += searchString;
} else {
// search query won't return any results
callback(0,[],{});
resultCallback(0,[],{});
return;
}
}
// add in the Group BY
queryString = queryString + allStreamsGroup;
if(params.order) {
order = params.order;
queryString = queryString + queryStringGroup;
if(order) {
if(order == 0) {
queryString += 'ORDER BY random() ';
} else if(order == 1) {
queryString += 'ORDER BY listeners DESC';
queryString += 'ORDER BY listeners DESC ';
} else if(order == 2) {
queryString += 'ORDER BY listeners ASC';
queryString += 'ORDER BY listeners ASC ';
}
}
if(params.limit) {
limit = params.limit;
if(Array.isArray(limit)) {
limit = limit[0];
} else {
queryString += 'LIMIT $'+countItems+' ';
countItems++;
items.push(limit);
}
if(limit) {
queryString += 'LIMIT $'+countItems+' ';
countItems++;
items.push(limit);
}
if(params.offset) {
offset = params.offset;
if(Array.isArray(offset)) {
offset = offset[0];
} else {
queryString += 'OFFSET $'+countItems+' ';
countItems++;
items.push(offset);
}
if(json) {
queryString += queryStringJsonEnd;
}
//console.log(queryString)
query(queryString, items, callback);
console.log(queryString);
query(queryString, items, resultCallback);
}
function genreFormatModifier(count, type)
{
if(count > 1) {
......
var query, cache;
function init(q, c) {
query = q;
cache = c;
return getCachedStreamById;
}
function getCachedStreamById(id, json, cb) {
var cacheString = JSON.stringify({"id":id,"json":json});
cache.wrap(cacheString, function (_cb) {
var params = JSON.parse(cacheString);
findById(params.id, params.json, _cb);
}, 5, cb);
}
function findById(id, json, resultCallback)
{
var queryStringJsonBeg = 'SELECT array_to_json(array_agg(row_to_json(t))) FROM (';
var queryStringJsonEnd = ' ) t';
var queryStringBeg = 'SELECT s.id, s.stream_name, s.stream_type, s.description \
,s.songname, s.url, s.avg_listening_time, s.codec_sub_types, s.bitrate, s.hits, \
s.cm, s.samplerate, s.channels, s.quality, s.genres, array_agg(sm.listenurl) AS listenurls, \
COUNT(sm.listeners) AS listeners, COUNT(sm.max_listeners) AS max_listeners \
FROM streams s \
INNER JOIN server_mounts AS sm ON s.id = sm.stream_id \
WHERE $1 = s.id \
GROUP BY s.id ';
var queryString;
if(json) {
queryString = queryStringJsonBeg+queryStringBeg;
} else {
queryString = queryStringBeg;
}
var items = [id];
if(json) {
queryString += queryStringJsonEnd;
}
console.log(queryString);
query(queryString, items, resultCallback);
}
module.exports = init;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment