survey-analytics
Version:
SurveyJS Dashboard is a UI component for visualizing and analyzing survey data. It interprets the form JSON schema to identify question types and renders collected responses using interactive charts and tables.
236 lines (230 loc) • 9.2 kB
JavaScript
/*!
surveyjs - SurveyJS Dashboard library v2.5.35
Copyright (c) 2015-2026 Devsoft Baltic OÜ - http://surveyjs.io/
License: SEE LICENSE IN LICENSE
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SurveyAnalyticsMongo = {}));
})(this, (function (exports) { 'use strict';
function choiceTransformationPipeline(result) {
var res = {};
var totalCount = 0;
result.forEach(function (item) {
res[item._id] = item.count;
totalCount += item.count;
});
return { data: res, totalCount: totalCount };
}
function numberTransformationPipeline(result) {
if (result.length == 0)
return { value: 0, minValue: 0, maxValue: 0 };
return { data: { value: result[0].average, minValue: result[0].min, maxValue: result[0].max } };
}
function histogramTransformationPipeline(result) {
var res = [];
var totalCount = 0;
result.forEach(function (item) {
res.push(item.count);
totalCount += item.count;
});
return { data: res, intervals: result, totalCount: totalCount };
}
var transformers = {
"boolean": choiceTransformationPipeline,
"radiogroup": choiceTransformationPipeline,
"dropdown": choiceTransformationPipeline,
"checkbox": choiceTransformationPipeline,
"tagbox": choiceTransformationPipeline,
"number": numberTransformationPipeline,
"rating": numberTransformationPipeline,
"histogram": histogramTransformationPipeline
};
function createPipeline(surveyId, questionId, visualizerType, questionType) {
var singleChoicePipeline = [
{ $match: { postid: surveyId } },
{ $project: { value: "$json." + questionId } },
{ $match: { value: { $exists: true } } },
{
$group: {
_id: "$value",
count: { $sum: 1 },
}
}
];
var multipleChoicePipeline = [
{ $match: { postid: surveyId } },
{ $project: { value: "$json." + questionId } },
{ $match: { value: { $exists: true } } },
{ $unwind: "$value" },
{
$group: {
_id: "$value",
count: { $sum: 1 },
}
}
];
var numberPipeline = [
{ $match: { postid: surveyId } },
{ $project: { value: "$json." + questionId } },
{ $match: { value: { $exists: true } } },
{
$group: {
_id: null,
count: { $sum: 1 },
average: { $avg: "$value" },
min: { $min: "$value" },
max: { $max: "$value" },
values: { $push: "$value" }
}
}
];
var histogramPipeline = [
{ $match: { postid: surveyId } },
{ $project: { value: "$json." + questionId } },
{ $match: { value: { $exists: true } } },
{
$bucketAuto: {
groupBy: "$value",
buckets: 10,
output: {
count: { $sum: 1 },
minValue: { $min: "$value" },
maxValue: { $max: "$value" }
}
}
},
{
$project: {
_id: 0,
start: "$minValue",
end: "$maxValue",
label: {
$concat: [
{ $toString: { $round: ["$minValue", 2] } },
" - ",
{ $toString: { $round: ["$maxValue", 2] } }
]
},
count: 1
}
}
];
var mongoPipelines = {
"boolean": singleChoicePipeline,
"radiogroup": singleChoicePipeline,
"dropdown": singleChoicePipeline,
"checkbox": multipleChoicePipeline,
"tagbox": multipleChoicePipeline,
"number": numberPipeline,
"rating": numberPipeline,
"histogram": histogramPipeline
};
var pipeline = mongoPipelines[visualizerType] || mongoPipelines[questionType] || [];
return pipeline;
}
var MongoDbAdapter = /** @class */ (function () {
function MongoDbAdapter(db, getId) {
this.db = db;
this.getId = getId;
}
MongoDbAdapter.prototype.create = function (collectionName, object) {
var _this = this;
object.id = object.id || this.getId();
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).insertOne(object)
.then(function (results) {
resolve(object.id);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
};
MongoDbAdapter.prototype.retrieve = function (collectionName, filter) {
var _this = this;
filter = filter || [];
var query = {};
filter.forEach(function (fi) { return query[fi.field] = fi.value; });
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).find(query).toArray()
.then(function (results) {
resolve(results);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
};
MongoDbAdapter.prototype.update = function (collectionName, object) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).updateOne({ id: object.id }, { $set: object })
.then(function (results) {
resolve(results);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
};
MongoDbAdapter.prototype.delete = function (collectionName, id) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).deleteMany({ id: id })
.then(function (results) {
resolve(results);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
};
MongoDbAdapter.prototype.retrievePaginated = function (collectionName, filter, order, offset, limit) {
var _this = this;
filter = filter || [];
var query = {};
filter.forEach(function (fi) {
if (!!fi.value) {
var val = fi.value;
query[fi.field] = val;
}
});
var sort = {};
order.forEach(function (fi) {
sort[fi.field] = fi.value == "desc" ? -1 : 1;
});
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).count(query).then(function (count) {
_this.db.collection(collectionName).find(query).sort(sort).skip(offset).limit(limit).toArray()
.then(function (results) {
var result = { data: results, totalCount: count };
resolve(result);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
});
};
MongoDbAdapter.prototype.retrieveSummary = function (collectionName, surveyId, questionId, questionType, visualizerType, filter) {
var _this = this;
var pipeline = createPipeline(surveyId, questionId, visualizerType, questionType);
return new Promise(function (resolve, reject) {
_this.db.collection(collectionName).aggregate(pipeline).toArray()
.then(function (results) {
var transformer = transformers[visualizerType] || transformers[questionType] || (function (r) { return r; });
var result = transformer(results);
resolve(result);
})
.catch(function (e) {
reject(JSON.stringify(e));
});
});
};
return MongoDbAdapter;
}());
exports.MongoDbAdapter = MongoDbAdapter;
}));
//# sourceMappingURL=survey.analytics.mongo.js.map