Browse Source

still wip

pull/1/head
rozetko 1 year ago
parent
commit
509b887d84
  1. 1
      .gitignore
  2. 13
      src/components/PluginConfigPage/PluginConfigPage.tsx
  3. 57
      src/components/PluginConfigPage/parts/ConfigurationForm/index.tsx
  4. 19
      src/plugin_state.ts

1
.gitignore vendored

@ -1,2 +1,3 @@
node_modules node_modules
dist dist
.eslintcache

13
src/components/PluginConfigPage/PluginConfigPage.tsx

@ -50,6 +50,7 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({
const pluginConfiguredQueryParam = queryParams.get(PLUGIN_CONFIGURED_QUERY_PARAM); const pluginConfiguredQueryParam = queryParams.get(PLUGIN_CONFIGURED_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);
@ -74,6 +75,7 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({
* Supplying the env var basically allows to skip the configuration form * Supplying the env var basically allows to skip the configuration form
* (check webpack.config.js to see how this is set) * (check webpack.config.js to see how this is set)
*/ */
console.log(pluginMetaDataExporterApiUrl);
if (!pluginMetaDataExporterApiUrl) { if (!pluginMetaDataExporterApiUrl) {
/** /**
* DataExporterApiUrl is not yet saved in the grafana plugin settings, but has been supplied as an env var * DataExporterApiUrl is not yet saved in the grafana plugin settings, but has been supplied as an env var
@ -94,6 +96,8 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({
if (dataExporterApiUrl) { if (dataExporterApiUrl) {
const pluginConnectionResponse = await PluginState.checkIfPluginIsConnected(dataExporterApiUrl); const pluginConnectionResponse = await PluginState.checkIfPluginIsConnected(dataExporterApiUrl);
console.log(pluginConnectionResponse);
if (typeof pluginConnectionResponse === 'string') { if (typeof pluginConnectionResponse === 'string') {
setPluginConnectionCheckError(pluginConnectionResponse); setPluginConnectionCheckError(pluginConnectionResponse);
} }
@ -102,7 +106,7 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({
}; };
/** /**
* don't check the plugin status (or trigger a data sync) 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) {
@ -167,12 +171,7 @@ export const PluginConfigPage: FC<DataExporterPluginConfigPageProps> = ({
</> </>
); );
} else if (!pluginIsConnected) { } else if (!pluginIsConnected) {
content = ( content = <ConfigurationForm onSuccessfulSetup={onSuccessfulSetup} defaultDataExporterApiUrl={DEFAULT_API_URL} />;
<ConfigurationForm
onSuccessfulSetup={onSuccessfulSetup}
defaultDataExporterApiUrl={DEFAULT_API_URL}
/>
);
} else { } else {
// plugin is fully connected and synced // plugin is fully connected and synced
content = <RemoveConfigButton />; content = <RemoveConfigButton />;

57
src/components/PluginConfigPage/parts/ConfigurationForm/index.tsx

@ -49,17 +49,12 @@ const FormErrorMessage: FC<{ errorMsg: string }> = ({ errorMsg }) => (
> >
<Text type="secondary"> <Text type="secondary">
Need help? Need help?
<br />- Reach out to the OnCall team in the{' '} <br />- file bugs on our GitHub Issues page{' '}
<a href="https://grafana.slack.com/archives/C02LSUUSE2G" target="_blank" rel="noreferrer"> <a
<Text type="link">#grafana-oncall</Text> href="https://code.corpglory.net/corpglory/grafana-data-exporter-app/issues"
</a>{' '} target="_blank"
community Slack channel rel="noreferrer"
<br />- Ask questions on our GitHub Discussions page{' '} >
<a href="https://github.com/grafana/oncall/discussions/categories/q-a" target="_blank" rel="noreferrer">
<Text type="link">here</Text>
</a>{' '}
<br />- Or file bugs on our GitHub Issues page{' '}
<a href="https://github.com/grafana/oncall/issues" target="_blank" rel="noreferrer">
<Text type="link">here</Text> <Text type="link">here</Text>
</a> </a>
</Text> </Text>
@ -71,25 +66,24 @@ const ConfigurationForm: FC<Props> = ({ onSuccessfulSetup, defaultDataExporterAp
const [setupErrorMsg, setSetupErrorMsg] = useState<string | null>(null); const [setupErrorMsg, setSetupErrorMsg] = useState<string | null>(null);
const [formLoading, setFormLoading] = useState<boolean>(false); const [formLoading, setFormLoading] = useState<boolean>(false);
const setupPlugin: SubmitHandler<FormProps> = useCallback(async ({ dataExporterApiUrl }) => { const setupPlugin: SubmitHandler<FormProps> = useCallback(
setFormLoading(true); async ({ dataExporterApiUrl }) => {
setFormLoading(true);
const errorMsg = await PluginState.installPlugin(dataExporterApiUrl); const errorMsg = await PluginState.installPlugin(dataExporterApiUrl);
if (!errorMsg) { if (!errorMsg) {
onSuccessfulSetup(); onSuccessfulSetup();
} else { } else {
setSetupErrorMsg(errorMsg); setSetupErrorMsg(errorMsg);
setFormLoading(false); setFormLoading(false);
} }
}, []); },
[onSuccessfulSetup]
);
return ( return (
<Form<FormProps> <Form<FormProps> defaultValues={{ dataExporterApiUrl: defaultDataExporterApiUrl }} onSubmit={setupPlugin}>
defaultValues={{ dataExporterApiUrl: defaultDataExporterApiUrl }}
onSubmit={setupPlugin}
data-testid="plugin-configuration-form"
>
{({ register, errors }) => ( {({ register, errors }) => (
<> <>
<div <div
@ -98,10 +92,10 @@ const ConfigurationForm: FC<Props> = ({ onSuccessfulSetup, defaultDataExporterAp
'margin-top': '24px', 'margin-top': '24px',
})} })}
> >
<p>1. Launch the OnCall backend</p> <p>1. Launch the DataExporter backend</p>
<Text type="secondary"> <Text type="secondary">
Run hobby, dev or production backend. See{' '} Run hobby, dev or production backend. See{' '}
<a href="https://github.com/grafana/oncall#getting-started" target="_blank" rel="noreferrer"> <a href="https://code.corpglory.net/corpglory/grafana-data-exporter" target="_blank" rel="noreferrer">
<Text type="link">here</Text> <Text type="link">here</Text>
</a>{' '} </a>{' '}
on how to get started. on how to get started.
@ -114,18 +108,17 @@ const ConfigurationForm: FC<Props> = ({ onSuccessfulSetup, defaultDataExporterAp
'margin-top': '24px', 'margin-top': '24px',
})} })}
> >
<p>2. Let us know the base URL of your OnCall API</p> <p>2. Let us know the base URL of your DataExporter API</p>
<Text type="secondary"> <Text type="secondary">
The OnCall backend must be reachable from your Grafana installation. Some examples are: The DataExporter backend must be reachable from your Grafana installation. Some examples are:
<br /> <br />
- http://host.docker.internal:8080 - http://host.docker.internal:8080
<br />- http://localhost:8080 <br />- http://localhost:8080
</Text> </Text>
</div> </div>
<Field label="OnCall backend URL" invalid={!!errors.dataExporterApiUrl} error="Must be a valid URL"> <Field label="DataExporter backend URL" invalid={!!errors.dataExporterApiUrl} error="Must be a valid URL">
<Input <Input
data-testid="dataExporterApiUrl"
{...register('dataExporterApiUrl', { {...register('dataExporterApiUrl', {
required: true, required: true,
validate: isValidUrl, validate: isValidUrl,

19
src/plugin_state.ts

@ -21,7 +21,9 @@ type InstallPluginResponse<DataExporterAPIResponse = any> = Pick<
dataExporterAPIResponse: DataExporterAPIResponse; dataExporterAPIResponse: DataExporterAPIResponse;
}; };
type PluginConnectedStatusResponse = string; type PluginConnectedStatusResponse = {
version: string;
};
class PluginState { class PluginState {
static DATA_EXPORTER_BASE_URL = '/plugin'; static DATA_EXPORTER_BASE_URL = '/plugin';
@ -159,13 +161,22 @@ class PluginState {
return null; return null;
}; };
static checkIfPluginIsConnected = async (DataExporterApiUrl: string): Promise<string> => { static checkIfPluginIsConnected = async (
dataExporterApiUrl: string
): Promise<PluginConnectedStatusResponse | string> => {
try { try {
return await makeRequest<PluginConnectedStatusResponse>(`${this.DATA_EXPORTER_BASE_URL}/status`, { const resp = await makeRequest<PluginConnectedStatusResponse>(`${this.DATA_EXPORTER_BASE_URL}/status`, {
method: 'GET', method: 'GET',
}); });
// TODO: check if the server version is compatible with the plugin
if (resp.version) {
return resp;
} else {
throw new Error(`Something is working at ${dataExporterApiUrl} but it's not DataExporter backend`);
}
} catch (e) { } catch (e) {
return this.getHumanReadableErrorFromDataExporterError(e, DataExporterApiUrl); return this.getHumanReadableErrorFromDataExporterError(e, dataExporterApiUrl);
} }
}; };

Loading…
Cancel
Save