Skip to content

Commit

Permalink
hotfix: improve handling huge object data
Browse files Browse the repository at this point in the history
Ref: LOG-7522
Semver: minor
  • Loading branch information
Samir Musali committed Oct 19, 2020
1 parent 71ba0c8 commit d9d99cb
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,39 @@ const prepareEvent = (eventData) => {
});
};

// Extra the Main, Meta, and Time Data
const extractData = (data) => {
let {Records, logFiles, ...meta} = data;
const timestamp = meta && meta.eventTime ? (new Date(meta.eventTime)).getTime() : undefined
data = Records || logFiles || data;
return Array.isArray(data) ? data.reduce((lines, entry) => {
lines.push({
line: JSON.stringify(entry)
, meta
, timestamp: entry.eventTime ? (new Date(entry.eventTime)).getTime() : timestamp
});
return lines;
}, []) : [{ line: JSON.stringify(data), meta, timestamp }];
};

// Gett the Logs from File
const getLogs = (params, callback) => {
const keyFormat = checkFormat(params.Key);
return s3.getObject(params, (error, data) => {
if (error) return callback(error);
if (error) return callback(`Error in Getting ${params.Key} from ${params.Bucket}: ${error}`);
data = keyFormat.gz ? zlib.gunzipSync(data.Body) : data.Body;
data = data.toString('ascii');

if (keyFormat.json) {
try {
data = JSON.parse(data);
return callback(null, Array.isArray(data) ? data.map(entry => JSON.stringify(entry)) : JSON.stringify(data));
return callback(null, extractData(data));
} catch (e) {
return callback(null, data.split('\n').map(line => line.trim()).filter((line) => {
try {
return line === JSON.stringify(JSON.parse(line));
} catch (e) {
return false;
}
}));
return callback(`Error in Parsing the Data from JSON file ${params.Key}: ${e}`);
}
}

return callback(null, data.split('\n'));
return callback(null, data.split('\n').map(line => { line }));
});
};

Expand All @@ -175,9 +184,12 @@ const batchify = (logs) => {
// Prepare the Logs
const prepareLogs = (logs, eventData) => {
return logs.filter(log => log !== '').map((log) => {
return Object.assign({}, eventData, {
line: log
});
return {
file: eventData.file
, timestamp: log.timestamp || eventData.timestamp
, meta: Object.assign({}, eventData.meta, log.meta)
, line: log.line
}
});
};

Expand Down

0 comments on commit d9d99cb

Please sign in to comment.