forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
React version of angular license view (elastic#111317)
* React version of angular license view * Fix time handling and linking to license upload * Use getPageData pattern instead of useClusters * Add note about locked time picker * Add disable support to monitoring toolbar * Disable toolbar on license page only * Remove old todo * Clean up render setup method ordering * Fix CI checks Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
13b8e40
commit f57f6b2
Showing
5 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
x-pack/plugins/monitoring/public/application/pages/license_page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { useContext, useState, useCallback, useEffect } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import moment from 'moment-timezone'; | ||
import { PageTemplate } from './page_template'; | ||
import { License } from '../../components'; | ||
import { GlobalStateContext } from '../global_state_context'; | ||
import { CODE_PATH_LICENSE, STANDALONE_CLUSTER_CLUSTER_UUID } from '../../../common/constants'; | ||
import { useKibana } from '../../../../../../src/plugins/kibana_react/public'; | ||
import { MonitoringTimeContainer } from './use_monitoring_time'; | ||
|
||
const CODE_PATHS = [CODE_PATH_LICENSE]; | ||
|
||
export const LicensePage: React.FC<{}> = () => { | ||
const title = i18n.translate('xpack.monitoring.license.licenseRouteTitle', { | ||
defaultMessage: 'License', | ||
}); | ||
|
||
const { setIsDisabled } = useContext(MonitoringTimeContainer.Context); | ||
useEffect(() => { | ||
setIsDisabled(true); | ||
return () => { | ||
setIsDisabled(false); | ||
}; | ||
}, [setIsDisabled]); | ||
|
||
const state = useContext(GlobalStateContext); | ||
const clusterUuid = state.cluster_uuid; | ||
const ccs = state.ccs; | ||
const [clusters, setClusters] = useState([] as any); | ||
|
||
const { services } = useKibana<{ data: any }>(); | ||
const timezone = services.uiSettings?.get('dateFormat:tz'); | ||
const uploadLicensePath = services.application?.getUrlForApp('management', { | ||
path: 'stack/license_management/upload_license', | ||
}); | ||
|
||
const getPageData = useCallback(async () => { | ||
const bounds = services.data?.query.timefilter.timefilter.getBounds(); | ||
let url = '../api/monitoring/v1/clusters'; | ||
if (clusterUuid) { | ||
url += `/${clusterUuid}`; | ||
} | ||
|
||
try { | ||
const response = await services.http?.fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
ccs, | ||
timeRange: { | ||
min: bounds.min.toISOString(), | ||
max: bounds.max.toISOString(), | ||
}, | ||
codePaths: CODE_PATHS, | ||
}), | ||
}); | ||
|
||
setClusters(formatClusters(response)); | ||
} catch (err) { | ||
// TODO handle error | ||
} | ||
}, [ccs, clusterUuid, services.data?.query.timefilter.timefilter, services.http]); | ||
|
||
return ( | ||
<PageTemplate title={title} pageTitle="" getPageData={getPageData}> | ||
{licenseComponent(clusters, timezone, uploadLicensePath)} | ||
</PageTemplate> | ||
); | ||
}; | ||
|
||
function licenseComponent( | ||
clusters: any, | ||
timezone: string, | ||
uploadLicensePath: string | undefined | ||
): any { | ||
if (clusters.length) { | ||
const cluster = clusters[0]; | ||
const isPrimaryCluster = cluster.isPrimary; | ||
const license = cluster.license; | ||
let expiryDate = license?.expiry_date_in_millis; | ||
|
||
if (expiryDate !== undefined) { | ||
expiryDate = formatDateTimeLocal(expiryDate, timezone); | ||
} | ||
|
||
const isExpired = Date.now() > expiryDate; | ||
|
||
return ( | ||
<License | ||
isPrimaryCluster={isPrimaryCluster} | ||
status={license.status} | ||
type={license.type} | ||
isExpired={isExpired} | ||
expiryDate={expiryDate} | ||
uploadLicensePath={uploadLicensePath} | ||
/> | ||
); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
// From x-pack/plugins/monitoring/common/formatting.ts with corrected typing | ||
// TODO open github issue to correct other usages | ||
export function formatDateTimeLocal(date: number | Date, timezone: string | null) { | ||
return moment.tz(date, timezone || moment.tz.guess()).format('LL LTS'); | ||
} | ||
|
||
function formatClusters(clusters: any) { | ||
return clusters.map(formatCluster); | ||
} | ||
|
||
function formatCluster(cluster: any) { | ||
if (cluster.cluster_uuid === STANDALONE_CLUSTER_CLUSTER_UUID) { | ||
cluster.cluster_name = 'Standalone Cluster'; | ||
} | ||
return cluster; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
*/ | ||
|
||
export const PageLoading: FunctionComponent<Props>; | ||
export const License: FunctionComponent<Props>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters