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

Commit

Permalink
Prettify Input payloads (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
feedmeapples authored Apr 22, 2021
1 parent 904af32 commit 3c46521
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
64 changes: 31 additions & 33 deletions server/temporal-client/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,21 @@ const moment = require('moment');
function buildHistory(getHistoryRes) {
const history = getHistoryRes.history;

history.events = getHistoryRes.history.events.map((e) => {
history.events = getHistoryRes.history.events.map(e => {
let attr = '';

if (e.eventType) {
attr = e.eventType.toLowerCase().replace(/\_\w/g, function(v) {
return v.toUpperCase();
});
attr = attr.replace(/\_/g, '');
attr = attr.replace(/EventType/i, '') + 'EventAttributes';
attr = attr.charAt(0).toLowerCase() + attr.slice(1);
}

let details;

if (e[attr]) {
details = JSON.parse(JSON.stringify(e[attr]), function replacer(
key,
value
) {
if (value && value.type && value.type === 'Buffer') {
return Buffer.from(value)
.toString()
.replace(/["]/g, '')
.trim();
}

return value;
attr = e.eventType + 'EventAttributes';
attr = attr.replace(/^./, function(v) {
return v.toLowerCase();
});
}

return {
eventTime: e.eventTime,
eventType: e.eventType,
eventId: e.eventId,
details,
details: e[attr],
};
});

Expand All @@ -63,13 +43,18 @@ function momentToProtoTime(time) {
};
}

[_searchAttributes, _memo, _queryResult, _payloads] = [
const [_searchAttributes, _memo, _queryResult, _payloads] = [
'searchAttributes',
'memo',
'queryResult',
'payloads',
];
_uiTransformPayloadKeys = [_searchAttributes, _memo, _queryResult, _payloads];
const _uiTransformPayloadKeys = [
_searchAttributes,
_memo,
_queryResult,
_payloads,
];

function uiTransform(item) {
if (!item || typeof item !== 'object') {
Expand All @@ -79,9 +64,11 @@ function uiTransform(item) {
Object.entries(item).forEach(([subkey, subvalue]) => {
if (subvalue && subvalue.seconds) {
const seconds = Number(subvalue.seconds);

item[subkey] = { duration: seconds };

const dt = moment(seconds * 1000);

if (dt.isValid() && dt.isAfter('2017-01-01')) {
item[subkey] = dt.toISOString();
}
Expand All @@ -98,7 +85,7 @@ function uiTransform(item) {
// most of Temporal's uses of buffer is just line-delimited JSON.
item[subkey] = stringval
.split('\n')
.filter((x) => x)
.filter(x => x)
.map(JSON.parse);

if (item[subkey].length === 1) {
Expand All @@ -110,23 +97,28 @@ function uiTransform(item) {
}
} else if (Array.isArray(subvalue)) {
if (subkey === _payloads) {
let values = [];
let payloads = [];

Object.entries(subvalue).forEach(([subkey, payload]) => {
const encoding = Buffer.from(
payload.metadata.encoding || ''
).toString();

if (
['json/plain', 'json/protobuf'].includes(encoding) &&
payload.data
) {
values = [...values, Buffer.from(payload.data || '').toString()];
const data = JSON.parse(Buffer.from(payload.data).toString());

payloads = [...payloads, data];
} else {
let data = Buffer.from(payload.data || '').toString('base64');

data = data.length > 20 ? `${data.slice(0, 20)}..` : data;
values = [...values, data];
payloads = [...payloads, data];
}
});
item[_payloads] = values;
item[_payloads] = payloads;
} else {
subvalue.forEach(uiTransform);
}
Expand All @@ -137,6 +129,7 @@ function uiTransform(item) {
if (_uiTransformPayloadKeys.includes(subkey)) {
if (subkey === _searchAttributes) {
let values = [];

Object.entries(subvalue.indexedFields).forEach(
([subkey, subvalue]) => {
values = [...values, subvalue.data.toString('utf8')];
Expand All @@ -145,6 +138,7 @@ function uiTransform(item) {
item[subkey] = values;
} else if (subkey === _memo) {
let values = [];

Object.entries(subvalue.fields).forEach(([subkey, subvalue]) => {
values = [...values, subvalue.data.toString('utf8')];
});
Expand All @@ -157,6 +151,7 @@ function uiTransform(item) {
}
}
});

return item;
}

Expand All @@ -174,16 +169,19 @@ function enumTransform(item) {
];

const itemL = item.toLowerCase();
prefix = enumPrefixes.find((e) => itemL.startsWith(e));

prefix = enumPrefixes.find(e => itemL.startsWith(e));

if (!prefix) {
return item;
}

let processed = itemL.replace(new RegExp(`^${prefix}`), '');

processed = processed.replace(/\_\w/g, function(v) {
return v[1].toUpperCase();
});

return processed;
}

Expand Down
6 changes: 4 additions & 2 deletions server/temporal-client/temporal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ TemporalClient.prototype.getHistory = async function(
maximumPageSize,
};

const res = await this.client.getWorkflowExecutionHistoryAsync(ctx, req);
let res = await this.client.getWorkflowExecutionHistoryAsync(ctx, req);

res = uiTransform(res);

if (res.history && res.history.events) {
res.history = buildHistory(res);
}

return uiTransform(res);
return res;
};

TemporalClient.prototype.archivedWorkflows = async function(
Expand Down

0 comments on commit 3c46521

Please sign in to comment.