Skip to content

Commit

Permalink
add utils/messager.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hlechner committed Feb 27, 2016
1 parent 53f49c9 commit e96bd27
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 41 deletions.
3 changes: 2 additions & 1 deletion server/src/ajax/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var $path = require('path'),
file = require('./file'),
envelope = require('./envelope').envelope,
debugMsg = require('../utils/messager').debugMsg,
library = require('../logic/library').library;

// runs the endpoint
Expand All @@ -30,7 +31,7 @@ function run(endpoint, query, res) {
throw "Missing parameters";
}
library.set(query.name, function (data) {
console.log("library changed");
debugMsg("library changed");
ok(data);
});
});
Expand Down
11 changes: 6 additions & 5 deletions server/src/db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
////////////////////////////////////////////////////////////////////////////////
/*global require, exports, console */
var sqlite = require('../tools/sqlite').sqlite,
debugMsg = require('../utils/messager').debugMsg,

db = function () {
var self,
name;

// creates database
function create(handler) {
console.log("DB - creating DB: " + name);
debugMsg("DB - creating DB: " + name);
sqlite.exec('create.sql', handler, [], true);
}

self = {
// switches databases
name: function (value, handler) {
name = value;
console.log("DB - switching over to DB: " + name);
debugMsg("DB - switching over to DB: " + name);
// switching to new db
sqlite.db(name);
if (!sqlite.exists()) {
Expand All @@ -31,20 +32,20 @@ db = function () {

// executes statement that return data
query: function (statement, handler) {
console.log("DB/" + name + " - executing query");
debugMsg("DB/" + name + " - executing query");
sqlite.exec(statement, handler, ['-header', '-line']);
},

// executes statement that changes data
nonQuery: function (statement, handler) {
console.log("DB/" + name + " - executing non-query");
debugMsg("DB/" + name + " - executing non-query");
sqlite.exec(statement, handler);
},

// executes statement that changes data
// feeds input to sqlite process with pipe
nonQueryPiped: function (statement, handler) {
console.log("DB/" + name + " - executing non-query with pipe");
debugMsg("DB/" + name + " - executing non-query with pipe");
sqlite.exec(statement, handler, [], true);
}
};
Expand Down
13 changes: 7 additions & 6 deletions server/src/db/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
////////////////////////////////////////////////////////////////////////////////
/*global require, exports, console */
var db = require('../db/db').db,
debugMsg = require('../utils/messager').debugMsg,

// escapes quotes in SQL statements by duplicating them
quotes = function (text) {
Expand Down Expand Up @@ -62,7 +63,7 @@ entity = {
where.length ? ["WHERE", where.join(" AND ")].join(" ") : ""
].join(" ");

console.log(statement);
debugMsg(statement);
db.query(statement, handler);

return this;
Expand All @@ -81,7 +82,7 @@ entity = {
"WHERE", this.key, "IN", "('" + keys.join("','") + "')"
].join(" ");

console.log(statement);
debugMsg(statement);
db.query(statement, handler);

return this;
Expand All @@ -99,7 +100,7 @@ entity = {
["(", pair.values.join(","), ")"].join("")
].join(" ");

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);

return this;
Expand All @@ -118,7 +119,7 @@ entity = {
where.length ? ["WHERE", where.join(" AND ")].join(" ") : ""
].join(" ");

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);

return this;
Expand Down Expand Up @@ -153,7 +154,7 @@ entity = {
statement.push("COMMIT");

if (counter > 0) {
console.log("ENTITY - multiSet SQL statement built: " + statement.length + " lines");
debugMsg("ENTITY - multiSet SQL statement built: " + statement.length + " lines");
db.nonQuery(statement.join(";\n"), handler);
} else if (handler) {
handler();
Expand All @@ -173,7 +174,7 @@ entity = {
where.join(" AND ")
].join(" ");

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);

return this;
Expand Down
9 changes: 5 additions & 4 deletions server/src/db/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var $path = require('path'),
$media = require('../db/media'),
quotes = require('../db/entity').quotes,
db = require('../db/db').db,
debugMsg = require('../utils/messager').debugMsg,

library = function () {
var self = {
Expand All @@ -17,7 +18,7 @@ library = function () {
"WHERE rootid =", rootid
].join(" ");

console.log(statement);
debugMsg(statement);
db.query(statement, handler);
},

Expand All @@ -41,7 +42,7 @@ library = function () {
filter ? $media.filter(filter, 'media') : ""
].join(" ");

console.log(statement);
debugMsg(statement);
db.query(statement, handler);
},

Expand All @@ -60,7 +61,7 @@ library = function () {
'COMMIT;'
].join('\n');

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);
},

Expand Down Expand Up @@ -157,7 +158,7 @@ library = function () {

if (count > 0) {
// executing statement
console.log("LIBRARY - ingest SQL statement built: " + statement.length + " lines");
debugMsg("LIBRARY - ingest SQL statement built: " + statement.length + " lines");
db.nonQueryPiped(statement.join('\n'), handler);
} else {
handler();
Expand Down
7 changes: 4 additions & 3 deletions server/src/db/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
////////////////////////////////////////////////////////////////////////////////
/*global require, exports, console */
var entity = require('../db/entity').entity,
db = require('../db/db').db,
db = require('../db/db').db,
debugMsg = require('../utils/messager').debugMsg,

// constructs a where clause that will retrieve
// media records filtered by tags
Expand Down Expand Up @@ -46,7 +47,7 @@ media = function (mediaid) {
"JOIN roots USING (rootid)",
"WHERE mediaid =", mediaid
].join(" ");
console.log(statement);
debugMsg(statement);
db.query(statement, handler);
};

Expand All @@ -57,7 +58,7 @@ media = function (mediaid) {
"JOIN roots USING (rootid)",
"WHERE mediaid IN", "('" + mediaids.join("','") + "')"
].join(" ");
console.log(statement);
debugMsg(statement);
db.query(statement, handler);
};

Expand Down
9 changes: 5 additions & 4 deletions server/src/db/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var $media = require('../db/media'),
db = require('../db/db').db,
entity = require('../db/entity').entity,
clause = require('../db/entity').clause,
debugMsg = require('../utils/messager').debugMsg,

tag = function () {
var base = Object.create(entity, {kind: {value: 'tags'}}),
Expand Down Expand Up @@ -40,7 +41,7 @@ tag = function () {
]).join(" ");

if (handler) {
console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);
return self;
} else {
Expand Down Expand Up @@ -91,7 +92,7 @@ tag = function () {
"COMMIT"
]).join(";\n");

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);
};

