Skip to content

Commit

Permalink
Render initialization status
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Mar 31, 2024
1 parent bdff512 commit c84287e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
46 changes: 27 additions & 19 deletions frontend/components/Layout/InitializationStatusChecker.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
import { FC, useContext, useEffect, useState } from "react"

import { NotificationContext } from "@/context/NotificationContext"
import { Notification, NotificationContext } from "@/context/NotificationContext"
import { useLocalStorage } from "@/hooks/useLocalStorage"
import { useInitializationStatus } from "@/repositories/initializationStatusRepository"
import { usePid } from "@/repositories/pidRepository"

const INITIAL_KEY = 'InitializationStatusChecker-closed'

export const InitializationStatusChecker: FC = () => {
const { setNotification } = useContext(NotificationContext)

const [closed, setClosed] = useState<boolean>(false)
const { pid } = usePid()
const key = pid ? `InitializationStatusChecker-closed-${pid}` : INITIAL_KEY
const [closed, setClosed] = useLocalStorage<boolean>(key, false)
const [initialized, setInitialized] = useState<boolean>(false)

// Stop loading if initialization process is finished
const { initializationStatus } = useInitializationStatus((initialized || closed) ? 0 : 100)

useEffect(() => {
if (!initializationStatus) return;
if (!initializationStatus || !pid) return;

let notification: Notification | undefined = undefined

if (initializationStatus.total === initializationStatus.loaded) {
setInitialized(true)

if (!closed) {
setNotification(
{
type: 'success',
message: `Successfully loaded ${initializationStatus.loaded} definitions!`,
onClose: () => setClosed(true)
}
)
notification = {
type: 'success',
message: `Successfully loaded ${initializationStatus.loaded} definitions!`,
onClose: () => setClosed(true)
}
} else if (!closed) {
const progress = Math.round(initializationStatus.loaded / initializationStatus.total * 100)

setNotification(
{
type: 'info',
message: `Loading definitions... ${progress}% (${initializationStatus.loaded}/${initializationStatus.total})`,
onClose: () => setClosed(true)
}
)
notification = {
type: 'info',
message: `Loading definitions... ${progress}% (${initializationStatus.loaded}/${initializationStatus.total})`,
onClose: () => setClosed(true)
}
}

if (closed) {
setNotification(null)
} else if (notification) {
setNotification(notification)
}
}, [closed, initializationStatus, setNotification])
}, [pid, closed, setClosed, initializationStatus, setNotification])

return null
}
1 change: 1 addition & 0 deletions frontend/constants/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const path = {
index: () => '/licenses',
},
api: {
pid: () => `/api/pid.json`,
initializationStatus: () => `/api/initialization_status.json`,
definitions: {
index: () => '/api/definitions.json',
Expand Down
18 changes: 18 additions & 0 deletions frontend/repositories/pidRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import useSWR from 'swr'

import { path } from '@/constants/path'

import { get } from './httpRequest'

type PidReponse = {
pid: number
}

export const usePid = () => {
const { data } = useSWR(path.api.pid(), async (): Promise<number> => {
const response = await get<PidReponse>(path.api.pid())
return response.pid
})

return { pid: data }
}
2 changes: 2 additions & 0 deletions lib/diver_down/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def call(env)
in ['GET', %r{\A/api/sources/(?<source>.+)\.json\z}]
source = Regexp.last_match[:source]
action.source(source)
in ['GET', %r{\A/api/pid\.json\z}]
action.pid
in ['GET', %r{\A/api/initialization_status\.json\z}]
action.initialization_status(@total_definition_files_size)
in ['GET', %r{\A/assets/}]
Expand Down
7 changes: 7 additions & 0 deletions lib/diver_down/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def initialization_status(total)
)
end

# GET /api/pid.json
def pid
json(
pid: Process.pid
)
end

# GET /api/definitions/:bit_id.json
#
# @param bit_id [Integer]
Expand Down
11 changes: 11 additions & 0 deletions spec/diver_down/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ def assert_source(source, expected_ids)
end
end

describe 'GET /api/pid.json' do
it 'returns pid' do
get '/api/pid.json'

expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)).to eq({
'pid' => Process.pid,
})
end
end

describe 'GET /api/sources.json' do
it 'returns [] if store is blank' do
get '/api/sources.json'
Expand Down

0 comments on commit c84287e

Please sign in to comment.