Working on milliseconds issue

parent fb8e3736
...@@ -48,6 +48,7 @@ public abstract class Action { ...@@ -48,6 +48,7 @@ public abstract class Action {
JSONObject jsonObject = new JSONObject() JSONObject jsonObject = new JSONObject()
.put(param_id_stu, PCBcontext.getPcbdb().getCurrentUser().get_id_stu()) .put(param_id_stu, PCBcontext.getPcbdb().getCurrentUser().get_id_stu())
.put(param_timestamp, datetime.format(currentTime)); .put(param_timestamp, datetime.format(currentTime));
Log.d("TIMESTAMP-----------> ", datetime.format(currentTime));
if (PCBcontext.getPcbdb().getCurrentUser().has_supervisor()) if (PCBcontext.getPcbdb().getCurrentUser().has_supervisor())
jsonObject.put(param_id_sup,PCBcontext.getPcbdb().getCurrentUser().get_id_sup()); jsonObject.put(param_id_sup,PCBcontext.getPcbdb().getCurrentUser().get_id_sup());
//TODO Decidir qué almacenar con DEVICE //TODO Decidir qué almacenar con DEVICE
......
...@@ -38,8 +38,8 @@ id_sup int(11) NOT NULL, ...@@ -38,8 +38,8 @@ id_sup int(11) NOT NULL,
id_ws int(11) NOT NULL, id_ws int(11) NOT NULL,
id_opentry int(11) , id_opentry int(11) ,
total_tries int DEFAULT 0, total_tries int DEFAULT 0,
begin timestamp NULL, begin timestamp(3) NULL,
end timestamp NULL, end timestamp(3) NULL,
PRIMARY KEY(id), PRIMARY KEY(id),
UNIQUE(id_stu), UNIQUE(id_stu),
UNIQUE(id_sup), UNIQUE(id_sup),
......
...@@ -42,7 +42,7 @@ module.exports = { ...@@ -42,7 +42,7 @@ module.exports = {
if (!params.type) return res.json(500, {error: "No type of action defined"}); if (!params.type) return res.json(500, {error: "No type of action defined"});
// Optional params // Optional params
var timestamp = null; if(params.timestamp) timestamp = params.timestamp.toISOString(); var timestamp = params.timestamp ? params.timestamp : null;
var sup = null; if(params.supervisor) sup = params.supervisor; var sup = null; if(params.supervisor) sup = params.supervisor;
var _try = null; if(params._try) _try = params._try; var _try = null; if(params._try) _try = params._try;
var description = null; if(params.description) description = params.description; var description = null; if(params.description) description = params.description;
......
...@@ -411,10 +411,16 @@ module.exports = { ...@@ -411,10 +411,16 @@ module.exports = {
supervisor: req.param('id_sup') supervisor: req.param('id_sup')
}) })
.then((stuSup) => { .then((stuSup) => {
if (!stuSup) { if (!stuSup)
res.badRequest(); res.badRequest();
throw new Error('stusup not found');
} stuSup.destroy({
student: req.param('id_stu'),
supervisor: req.param('id_sup')
}).then(function(err){
if (err)
throw err;
});
const socketToOmit = req.isSocket ? req.socket : undefined; const socketToOmit = req.isSocket ? req.socket : undefined;
const unlinkSupervisorFromStudentEvent = sails.hooks.events.unlinkSupervisorFromStudent( const unlinkSupervisorFromStudentEvent = sails.hooks.events.unlinkSupervisorFromStudent(
...@@ -1002,15 +1008,29 @@ module.exports = { ...@@ -1002,15 +1008,29 @@ module.exports = {
} }
}, },
// /**
// Logs a TRY action and broadcast to anyone subscribed to this student * Logs a TRY action and broadcast to anyone subscribed to this student
* @param {request} req
* {
* "action": <action> ("add", "delete", ...),
* "attributes": {
* "id_stu": <id_stu>,
* "timestamp": <timestamp_string_in_ISO_format> (e.g.: "2016-07-13 17:50:00.224+0200"),
* "picto": {...}
* }
* }
* @param {response} res {<action_created>}
*/
action: function (req, res) { action: function (req, res) {
var action = req.param('action'); var action = req.param('action');
var attributes = req.param('attributes'); var attributes = req.param('attributes');
sails.log.debug("Inside action. Student:" + attributes.id_stu); sails.log.debug("Inside action. Student:" + attributes.id_stu);
if (req.isSocket) { if (!req.isSocket) {
sails.log.debug("No socket request for action");
res.badRequest();
} else {
sails.log.debug("websockets - room " + sails.hooks.rooms.student(attributes.id_stu)); sails.log.debug("websockets - room " + sails.hooks.rooms.student(attributes.id_stu));
// BROADCAST to everyone subscribed to this student // BROADCAST to everyone subscribed to this student
const socketToOmit = (req.isSocket) ? req.socket : undefined; const socketToOmit = (req.isSocket) ? req.socket : undefined;
...@@ -1029,29 +1049,20 @@ module.exports = { ...@@ -1029,29 +1049,20 @@ module.exports = {
Action.create({ Action.create({
type: action, type: action,
timestamp: attributes.timestamp, timestamp: attributes.timestamp, // it comes already in ISO format
supervisor: sup, supervisor: sup,
student: attributes.id_stu, student: attributes.id_stu,
description: desc description: desc
}) })
.exec(function (err, created) { .then(function (created) {
if (err) { res.ok({
sails.log.error(err.details); action: created
res.serverError(err.details); });
} })
else if (created) .fail(function(err) {
res.json({ sails.log.error(err.details);
action: created res.serverError(err.details);
});
});
/*
res.json({
msg: "Action "+ action +" action from student " + attributes.id_stu
}); });
*/
} else {
sails.log.debug("No socket request for action");
} }
}, },
......
...@@ -17,28 +17,25 @@ module.exports = { ...@@ -17,28 +17,25 @@ module.exports = {
close: function(req, res) { close: function(req, res) {
if (!req.params.id_ws) if (!req.params.id_ws)
res.badRequest("No working session defined"); res.badRequest("No working session defined");
StuOpenTry.findOne( { id_ws : req.params.id_ws } ).exec(function(err, t) {
if (err) {
sails.log.error("Error Recovering from ws "+err);
return res.json(500, {error: 'recovering from ws:'+err});
}
if(t && t.openTry!=null) /*WS recovery*/ {
sails.log.debug("Recovering WS required for WS "+req.params.id_ws);
var ws_end;
if (t.end==null) ws_end = t.begin;
else ws_end = t.end;
WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()}).exec(function(err,u) {
if (err) {
sails.log.error("Error updating no closed WS " + t.id_ws);
return res.json(500, {error: 'Working Session could not be closed'});
}});
return res.json(200, {end: ws_end.toISOString()});
}
else
return res.json(200, {result:'no recovery required'});
StuOpenTry.findOne( { id_ws : req.params.id_ws } )
.then(function(t) {
var ws_end = req.params.end ? req.params.end : new Date().toISOString();
if (t && t.openTry) /*WS recovery*/ {
ws_end = t.end ? t.end.toISOString() : t.begin.toISOString();
}
WorkingSession.update({id: req.params.id_ws}, {end: ws_end})
.then(function(t) {
res.ok({end: ws_end});
})
.fail(function(err) {
console.log("could not update " + req.params.id_ws);
throw err;
});
})
.fail(function(err) {
res.serverError('Error closing working session: ' + err);
}); });
}, },
...@@ -60,15 +57,17 @@ module.exports = { ...@@ -60,15 +57,17 @@ module.exports = {
console.log(JSON.stringify(params)); console.log(JSON.stringify(params));
if (!params.id_sup) return res.badRequest("No supervisor defined"); if (!params.id_sup) res.badRequest("No supervisor defined");
if (!params.id_ins) return res.badRequest("No instruction defined"); if (!params.id_ins) res.badRequest("No instruction defined");
if (!params.id_stu) return res.badRequest("No student defined"); if (!params.id_stu) res.badRequest("No student defined");
if (!params.desc) params.desc = ""; if (!params.desc) params.desc = "";
var data = {}; var data = {};
data.supervisor = params.id_sup; data.supervisor = params.id_sup;
data.instruction = params.id_ins; data.instruction = params.id_ins;
data.description = params.desc; data.description = params.desc;
console.log("BEGIN " + params.begin);
data.begin = params.begin; // data comes in ISO format
StuOpenTry.findOne({or: [ // pending open try? StuOpenTry.findOne({or: [ // pending open try?
{ id_sup : params.id_sup }, { id_sup : params.id_sup },
...@@ -77,8 +76,8 @@ module.exports = { ...@@ -77,8 +76,8 @@ module.exports = {
.then(function(t) { .then(function(t) {
var ws = null; var ws = null;
if (t && t.openTry) { // YES, so WS not closed, we update to close it if (t && t.openTry) { // YES, so WS not closed, we update to close it
var ws_end = t.end ? t.end : t.begin; var ws_end = t.end ? t.end.toISOString() : t.begin.toISOString();
ws = WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()}) ws = WorkingSession.update({id: t.id_ws}, {end: ws_end})
.then(function(ws) { .then(function(ws) {
return ws; return ws;
}); });
...@@ -127,91 +126,6 @@ module.exports = { ...@@ -127,91 +126,6 @@ module.exports = {
.fail(function(err) { .fail(function(err) {
res.serverError("Session not created: " + err); res.serverError("Session not created: " + err);
}); });
// ---
//
// StuOpenTry.findOne( {or: [
// { id_sup : params.id_sup },
// { id_stu : params.id_stu }
// ]} ).exec(function(err, t) {
// if (err)
// res.serverError("Recovering from ws: "+err);
//
// if(t || t.openTry==null) { //WS closed correctly
// sails.log.debug("No open try, let's create session");
//
// WorkingSession.create(data).exec(function(err, ws){
// if(err || !ws)
// return res.serverError("Creating new Working Sesion error "+err);
//
// // Create the InitSession Action
// // This creates, also, a new try (see triggers-session-constraints.sql)
// Action.create({
// "type": "initsession",
// "timestamp": ws.begin,
// "supervisor": ws.supervisor,
// "student": params.id_stu
// }).exec(function(err, action){
// if(err || !action)
// res.serverError("Creating initial action for new Working Sesion. Error "+err);
//
// StuOpenTry.findOne( {id_stu : params.id_stu } ).exec(function(err, t) {
// if (err)
// res.serverError('Could not open try for new session: '+err);
//
// // Return the working session and try created
// sails.log.debug("Initial action for new Working Sesion "+JSON.stringify(ws)+". Action:"+JSON.stringify(action));
// res.ok({
// "id": ws.id,
// "first_try_id":t.openTry,
// "recovered_ws": null
// })
// })
// });
// });
// }
// else { //Not closed previous WS must be recovered and closed
// sails.log.debug("Open try found, recovering WS for student "+params.id_stu);
//
// var ws_end;
// if (t.end==null)
// ws_end = t.begin;
// else
// ws_end = t.end;
// WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()}).exec(function(err,u) {
// if (err)
// res.serverError("Error updating non closed WS " + t.id_ws);
//
// WorkingSession.create(data).exec(function(err, ws){
// if(err || !ws)
// res.serverError("Creating new Working Sesion error "+err);
//
// // Create the InitSession Action
// Action.create({
// "type": "initsession",
// "timestamp": ws.begin,
// "supervisor": ws.supervisor,
// "student": params.id_stu
// }).exec(function(err, action){
// if(err || !action)
// res.serverError("Creating initial action for new Working Sesion. Error " + err);
//
// StuOpenTry.findOne( {id_stu : params.id_stu} ).exec(function(err, t) {
// if (err)
// res.serverError("Error Recovering from ws "+err);
//
// // Return the working session and try created
// res.ok({
// "id": ws.id,
// "first_try_id":t.openTry,
// "recovered_ws": null
// })
// })
// });
// });
// });
// }
// });
}, },
/** /**
......
...@@ -73,5 +73,10 @@ module.exports = { ...@@ -73,5 +73,10 @@ module.exports = {
columnName: "gps_lon", columnName: "gps_lon",
type: "float" type: "float"
} }
} },
beforeCreate: function (attrs, next) {
sails.log.debug("Inserting action with following attrs: " + JSON.stringify(attrs));
next();
},
}; };
...@@ -128,8 +128,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -128,8 +128,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
//Modify the desc. of a try //Modify the desc. of a try
$scope.update_try = function(t){ $scope.update_try = function(t){
var update_data={}; var update_data={};
if (t.description!=null) update_data.description=t.description; if (t.description)
if (t.result!=null) update_data.result=t.result; update_data.description = t.description;
if (t.result)
update_data.result = t.result;
$http $http
.put(config.backend+'/try/' + t.id, update_data) .put(config.backend+'/try/' + t.id, update_data)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
...@@ -190,12 +192,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -190,12 +192,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
.post(config.backend+ '/ws/'+$scope.wsessions[0].id+'/close') .post(config.backend+ '/ws/'+$scope.wsessions[0].id+'/close')
.then( .then(
function(data, status, headers, config) { function(data, status, headers, config) {
$scope.wsessions[0].end=data.data.end; $scope.wsessions[0].end=data.data.end;
$scope.ws_recover=false; $scope.ws_recover=false;
} }
, function(data, status, headers, config) { ,function(data, status, headers, config) {
} }
); );
} }
...@@ -213,7 +213,8 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -213,7 +213,8 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
.post(config.backend+'/ws', { .post(config.backend+'/ws', {
"id_sup": $scope.user.id, "id_sup": $scope.user.id,
"id_stu": $scope.studentData.id, "id_stu": $scope.studentData.id,
"id_ins": $scope.selectedIns.id "id_ins": $scope.selectedIns.id,
"begin": new Date().toISOString()
}) })
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
...@@ -240,7 +241,7 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -240,7 +241,7 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
ngToast.danger({ content: translation }); ngToast.danger({ content: translation });
}); });
}); });
}; }
// //
// update the description of a given working session // update the description of a given working session
...@@ -259,16 +260,21 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -259,16 +260,21 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
// //
// Finish a working session updating its end time // Finish a working session updating its end time
// //
$scope.stop_ws = function (){ $scope.stop_ws = function () {
// For view // For view
$scope.sessionRunning = false; $scope.sessionRunning = false;
if ($scope.wsessions.length>0) $scope.wsessions[$scope.wsessions.length-1].tries.pop(); if ($scope.wsessions.length>0)
$scope.ws.end = new Date(); $scope.wsessions[$scope.wsessions.length-1].tries.pop();
$scope.ws.end = new Date().toISOString();
$http
.put(config.backend+'/ws/' + $scope.ws.id, { "end": $scope.ws.end, $http
"id_stu": $scope.studentData.id .post(
}) config.backend+'/ws/' + $scope.ws.id + '/close',
{
"end": $scope.ws.end,
"id_stu": $scope.studentData.id
}
)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
// Empty actual WS and actual try // Empty actual WS and actual try
......
...@@ -48,9 +48,10 @@ dashboardControllers.controller('StudentCtrl', function StudentCtrl( ...@@ -48,9 +48,10 @@ dashboardControllers.controller('StudentCtrl', function StudentCtrl(
// WebSockets communication // WebSockets communication
// //
io.socket.on('update_peers', function (data) { io.socket.on('update_peers', function (data) {
$translate('num_peers').then(function (translation) { // REMOVED (too invasive)
ngToast.success(translation + ': ' + data.count); // $translate('num_peers').then(function (translation) {
}); // ngToast.success(translation + ': ' + data.count);
//});
$scope.studentData.num_peers = data.count; $scope.studentData.num_peers = data.count;
}); });
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
"sails": "^0.12.3", "sails": "^0.12.3",
"sails-disk": "~0.10.0", "sails-disk": "~0.10.0",
"sails-generate-auth": "^0.2.0", "sails-generate-auth": "^0.2.0",
"sails-mysql": "^0.10.12", "sails-mysql": "^0.12",
"sails-test-helper": "^0.3.5", "sails-test-helper": "^0.3.5",
"socket.io": "~1.3.2", "socket.io": "~1.3.2",
"socket.io-redis": "^0.1.4", "socket.io-redis": "^0.1.4",
......
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 sign in to comment