diff --git a/ui/src/dashboards/constants/dashboardHeader.ts b/ui/src/dashboards/constants/dashboardHeader.ts new file mode 100644 index 0000000000..26a685c615 --- /dev/null +++ b/ui/src/dashboards/constants/dashboardHeader.ts @@ -0,0 +1,4 @@ +export const EMPTY_LINKS = { + links: [], + active: null, +} diff --git a/ui/src/hosts/apis/index.js b/ui/src/hosts/apis/index.js index cad41f831c..41cf25b8b8 100644 --- a/ui/src/hosts/apis/index.js +++ b/ui/src/hosts/apis/index.js @@ -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', diff --git a/ui/src/hosts/containers/HostPage.js b/ui/src/hosts/containers/HostPage.js index 83e14aea82..f2812c6381 100644 --- a/ui/src/hosts/containers/HostPage.js +++ b/ui/src/hosts/containers/HostPage.js @@ -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 } } diff --git a/ui/src/hosts/utils/hostsSwitcherLinks.ts b/ui/src/hosts/utils/hostsSwitcherLinks.ts index af8f8401f5..6c50e0a14a 100644 --- a/ui/src/hosts/utils/hostsSwitcherLinks.ts +++ b/ui/src/hosts/utils/hostsSwitcherLinks.ts @@ -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 => { - 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} } diff --git a/ui/src/types/apis/hosts.ts b/ui/src/types/apis/hosts.ts deleted file mode 100644 index 86fb4b1900..0000000000 --- a/ui/src/types/apis/hosts.ts +++ /dev/null @@ -1,4 +0,0 @@ -import {HostNames} from 'src/types/hosts' -import {Source} from 'src/types/sources' - -export type GetAllHosts = (source: Source) => Promise diff --git a/ui/test/hosts/utils/hostsSwitcherLinks.test.ts b/ui/test/hosts/utils/hostsSwitcherLinks.test.ts index acb20e7e10..88e396d9f5 100644 --- a/ui/test/hosts/utils/hostsSwitcherLinks.test.ts +++ b/ui/test/hosts/utils/hostsSwitcherLinks.test.ts @@ -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: [