Skip to content

Commit

Permalink
Merge pull request #136 from klarna-incubator/minor-fixes
Browse files Browse the repository at this point in the history
Minor fixes and review score in bottom panel
  • Loading branch information
Tyouxik authored Nov 20, 2024
2 parents 0148e7b + a910d42 commit 62df8c5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
16 changes: 12 additions & 4 deletions app/src/components/model/panels/bottom/BottomPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export function BottomPanel() {
: [];
}

const passedResults = validationResults.filter((result) => result.testResult);
const successRatio =
validationResults.length === 0
? 0
: Number((passedResults.length / validationResults.length).toFixed(2));

if (tab === 0) {
// TAB.ALL
filteredResults = allNegativeResults;
Expand All @@ -57,16 +63,15 @@ export function BottomPanel() {
}

useEffect(() => {
if (!selectedComponent) {
if (!selectedComponent && tab === TAB.SELECTED_COMPONENT) {
setTab(0);
} else {
setTab(2);
}
}, [selectedComponent]);
}, [selectedComponent, tab]);

if (bottomPanelCollapsed) {
return <></>;
}

return (
<Box sx={{ gridArea: "bottom", backgroundColor: "rgb(20,20,20)" }}>
{!isLoading && (
Expand All @@ -77,12 +82,15 @@ export function BottomPanel() {
allLength={allNegativeResults.length}
modelLength={modelResults.length}
selectedLength={selectedResults.length}
selectedComponent={selectedComponent}
successRatio={successRatio}
/>
<ValidationTab
tab={tab}
setTab={setTab}
filteredResults={filteredResults}
isLoading={isLoading}
selectedComponent={selectedComponent}
/>
</>
)}
Expand Down
28 changes: 20 additions & 8 deletions app/src/components/model/panels/bottom/BottomTabsHeader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Paper, Grow, Tab, Tabs, Badge, Typography, Box } from "@mui/material";
import { useSelectedComponent } from "../../hooks/useSelectedComponent";

export const TAB = {
ALL: 0,
Expand All @@ -26,36 +25,49 @@ function getBadgeColor(length) {
return "error";
}

function getSuccessRatioColor(ratio) {
if (ratio >= 0.75) {
return "success";
} else if (ratio >= 0.5) {
return "warning";
}
return "error";
}

export function BottomTabsHeader({
tab,
setTab,
allLength,
modelLength,
selectedLength,
selectedComponent,
successRatio,
}) {
const selected = useSelectedComponent();

// This fixes an annoying MUI console error when you deselect a component
const tabHck = !selected && tab === TAB.COMPONENT ? TAB.SYSTEM : tab;

return (
<Paper elevation={3} sx={{ display: "flex" }}>
<Box
sx={{
mx: 3,
display: "flex",
gap: 1,
justifyContent: "center",
alignItems: "center",
}}
>
<Typography variant="h6" sx={{ fontSize: "0.8rem" }}>
QUALITY CHECK
</Typography>
<Badge
showZero
badgeContent={`${successRatio * 100}%`}
color={getSuccessRatioColor(successRatio)}
sx={tabBadgeStyle}
></Badge>
</Box>
<Box sx={{ flexGrow: 1 }}>
<Grow in={true}>
<Tabs
value={tabHck}
value={tab}
onChange={(_, v) => setTab(v)}
textColor="inherit"
variant="fullWidth"
Expand Down Expand Up @@ -97,7 +109,7 @@ export function BottomTabsHeader({
}
value={TAB.MODEL}
/>
{selected && (
{selectedComponent && (
<Tab
disableRipple
label={
Expand Down
11 changes: 7 additions & 4 deletions app/src/components/model/panels/bottom/ValidationTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import DoneIcon from "@mui/icons-material/Done";
import CloseIcon from "@mui/icons-material/Close";
import { useSetSelected } from "../../hooks/useSetSelected";
import { useDeselectAll } from "../../hooks/useSetMultipleSelected";
import { useSelectedComponent } from "../../hooks/useSelectedComponent";

function selectElement(type, elementId, setSelected, deselectAll, setTab) {
deselectAll();
Expand Down Expand Up @@ -74,12 +73,16 @@ function renderResults(results, setSelected, deselectAll, setTab) {
});
}

export function ValidationTab({ tab, setTab, filteredResults, isLoading }) {
export function ValidationTab({
tab,
setTab,
filteredResults,
isLoading,
selectedComponent,
}) {
const setSelected = useSetSelected();
const deselectAll = useDeselectAll();

const selectedComponent = useSelectedComponent();

if (isLoading) {
return (
<Box
Expand Down
38 changes: 38 additions & 0 deletions core/src/validation/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,42 @@ describe("ValidationEngine", () => {
expect(resultList.length).toBe(1);
expect(resultList[0].ruleName).toBe("should be selected");
});

it("should select appropriate component rules based on affectedType", async () => {
validationEngine.register([
{
type: "component",
name: "should apply to all components",
affectedType: [],
test: async ({ component }) => {
return false;
},
messageTrue: "should never be true",
messageFalse: "Yep, it applied to all components",
},
{
type: "component",
name: "should apply to external entity only",
affectedType: ["ee"],
test: async ({ component }) => {
return false;
},
messageTrue: "should never be true",
messageFalse: "Yep, it applied to external entity only",
},
]);

const modelId = await createSampleModel(dal);
const resultList = await validationEngine.getResults(modelId);
const applyAllResult = resultList.filter((r) => {
return r.ruleName === "should apply to all components";
});
const applyEEOnlyResult = resultList.filter((r) => {
return r.ruleName === "should apply to external entity only";
});

expect(applyAllResult.length).toBe(2);
expect(applyEEOnlyResult.length).toBe(1);
expect(applyEEOnlyResult[0].elementName).toBe("omegalul");
});
});
8 changes: 8 additions & 0 deletions core/src/validation/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ export class ValidationEngine extends EventEmitter {
}
}
}

// Skip the rule if the component type is not in the affectedType array
if (rule.affectedType.length > 0) {
if (!rule.affectedType.includes(component.type)) {
continue;
}
}

try {
const testResult = await rule.test({
...ruleArgs,
Expand Down

0 comments on commit 62df8c5

Please sign in to comment.