/* * Copyright 2020 EPAM Systems * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React, { useEffect, useMemo, useState } from "react"; import { Menu, sendAlertEvent } from "@drill4j/ui-kit"; import { Link } from "react-router-dom"; import { percentFormatter } from "@drill4j/common-utils"; import tw, { styled } from "twin.macro"; import axios from "axios"; import { PLUGIN_ID, getGroupModalPath, test2CodePluginSocket, } from "common"; import { List, ListColumn, Modals } from "components"; import { ServiceGroupSummary } from "types/service-group-summary"; import { useGroupData, useGroupRouteParams } from "hooks"; // eslint-disable-next-line import/named import { routesWithoutAdminPath } from "hooks/use-navigation"; import { ScopeSummary } from "types"; import { AgentInfo } from "@drill4j/types-admin"; import { TestToCodeNameCell } from "./test-to-code-name-cell"; import { TestToCodeCoverageCell } from "./test-to-code-coverage-cell"; import { TestToCodeCell } from "./test-to-code-cell"; import { TestToCodeHeaderCell } from "./test-to-code-header-cell"; export interface GroupRootComponentProps { getAgentPluginPath: (props: { agentId: string; path?: string}) => string; getAgentDashboardPath: (props: { agentId: string;}) => string; openSettingsPanel: (agent: AgentInfo) => void; } export const Group = ({ getAgentPluginPath, openSettingsPanel, getAgentDashboardPath }: GroupRootComponentProps) => { const { groupId } = useGroupRouteParams(); const { summaries = [], aggregated } = useGroupData("/group/summary", groupId) || {}; const serviceGroupSummaries = summaries.map((agentSummary) => ({ ...agentSummary, ...agentSummary.summary, })); const [groupScopes, setGroupScopes] = useState<{[key: string]: ScopeSummary[]}>({}); useEffect(() => { function handleDataChange(newGroupScopes: ScopeSummary[], agentId: string) { if (Array.isArray(newGroupScopes)) { setGroupScopes((prevGroupScopes) => ({ ...prevGroupScopes, [agentId]: newGroupScopes })); } } const unsubscribe = summaries.map(({ id: agentId = "", buildVersion = "" }) => test2CodePluginSocket.subscribe( "/build/scopes/finished", (newData) => handleDataChange(newData, agentId), { agentId, buildVersion, type: "AGENT", }, )); return () => { unsubscribe.forEach((unsub) => unsub()); }; }, [summaries.length]); const toggleGroupScopes = async (enabled: boolean) => { try { await Promise.allSettled(Object.keys(groupScopes).map((agentId) => groupScopes[agentId] .filter((scope) => scope.enabled === enabled) .map((scope) => axios.post(`/agents/${agentId}/plugins/${PLUGIN_ID}/dispatch-action`, { type: "TOGGLE_SCOPE", payload: { scopeId: scope.id }, })))); sendAlertEvent({ type: "SUCCESS", title: `Group scopes have been ${enabled ? "ignored" : "included"} in build stats.`, }); } catch (error) { sendAlertEvent({ type: "ERROR", title: "There is some issue with your action. Please try again later.", }); } }; return (
( )} HeaderCell={() => (
Test2Code
Online Agents {summaries.length}
)} /> } HeaderCell={() => ( )} /> } HeaderCell={() => } /> ( )} HeaderCell={() => ( )} /> ( null, Content: ({ children }: { children: JSX.Element }) => ( {children} ), }, { label: "Settings", icon: "Settings", onClick: () => null, Content: ({ children }: { children: JSX.Element }) => ( openSettingsPanel(agent)}> {children} ), }, ]} /> )} HeaderCell={() => } />
); }; const MenuWrapper = styled.div` ${tw`flex justify-end mr-4`} `; interface MenuHeaderCellProps { toggleGroupScopes: (value: boolean) => void } const MenuHeaderCell = ({ toggleGroupScopes }: MenuHeaderCellProps) => { const items = useMemo(() => [ { label: "Finish all scopes", icon: "Check", onClick: () => null, Content: ({ children }: { children: JSX.Element }) => ( {children} ), }, { label: "Ignore in stats", icon: "EyeCrossed", onClick: () => toggleGroupScopes(true), }, { label: "Include in stats", icon: "Eye", onClick: () => toggleGroupScopes(false), }, { label: "Sessions Management", icon: "ManageSessions", onClick: () => null, Content: ({ children }: { children: JSX.Element }) => ( {children} ), }, ] as any[], []); return ( ); };