Skip to content

Commit

Permalink
DEVPROD-8302: Introduce SubsectionHeaderRow type (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
SupaJoon authored Jun 24, 2024
1 parent d499720 commit a5ae110
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ describe("getLinesInProcessedLogLinesFromSelectedLines", () => {
{
functionName: "test",
isOpen: true,
range: { end: 5, start: 4 },
range: { end: 6, start: 4 },
rowType: RowType.SectionHeader,
},
{
commandName: "shell.exec",
functionName: "test",
isOpen: true,
range: { end: 6, start: 4 },
rowType: RowType.SubsectionHeader,
},
4,
5,
6,
];
Expand All @@ -39,7 +47,7 @@ describe("getLinesInProcessedLogLinesFromSelectedLines", () => {
processedLogLines,
selectedLines,
);
expect(result).toStrictEqual([2, 5, 6]);
expect(result).toStrictEqual([2, 4, 5, 6]);
});
it("should return the starting line if no ending line is provided", () => {
const processedLogLines = [1, 2, 3, 4, 5];
Expand Down
14 changes: 12 additions & 2 deletions apps/parsley/src/components/LogRow/BaseRow/SharingMenu/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ProcessedLogLines, SelectedLineRange } from "types/logs";
import { findLineIndex } from "utils/findLineIndex";
import { isSectionHeaderRow, isSkippedLinesRow } from "utils/logRowTypes";
import {
isSectionHeaderRow,
isSkippedLinesRow,
isSubsectionHeaderRow,
} from "utils/logRowTypes";

const getLinesInProcessedLogLinesFromSelectedLines = (
processedLogLines: ProcessedLogLines,
Expand All @@ -17,7 +21,13 @@ const getLinesInProcessedLogLinesFromSelectedLines = (
const lines: number[] = [];
for (let i = startingIndex; i <= endingIndex; i++) {
const line = processedLogLines[i];
if (!(isSkippedLinesRow(line) || isSectionHeaderRow(line))) {
if (
!(
isSkippedLinesRow(line) ||
isSectionHeaderRow(line) ||
isSubsectionHeaderRow(line)
)
) {
lines.push(line);
}
}
Expand Down
10 changes: 9 additions & 1 deletion apps/parsley/src/components/LogRow/RowRenderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { SectionStatus } from "constants/logs";
import { useLogContext } from "context/LogContext";
import { useHighlightParam } from "hooks/useHighlightParam";
import { ProcessedLogLines } from "types/logs";
import { isSectionHeaderRow, isSkippedLinesRow } from "utils/logRowTypes";
import {
isSectionHeaderRow,
isSkippedLinesRow,
isSubsectionHeaderRow,
} from "utils/logRowTypes";
import AnsiRow from "../AnsiRow";
import ResmokeRow from "../ResmokeRow";
import SectionHeader from "../SectionHeader";
Expand Down Expand Up @@ -81,6 +85,10 @@ const ParsleyRow: RowRendererFunction = ({ processedLogLines }) => {
);
}

if (isSubsectionHeaderRow(processedLogLine)) {
return <div>subsection start</div>;
}

return (
<Row
failingLine={failingLine}
Expand Down
16 changes: 15 additions & 1 deletion apps/parsley/src/types/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Range {
enum RowType {
SkippedLines = "SkippedLines",
SectionHeader = "SectionHeader",
SubsectionHeader = "SubsectionHeader",
}

interface SkippedLinesRow {
Expand All @@ -26,7 +27,19 @@ interface SectionHeaderRow {
isOpen: boolean;
}

type ProcessedLogLine = number | SkippedLinesRow | SectionHeaderRow;
interface SubsectionHeaderRow {
rowType: RowType.SubsectionHeader;
functionName: string;
commandName: string;
range: Range;
isOpen: boolean;
}

type ProcessedLogLine =
| number
| SkippedLinesRow
| SectionHeaderRow
| SubsectionHeaderRow;

type ProcessedLogLines = ProcessedLogLine[];

Expand All @@ -53,6 +66,7 @@ export type {
ProcessedLogLine,
ProcessedLogLines,
SectionHeaderRow,
SubsectionHeaderRow,
SelectedLineRange,
SkippedLinesRow,
Range,
Expand Down
67 changes: 56 additions & 11 deletions apps/parsley/src/utils/findLineIndex/findLineIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ const processedLines: ProcessedLogLines = [
{ range: { end: 6, start: 4 }, rowType: RowType.SkippedLines },
6,
{ range: { end: 10, start: 7 }, rowType: RowType.SkippedLines },
10,
{
functionName: "f-1",
isOpen: true,
range: { end: 13, start: 10 },
range: { end: 11, start: 10 },
rowType: RowType.SectionHeader,
},
11,
{
commandName: "shell.exec",
functionName: "f-1",
isOpen: false,
range: { end: 11, start: 10 },
rowType: RowType.SubsectionHeader,
},
12,
{
functionName: "f-2",
Expand All @@ -29,9 +34,38 @@ const processedLines: ProcessedLogLines = [
range: { end: 17, start: 15 },
rowType: RowType.SectionHeader,
},
15,
16,
{
commandName: "shell.exec",
functionName: "f-3",
isOpen: false,
range: { end: 17, start: 15 },
rowType: RowType.SubsectionHeader,
},
{
functionName: "f-4",
isOpen: true,
range: { end: 19, start: 17 },
rowType: RowType.SectionHeader,
},
17,
18,
{
functionName: "f-5",
isOpen: true,
range: { end: 23, start: 19 },
rowType: RowType.SectionHeader,
},
{
commandName: "shell.exec",
functionName: "f-5",
isOpen: true,
range: { end: 23, start: 19 },
rowType: RowType.SubsectionHeader,
},
19,
20,
21,
22,
];

describe("findLineIndex", () => {
Expand All @@ -48,13 +82,24 @@ describe("findLineIndex", () => {

it("should return -1 when line number is not represented in the array", () => {
expect(findLineIndex(processedLines, -1)).toBe(-1);
expect(findLineIndex(processedLines, 18)).toBe(-1);
expect(findLineIndex(processedLines, 55)).toBe(-1);
});

it("should correctly determine index when line number is represented in a Range object belonging to a closed SectionHeaderRow", () => {
expect(findLineIndex(processedLines, 14)).toBe(10);
});
it("should correctly determine index when line number is represented in a Range object belonging to an open SectionHeaderRow", () => {
expect(findLineIndex(processedLines, 16)).toBe(13);
describe("when line number is represented in a Range object belonging to a section", () => {
it("determine index when line number is in closed SectionHeaderRow", () => {
expect(findLineIndex(processedLines, 14)).toBe(9);
});

it("determine index when line number is in an open SectionHeaderRow", () => {
expect(findLineIndex(processedLines, 18)).toBe(14);
});

it("determine index when line number is belongs to a closed SubsectionHeaderRow", () => {
expect(findLineIndex(processedLines, 15)).toBe(11);
});

it("determine index when line number is belongs to an open SubsectionHeaderRow", () => {
expect(findLineIndex(processedLines, 20)).toBe(18);
});
});
});
17 changes: 11 additions & 6 deletions apps/parsley/src/utils/findLineIndex/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ProcessedLogLines } from "types/logs";
import { isSectionHeaderRow, isSkippedLinesRow } from "utils/logRowTypes";
import {
isSectionHeaderRow,
isSkippedLinesRow,
isSubsectionHeaderRow,
} from "utils/logRowTypes";

/**
* `findLineIndex` employs binary search to search for the index of a line number within the
Expand All @@ -23,9 +27,10 @@ export const findLineIndex = (

const midIdx = Math.floor((start + end) / 2);
const midItem = processedLines[midIdx];
const isCollapsedRow =
isSkippedLinesRow(midItem) ||
(isSectionHeaderRow(midItem) && midItem.isOpen === false);
const isClosedSectionRow =
(isSectionHeaderRow(midItem) || isSubsectionHeaderRow(midItem)) &&
midItem.isOpen === false;
const isCollapsedRow = isSkippedLinesRow(midItem) || isClosedSectionRow;
// If the item is a collapsed row, we'll shift our search
// depending on the first and last line numbers in its range.
if (isCollapsedRow) {
Expand All @@ -40,8 +45,8 @@ export const findLineIndex = (
if (lineNumber > lastItem) {
return findLineIndex(processedLines, lineNumber, midIdx + 1, end);
}
} else if (isSectionHeaderRow(midItem)) {
// SectionHeader rows aren't directly associated with a
} else if (isSectionHeaderRow(midItem) || isSubsectionHeaderRow(midItem)) {
// Section rows aren't directly associated with a
// log line number, so they are skipped in the search.
const leftResult = findLineIndex(
processedLines,
Expand Down
8 changes: 7 additions & 1 deletion apps/parsley/src/utils/logRowTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RowType,
SectionHeaderRow,
SkippedLinesRow,
SubsectionHeaderRow,
} from "types/logs";

/**
Expand All @@ -21,4 +22,9 @@ const isSectionHeaderRow = (
): logLine is SectionHeaderRow =>
typeof logLine === "object" && logLine.rowType === RowType.SectionHeader;

export { isSectionHeaderRow, isSkippedLinesRow };
const isSubsectionHeaderRow = (
logLine: ProcessedLogLine,
): logLine is SubsectionHeaderRow =>
typeof logLine === "object" && logLine.rowType === RowType.SubsectionHeader;

export { isSectionHeaderRow, isSkippedLinesRow, isSubsectionHeaderRow };
14 changes: 12 additions & 2 deletions apps/parsley/src/utils/searchLogs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ProcessedLogLines } from "types/logs";
import { isSectionHeaderRow, isSkippedLinesRow } from "utils/logRowTypes";
import {
isSectionHeaderRow,
isSkippedLinesRow,
isSubsectionHeaderRow,
} from "utils/logRowTypes";

interface searchOptions {
searchRegex: RegExp;
Expand All @@ -15,7 +19,13 @@ const searchLogs = (options: searchOptions): number[] => {
const matchingIndices: number[] = [];
for (let i = 0; i < processedLogLines.length; i++) {
const lineIndex = processedLogLines[i];
if (!(isSkippedLinesRow(lineIndex) || isSectionHeaderRow(lineIndex))) {
if (
!(
isSkippedLinesRow(lineIndex) ||
isSectionHeaderRow(lineIndex) ||
isSubsectionHeaderRow(lineIndex)
)
) {
// Since processLogLines is ordered by line number, we can stop searching if we are out of range for our upper bound
if (upperBound && lineIndex > upperBound) {
break;
Expand Down

0 comments on commit a5ae110

Please sign in to comment.