Skip to content

Commit

Permalink
fix(bots): normalized bot status & added filters
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Feb 23, 2020
1 parent 17c69e4 commit a3c0d71
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/bp/common/typings.ts
Expand Up @@ -188,7 +188,7 @@ export interface ServerHealth {
}

export interface BotHealth {
status: 'mounted' | 'unmounted' | 'disabled' | 'error'
status: 'healthy' | 'unhealthy' | 'unmounted' | 'disabled'
errorCount: number
criticalCount: number
warningCount: number
Expand Down
22 changes: 12 additions & 10 deletions src/bp/core/services/bot-service.ts
Expand Up @@ -589,11 +589,9 @@ export class BotService {
}, botId)
)

BotService.setBotStatus(botId, 'mounted')
BotService.setBotStatus(botId, 'healthy')
return true
} catch (err) {
BotService.setBotStatus(botId, 'error')

this.logger
.forBot(botId)
.attachError(err)
Expand Down Expand Up @@ -758,17 +756,21 @@ export class BotService {
}

public static incrementBotStats(botId: string, type: 'error' | 'warning' | 'critical') {
const info = this._botHealth[botId] || DEFAULT_BOT_HEALTH
if (!this._botHealth[botId]) {
this._botHealth[botId] = DEFAULT_BOT_HEALTH
}

this._botHealth[botId] = {
...info,
errorCount: info.errorCount + (type === 'error' ? 1 : 0),
criticalCount: info.criticalCount + (type === 'critical' ? 1 : 0),
warningCount: info.warningCount + (type === 'warning' ? 1 : 0)
if (type === 'error') {
this._botHealth[botId].errorCount++
} else if (type === 'warning') {
this._botHealth[botId].warningCount++
} else if (type === 'critical') {
this._botHealth[botId].criticalCount++
this._botHealth[botId].status = 'unhealthy'
}
}

public static setBotStatus(botId: string, status: 'mounted' | 'unmounted' | 'disabled' | 'error') {
public static setBotStatus(botId: string, status: 'healthy' | 'unhealthy' | 'unmounted' | 'disabled') {
this._botHealth[botId] = {
...(this._botHealth[botId] || DEFAULT_BOT_HEALTH),
status
Expand Down
2 changes: 1 addition & 1 deletion src/bp/ui-admin/src/Pages/Components/Dropdown.tsx
Expand Up @@ -51,7 +51,7 @@ const Dropdown: FC<Props> = props => {
}}
>
<Button
text={activeItem && activeItem.label}
text={props.small ? <small>{activeItem && activeItem.label}</small> : activeItem && activeItem.label}
icon={props.icon}
rightIcon={props.rightIcon || 'double-caret-vertical'}
small={props.small}
Expand Down
42 changes: 31 additions & 11 deletions src/bp/ui-admin/src/Pages/Server/BotHealth.tsx
Expand Up @@ -12,6 +12,9 @@ import { switchWorkspace } from '~/reducers/user'
import { toastFailure, toastSuccess } from '~/utils/toaster'
import { getActiveWorkspace } from '~/Auth'

import Dropdown, { Option } from '../Components/Dropdown'
import { filterText } from '../Logs/utils'

type Props = {
health?: ServerHealth[]
botsByWorkspace?: { [workspaceId: string]: string[] }
Expand All @@ -22,6 +25,14 @@ type Props = {

const getKey = entry => `${entry.hostname} (${entry.serverId})`

const STATUS: Option[] = [
{ label: 'All', value: '' },
{ label: 'Healthy', value: 'healthy' },
{ label: 'Unhealthy', value: 'unhealthy' },
{ label: 'Disabled', value: 'disabled' },
{ label: 'Unmounted', value: 'unmounted' }
]

const BotHealth: FC<Props> = props => {
const [data, setData] = useState<any>()
const [columns, setColumns] = useState<any>([])
Expand Down Expand Up @@ -53,6 +64,10 @@ const BotHealth: FC<Props> = props => {
setData(data)
}

const filterStatus = ({ onChange }) => {
return <Dropdown items={STATUS} defaultItem={STATUS[0]} onChange={option => onChange(option.value)} small />
}

const goToBotLogs = async (botId: string) => {
if (props.botsByWorkspace) {
const workspace = _.findKey(props.botsByWorkspace, x => x.includes(botId))
Expand All @@ -76,25 +91,22 @@ const BotHealth: FC<Props> = props => {
{
Header: 'Status',
Cell: cell => {
const hasCriticalIssue = !!Object.values(cell.original.data).find((x: any) => x.criticalCount > 0)
if (hasCriticalIssue) {
return <span className="logCritical">Unhealthy</span>
}

switch (_.get(cell.original, `data[${key}].status`)) {
default:
return 'N/A'
case 'error':
return <span className="logError">Error</span>
case 'mounted':
case 'unhealthy':
return <span className="logCritical">Unhealthy</span>
case 'healthy':
return <span className="logInfo">Healthy</span>
case 'disabled':
return 'Disabled'
case 'unmounted':
return 'Unmounted'
}
},
width: 80,
Filter: filterStatus,
filterable: true,
width: 100,
accessor: `data[${key}].status`
},
{
Expand Down Expand Up @@ -134,7 +146,9 @@ const BotHealth: FC<Props> = props => {
</span>
)
},
width: 250
width: 250,
Filter: filterText,
filterable: true
},
...hostColumns,
{
Expand Down Expand Up @@ -168,7 +182,13 @@ const BotHealth: FC<Props> = props => {
}

return (
<ReactTable columns={columns} data={data} defaultPageSize={10} className="-striped -highlight monitoringOverview" />
<ReactTable
columns={columns}
data={data}
defaultPageSize={10}
defaultSorted={[{ id: 'botId', desc: false }]}
className="-striped -highlight monitoringOverview"
/>
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/bp/ui-admin/src/Pages/Workspace/Bots/index.tsx
Expand Up @@ -10,13 +10,13 @@ import {
Position
} from '@blueprintjs/core'
import { BotConfig } from 'botpress/sdk'
import { confirmDialog } from 'botpress/shared'
import { ServerHealth } from 'common/typings'
import _ from 'lodash'
import React, { Component, Fragment } from 'react'
import { connect } from 'react-redux'
import { generatePath, RouteComponentProps } from 'react-router'
import { Alert, Col, Row } from 'reactstrap'
import { confirmDialog } from 'botpress/shared'
import { toastSuccess } from '~/utils/toaster'
import { toastFailure } from '~/utils/toaster'
import { filterList } from '~/utils/util'
Expand Down Expand Up @@ -178,7 +178,7 @@ class Bots extends Component<Props> {

return _.some(
this.props.health.map(x => x.bots[botId]),
s => s && s.status === 'error'
s => s && s.status === 'unhealthy'
)
}

Expand Down

0 comments on commit a3c0d71

Please sign in to comment.