Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit df11732

Browse files
author
Noah Hanjun Lee
authored
feat: subscribe deployment events in UI (#93)
Add to subscribe deployment events in Deployment page and Repo page.
1 parent 8e448e1 commit df11732

File tree

7 files changed

+62
-21
lines changed

7 files changed

+62
-21
lines changed

internal/pkg/store/event.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func (s *Store) ListEventsGreaterThanTime(ctx context.Context, t time.Time) ([]*
3434
WithDeployment(func(dq *ent.DeploymentQuery) {
3535
dq.
3636
WithUser().
37-
WithRepo()
37+
WithRepo().
38+
WithDeploymentStatuses()
3839
}).
3940
Limit(limit).
4041
All(ctx)

internal/server/api/v1/stream/stream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ func NewStream(i Interactor) *Stream {
3030
}
3131
}
3232

33+
// GetEvents streams a deployment or a approval.
34+
// Subscriber specify the event type, 'created' or 'updated', by comparing
35+
// the created time and the updated time.
3336
func (s *Stream) GetEvents(c *gin.Context) {
3437
ctx := c.Request.Context()
3538

3639
v, _ := c.Get(gb.KeyUser)
3740
u, _ := v.(*ent.User)
3841

3942
debugID := randstr()
40-
s.log.Debug("create a stream.", zap.String("debug_id", debugID))
4143

4244
events := make(chan *ent.Event, 10)
4345

@@ -73,10 +75,8 @@ L:
7375
for {
7476
select {
7577
case <-w.CloseNotify():
76-
s.log.Debug("stream canceled.", zap.String("debug_id", debugID))
7778
break L
7879
case <-time.After(time.Hour):
79-
s.log.Debug("stream canceled.", zap.String("debug_id", debugID))
8080
break L
8181
case <-time.After(time.Second * 30):
8282
c.Render(-1, sse.Event{

ui/src/redux/deployment.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ export const deploymentSlice = createSlice({
246246
reducers: {
247247
setNumber: (state, action: PayloadAction<number>) => {
248248
state.number = action.payload
249+
},
250+
handleDeploymentEvent: (state, action: PayloadAction<Deployment>) => {
251+
const event = action.payload
252+
253+
if (event.id !== state.deployment?.id) {
254+
return
255+
}
256+
257+
state.deployment = event
249258
}
250259
},
251260
extraReducers: builder => {

ui/src/redux/home.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,17 @@ export const homeSlice = createSlice({
7777
}
7878

7979
if (!repo.deployments) {
80-
return repo
80+
repo.deployments = []
8181
}
8282

8383
// Unshift a new deployment when the event is create.
84-
if (repo.deployments.length >= 1 && event.id > repo.deployments[0].id) {
84+
if (event.createdAt.getTime() === event.updatedAt.getTime()) {
8585
repo.deployments.unshift(event)
8686
return repo
8787
}
8888

8989
repo.deployments = repo.deployments.map((deployment) => {
90-
// Update the deployment if the deployment event is matched.
91-
if (deployment.id === event.id) {
92-
return event
93-
}
94-
95-
return deployment
90+
return (deployment.id === event.id)? event : deployment
9691
})
9792
return repo
9893
})

ui/src/redux/repoHome.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ export const repoHomeSlice = createSlice({
7373
decreasePage: (state) => {
7474
state.page = state.page - 1
7575
},
76+
handleDeploymentEvent: (state, action: PayloadAction<Deployment>) => {
77+
const event = action.payload
78+
79+
if (event.repo?.id !== state.repo?.id) {
80+
return
81+
}
82+
83+
if (!(state.env === "" || event.env === state.env)) {
84+
return
85+
}
86+
87+
// Unshift a new deployment when the event is create.
88+
if (event.createdAt.getTime() === event.updatedAt.getTime()) {
89+
state.deployments.unshift(event)
90+
return
91+
}
92+
93+
state.deployments = state.deployments.map((deployment) => {
94+
return (deployment.id === event.id)? event : deployment
95+
})
96+
},
7697
},
7798
extraReducers: builder => {
7899
builder

ui/src/views/Deployment.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useParams } from "react-router-dom"
66
import { useAppSelector, useAppDispatch } from "../redux/hooks"
77
import {
88
init,
9-
deploymentSlice,
9+
deploymentSlice as slice,
1010
fetchDeployment,
1111
fetchDeploymentChanges,
1212
fetchApprovals,
@@ -26,15 +26,14 @@ import {
2626
ApprovalStatus,
2727
RequestStatus
2828
} from "../models"
29+
import { subscribeDeploymentEvent } from "../apis"
2930

3031
import Main from "./Main"
3132
import ApproversSelector from "../components/ApproversSelector"
3233
import ApprovalDropdown from "../components/ApprovalDropdown"
3334
import Spin from "../components/Spin"
3435
import DeployConfirm from "../components/DeployConfirm"
3536

36-
const { actions } = deploymentSlice
37-
3837
interface Params {
3938
namespace: string
4039
name: string
@@ -57,13 +56,21 @@ export default function DeploymentView(): JSX.Element {
5756
useEffect(() => {
5857
const f = async () => {
5958
await dispatch(init({namespace, name}))
60-
await dispatch(actions.setNumber(parseInt(number, 10)))
59+
await dispatch(slice.actions.setNumber(parseInt(number, 10)))
6160
await dispatch(fetchDeployment())
6261
await dispatch(fetchDeploymentChanges())
6362
await dispatch(fetchApprovals())
6463
await dispatch(fetchMyApproval())
6564
}
6665
f()
66+
67+
const de = subscribeDeploymentEvent((d) => {
68+
dispatch(slice.actions.handleDeploymentEvent(d))
69+
})
70+
71+
return () => {
72+
de.close()
73+
}
6774
// eslint-disable-next-line
6875
}, [dispatch])
6976

ui/src/views/RepoHome.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { shallowEqual } from "react-redux";
44
import { PageHeader, Select } from 'antd'
55

66
import { useAppSelector, useAppDispatch } from '../redux/hooks'
7-
import { repoHomeSlice, init, fetchEnvs, fetchDeployments, perPage } from '../redux/repoHome'
7+
import { repoHomeSlice as slice, init, fetchEnvs, fetchDeployments, perPage } from '../redux/repoHome'
8+
import { subscribeDeploymentEvent } from "../apis"
89

910
import ActivityLogs from '../components/ActivityLogs'
1011
import Spin from '../components/Spin'
1112
import Pagination from '../components/Pagination'
1213

13-
const { actions } = repoHomeSlice
1414
const { Option } = Select
1515

1616
interface Params {
@@ -35,23 +35,31 @@ export default function RepoHome(): JSX.Element {
3535
await dispatch(fetchDeployments())
3636
}
3737
f()
38+
39+
const de = subscribeDeploymentEvent((d) => {
40+
dispatch(slice.actions.handleDeploymentEvent(d))
41+
})
42+
43+
return () => {
44+
de.close()
45+
}
3846
// eslint-disable-next-line
3947
}, [dispatch])
4048

4149
const isLast = deployments.length < perPage
4250

4351
const onChangeEnv = (env: string) => {
44-
dispatch(actions.setEnv(env))
52+
dispatch(slice.actions.setEnv(env))
4553
dispatch(fetchDeployments())
4654
}
4755

4856
const onClickPrev = () => {
49-
dispatch(actions.decreasePage())
57+
dispatch(slice.actions.decreasePage())
5058
dispatch(fetchDeployments())
5159
}
5260

5361
const onClickNext = () => {
54-
dispatch(actions.increasePage())
62+
dispatch(slice.actions.increasePage())
5563
dispatch(fetchDeployments())
5664
}
5765

0 commit comments

Comments
 (0)