Skip to content

Commit

Permalink
Feature #53 Tracing
Browse files Browse the repository at this point in the history
discussions
  • Loading branch information
dtseytlin committed Mar 19, 2016
1 parent 3b99b5d commit 2183f1b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 30 deletions.
28 changes: 24 additions & 4 deletions backend/app/bologger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@ var
co = require('co'),
thunkify = require('thunkify'),
Query = require('app/util').Query,
sql = require('sql'),
query = new Query(),
thunkQuery = thunkify(query);


function BoLogger() {
this.data = {};
}

BoLogger.prototype.init = function* (object) {
this.data.essence = yield common.getEssenceId(object);
};

BoLogger.prototype.extend = function (data) {
this.data = (_.extend(this.data, _.pick(data, Log.insertCols)));
};

BoLogger.prototype.log = function (data) {
co(function* () {
console.log(data);
data.essence = yield * common.getEssenceId(data.object);
var id = yield thunkQuery(Log.insert(_.pick(data, Log.insertCols)).returning(Log.id));
co.call(this, function* () {
//data.essence = yield * common.getEssenceId(data.object);
if (data.object) {
yield this.init(data.object);
}
this.extend(data)
var id = yield thunkQuery(Log.insert(this.data).returning(Log.id));
return data;
}).then(function (data) {
next();
Expand All @@ -30,5 +43,12 @@ BoLogger.prototype.log = function (data) {
});

};
BoLogger.prototype.error = function (data, result) {
this.log(_.extend(data, {error: true, result: result}));
};

var getEssenceId = function* (essenceName) {
return yield thunkQuery(Essence.select().from(Essence).where([sql.functions.UPPER(Essence.name).equals(essenceName.toUpperCase())]));
};

module.exports = BoLogger;
118 changes: 94 additions & 24 deletions backend/app/controllers/discussions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var
auth = require('app/auth'),
config = require('config'),
common = require('app/queries/common'),
BoLogger = require('app/bologger'),
bologger = new BoLogger(),
Product = require('app/models/products'),
ProductUOA = require('app/models/product_uoa'),
Project = require('app/models/projects'),
Expand Down Expand Up @@ -67,6 +69,9 @@ module.exports = {

select: function (req, res, next) {
co(function* () {
var task = yield * common.getTask(req.query.taskId);
var productId = task.productId;
var uoaId = task.uoaId;
var selectFields =
'SELECT '+
'"Discussions".*, '+
Expand All @@ -89,9 +94,9 @@ module.exports = {
selectWhere = setWhereInt(selectWhere, req.query.questionId, 'Discussions', 'questionId');
selectWhere = setWhereInt(selectWhere, req.query.userId, 'Discussions', 'userId');
selectWhere = setWhereInt(selectWhere, req.query.userFromId, 'Discussions', 'userFromId');
selectWhere = setWhereInt(selectWhere, req.query.taskId, 'Discussions', 'taskId');
selectWhere = setWhereInt(selectWhere, req.query.uoaId, 'UnitOfAnalysis', 'id');
selectWhere = setWhereInt(selectWhere, req.query.productId, 'Products', 'id');
//selectWhere = setWhereInt(selectWhere, req.query.taskId, 'Discussions', 'taskId');
selectWhere = setWhereInt(selectWhere, uoaId, 'UnitOfAnalysis', 'id');
selectWhere = setWhereInt(selectWhere, productId, 'Products', 'id');
selectWhere = setWhereInt(selectWhere, req.query.stepId, 'WorkflowSteps', 'id');
selectWhere = setWhereInt(selectWhere, req.query.surveyId, 'Surveys', 'id');

Expand All @@ -112,6 +117,13 @@ module.exports = {
req.body = _.extend(req.body, {userFromId: req.user.id}); // add from user id
req.body = _.pick(req.body, Discussion.insertCols); // insert only columns that may be inserted
var result = yield thunkQuery(Discussion.insert(req.body).returning(Discussion.id));
bologger.log({
user: req.user.id,
action: 'insert',
object: 'discussions',
entity: result[0].id,
info: 'Add discussion`s entry'
});
var entry=_.first(result);
var newStep;
if (isReturn) {
Expand Down Expand Up @@ -174,14 +186,20 @@ module.exports = {
yield * checkUpdate(req);
req.body = _.extend(req.body, {updated: new Date()}); // update `updated`
req.body = _.pick(req.body, Discussion.updateCols); // update only columns that may be updated
var result = yield thunkQuery(Discussion.update(req.body).where(Discussion.id.equals(req.params.id)));
var result = yield thunkQuery(Discussion.update(req.body).where(Discussion.id.equals(req.params.id)).returning(Discussion.id));
bologger.log({
user: req.user.id,
action: 'update',
object: 'discussions',
entity: result[0].id,
info: 'Update body of discussion`s entry'
});
var entry = yield * common.getDiscussionEntry(req.params.id);
var essenceId = yield * common.getEssenceId('Discussions');
var userFrom = yield * common.getUser(req.user.id);
var userTo = yield * common.getUser(entry.userId);
// static blindRewiev
var taskId = yield * checkOneId(req.body.taskId, Task, 'id', 'taskId', 'Task'); // ToDo: exclude unwanted query
var task = yield * common.getTask(taskId);
var task = yield * common.getTask(entry.taskId);
var productId = task.productId;
var uoaId = task.uoaId;
var step4userTo = yield * getUserToStep(productId, uoaId, userTo.id);
Expand Down Expand Up @@ -227,6 +245,13 @@ module.exports = {
co(function* () {
return yield thunkQuery(Discussion.delete().where(Discussion.id.equals(req.params.id)));
}).then(function (data) {
bologger.log({
user: req.user.id,
action: 'delete',
object: 'discussions',
entity: req.params.id,
info: 'Delete discussion`s entry'
});
res.status(204).end();
}, function (err) {
next(err);
Expand Down Expand Up @@ -440,7 +465,7 @@ function* getUserList(user, taskId, productId, uoaId, currentStep, tag) {
'"Tasks"."productId" as productid, '+
'"Tasks"."uoaId" as uoaid '+
'FROM "Discussions" ' +
'INNER JOIN "Tasks" ON "Discussions"."returnTaskId" = "Tasks"."id" '+
'INNER JOIN "Tasks" ON "Discussions"."taskId" = "Tasks"."id" '+
'INNER JOIN "WorkflowSteps" ON "Tasks"."stepId" = "WorkflowSteps"."id" '+
'INNER JOIN "Users" ON "Tasks"."userId" = "Users"."id" '+
'WHERE "Discussions"."returnTaskId" = ' + taskId.toString() + ' '+
Expand Down Expand Up @@ -628,12 +653,28 @@ function* getCurrentStep(taskId) {
}

function* updateProductUOAStep(object) {
var updateProductUOAQuery =
'UPDATE "ProductUOA" '+
'SET "currentStepId" = ' +object.stepId.toString()+ ' '+
'WHERE "productId"= '+object.productId.toString()+
' AND "UOAid" = '+object.uoaId.toString();
return yield thunkQuery(updateProductUOAQuery);
var res = yield thunkQuery(ProductUOA.update({currentStepId: object.stepId})
.where(ProductUOA.productId.equals(object.productId)
.and(ProductUOA.UOAid.equals(object.uoaId))
)
.returning(ProductUOA.currentStepId)
);
if (_.first(res)) {
bologger.log({
action: 'update',
object: 'productUOA',
entity: null,
entities: {productId: object.productId, uoaId: object.uoaId, currentStepId: object.stepId},
info: 'Update current step for survey'
});
} else {
bologger.error({
action: 'update',
object: 'productUOA',
entity: null,
info: 'Update current step for survey'
}, 'Couldn`t find survey for (productId, uoaId) = ('+object.productId.toString()+', '+object.uoaId.toString()+')');
}
}

function* checkUpdateProductUOAStep(object) {
Expand All @@ -652,21 +693,50 @@ function* checkUpdateProductUOAStep(object) {
'"Tasks"."productId" = '+object.productId.toString();
result = yield thunkQuery(query);
if (!_.first(result)) {
var updateProductUOAQuery =
'UPDATE "ProductUOA" '+
'SET "currentStepId" = ' +object.stepId.toString()+ ' '+
'WHERE "productId"= '+object.productId.toString()+
' AND "UOAid" = '+object.uoaId.toString();
return yield thunkQuery(updateProductUOAQuery);
var res = yield thunkQuery(ProductUOA.update({currentStepId: object.stepId})
.where(ProductUOA.productId.equals(object.productId)
.and(ProductUOA.UOAid.equals(object.uoaId))
)
.returning(ProductUOA.currentStepId)
);
if (_.first(res)) {
bologger.log({
action: 'update',
object: 'productUOA',
entity: null,
entities: {productId: object.productId, uoaId: object.uoaId, currentStepId: object.stepId},
info: 'Update current step for survey (when resolving)'
});
} else {
bologger.error({
action: 'update',
object: 'productUOA',
entity: null,
info: 'Update current step for survey (when resolving)'
}, 'Couldn`t find survey for (productId, uoaId) = ('+object.productId.toString()+', '+object.uoaId.toString()+')');
}
}
}

function* updateReturnTask(discussionId) {
var updateReturnTaskQuery =
'UPDATE "Discussions" '+
'SET "isResolve" = true, "updated" = now() '+
'WHERE "id"= '+discussionId.toString();
return yield thunkQuery(updateReturnTaskQuery);
var res = yield thunkQuery(Discussion.update({isResolve: true, updated: new Date()})
.where(Discussion.id.equals(discussionId))
.returning(Discussion.id)
);
if (_.first(res)) {
bologger.log({
action: 'update',
entity: discussionId,
info: 'Update task, that was returned before (resolve task)'
});
} else {
bologger.error({
action: 'update',
entity: discussionId,
info: 'Update task, that was returned before (resolve task)'
}, 'Couldn`t find discussion`s entry with id = `'+discussionId+'`');

}
}

function* getUserToStep(productId, uoaId, userId) {
Expand Down
6 changes: 4 additions & 2 deletions backend/app/models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Log = sql.define({
'entities',
'quantity',
'info',
'error'
'error',
'result'
]
});
Log.insertCols = [
Expand All @@ -25,7 +26,8 @@ Log.insertCols = [
'entities',
'quantity',
'info',
'error'
'error',
'result'
];

module.exports = Log;
9 changes: 9 additions & 0 deletions backend/db_dump/patches/20160319-01-Logs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALTER TABLE "public"."Logs"
ADD COLUMN "result" varchar;

ALTER TABLE "public"."Essences"
ALTER COLUMN "name" SET NOT NULL;

DROP INDEX "Essences_upper_idx";

CREATE UNIQUE INDEX "Essences_upper_idx" ON "Essences" (upper(name::text));
3 changes: 3 additions & 0 deletions backend/db_dump/patches/20160319-02-essences-patch1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
update "Essences"
set "name" = 'UnitOfAnalysisType'
WHERE "fileName" = 'uoatypes'
38 changes: 38 additions & 0 deletions backend/db_dump/patches/20160319-03-essences.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
WITH new_values ("tableName","name","fileName","nameField") as (
values
('Surveys', 'Surveys', 'surveys', 'title'),
('SurveyQuestions', 'Survey Questions', 'survey_questions', 'label'),
('SurveyQuestionOptions', 'Survey Question Options', 'survey_question_options', 'label'),
('SurveyAnswers', 'Survey Answers', 'survey_answers', 'value'),
('Groups', 'Groups', 'groups', 'title'),
('Organizations', 'Organizations', 'organizations', 'name'),
('Tasks', 'Tasks', 'tasks', 'title'),
('WorflowSteps', 'WorflowSteps', 'worflowSteps', 'title'),
('Products', 'Products', 'products', 'title'),
('UnitOfAnalysis', 'UnitOfAnalysis', 'uoas', 'name'),
('UnitOfAnalysisType', 'UnitOfAnalysisType', 'uoatypes', 'name'),
('UnitOfAnalysisClassType', 'UnitOfAnalysisClassType', 'uoaclasstypes', 'name'),
('UnitOfAnalysisTag', 'UnitOfAnalysisTag', 'uoatags', 'name'),
('Projects', 'projects', 'projects', 'codeName'),
('Discussions', 'Discussions', 'discussions', 'name'),
('Users', 'Users', 'users', 'email'),
('Notifications', 'notifications', 'notifications', 'body'),
('ProductUOA', 'productUoa', 'product_uoa', 'productId')
),
upsert as
(
update "Essences" m
set "tableName" = nv."tableName",
"fileName" = nv."fileName",
"nameField" = nv."nameField"
FROM new_values nv
WHERE upper(m."name") = upper(nv."name")
RETURNING m.*
)
-- select * from upsert
INSERT INTO "Essences" ("tableName","name","fileName","nameField")
SELECT "tableName","name","fileName","nameField"
FROM new_values
WHERE NOT EXISTS (SELECT 1
FROM upsert up
WHERE upper(up."name") = upper(new_values."name"))

0 comments on commit 2183f1b

Please sign in to comment.