Commit 4b4ea243 authored by Filipa Lacerda's avatar Filipa Lacerda

Updates the incremental trace

Adds function to check the last line number
parent 38931822
...@@ -26,8 +26,7 @@ export default { ...@@ -26,8 +26,7 @@ export default {
if (log.append) { if (log.append) {
if (isNewJobLogActive()) { if (isNewJobLogActive()) {
state.originalTrace = state.originalTrace.concat(log.trace); state.trace = updateIncrementalTrace(log.lines, state.trace);
state.trace = updateIncrementalTrace(state.originalTrace, state.trace, log.lines);
} else { } else {
state.trace += log.html; state.trace += log.html;
} }
...@@ -38,7 +37,6 @@ export default { ...@@ -38,7 +37,6 @@ export default {
// html or size. We keep the old value otherwise these // html or size. We keep the old value otherwise these
// will be set to `undefined` // will be set to `undefined`
if (isNewJobLogActive()) { if (isNewJobLogActive()) {
state.originalTrace = log.lines || state.trace;
state.trace = logLinesParser(log.lines) || state.trace; state.trace = logLinesParser(log.lines) || state.trace;
} else { } else {
state.trace = log.html || state.trace; state.trace = log.html || state.trace;
......
...@@ -19,7 +19,6 @@ export default () => ({ ...@@ -19,7 +19,6 @@ export default () => ({
isScrolledToBottomBeforeReceivingTrace: true, isScrolledToBottomBeforeReceivingTrace: true,
trace: isNewJobLogActive() ? [] : '', trace: isNewJobLogActive() ? [] : '',
originalTrace: [],
isTraceComplete: false, isTraceComplete: false,
traceSize: 0, traceSize: 0,
isTraceSizeVisible: false, isTraceSizeVisible: false,
......
...@@ -63,6 +63,30 @@ export const isCollapsibleSection = (acc = [], last = {}, section = {}) => ...@@ -63,6 +63,30 @@ export const isCollapsibleSection = (acc = [], last = {}, section = {}) =>
!section.section_duration && !section.section_duration &&
section.section === last.line.section; section.section === last.line.section;
/**
* Returns the lineNumber of the last line in
* a parsed log
*
* @param Array acc
* @returns Number
*/
export const getIncrementalLineNumber = acc => {
let lineNumberValue;
const lastIndex = acc.length - 1;
const lastElement = acc[lastIndex];
const nestedLines = lastElement.lines;
if (lastElement.isHeader && !nestedLines.length && lastElement.line) {
lineNumberValue = lastElement.line.lineNumber;
} else if (lastElement.isHeader && nestedLines.length) {
lineNumberValue = nestedLines[nestedLines.length - 1].lineNumber;
} else {
lineNumberValue = lastElement.lineNumber;
}
return lineNumberValue === 0 ? 1 : lineNumberValue + 1;
};
/** /**
* Parses the job log content into a structure usable by the template * Parses the job log content into a structure usable by the template
* *
...@@ -75,13 +99,14 @@ export const isCollapsibleSection = (acc = [], last = {}, section = {}) => ...@@ -75,13 +99,14 @@ export const isCollapsibleSection = (acc = [], last = {}, section = {}) =>
* - adds the index as lineNumber * - adds the index as lineNumber
* *
* @param Array lines * @param Array lines
* @param Number lineNumberStart
* @param Array accumulator * @param Array accumulator
* @returns Array parsed log lines * @returns Array parsed log lines
*/ */
export const logLinesParser = (lines = [], lineNumberStart, accumulator = []) => export const logLinesParser = (lines = [], accumulator = []) =>
lines.reduce((acc, line, index) => { lines.reduce(
const lineNumber = lineNumberStart ? lineNumberStart + index : index; (acc, line, index) => {
const lineNumber = accumulator.length > 0 ? getIncrementalLineNumber(acc) : index;
const last = acc[acc.length - 1]; const last = acc[acc.length - 1];
// If the object is an header, we parse it into another structure // If the object is an header, we parse it into another structure
...@@ -100,7 +125,9 @@ export const logLinesParser = (lines = [], lineNumberStart, accumulator = []) => ...@@ -100,7 +125,9 @@ export const logLinesParser = (lines = [], lineNumberStart, accumulator = []) =>
} }
return acc; return acc;
}, accumulator); },
[...accumulator],
);
/** /**
* Finds the repeated offset, removes the old one * Finds the repeated offset, removes the old one
...@@ -113,7 +140,7 @@ export const logLinesParser = (lines = [], lineNumberStart, accumulator = []) => ...@@ -113,7 +140,7 @@ export const logLinesParser = (lines = [], lineNumberStart, accumulator = []) =>
* @returns Array * @returns Array
* *
*/ */
export const findOffsetAndRemove = (newLog, oldParsed) => { export const findOffsetAndRemove = (newLog = [], oldParsed = []) => {
const cloneOldLog = [...oldParsed]; const cloneOldLog = [...oldParsed];
const lastIndex = cloneOldLog.length - 1; const lastIndex = cloneOldLog.length - 1;
const last = cloneOldLog[lastIndex]; const last = cloneOldLog[lastIndex];
...@@ -140,40 +167,13 @@ export const findOffsetAndRemove = (newLog, oldParsed) => { ...@@ -140,40 +167,13 @@ export const findOffsetAndRemove = (newLog, oldParsed) => {
* We need to check if that is the case by looking for the offset property * We need to check if that is the case by looking for the offset property
* before parsing the incremental part * before parsing the incremental part
* *
* @param array originalTrace
* @param array oldLog * @param array oldLog
* @param array newLog * @param array newLog
*/ */
export const updateIncrementalTrace = (originalTrace = [], oldLog = [], newLog = []) => { export const updateIncrementalTrace = (newLog, oldParsed = []) => {
const firstLine = newLog[0]; const parsedLog = findOffsetAndRemove(newLog, oldParsed);
const firstLineOffset = firstLine.offset;
// We are going to return a new array,
// let's make a shallow copy to make sure we
// are not updating the state outside of a mutation first.
const cloneOldLog = [...oldLog];
const lastIndex = cloneOldLog.length - 1; return logLinesParser(newLog, parsedLog);
const lastLine = cloneOldLog[lastIndex];
// The last line may be inside a collpasible section
// If it is, we use the not parsed saved log, remove the last element
// and parse the first received part togheter with the incremental log
if (
lastLine.isHeader &&
(lastLine.line.offset === firstLineOffset ||
(lastLine.lines.length &&
lastLine.lines[lastLine.lines.length - 1].offset === firstLineOffset))
) {
const cloneOriginal = [...originalTrace];
cloneOriginal.splice(cloneOriginal.length - 1);
return logLinesParser(cloneOriginal.concat(newLog));
} else if (lastLine.offset === firstLineOffset) {
cloneOldLog.splice(lastIndex);
return cloneOldLog.concat(logLinesParser(newLog, cloneOldLog.length));
}
// there are no matches, let's parse the new log and return them together
return cloneOldLog.concat(logLinesParser(newLog, cloneOldLog.length));
}; };
export const isNewJobLogActive = () => gon && gon.features && gon.features.jobLogJson; export const isNewJobLogActive = () => gon && gon.features && gon.features.jobLogJson;
...@@ -73,6 +73,7 @@ describe('Jobs Store Mutations', () => { ...@@ -73,6 +73,7 @@ describe('Jobs Store Mutations', () => {
html, html,
size: 511846, size: 511846,
complete: true, complete: true,
lines: [],
}); });
expect(stateCopy.trace).toEqual(html); expect(stateCopy.trace).toEqual(html);
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
addDurationToHeader, addDurationToHeader,
isCollapsibleSection, isCollapsibleSection,
findOffsetAndRemove, findOffsetAndRemove,
getIncrementalLineNumber,
} from '~/jobs/store/utils'; } from '~/jobs/store/utils';
import { import {
utilsMockData, utilsMockData,
...@@ -292,11 +293,91 @@ describe('Jobs Store Utils', () => { ...@@ -292,11 +293,91 @@ describe('Jobs Store Utils', () => {
}); });
}); });
describe('getIncrementalLineNumber', () => {
describe('when last line is 0', () => {
it('returns 1', () => {
const log = [
{
content: [],
lineNumber: 0,
},
];
expect(getIncrementalLineNumber(log)).toEqual(1);
});
});
describe('with unnested line', () => {
it('returns the lineNumber of the last item in the array', () => {
const log = [
{
content: [],
lineNumber: 10,
},
{
content: [],
lineNumber: 101,
},
];
expect(getIncrementalLineNumber(log)).toEqual(102);
});
});
describe('when last line is the header section', () => {
it('returns the lineNumber of the last item in the array', () => {
const log = [
{
content: [],
lineNumber: 10,
},
{
isHeader: true,
line: {
lineNumber: 101,
content: [],
},
lines: [],
},
];
expect(getIncrementalLineNumber(log)).toEqual(102);
});
});
describe('when last line is a nested line', () => {
it('returns the lineNumber of the last item in the nested array', () => {
const log = [
{
content: [],
lineNumber: 10,
},
{
isHeader: true,
line: {
lineNumber: 101,
content: [],
},
lines: [
{
lineNumber: 102,
content: [],
},
{ lineNumber: 103, content: [] },
],
},
];
expect(getIncrementalLineNumber(log)).toEqual(104);
});
});
});
describe('updateIncrementalTrace', () => { describe('updateIncrementalTrace', () => {
describe('without repeated section', () => { describe('without repeated section', () => {
it('concats and parses both arrays', () => { it('concats and parses both arrays', () => {
const oldLog = logLinesParser(originalTrace); const oldLog = logLinesParser(originalTrace);
const result = updateIncrementalTrace(originalTrace, oldLog, regularIncremental); const result = updateIncrementalTrace(regularIncremental, oldLog);
expect(result).toEqual([ expect(result).toEqual([
{ {
...@@ -324,7 +405,7 @@ describe('Jobs Store Utils', () => { ...@@ -324,7 +405,7 @@ describe('Jobs Store Utils', () => {
describe('with regular line repeated offset', () => { describe('with regular line repeated offset', () => {
it('updates the last line and formats with the incremental part', () => { it('updates the last line and formats with the incremental part', () => {
const oldLog = logLinesParser(originalTrace); const oldLog = logLinesParser(originalTrace);
const result = updateIncrementalTrace(originalTrace, oldLog, regularIncrementalRepeated); const result = updateIncrementalTrace(regularIncrementalRepeated, oldLog);
expect(result).toEqual([ expect(result).toEqual([
{ {
...@@ -343,7 +424,7 @@ describe('Jobs Store Utils', () => { ...@@ -343,7 +424,7 @@ describe('Jobs Store Utils', () => {
describe('with header line repeated', () => { describe('with header line repeated', () => {
it('updates the header line and formats with the incremental part', () => { it('updates the header line and formats with the incremental part', () => {
const oldLog = logLinesParser(headerTrace); const oldLog = logLinesParser(headerTrace);
const result = updateIncrementalTrace(headerTrace, oldLog, headerTraceIncremental); const result = updateIncrementalTrace(headerTraceIncremental, oldLog);
expect(result).toEqual([ expect(result).toEqual([
{ {
...@@ -369,11 +450,7 @@ describe('Jobs Store Utils', () => { ...@@ -369,11 +450,7 @@ describe('Jobs Store Utils', () => {
describe('with collapsible line repeated', () => { describe('with collapsible line repeated', () => {
it('updates the collapsible line and formats with the incremental part', () => { it('updates the collapsible line and formats with the incremental part', () => {
const oldLog = logLinesParser(collapsibleTrace); const oldLog = logLinesParser(collapsibleTrace);
const result = updateIncrementalTrace( const result = updateIncrementalTrace(collapsibleTraceIncremental, oldLog);
collapsibleTrace,
oldLog,
collapsibleTraceIncremental,
);
expect(result).toEqual([ expect(result).toEqual([
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment