UNPKG

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.

359 lines (340 loc) 13.2 kB
/*! * surveyjs - SurveyJS Dashboard library v2.5.2 * Copyright (c) 2015-2025 Devsoft Baltic OÜ - http://surveyjs.io/ * License: SEE LICENSE IN LICENSE */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define("SurveyAnalyticsMongo", [], factory); else if(typeof exports === 'object') exports["SurveyAnalyticsMongo"] = factory(); else root["SurveyAnalyticsMongo"] = factory(); })(this, () => { return /******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ "./src/mongo/index.ts": /*!****************************!*\ !*** ./src/mongo/index.ts ***! \****************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ MongoDbAdapter: () => (/* binding */ MongoDbAdapter) /* harmony export */ }); /* harmony import */ var _result_transformers__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./result-transformers */ "./src/mongo/result-transformers.ts"); /* harmony import */ var _pipelines__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pipelines */ "./src/mongo/pipelines.ts"); 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 = (0,_pipelines__WEBPACK_IMPORTED_MODULE_1__.createPipeline)(surveyId, questionId, visualizerType, questionType); return new Promise(function (resolve, reject) { _this.db.collection(collectionName).aggregate(pipeline).toArray() .then(function (results) { var transformer = _result_transformers__WEBPACK_IMPORTED_MODULE_0__.transformers[visualizerType] || _result_transformers__WEBPACK_IMPORTED_MODULE_0__.transformers[questionType] || (function (r) { return r; }); var result = transformer(results); resolve(result); }) .catch(function (e) { reject(JSON.stringify(e)); }); }); }; return MongoDbAdapter; }()); /***/ }), /***/ "./src/mongo/pipelines.ts": /*!********************************!*\ !*** ./src/mongo/pipelines.ts ***! \********************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ createPipeline: () => (/* binding */ createPipeline) /* harmony export */ }); 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; } /***/ }), /***/ "./src/mongo/result-transformers.ts": /*!******************************************!*\ !*** ./src/mongo/result-transformers.ts ***! \******************************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ transformers: () => (/* binding */ transformers) /* harmony export */ }); 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 }; /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk. (() => { /*!******************************!*\ !*** ./src/entries/mongo.ts ***! \******************************/ __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ MongoDbAdapter: () => (/* reexport safe */ _mongo__WEBPACK_IMPORTED_MODULE_0__.MongoDbAdapter) /* harmony export */ }); /* harmony import */ var _mongo__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../mongo */ "./src/mongo/index.ts"); })(); /******/ return __webpack_exports__; /******/ })() ; }); //# sourceMappingURL=survey.analytics.mongo.js.map