Skip to content

Commit

Permalink
sentence-ui first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienDesola committed Oct 15, 2017
1 parent 1199dd2 commit 1b9b187
Show file tree
Hide file tree
Showing 21 changed files with 329 additions and 6 deletions.
4 changes: 4 additions & 0 deletions api/controllers/DashboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ module.exports = {
res.view('me/index', {User: req.session.User, pageName: req.__('pagename-me')});
},

sentence(req, res, next) {
res.view('sentence/index', {User: req.session.User, pageName: req.__('pagename-sentence')});
}

};

23 changes: 22 additions & 1 deletion api/controllers/SentenceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
*/

module.exports = {

index(req, res, next) {
gladys.sentence.get(req.query)
.then(function(events){
return res.json(events);
})
.catch(next);
},
update(req, res, next) {
req.body.id = req.params.id;
gladys.sentence.update(req.body)
.then(function(sentence){
return res.json(sentence);
})
.catch(next);
},
getLabels(req, res, next) {
gladys.sentence.getLabels()
.then(function(sentence){
return res.json(sentence);
})
.catch(next);
}
};

2 changes: 1 addition & 1 deletion api/core/brain/brain.classify.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function classify(user, message){
}

// foreach classified label
return Promise.map(classifications, function(classification){
return Promise.map(classifications, function(classification) {
var splitted = classification.split(sails.config.brain.separator);
sails.log.info(`brain : classify : Identified label ${classification}`);
return callAction(clone(scope), message, splitted[0], splitted[1])
Expand Down
3 changes: 3 additions & 0 deletions api/core/sentence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

module.exports.create = require('./sentence.create.js');
module.exports.createLocal = require('./sentence.createLocal.js');
module.exports.update = require('./sentence.update.js');
module.exports.getAll = require('./sentence.getAll.js');
module.exports.get = require('./sentence.get.js');
module.exports.getLabels = require('./sentence.getLabels.js');
module.exports.insertBatch = require('./sentence.insertBatch.js');
2 changes: 1 addition & 1 deletion api/core/sentence/sentence.createLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function createLocal({ text, label, service, language }){
uuid: uuid.v4(),
text,
label,
status: label === 'no-command-detected' ? 'pending' : 'approuve',
status: label === 'no-command-detected' ? 'pending' : 'approved',
service,
language
});
Expand Down
8 changes: 8 additions & 0 deletions api/core/sentence/sentence.get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var queries = require('./sentence.queries.js');

module.exports = function(options){
options = options || {};
options.skip = parseInt(options.skip) || 0;
options.take = parseInt(options.take) || 50;
return gladys.utils.sql(queries.get, [options.take, options.skip]);
};
5 changes: 5 additions & 0 deletions api/core/sentence/sentence.getLabels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var queries = require('./sentence.queries.js');

module.exports = function(){
return gladys.utils.sql(queries.getLabels, []);
};
2 changes: 2 additions & 0 deletions api/core/sentence/sentence.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ module.exports = {
getAll: 'SELECT * FROM sentence;',
getByUuid: 'SELECT * FROM sentence WHERE uuid = ?;',
getByText: 'SELECT * FROM sentence WHERE lower(text) = lower(?);',
get: 'SELECT * FROM sentence LIMIT ? OFFSET ?;',
getLabels: 'SELECT DISTINCT label from sentence;',
cleanSentencesTable: 'DELETE FROM sentence;'
};
13 changes: 13 additions & 0 deletions api/core/sentence/sentence.update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var Promise = require('bluebird');

module.exports = function update(sentence){
var id = sentence.id;
return Sentence.update({id}, sentence)
.then(function(sentence){
if(sentence.length === 0){
return Promise.reject(new Error('NotFound'));
} else {
return Promise.resolve(sentence[0]);
}
})
};
2 changes: 1 addition & 1 deletion api/models/Sentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
},
status: {
type: 'string',
enum: ['pending', 'reject', 'approved', 'official'],
enum: ['pending', 'rejected', 'approved', 'official'],
defaultsTo: 'pending'
}

Expand Down
81 changes: 81 additions & 0 deletions assets/js/app/sentence/sentence.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Gladys Project
* http://gladysproject.com
* Software under licence Creative Commons 3.0 France
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
* You may not use this software for commercial purposes.
* @author :: Pierre-Gilles Leymarie
*/


(function () {
'use strict';

angular
.module('gladys')
.controller('SentenceCtrl', SentenceCtrl);

SentenceCtrl.$inject = ['sentenceService', '$scope', 'notificationService'];

function SentenceCtrl(sentenceService, $scope, notificationService) {
/* jshint validthis: true */
var vm = this;
vm.sentences = [];
vm.labels = [];
vm.change = change;
vm.reject = reject;
vm.approve = approve;
vm.loadMore = loadMore;
vm.get = get;
vm.remoteIsBusy = false;
vm.noMoreSentences = false;
vm.startValue = 0;
vm.nbElementToGet = 50;
// helpers
vm.eq = (a, b) => a === b;
vm.neq = (a, b) => a != b;

activate();

function activate() {
return getLabels()
.then(loadMore());
}

function change(sentence) {
sentence.status = "approved";
return sentenceService.update(sentence)
}
function reject(id) {
return sentenceService.reject(id)
.then( ({ data }) => vm.sentences[vm.sentences.findIndex(sentence => sentence.id === data.id)] = data)
}
function approve(id) {
return sentenceService.approve(id)
.then( ({ data }) => vm.sentences[vm.sentences.findIndex(sentence => sentence.id === data.id)] = data)
}
function getLabels() {
return sentenceService.getLabels().then(data => vm.labels = data.data);
}
function loadMore(){
if(!vm.remoteIsBusy && !vm.noMoreSentences){
return get(vm.startValue+vm.nbElementToGet, vm.startValue)
.then(() => vm.startValue += vm.nbElementToGet)
}
else {
Promise.resolve()
}
}
function get(take, skip) {
vm.remoteIsBusy = true;
return sentenceService.get(take, skip)
.then(function(data){
if(data.data.length === 0){
vm.noMoreSentences = true;
}
vm.sentences = vm.sentences.concat(data.data);
vm.remoteIsBusy = false;
});
}
}
})();
53 changes: 53 additions & 0 deletions assets/js/app/sentence/sentence.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Gladys Project
* http://gladysproject.com
* Software under licence Creative Commons 3.0 France
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
* You may not use this software for commercial purposes.
* @author :: Pierre-Gilles Leymarie
*/

(function () {
'use strict';

angular
.module('gladys')
.factory('sentenceService', sentenceService);

sentenceService.$inject = ['$http'];

function sentenceService($http) {

var service = {
get: get,
update: update,
reject: reject,
approve: approve,
getLabels: getLabels
};

return service;

// all about sentences

function get(take, skip) {
return $http({method: 'GET', url: '/sentence', params: {take: take, skip: skip}});
}

function update(sentence){
return $http({method: 'PATCH', url: '/sentence/' + sentence.id, data: sentence});
}

function reject(id){
return $http({method: 'PATCH', url: '/sentence/' + id, data: { status: 'rejected' } });
}

function approve(id){
return $http({method: 'PATCH', url: '/sentence/' + id, data: { status: 'approved' } });
}

function getLabels() {
return $http({method: 'GET', url: '/sentence/label' });
}
}
})();
4 changes: 2 additions & 2 deletions config/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.connections = {
host: process.env.MYSQL_HOST_TEST || 'localhost',
port: process.env.MYSQL_PORT_TEST || 3306,
user: process.env.MYSQL_USER_TEST || 'root',
password: (typeof process.env.MYSQL_PASSWORD_TEST !== 'undefined') ? process.env.MYSQL_PASSWORD_TEST : 'root',
password: process.env.MYSQL_PASSWORD_TEST,
database: process.env.MYSQL_DATABASE_TEST || 'gladystest'
},

Expand All @@ -54,7 +54,7 @@ module.exports.connections = {
host: process.env.MYSQL_HOST || 'localhost',
port: process.env.MYSQL_PORT || 3306,
user: process.env.MYSQL_USER || 'root',
password: (typeof process.env.MYSQL_PASSWORD !== 'undefined') ? process.env.MYSQL_PASSWORD : 'root',
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE || 'gladys'
},

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"pagename-music":"Music",
"pagename-module": "Modules",
"pagename-device": "Device",
"pagename-sentence": "Brain",
"dashboard-default-box-title": "Welcome in Gladys !",
"dashboard-default-box-text": "You are on the dashboard of Gladys. To add boxs on this dashboard, go to \"Parameters\" then \"Boxs\".",
"modules-installed": "Installed Modules",
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
"pagename-music":"Musique",
"pagename-module": "Modules",
"pagename-device": "Device",
"pagename-sentence": "Cerveau",
"dashboard-default-box-title": "Bienvenue dans Gladys !",
"dashboard-default-box-text": "Vous êtes sur le dashboard principal de Gladys, c'est ici que sont regroupées les informations importantes. Pour ajouter des boxs sur ce dashboard, vous pouvez aller dans \"Paramètres\" puis \"Boxs\".",
"modules-installed": "Modules installés",
Expand Down
1 change: 1 addition & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports.policies = {
Room : ['checkToken'],
Scenario : ['checkToken'],
Script : ['checkToken'],
Sentence : ['checkToken'],
Session : {
newUser: ['signupAllowed'],
createUser: ['signupAllowed'],
Expand Down
5 changes: 5 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ module.exports.routes = {
'patch /devicetype/:id': 'DeviceTypeController.update',
'post /devicetype/:id/exec': 'DeviceTypeController.exec',
'get /devicetype/:id': 'DeviceTypeController.getById',

// Sentence
'get /sentence': 'SentenceController.index',
'get /sentence/label': 'SentenceController.getLabels',
'patch /sentence/:id': 'SentenceController.update',

// Event
'get /event': 'EventController.index',
Expand Down
21 changes: 21 additions & 0 deletions test/unit/api/core/sentence/sentence.get.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var should = require('should');
var validateSentenceParam = require('../../validator/sentenceValidator');

describe('sentence', function() {

describe('get', function() {

it('should return the first sentence', function (done) {


gladys.sentence.get({take: 1, skip: 0})
.then(function(result){
validateSentenceParam(result);
result.length.should.not.equal(0);
done();
}).catch(done);
});

});

});
21 changes: 21 additions & 0 deletions test/unit/api/validator/sentenceValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = validate;

var should = require('should');

function validate(sentence) {
if(sentence instanceof Array) {
sentence.forEach(validateSentence);
} else {
validateSentence(sentence);
}
}

function validateSentence(sentence) {
sentence.should.be.instanceOf(Object);
sentence.should.have.property('uuid');
sentence.should.have.property('text');
sentence.should.have.property('label');
sentence.should.have.property('service');
sentence.should.have.property('language');
sentence.should.have.property('status');
}
7 changes: 7 additions & 0 deletions views/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
<i class="fa fa-map-marker"></i> <span><%= __('Maps') %></span>
</a>
</li>
<li id="scenarioLi" class="treeview">
<a href="/dashboard/sentence">
<i class="fa fa-bolt"></i> <span><%= __('Brain') %></span>
</a>
</li>
<li id="scenarioLi" class="treeview">
<a href="/dashboard/scenario">
<i class="fa fa-tasks"></i> <span><%= __('Scenarios') %></span>
Expand Down Expand Up @@ -310,6 +315,8 @@
<script src="/js/app/script/script.service.js"></script>
<script src="/js/app/sensor/sensor.controller.js"></script>
<script src="/js/app/sensor/sensor.service.js"></script>
<script src="/js/app/sentence/sentence.controller.js"></script>
<script src="/js/app/sentence/sentence.service.js"></script>
<script src="/js/app/sleep/sleep.controller.js"></script>
<script src="/js/app/sleep/sleep.service.js"></script>
<script src="/js/app/socket/socket.service.js"></script>
Expand Down
Loading

0 comments on commit 1b9b187

Please sign in to comment.