// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import React, { useState, useEffect } from "react"; import Button from "aws-northstar/components/Button"; import Modal from "aws-northstar/components/Modal"; import LoadingIndicator from "aws-northstar/components/LoadingIndicator"; import Grid from "aws-northstar/layouts/Grid"; import Inline from "aws-northstar/layouts/Inline"; import Icon from "aws-northstar/components/Icon"; import StatusIndicator from "aws-northstar/components/StatusIndicator"; import PieChart, { Pie, Cell, NORTHSTAR_COLORS, Legend, LabelList, } from "aws-northstar/charts/PieChart"; import { AGENT_NAME, genLogger, LAMBDA_PREFIX } from "../lib"; import { useInterval } from "../hooks"; const name = "StatisticsModal"; const { error } = genLogger(name); // Most visible NorthStar colors const COLORS_ARRAY = [ NORTHSTAR_COLORS.ORANGE, NORTHSTAR_COLORS.ORANGE_LIGHT, NORTHSTAR_COLORS.BLUE_DARK, NORTHSTAR_COLORS.GREEN_DARK, NORTHSTAR_COLORS.RED, ]; const buildUrl = (method) => `https://${LAMBDA_PREFIX}.execute-api.us-east-1.amazonaws.com/${method}`; const transformDataForPie = (data) => { if (data.length > 0) { const intents = data.map(({ intent }) => intent); const keys = [...new Set(intents)]; const newObjects = keys.map((name) => ({ name, value: 0 })); intents.forEach((i) => newObjects.find(({ name }) => name === i).value++); return newObjects; } }; const getPieChartData = () => fetch( `https://${LAMBDA_PREFIX}.execute-api.us-east-1.amazonaws.com/getIntentsForAgent?agentName=${AGENT_NAME}` ) .then((res) => res.json()) .then(transformDataForPie); const getStatData = (method) => fetch(buildUrl(method)).then((res) => { return res.text(); }); const StatItem = ({ text, icon, method }) => { const [data, setData] = useState(null); const date = useInterval(5000); useEffect(() => { const asyncFunc = async () => { try { const data = await getStatData(method); setData(data); } catch (e) { setData("Error"); } }; asyncFunc(); }, [date, method]); return (
{icon && } {data === null ? ( ) : data instanceof Error || data === "Error" ? ( Error ) : ( {String(data)} )} {text}
); }; const StatisticsModal = () => { const [visible, setVisible] = useState(false); const [pieData, setPieData] = useState([]); const date = useInterval(5000); useEffect(() => { let isCancelled = false; const asyncFunc = async () => { try { const d = await getPieChartData(); if (Array.isArray(d)) { const newD = d.map((obj) => { if (typeof obj.name === "undefined") obj.name = "unknown"; return obj; }); if (!isCancelled) { setPieData(newD); } else { throw new Error("pie chart data not an array"); } } } catch (e) { if (!isCancelled) error(e); } }; asyncFunc(); return () => (isCancelled = true); }, [date]); return ( <> Contact Center Stats } visible={visible} onClose={() => setVisible(false)} width="90%" >
{visible && ( {pieData.length > 0 && pieData.map((_, i) => ( ))} )}
); }; export default StatisticsModal;