-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer_modules.js
More file actions
114 lines (81 loc) · 3.7 KB
/
answer_modules.js
File metadata and controls
114 lines (81 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var fs = require('fs-extra');
var hogan = require('hogan');
var utils = require('./utils.js');
var path = require('path');
var config = require('./config.js')
function submitAnswerModuleForm() {
var configPath = document.getElementById('configPath');
var manifestJSON = fs.readFileSync(configPath.value, 'utf-8');
var manifest = JSON.parse(manifestJSON);
installAnswerModule(manifest);
}
function installAnswerModule(manifest) {
var db = process.mainModule.exports.db;
var requiredDataSources = [];
manifest.requiredDataTypes.forEach(function(dataType) {
var dataSource = dataType.split(".")[0];
requiredDataSources.push(dataSource);
});
var requiredDataSourcesSet = new Set(requiredDataSources);
requiredDataSources = [];
requiredDataSourcesSet.forEach(function(value) {
requiredDataSources.push(value);
});
db.dataSources.find({name: {$in: requiredDataSources}}, function(err, docs) {
var foundDataTypes = [];
docs.forEach(function(doc) {
var installedDataTypes = doc.dataTypes.filter(function(dataType) {
return $.inArray(doc.name + "." + dataType.name, manifest.requiredDataTypes);
});
installedDataTypes.forEach(function(dataType) {
foundDataTypes.push({name: dataType.name, description: dataType.description})
});
});
var template = $('#answerModuleConsent').html();
var html = hogan.compile(template).render({name: manifest.name, requiredDataTypes: foundDataTypes});
$('#answerModuleConsentContent').html(html);
$('#answerModuleConsentModal').openModal();
});
}
function agreedCallback() {
var configPath = document.getElementById('configPath');
var manifestJSON = fs.readFileSync(configPath.value, 'utf-8');
var manifest = JSON.parse(manifestJSON);
var localAnswerModulePath = path.join(config.dataPath, 'answerModules', manifest.name);
fs.mkdirsSync(localAnswerModulePath);
fs.copySync(path.join(path.dirname(configPath.value), manifest.scriptPath), path.join(localAnswerModulePath, manifest.scriptPath));
//fs.mkdirsSync(path.join(config.dropboxRoot, manifest.dropboxDirectory));
process.mainModule.exports.db.answerModules.insert(manifest, function(err, doc) {
console.log("Answer module saved");
$('#addAnswerModuleModal').submit();
//fix lingering modal overlay bug in materialize.css
$('.lean-modal').remove();
getAllAnswerModules();
});
}
function getAllAnswerModules() {
process.mainModule.exports.db.answerModules.find({}, function (err, results) {
var template = $('#answerModulesList').html();
var html = hogan.compile(template).render({answerModules: results});
$('#answerModulesContainer').html(html);
$('.collapsible').collapsible();
});
}
function runAnswerModule(answerModuleName) {
var db = process.mainModule.exports.db;
db.answerModules.findOne({name: answerModuleName}, function (err, doc) {
db.dataTypes.find({name: {$in: doc.requiredDataTypes}}, function (err, docs){
var rawDataDBs = [];
docs.forEach(function (dataType) {
var root = path.join(config.dataPath, 'dataSources', dataType.name.split('.')[0]);
rawDataDBs.push(path.join(root, dataType.cache_db_name));
});
params = {};
params.results = path.join(config.dropboxRoot, doc.dropboxDirectory);
params.dbs = rawDataDBs;
utils.spawnPython(path.join(config.dataPath, 'answerModules', answerModuleName, doc.scriptPath), JSON.stringify(params),
function (code) {
});
});
});
}