UNPKG

@nitin15j/plugin-dora-frontend

Version:
75 lines (72 loc) 2.6 kB
import { useState, useCallback, useEffect } from 'react'; import { useApi } from '@backstage/core-plugin-api'; import { doraBackendApiRef } from '../../../../api/DORABackendClient.esm.js'; import { processMetricsResponse, processInsights } from '../../common/utils/processors.esm.js'; import { useDoraConfig, getEntityConfig } from '../../common/utils/config.esm.js'; function useEntityMetrics(entity) { const doraApi = useApi(doraBackendApiRef); const [metrics, setMetrics] = useState([]); const [insights, setInsights] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [selectedTimeRange, setSelectedTimeRange] = useState("last28days"); const baseConfig = useDoraConfig(); const { projectSlug, environment, deployment } = getEntityConfig(entity, baseConfig); const fetchMetrics = useCallback(async (timeRange) => { setLoading(true); try { const result = await doraApi.getProjectDORAMetrics( projectSlug, environment, deployment, timeRange ); if (!result || !result.project) { throw new Error("No metrics data available"); } const project = result.project; const metricsData = processMetricsResponse(project, true); setMetrics(metricsData); setError(null); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch metrics"); } finally { setLoading(false); } }, [doraApi, projectSlug, environment, deployment]); const fetchMetricsInsights = useCallback(async (timeRange) => { setLoading(true); try { const result = await doraApi.getProjectDORAMetricsInsights( projectSlug, environment, deployment, timeRange ); if (!result?.project?.insights) { throw new Error("No insights data available"); } const processedInsights = processInsights(result.project.insights); setInsights(processedInsights); setError(null); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch insights"); } finally { setLoading(false); } }, [doraApi, projectSlug, environment, deployment]); useEffect(() => { fetchMetrics(selectedTimeRange); fetchMetricsInsights(selectedTimeRange); }, [entity, fetchMetrics, fetchMetricsInsights, selectedTimeRange]); return { metrics, insights, loading, error, selectedTimeRange, setSelectedTimeRange }; } export { useEntityMetrics }; //# sourceMappingURL=useEntityMetrics.esm.js.map