Skip to content

Commit

Permalink
Fix sending usage metrics/analytics by using REST endpoint (#984)
Browse files Browse the repository at this point in the history
* send analytics as xml in request body

* Removed form-data
  • Loading branch information
ktuite authored Sep 13, 2023
1 parent a0c83d8 commit c920efd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"external": {
"sentry": {},
"analytics": {
"url": "https://data.getodk.cloud/v1/key/eOZ7S4bzyUW!g1PF6dIXsnSqktRuewzLTpmc6ipBtRq$LDfIMTUKswCexvE0UwJ9/projects/1/submission",
"url": "https://data.getodk.cloud/v1/key/eOZ7S4bzyUW!g1PF6dIXsnSqktRuewzLTpmc6ipBtRq$LDfIMTUKswCexvE0UwJ9/projects/1/forms/odk-analytics/submissions",
"formId": "odk-analytics",
"version": "v2023.4.0_1"
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bin/run-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { run } = require('../task/task');
const { runAnalytics } = require('../task/analytics');

const { program } = require('commander');
program.option('-f', 'Force analytics to be sent (if configured) even if not scheduled yet.');
program.option('-f, --force', 'Force analytics to be sent (if configured) even if not scheduled yet.');
program.parse();

const options = program.opts();
Expand Down
16 changes: 5 additions & 11 deletions lib/external/odk-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

const { request } = require('https');
const { isBlank } = require('../util/util');
const FormData = require('form-data');
const Problem = require('../util/problem');
const { pipeline } = require('stream');

Expand All @@ -24,30 +23,25 @@ const mock = {

const odkAnalytics = (url) => {
const _submit = (formXml) => new Promise((resolve, reject) => {
const formData = new FormData();
const fileBuffer = Buffer.from(formXml, 'utf-8');
formData.append('xml_submission_file', fileBuffer, 'submission.xml');

const formHeaders = formData.getHeaders();
formHeaders['X-OpenRosa-Version'] = '1.0';

const options = {
method: 'POST',
headers: formHeaders
headers: {
'Content-Type': 'text/xml'
}
};

const req = request(
url,
options,
response => {
if (response.statusCode === 201)
if (response.statusCode === 200)
resolve();
else
reject(Problem.internal.analyticsUnexpectedResponse());
}
);

pipeline(formData, req, (err) => { if (err != null) reject(err); });
pipeline(formXml, req, (err) => { if (err != null) reject(err); });
});

return {
Expand Down
15 changes: 11 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"express": "~4.18",
"express-graceful-exit": "~0.5",
"fast-myers-diff": "~3",
"form-data": "~4",
"htmlparser2": "~3.9",
"knex": "~0.21",
"luxon": "~0.3",
Expand Down

0 comments on commit c920efd

Please sign in to comment.