Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Fix history exported JSON formatting (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples authored Sep 20, 2021
1 parent 8135f5f commit 1897bfd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ router.get(
}

ctx.res.write(
(nextPageToken ? ',' : '[') +
(nextPageToken ? ',' : '{ "events": [') +
page.history.events.map(losslessJSON.stringify).join(',')
);
nextPageToken =
page.nextPageToken && Buffer.from(page.nextPageToken, 'base64');
} while (nextPageToken);

ctx.res.write(']');
ctx.res.write('] }');
ctx.body = '';
}
);
Expand Down
33 changes: 31 additions & 2 deletions server/temporal-client/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Long = require('long');
const losslessJSON = require('lossless-json');
const moment = require('moment');
const logger = require('../logger')
const logger = require('../logger');

function buildHistory(getHistoryRes) {
const history = getHistoryRes.history;
Expand Down Expand Up @@ -211,8 +211,21 @@ function cliTransform(item) {
return item;
}

removeProtoOneOf(item);

Object.entries(item).forEach(([subkey, subvalue]) => {
if (subvalue && typeof subvalue.unsigned === 'boolean') {
if (subvalue && subvalue.seconds) {
const seconds = Number(subvalue.seconds);
const dt = moment(seconds * 1000);

if (dt.isValid() && dt.isAfter('2017-01-01')) {
// iso date
item[subkey] = dt.toISOString();
} else {
// duration
item[subkey] = subvalue.seconds + 's';
}
} else if (subvalue && typeof subvalue.unsigned === 'boolean') {
item[subkey] = new losslessJSON.LosslessNumber(
Long.fromValue(subvalue).toString()
);
Expand All @@ -222,6 +235,9 @@ function cliTransform(item) {
subvalue.forEach(cliTransform);
} else if (subvalue && typeof subvalue === 'object') {
cliTransform(subvalue);
} else if (typeof subvalue == 'string') {
subvalue = enumTransform(subvalue);
item[subkey] = subvalue;
} else if (subvalue === null || subvalue === undefined) {
delete item[subkey];
}
Expand All @@ -230,6 +246,19 @@ function cliTransform(item) {
return item;
}


function removeProtoOneOf(item) {
if (item.eventId && item.attributes) {
delete item.attributes;
}
if (item.failure?.failureInfo) {
delete item.failure.failureInfo;
}
if (item.cause?.failureInfo) {
delete item.cause.failureInfo;
}
}

module.exports = {
buildHistory,
buildWorkflowExecutionRequest,
Expand Down

0 comments on commit 1897bfd

Please sign in to comment.