Skip to content

Commit 068fa33

Browse files
committed
build stats added
1 parent 09e6a71 commit 068fa33

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

src/components/TestDetailsModal.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { TestRunDetails } from "./TestRunDetails";
3737
import useImage from "use-image";
3838
import { routes } from "../constants";
3939
import { NoImagePlaceholder } from "./NoImageAvailable";
40+
import { useBuildDispatch, updateBuild } from "../contexts/build.context";
4041

4142
const useStyles = makeStyles((theme) => ({
4243
imageContainer: {
@@ -65,6 +66,7 @@ const TestDetailsModal: React.FunctionComponent<{
6566
}> = ({ testRun, updateTestRun }) => {
6667
const classes = useStyles();
6768
const history = useHistory();
69+
const buildDispatch = useBuildDispatch();
6870

6971
const stageWidth = (window.innerWidth / 2) * 0.9;
7072
const stageHeigth = window.innerHeight;
@@ -172,6 +174,7 @@ const TestDetailsModal: React.FunctionComponent<{
172174
onClick={() =>
173175
testRunService.approve(testRun.id).then((testRun) => {
174176
updateTestRun(testRun);
177+
updateBuild(buildDispatch, testRun);
175178
})
176179
}
177180
>
@@ -180,9 +183,10 @@ const TestDetailsModal: React.FunctionComponent<{
180183
<Button
181184
color="secondary"
182185
onClick={() =>
183-
testRunService
184-
.reject(testRun.id)
185-
.then((testRun) => updateTestRun(testRun))
186+
testRunService.reject(testRun.id).then((testRun) => {
187+
updateTestRun(testRun);
188+
updateBuild(buildDispatch, testRun);
189+
})
186190
}
187191
>
188192
Reject

src/contexts/build.context.tsx

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import * as React from "react";
2-
import { Build } from "../types";
1+
import React from "react";
2+
import { Build, TestRun } from "../types";
33
import { buildsService } from "../services";
4+
import { TestStatus } from "../types/testStatus";
45

56
interface IRequestAction {
67
type: "request";
@@ -22,7 +23,23 @@ interface IDeleteAction {
2223
payload: string;
2324
}
2425

25-
type IAction = IRequestAction | IGetAction | IDeleteAction | ISelectAction;
26+
interface IUpdateAction {
27+
type: "update";
28+
payload: TestRun;
29+
}
30+
31+
interface IStatsAction {
32+
type: "stats";
33+
payload: string;
34+
}
35+
36+
type IAction =
37+
| IRequestAction
38+
| IGetAction
39+
| IDeleteAction
40+
| ISelectAction
41+
| IStatsAction
42+
| IUpdateAction;
2643

2744
type Dispatch = (action: IAction) => void;
2845
type State = {
@@ -49,6 +66,57 @@ function buildReducer(state: State, action: IAction): State {
4966
...state,
5067
selectedBuildId: action.payload,
5168
};
69+
case "stats":
70+
return {
71+
...state,
72+
buildList: state.buildList.map((build) => {
73+
if (build.id === action.payload) {
74+
// reset stats
75+
build.passedCount = 0;
76+
build.unresolvedCount = 0;
77+
build.failedCount = 0;
78+
79+
// calculate stats
80+
build.testRuns.forEach((testRun) => {
81+
switch (testRun.status) {
82+
case TestStatus.approved:
83+
case TestStatus.ok: {
84+
build.passedCount += 1;
85+
break;
86+
}
87+
case TestStatus.unresolved:
88+
case TestStatus.new: {
89+
build.unresolvedCount += 1;
90+
break;
91+
}
92+
case TestStatus.failed: {
93+
build.failedCount += 1;
94+
break;
95+
}
96+
}
97+
});
98+
}
99+
return build;
100+
}),
101+
};
102+
case "update":
103+
return {
104+
...state,
105+
buildList: state.buildList.map((build) => {
106+
if (build.id === action.payload.buildId) {
107+
return {
108+
...build,
109+
testRuns: build.testRuns.map((testRun) => {
110+
if (testRun.id === action.payload.id) {
111+
return action.payload;
112+
}
113+
return testRun;
114+
}),
115+
};
116+
}
117+
return build;
118+
}),
119+
};
52120
case "get":
53121
return {
54122
...state,
@@ -99,6 +167,7 @@ async function getBuildList(dispatch: Dispatch, id: string) {
99167
.getList(id)
100168
.then((items) => {
101169
dispatch({ type: "get", payload: items });
170+
items.forEach((build) => dispatch({ type: "stats", payload: build.id }));
102171
})
103172
.catch((error) => {
104173
console.log(error.toString());
@@ -124,11 +193,17 @@ async function selectBuild(dispatch: Dispatch, id: string) {
124193
dispatch({ type: "select", payload: id });
125194
}
126195

196+
async function updateBuild(dispatch: Dispatch, testRun: TestRun) {
197+
dispatch({ type: "update", payload: testRun });
198+
dispatch({ type: "stats", payload: testRun.buildId });
199+
}
200+
127201
export {
128202
BuildProvider,
129203
useBuildState,
130204
useBuildDispatch,
131205
getBuildList,
132206
deleteBuild,
133207
selectBuild,
208+
updateBuild,
134209
};

src/types/build.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { TestRun } from "./testRun";
22

33
export interface Build {
4-
id: string,
5-
projectName: string,
6-
branchName: string,
7-
status: string,
8-
createdAt: Date,
9-
createdBy: string,
10-
testRuns: TestRun[]
11-
}
4+
id: string;
5+
projectName: string;
6+
branchName: string;
7+
status: string;
8+
createdAt: Date;
9+
createdBy: string;
10+
testRuns: TestRun[];
11+
12+
// stats
13+
unresolvedCount: number;
14+
passedCount: number;
15+
failedCount: number;
16+
}

src/types/testRun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TestStatus } from "./testStatus";
22

33
export interface TestRun {
44
id: string;
5-
buildId: number;
5+
buildId: string;
66
imageName: string;
77
diffName: string;
88
diffPercent: number;

0 commit comments

Comments
 (0)