/
submit-changesets.js
257 lines (230 loc) · 7.62 KB
/
submit-changesets.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var fs = require('fs');
var request = require('request');
var async = require('async');
var getOsmSubmissionsDirs = require('../helpers/get-osm-submissions-dirs');
var generateChangeset = require('../osm/generate-changeset');
var osm2osc = require('../osm/osm2osc');
var osmApi = require('../../../settings').osmApi;
var NUM_PARALLEL_SUBMISSIONS = 3;
module.exports = {
submitAllChangesetsForForm: submitAllChangesetsForForm,
createAndSubmitChangesets: createAndSubmitChangesets
};
var q = async.queue(function (osmDir, cb) {
createChangesetAndOsc(osmApi, osmDir.dir, osmDir.files, cb);
}, NUM_PARALLEL_SUBMISSIONS);
q.drain = function() {
console.log('All changesets in the queue have been submitted.');
};
/**
* Submits all of the OSM files in a form's submissions directory
* that have not yet been submitted.
*
* @param formName - the name of the form (form_id)
* @param cb - callback with error or status
*/
function submitAllChangesetsForForm(formName, cb) {
getOsmSubmissionsDirs(formName, {unsubmittedOnly: true}, function (err, osmDirs) {
if (err) {
cb(err);
return;
}
createAndSubmitChangesets(osmDirs, cb);
});
}
function createAndSubmitChangesets(osmDirs, cb) {
if (typeof cb !== 'function') {
cb = function(err) {
if (err) {
console.error('Automatic OMK submission OSM API error. ' + JSON.stringify(err));
}
};
}
if (osmDirs.length < 1) {
cb(null, {
done: true,
msg: 'There are no OSM files to submit. Skipping creating and submitting changesets.',
status: 200
});
return;
}
q.push(osmDirs, function (err) {
if (err) {
cb(err);
return;
}
cb(null, {
done: true,
msg: 'Completed submitting changeset to OSM.'
});
});
cb(null, {
started: true,
msg: 'Began creating and submitting changesets to OSM API.',
status: 201
});
}
function createChangesetAndOsc(osmApi, submissionsDir, osmFiles, cb) {
var changesetXml = generateChangeset(submissionsDir);
changesetCreate(osmApi, changesetXml, function(err, changesetId) {
if (err) {
cb({
status: err.status,
msg: 'Error creating changeset.',
err:err
});
return;
}
osm2osc(osmFiles, changesetId, function (err, oscXml, changesetId) {
if (err) {
cb(err);
return;
}
changesetUpload(osmApi, changesetId, oscXml, function(err, diffResult, changesetId) {
// This isn't truly an error state. Conflicts are common and normal.
if (err) {
saveConflictAndUpdateDataJson(submissionsDir, err, changesetId);
cb();
return;
}
// Saving the diffResult to the submissions dir and update data.json
saveDiffResultAndUpdateDataJson(submissionsDir, diffResult, changesetId);
changesetClose(osmApi, changesetId, function (err) {
if (err) {
cb(err);
return;
}
cb();
});
});
});
});
}
function changesetCreate(osmApi, changesetXml, cb) {
var opts = {
method: 'PUT',
headers: {'Content-Type': 'text/xml'},
uri: osmApi.server + '/api/0.6/changeset/create',
auth: {
user: osmApi.user,
pass: osmApi.pass
},
body: changesetXml
};
request(opts, function (err, response, body) {
if (err) {
cb(err);
return;
}
if (response.statusCode !== 200) {
cb({
status: response.statusCode,
body: body,
msg: 'Could not open changeset on OSM API. ' + body
});
return;
}
var changesetId = parseInt(body);
cb(null, changesetId);
console.log('Opened Changeset. ID: ' + changesetId + ' - ' + opts.uri);
});
}
function changesetUpload(osmApi, changesetId, oscXml, cb) {
var opts = {
method: 'POST',
headers: {'Content-Type': 'text/xml'},
uri: osmApi.server + '/api/0.6/changeset/' + changesetId + '/upload',
auth: {
user: osmApi.user,
pass: osmApi.pass
},
body: oscXml
};
request(opts, function (err, response, body) {
if (err) {
cb(err, null, changesetId);
return;
}
if (response.statusCode !== 200) {
cb({
status: response.statusCode,
body: body,
msg: 'Could not upload changeset on OSM API. ' + body
}, null, changesetId);
return;
}
cb(null, body, changesetId);
console.log('Uploaded OSC. ID: ' + changesetId + ' - ' + opts.uri);
});
}
function changesetClose(osmApi, changesetId, cb) {
var opts = {
method: 'PUT',
headers: {'Content-Type': 'text/xml'},
uri: osmApi.server + '/api/0.6/changeset/' + changesetId + '/close',
auth: {
user: osmApi.user,
pass: osmApi.pass
}
};
request(opts, function (err, response, body) {
if (err) {
cb(err);
return;
}
if (response.statusCode !== 200) {
cb({
status: response.statusCode,
body: body,
msg: 'Could not close changeset on OSM API. ' + body
});
return;
}
cb();
console.log('Closed Changeset. ID: ' + changesetId + ' - ' + opts.uri);
});
}
/**
* Saving the diff result can be helpful to not only keep track that the submission
* has been successfully submitted, but also to see how IDs potentially have been
* rewritten.
*/
function saveDiffResultAndUpdateDataJson(submissionsDir, diffResult, changesetId) {
var diffResultFileName = 'diffResult-' + changesetId + '.xml';
fs.writeFile(submissionsDir + '/' + diffResultFileName, diffResult, function (err) {
// do nothing
});
updateDataJson(submissionsDir, changesetId, diffResultFileName, null);
}
function saveConflictAndUpdateDataJson(submissionsDir, err, changesetId) {
var conflictFileName = 'conflict-' + changesetId + '.json';
var jsonStr = JSON.stringify(err, null, 2);
fs.writeFile(submissionsDir + '/' + conflictFileName, jsonStr, function (err) {
// do nothing
});
console.log('Conflict uploading changeset. - ' + JSON.stringify(err));
updateDataJson(submissionsDir, changesetId, null, conflictFileName);
}
function updateDataJson(submissionsDir, changesetId, diffResultFileName, conflictFileName) {
var dataJsonPath = submissionsDir + '/data.json';
fs.readFile(dataJsonPath, function (err, data) {
if (err) {
return;
}
var dataObj = JSON.parse(data);
delete dataObj.osmApi;
dataObj.osmApi = {
changesetId: changesetId,
changesetUrl: osmApi.server + '/changeset/' + changesetId
};
if (diffResultFileName) {
dataObj.osmApi.diffResult = diffResultFileName;
} else if (conflictFileName) {
dataObj.osmApi.conflict = conflictFileName;
}
var dataJsonStr = JSON.stringify(dataObj, null, 2);
fs.writeFile(dataJsonPath, dataJsonStr, function (err) {
// do nothing
});
});
}