|
|
@ -4,7 +4,7 @@ import { Legend, LoadingPlaceholder } from '@grafana/ui'; |
|
|
|
import { useLocation } from 'react-router-dom'; |
|
|
|
import { useLocation } from 'react-router-dom'; |
|
|
|
|
|
|
|
|
|
|
|
// import logo from '../../img/logo.svg';
|
|
|
|
// import logo from '../../img/logo.svg';
|
|
|
|
import PluginState from '../../plugin_state'; |
|
|
|
import PluginState, { PluginConnectedStatusResponse } from '../../plugin_state'; |
|
|
|
|
|
|
|
|
|
|
|
import ConfigurationForm from './parts/ConfigurationForm'; |
|
|
|
import ConfigurationForm from './parts/ConfigurationForm'; |
|
|
|
import RemoveCurrentConfigurationButton from './parts/RemoveCurrentConfigurationButton'; |
|
|
|
import RemoveCurrentConfigurationButton from './parts/RemoveCurrentConfigurationButton'; |
|
|
@ -14,6 +14,8 @@ import { DataExporterPluginConfigPageProps } from 'types'; |
|
|
|
const PLUGIN_CONFIGURED_QUERY_PARAM = 'pluginConfigured'; |
|
|
|
const PLUGIN_CONFIGURED_QUERY_PARAM = 'pluginConfigured'; |
|
|
|
const PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE = 'true'; |
|
|
|
const PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE = 'true'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const PLUGIN_CONFIGURED_VERSION_QUERY_PARAM = 'pluginConfiguredVersion'; |
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_API_URL = 'http://localhost:8080'; |
|
|
|
const DEFAULT_API_URL = 'http://localhost:8080'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -22,9 +24,12 @@ const DEFAULT_API_URL = 'http://localhost:8080'; |
|
|
|
* |
|
|
|
* |
|
|
|
* Don't refresh the page if the plugin is already enabled.. |
|
|
|
* Don't refresh the page if the plugin is already enabled.. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export const reloadPageWithPluginConfiguredQueryParams = (pluginEnabled: boolean): void => { |
|
|
|
export const reloadPageWithPluginConfiguredQueryParams = ( |
|
|
|
|
|
|
|
{ version }: PluginConnectedStatusResponse, |
|
|
|
|
|
|
|
pluginEnabled: boolean |
|
|
|
|
|
|
|
): void => { |
|
|
|
if (!pluginEnabled) { |
|
|
|
if (!pluginEnabled) { |
|
|
|
window.location.href = `${window.location.href}?${PLUGIN_CONFIGURED_QUERY_PARAM}=${PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE}`; |
|
|
|
window.location.href = `${window.location.href}?${PLUGIN_CONFIGURED_QUERY_PARAM}=${PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE}&${PLUGIN_CONFIGURED_VERSION_QUERY_PARAM}=${version}`; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -48,71 +53,53 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({ |
|
|
|
const { search } = useLocation(); |
|
|
|
const { search } = useLocation(); |
|
|
|
const queryParams = new URLSearchParams(search); |
|
|
|
const queryParams = new URLSearchParams(search); |
|
|
|
const pluginConfiguredQueryParam = queryParams.get(PLUGIN_CONFIGURED_QUERY_PARAM); |
|
|
|
const pluginConfiguredQueryParam = queryParams.get(PLUGIN_CONFIGURED_QUERY_PARAM); |
|
|
|
|
|
|
|
const pluginConfiguredVersionQueryParam = queryParams.get(PLUGIN_CONFIGURED_VERSION_QUERY_PARAM); |
|
|
|
|
|
|
|
|
|
|
|
const pluginConfiguredRedirect = pluginConfiguredQueryParam === PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE; |
|
|
|
const pluginConfiguredRedirect = pluginConfiguredQueryParam === PLUGIN_CONFIGURED_QUERY_PARAM_TRUTHY_VALUE; |
|
|
|
console.log(pluginConfiguredRedirect); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [checkingIfPluginIsConnected, setCheckingIfPluginIsConnected] = useState<boolean>(!pluginConfiguredRedirect); |
|
|
|
const [checkingIfPluginIsConnected, setCheckingIfPluginIsConnected] = useState<boolean>(!pluginConfiguredRedirect); |
|
|
|
const [pluginConnectionCheckError, setPluginConnectionCheckError] = useState<string | null>(null); |
|
|
|
const [pluginConnectionCheckError, setPluginConnectionCheckError] = useState<string | null>(null); |
|
|
|
|
|
|
|
|
|
|
|
const [pluginIsConnected, setPluginIsConnected] = useState<Boolean | null>(pluginConfiguredRedirect); |
|
|
|
const [pluginIsConnected, setPluginIsConnected] = useState<PluginConnectedStatusResponse | null>( |
|
|
|
|
|
|
|
pluginConfiguredRedirect ? { version: pluginConfiguredVersionQueryParam as string } : null |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const [resettingPlugin, setResettingPlugin] = useState<boolean>(false); |
|
|
|
const [resettingPlugin, setResettingPlugin] = useState<boolean>(false); |
|
|
|
const [pluginResetError, setPluginResetError] = useState<string | null>(null); |
|
|
|
const [pluginResetError, setPluginResetError] = useState<string | null>(null); |
|
|
|
|
|
|
|
|
|
|
|
const pluginMetaDataExporterApiUrl = jsonData?.dataExporterApiUrl; |
|
|
|
const pluginMetaDataExporterApiUrl = jsonData?.dataExporterApiUrl; |
|
|
|
const dataExporterApiUrl = pluginMetaDataExporterApiUrl || DEFAULT_API_URL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resetQueryParams = useCallback(() => removePluginConfiguredQueryParams(pluginIsEnabled), [pluginIsEnabled]); |
|
|
|
const resetQueryParams = useCallback(() => removePluginConfiguredQueryParams(pluginIsEnabled), [pluginIsEnabled]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkConnection = useCallback(async () => { |
|
|
|
|
|
|
|
setCheckingIfPluginIsConnected(true); |
|
|
|
|
|
|
|
setPluginConnectionCheckError(null); |
|
|
|
|
|
|
|
if (!pluginMetaDataExporterApiUrl) { |
|
|
|
|
|
|
|
setCheckingIfPluginIsConnected(false); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const pluginConnectionResponse = await PluginState.checkIfPluginIsConnected(pluginMetaDataExporterApiUrl); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof pluginConnectionResponse === 'string') { |
|
|
|
|
|
|
|
setPluginConnectionCheckError(pluginConnectionResponse); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
setPluginIsConnected(pluginConnectionResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setCheckingIfPluginIsConnected(false); |
|
|
|
|
|
|
|
}, [pluginMetaDataExporterApiUrl]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(resetQueryParams, [resetQueryParams]); |
|
|
|
useEffect(resetQueryParams, [resetQueryParams]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
const configurePluginAndSyncData = async () => { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* If the plugin has never been configured, DataExporterApiUrl will be undefined in the plugin's jsonData |
|
|
|
|
|
|
|
* In that case, check to see if DataExporter_API_URL has been supplied as an env var. |
|
|
|
|
|
|
|
* Supplying the env var basically allows to skip the configuration form |
|
|
|
|
|
|
|
* (check webpack.config.js to see how this is set) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
console.log(pluginMetaDataExporterApiUrl); |
|
|
|
|
|
|
|
if (!pluginMetaDataExporterApiUrl) { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* DataExporterApiUrl is not yet saved in the grafana plugin settings, but has been supplied as an env var |
|
|
|
|
|
|
|
* lets auto-trigger a self-hosted plugin install w/ the DataExporterApiUrl passed in as an env var |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
const errorMsg = await PluginState.installPlugin(dataExporterApiUrl); |
|
|
|
|
|
|
|
if (errorMsg) { |
|
|
|
|
|
|
|
setPluginConnectionCheckError(errorMsg); |
|
|
|
|
|
|
|
setCheckingIfPluginIsConnected(false); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* If the DataExporterApiUrl is not set in the plugin settings, and not supplied via an env var |
|
|
|
|
|
|
|
* there's no reason to check if the plugin is connected, we know it can't be |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
if (dataExporterApiUrl) { |
|
|
|
|
|
|
|
const pluginConnectionResponse = await PluginState.checkIfPluginIsConnected(dataExporterApiUrl); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(pluginConnectionResponse); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof pluginConnectionResponse === 'string') { |
|
|
|
|
|
|
|
setPluginConnectionCheckError(pluginConnectionResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
setCheckingIfPluginIsConnected(false); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* don't check the plugin status if the user was just redirected after a successful |
|
|
|
* don't check the plugin status if the user was just redirected after a successful |
|
|
|
* plugin setup |
|
|
|
* plugin setup |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
if (!pluginConfiguredRedirect) { |
|
|
|
if (!pluginConfiguredRedirect) { |
|
|
|
configurePluginAndSyncData(); |
|
|
|
checkConnection(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, [pluginMetaDataExporterApiUrl, dataExporterApiUrl, pluginConfiguredRedirect]); |
|
|
|
}, [pluginMetaDataExporterApiUrl, pluginConfiguredRedirect, checkConnection]); |
|
|
|
|
|
|
|
|
|
|
|
const resetState = useCallback(() => { |
|
|
|
const resetState = useCallback(() => { |
|
|
|
setPluginResetError(null); |
|
|
|
setPluginResetError(null); |
|
|
@ -150,10 +137,6 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({ |
|
|
|
setResettingPlugin(false); |
|
|
|
setResettingPlugin(false); |
|
|
|
}, [resetState]); |
|
|
|
}, [resetState]); |
|
|
|
|
|
|
|
|
|
|
|
const onSuccessfulSetup = useCallback(async () => { |
|
|
|
|
|
|
|
reloadPageWithPluginConfiguredQueryParams(false); |
|
|
|
|
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const RemoveConfigButton = useCallback( |
|
|
|
const RemoveConfigButton = useCallback( |
|
|
|
() => <RemoveCurrentConfigurationButton disabled={resettingPlugin} onClick={triggerPluginReset} />, |
|
|
|
() => <RemoveCurrentConfigurationButton disabled={resettingPlugin} onClick={triggerPluginReset} />, |
|
|
|
[resettingPlugin, triggerPluginReset] |
|
|
|
[resettingPlugin, triggerPluginReset] |
|
|
@ -171,7 +154,7 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({ |
|
|
|
</> |
|
|
|
</> |
|
|
|
); |
|
|
|
); |
|
|
|
} else if (!pluginIsConnected) { |
|
|
|
} else if (!pluginIsConnected) { |
|
|
|
content = <ConfigurationForm onSuccessfulSetup={onSuccessfulSetup} defaultDataExporterApiUrl={DEFAULT_API_URL} />; |
|
|
|
content = <ConfigurationForm onSuccessfulSetup={checkConnection} defaultDataExporterApiUrl={DEFAULT_API_URL} />; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// plugin is fully connected and synced
|
|
|
|
// plugin is fully connected and synced
|
|
|
|
content = <RemoveConfigButton />; |
|
|
|
content = <RemoveConfigButton />; |
|
|
|