Expand Down Expand Up @@ -120,7 +121,7 @@ tag = function () {
].join(" ");

if (handler) {
console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);
return self;
} else {
Expand Down Expand Up @@ -157,7 +158,7 @@ tag = function () {
"END TRANSACTION"
].join(";\n");

console.log(statement);
debugMsg(statement);
db.nonQuery(statement, handler);
};

Expand Down
7 changes: 4 additions & 3 deletions server/src/logic/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var vlc = require('../tools/vlc').vlc,
entity = require('../db/media').media,
thumbs = require('../logic/thumbs').thumbs,
debugMsg = require('../utils/messager').debugMsg,

media = function (mediaid) {
var self = {
Expand All @@ -22,9 +23,9 @@ media = function (mediaid) {
thumbs.generate([mediaid]);
// starting playback
var path = data[0].root + data[0].path;
console.log("MEDIA - starting playback of file: " + path);
debugMsg("MEDIA - starting playback of file: " + path);
vlc.exec(path, function (path) {
console.log("MEDIA - playback finished or interrupted");
debugMsg("MEDIA - playback finished or interrupted");
});
// not waiting for playback to finish
if (handler) {
Expand All @@ -36,7 +37,7 @@ media = function (mediaid) {

// rates media file
rate: function (rating, handler) {
console.log("MEDIA - rating media: " + mediaid + " at: " + rating);
debugMsg("MEDIA - rating media: " + mediaid + " at: " + rating);
entity(mediaid).set({rating: rating}, handler);
return self;
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/logic/processes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var thumb = require('../logic/thumb').thumb,
media = require('../db/media').media,
queue = require('../utils/queue').queue,
ffmpeg = require('../tools/ffmpeg').ffmpeg,
debugMsg = require('../utils/messager').debugMsg,

processes,

Expand Down Expand Up @@ -77,7 +78,7 @@ processes = {
entry = {rootid: tmp[0], mediaid: tmp[1], root: tmp[2], path: tmp[3], hash: tmp[4], keywords: {}};
thumb.generate(entry.root + entry.path, entry.hash, function (data) {
if (typeof data !== 'undefined') {
console.log("PROCESSES - generated thumbnail: " + entry.hash);
debugMsg("PROCESSES - generated thumbnail: " + entry.hash);
entry.keywords = filter(data);
}
finish(entry);
Expand Down
7 changes: 4 additions & 3 deletions server/src/logic/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var library = require('../db/library').library,
entity = require('../db/root').root,
walker = require('../utils/walker').walker,
processes = require('../logic/processes').processes,
debugMsg = require('../utils/messager').debugMsg,

// - path: root path
root = function (path) {
Expand Down Expand Up @@ -41,12 +42,12 @@ root = function (path) {

// ingests suitable files into the library
add: function (handler) {
console.log("ROOT - initializing...");
debugMsg("ROOT - initializing...");
entity.add({'path': path}, function () {
entity.get({'path': path}, function (data) {
var rootid = data[0].rootid;
console.log("ROOT - initialized: " + rootid);
console.log("ROOT - ingesting video metadata into library");
debugMsg("ROOT - initialized: " + rootid);
debugMsg("ROOT - ingesting video metadata into library");
self.scan(function (metadata, count) {
// ending request and returning when nothing found
if (count === 0) {
Expand Down
2 changes: 2 additions & 0 deletions server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var $http = require('http'),
}
}());

module.exports.DEBUG = DEBUG;

// creating server object
server = $http.createServer(function (req, res) {
var url = $url.parse(req.url, true),
Expand Down
9 changes: 5 additions & 4 deletions server/src/tools/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var $fs = require('fs'),
tool = require('./tool').tool,
parser = require('../utils/parser').parser,
tempFile = 'temp.sql',
debugMsg = require('../utils/messager').debugMsg,

sqlite = function () {
var db = 'default',
Expand Down Expand Up @@ -96,7 +97,7 @@ sqlite = function () {

if (statement.match(/^.+\.sql$/ig)) {
// reading statement from file
console.log("SQLITE - reading SQL command from file: " + statement);
debugMsg("SQLITE - reading SQL command from file: " + statement);
statement = $fs.readFileSync('../db/' + statement);
}

Expand All @@ -108,7 +109,7 @@ sqlite = function () {
function onComplete(code, data) {
switch (code) {
case SQLITE_OK:
console.log("SQLITE - retrieved " + (data ? data.length : 0) + " records.");
debugMsg("SQLITE - retrieved " + (data ? data.length : 0) + " records.");
if (handler) {
handler(data);
}
Expand All @@ -119,15 +120,15 @@ sqlite = function () {
throw "Database or table still locked after " + retries + " retries, query failed";
} else {
retries++;
console.log("SQLITE - database or table locked, re-running query (" + retries + "/" + RETRY_COUNT + ")");
debugMsg("SQLITE - database or table locked, re-running query (" + retries + "/" + RETRY_COUNT + ")");
setTimeout(function () {
tool.exec.call(self, args, onComplete, true);
}, RETRY_DELAY);
}
break;
default:
console.log("SQLITE - query failed");
console.log(statement);
debugMsg(statement);
if (handler) {
handler();
}
Expand Down
5 changes: 3 additions & 2 deletions server/src/tools/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
////////////////////////////////////////////////////////////////////////////////
/*global require, exports, Buffer, console */
var $child_process = require('child_process'),
system = require('../utils/system').system,
system = require('../utils/system').system,
debugMsg = require('../utils/messager').debugMsg,

tool = {
// line break string depending on OS
Expand Down Expand Up @@ -48,7 +49,7 @@ tool = {
}

// starting tool
console.log(["TOOL - executing:", that.executable, args ? args.join(" ") : ""].join(" "));
debugMsg(["TOOL - executing:", that.executable, args ? args.join(" ") : ""].join(" "));
that.child = $child_process.spawn(that.executable, args);

// callback
Expand Down
Loading

0 comments on commit e96bd27

Please sign in to comment.