Skip to content

Commit

Permalink
Merge pull request #62 from inaes-tic/mosto-messages-iobackends-use-this
Browse files Browse the repository at this point in the history
Mosto messages iobackends use this
  • Loading branch information
josx committed Apr 9, 2014
2 parents 56d997e + dad962b commit 3d845b3
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ module.exports = {
dbUser: "",
dbPassword: "",
},
TestingDB: {
dbName: "mediadb_test",
dbHost: "localhost",
dbPort: 27017,
dbUser: "",
dbPassword: "",
},
Redis: {
host: "localhost",
port: 6379,
Expand Down
7 changes: 7 additions & 0 deletions config/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ module.exports = {
dbUser: process.env.DBUSER,
dbPassword: process.env.DBPASSWORD,
},
TestingDB: {
dbName: process.env.TESTDBNAME,
dbHost: process.env.DBHOST,
dbPort: parseInt(process.env.DBPORT),
dbUser: process.env.DBUSER,
dbPassword: process.env.DBPASSWORD,
},
Redis: {
host: process.env.REDISHOST,
port: parseInt(process.env.REDISPORT),
Expand Down
32 changes: 32 additions & 0 deletions config/schema_validator.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,38 @@
}
}
},
"TestingDB" : {
"type" : "object",
"title" : "Database for tests.",
"description" : "Authentication params for the database used by tests.",
"properties" : {
"dbName" : {
"type": "string",
"title": "Name",
"description" : ""
},
"dbHost" : {
"type": "string",
"title": "Host",
"description" : ""
},
"dbPort" : {
"type": "integer",
"title": "Port",
"description" : ""
},
"dbUser" : {
"type": "string",
"title": "User",
"description" : ""
},
"dbPassword" : {
"type": "string",
"title": "Password",
"description" : ""
}
}
},
"Redis" : {
"type" : "object",
"title" : "Redis",
Expand Down
101 changes: 101 additions & 0 deletions models/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var App, server = false;
if (typeof exports !== 'undefined') {
App = exports;
server = true;
moment = require('moment');
} else {
App = root.App = {};
}
Expand Down Expand Up @@ -278,5 +279,105 @@ App.RelationalConfig = Backbone.RelationalModel.extend({
});
}

App.MostoCodes = { // message code and their descriptions
// FORMAT: [description, default message[, isError (boolean)]]

// 1xx are info codes
101: ["FORCE CHECKOUT", "Fetching more playlists.", false],
// 2xx are warning codes
201: ["BLANK PLAYING", "Blank clip playing", false],
202: ["OUT OF SYNC", "Melted was out of sync", false],
203: ["STARTED PLAYING", "Melted was stopped and was started", false],
204: ["NO CLIPS", "No clips loaded.", false],
// 4xx are "client error" codes. A problem in db content, for example
// 5xx are "server error" codes. Mosto couldn't find the requested file,
// connection problem with melted, etc
500: ["MOSTO DEAD", "Mosto is silent", true],
501: ["MELTED CONNECTION ERROR", "Cannot connect to melted", true],
502: ["FILE NOT FOUND", "Requested media file cannot be found", true],
503: ["SYNC MELTED ERROR", "Error inside syncMelted.", true],
};

App.MostoMessage = Backbone.Model.extend({
urlRoot: 'message',
backend: 'mostomessagesbackend',
idAttribute: '_id',
initialize: function() {
Backbone.Model.prototype.initialize.apply(this, arguments);

if(!this.get('start')) {
this.set('start', moment().valueOf());
}
var code = this.get('code');
if(!(code == -1)) {
var data = App.MostoCodes[code];
var attrs = this.toJSON();
if(!attrs.description || attrs.description == 'INVALID')
this.set('description', data[0]);
if(!attrs.message || attrs.message == 'INVALID')
this.set('message', data[1]);
if(data[2]) {
// "sticky" errors have a "failing" status
this.set('status', 'failing');
this.set('type', 'error');
} else {
// but one-shot errors end immediatly
if(undefined == this.get('end'))
this.set('end', this.get('start'));
}
}
},
isError: function() {
return this.get('type') == 'error';
},
isNotification: function() {
return this.get('type') == 'notification';
},
resolve: function() {
this.set('status', 'fixed');
this.set('end', moment().valueOf());
},
reopen: function() {
this.set('status', 'failing');
this.set('start', moment().valueOf());
this.set('end', 0);
},
defaults: {
code: -1,
description: "INVALID",
message: "INVALID",
status: "one-shot", // "one-shot" , "failing", "fixed"
reference: "",
sticky: false,
type: 'notification',
start: 0, // moment.valueOf() , something like 1392046763435
end: 0, // moment.valueOf() , something like 1392046763435
},
});

App.MostoMessagesCollection = Backbone.Collection.extend({
url: 'message',
model: App.MostoMessage,
backend: 'mostomessagesbackend',
comparator: function(message) { return -message.get('start') },
initialize: function () {
if(!server) {
this.bindBackend();
}
},
});

App.FilteredMessagesCollection = App.MostoMessagesCollection.extend({
initialize: function(models, options) {
this.parent = options.parent;
this.filter_func = options.filter || function() { return true };

this.parent.on('add', this.refilter.bind(this));
},
refilter: function(model) {
this.set(this.parent.filter(this.filter_func.bind(this)));
},
});

if(server) module.exports = App;
else root.App = App;

0 comments on commit 3d845b3

Please sign in to comment.