Refactor switcher links utils

pull/10616/head
Delmer Reed 2018-07-10 17:45:32 -04:00
parent 131b878913
commit d7e7981143
6 changed files with 35 additions and 38 deletions

View File

@ -0,0 +1,4 @@
export const EMPTY_LINKS = {
links: [],
active: null,
}

View File

@ -1,6 +1,10 @@
import {proxy} from 'utils/queryUrlGenerator'
import replaceTemplate from 'src/tempVars/utils/replace'
import AJAX from 'utils/ajax'
import {
linksFromHosts,
updateActiveHostLink,
} from 'src/hosts/utils/hostsSwitcherLinks'
import _ from 'lodash'
export const getCpuAndLoadForHosts = (
@ -95,7 +99,7 @@ export const getCpuAndLoadForHosts = (
})
}
export async function getAllHosts(source) {
async function getAllHosts(source) {
const {
telegrafDB,
links: {proxy: proxyLink},
@ -127,6 +131,16 @@ export async function getAllHosts(source) {
}
}
export const loadHostsLinks = async (
source,
{activeHost = {}, getHostNamesAJAX = getAllHosts} = {}
) => {
const hostNames = await getHostNamesAJAX(source)
const allLinks = linksFromHosts(hostNames, source)
return updateActiveHostLink(allLinks, activeHost)
}
export const getLayouts = () =>
AJAX({
method: 'GET',

View File

@ -8,14 +8,15 @@ import DashboardHeader from 'src/dashboards/components/DashboardHeader'
import FancyScrollbar from 'shared/components/FancyScrollbar'
import ManualRefresh from 'src/shared/components/ManualRefresh'
import {generateForHosts} from 'src/utils/tempVars'
import * as hostsSwitcher from 'src/hosts/utils/hostsSwitcherLinks'
import {timeRanges} from 'shared/data/timeRanges'
import {
getLayouts,
getAppsForHost,
getMeasurementsForHost,
loadHostsLinks,
} from 'src/hosts/apis'
import {EMPTY_LINKS} from 'src/dashboards/constants/dashboardHeader'
import {setAutoRefresh, delayEnablePresentationMode} from 'shared/actions/app'
import {ErrorHandling} from 'src/shared/decorators/errors'
@ -26,7 +27,7 @@ class HostPage extends Component {
super(props)
this.state = {
layouts: [],
hostLinks: hostsSwitcher.EMPTY_LINKS,
hostLinks: EMPTY_LINKS,
timeRange: timeRanges.find(tr => tr.lower === 'now() - 1h'),
dygraphs: [],
}
@ -198,12 +199,10 @@ class HostPage extends Component {
params: {hostID},
} = this.props
const allLinks = await hostsSwitcher.loadHostsLinks(source)
const hostLinks = hostsSwitcher.updateActiveHostLink(allLinks, {
name: hostID,
})
const activeHost = {name: hostID}
const links = await loadHostsLinks(source, {activeHost})
return hostLinks
return links
}
}

View File

@ -1,6 +1,3 @@
import {getAllHosts as getAllHostsAJAX} from 'src/hosts/apis'
import {GetAllHosts} from 'src/types/apis/hosts'
import {Source} from 'src/types/sources'
import {HostNames, HostName} from 'src/types/hosts'
import {DashboardSwitcherLinks} from 'src/types/dashboards'
@ -10,18 +7,7 @@ export const EMPTY_LINKS = {
active: null,
}
const hostNamesAJAX = getAllHostsAJAX as GetAllHosts
export const loadHostsLinks = async (
source: Source,
getHostNamesAJAX: GetAllHosts = hostNamesAJAX
): Promise<DashboardSwitcherLinks> => {
const hostNames = await getHostNamesAJAX(source)
return linksFromHosts(hostNames, source)
}
const linksFromHosts = (
export const linksFromHosts = (
hostNames: HostNames,
source: Source
): DashboardSwitcherLinks => {
@ -37,10 +23,10 @@ const linksFromHosts = (
}
export const updateActiveHostLink = (
dashboardLinks: DashboardSwitcherLinks,
hostLinks: DashboardSwitcherLinks,
host: HostName
) => {
const active = dashboardLinks.links.find(link => link.key === host.name)
): DashboardSwitcherLinks => {
const active = hostLinks.links.find(link => link.key === host.name)
return {...dashboardLinks, active}
return {...hostLinks, active}
}

View File

@ -1,4 +0,0 @@
import {HostNames} from 'src/types/hosts'
import {Source} from 'src/types/sources'
export type GetAllHosts = (source: Source) => Promise<HostNames>

View File

@ -1,12 +1,12 @@
import {
loadHostsLinks,
updateActiveHostLink,
linksFromHosts,
} from 'src/hosts/utils/hostsSwitcherLinks'
import {source} from 'test/resources'
import {HostNames} from 'src/types/hosts'
describe('hosts.utils.hostsSwitcherLinks', () => {
describe('loadHostLinks', () => {
describe('hosts.utils.hostSwitcherLinks', () => {
describe('linksFromHosts', () => {
const socure = {...source, id: '897'}
const hostNames: HostNames = {
@ -18,10 +18,8 @@ describe('hosts.utils.hostsSwitcherLinks', () => {
},
}
const getHostsAJAX = async () => hostNames
it('can load host links for a given source', async () => {
const actualLinks = await loadHostsLinks(socure, getHostsAJAX)
it('can build host links for a given source', () => {
const actualLinks = linksFromHosts(hostNames, socure)
const expectedLinks = {
links: [