Skip to content

Commit

Permalink
Merge branch 'main' into vecdb
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMcIntosh committed Feb 28, 2024
2 parents c159fa7 + 18ada88 commit 9120a7e
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 73 deletions.
24 changes: 8 additions & 16 deletions src/components/Chart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const Chart: React.FC<{
left: "3%",
right: "4%",
bottom: "3%",
top: "10%",
containLabel: true,
},
xAxis: [
Expand All @@ -68,6 +69,10 @@ export const Chart: React.FC<{
yAxis: [
{
type: "value",
name: "char.",
nameTextStyle: {
align: "right",
},
},
],
series: [
Expand All @@ -77,42 +82,29 @@ export const Chart: React.FC<{
stack: "Ad",
data: humanData,
barWidth: "80%",
itemStyle: { normal: { color: "#91cc75" } },
},
{
name: "Refact",
type: "bar",
stack: "Ad",
data: refactData,
barWidth: "80%",
itemStyle: { normal: { color: "#5470c6" } },
},
],
};

return (
<Box mt="3" width="100%">
<Text as="p" size="2">
<Text as="p" size="2" mt="5">
Refact vs Human
</Text>
<ReactEChartsCore
echarts={echarts}
option={option}
style={{ width: "100%", height: "300px" }}
/>
<Box>
{dates.map((date: string, index: number) => (
<Box key={index}>
<Text size="1" as="p">
Date: {date}
</Text>
<Text size="1" as="p">
Human: {humanData[index]} ch.
</Text>
<Text size="1" mb="2" as="p">
Refact: {refactData[index]} ch.
</Text>
</Box>
))}
</Box>
</Box>
);
};
48 changes: 48 additions & 0 deletions src/components/StatisticView/StatisticView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from "react";
import { Box, Flex, Heading } from "@radix-ui/themes";
import { Table } from "../Table/Table";
import { Chart } from "../Chart/Chart";
import { StatisticData } from "../../services/refact";
import { Spinner } from "../Spinner";
import { ErrorCallout } from "../Callout";

export const StatisticView: React.FC<{
statisticData: StatisticData | null;
isLoading: boolean;
error: string;
}> = ({ statisticData, isLoading, error }) => {
if (isLoading) {
return <Spinner />;
}

if (error || !statisticData) {
return <ErrorCallout>{error}</ErrorCallout>;
}

return (
<Box
style={{
width: "inherit",
}}
>
<Flex
direction="column"
style={{
width: "inherit",
}}
>
<Heading as="h3" align="center" mb="5">
Statistics
</Heading>
<Flex align="center" justify="center" direction="column">
<Table refactImpactTable={statisticData.table_refact_impact.data} />
<Chart
refactImpactDatesWeekly={
statisticData.refact_impact_dates.data.weekly
}
/>
</Flex>
</Flex>
</Box>
);
};
8 changes: 4 additions & 4 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { formatTableCell } from "./formatTableCell";

const convertedColumnNames: Record<ColumnName, string> = {
lang: "Lang.",
refact: "Refact",
human: "Human",
total: "Total",
refact_impact: "Refact Impact",
refact: "Refact (char.)",
human: "Human (char.)",
total: "Total (char.)",
refact_impact: "Refact Impact (%)",
completions: "Compl.",
};

Expand Down
73 changes: 73 additions & 0 deletions src/events/statistic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
export enum EVENT_NAMES_FROM_STATISTIC {
BACK_FROM_STATISTIC = "back_from_statistic",
}

export enum EVENT_NAMES_TO_STATISTIC {
REQUEST_STATISTIC_DATA = "request_statistic_data",
RECEIVE_STATISTIC_DATA = "receive_statistic_data",
RECEIVE_STATISTIC_DATA_ERROR = "receive_statistic_data_error",
SET_LOADING_STATISTIC_DATA = "set_loading_statistic_data",
}

interface BaseAction {
type: EVENT_NAMES_FROM_STATISTIC | EVENT_NAMES_TO_STATISTIC;
payload?: { data?: string; [key: string]: unknown };
}

export interface ActionToStatistic extends BaseAction {
type: EVENT_NAMES_TO_STATISTIC;
}

export interface RequestDataForStatistic extends ActionToStatistic {
type: EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA;
}

export interface SetLoadingStatisticData extends ActionToStatistic {
type: EVENT_NAMES_TO_STATISTIC.SET_LOADING_STATISTIC_DATA;
}

export function isActionToStatistic(
action: unknown,
): action is ActionToStatistic {
if (!action) return false;
if (typeof action !== "object") return false;
if (!("type" in action)) return false;
if (typeof action.type !== "string") return false;
const ALL_EVENT_NAMES: Record<string, string> = {
...EVENT_NAMES_TO_STATISTIC,
};
return Object.values(ALL_EVENT_NAMES).includes(action.type);
}

export function isRequestDataForStatistic(
action: unknown,
): action is RequestDataForStatistic {
if (!isActionToStatistic(action)) return false;
return action.type === EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA;
}

export function isReceiveDataForStatistic(
action: unknown,
): action is RequestDataForStatistic {
if (!isActionToStatistic(action)) return false;
return action.type === EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA;
}

export interface ReceiveDataForStatisticError extends ActionToStatistic {
type: EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA_ERROR;
payload: {
data: string;
message: string;
};
}

export function isReceiveDataForStatisticError(
action: unknown,
): action is ReceiveDataForStatisticError {
if (!isActionToStatistic(action)) return false;
return action.type === EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA_ERROR;
}

export function isSetLoadingStatisticData(
action: unknown,
): action is SetLoadingStatisticData {
if (!isActionToStatistic(action)) return false;
return action.type === EVENT_NAMES_TO_STATISTIC.SET_LOADING_STATISTIC_DATA;
}
58 changes: 9 additions & 49 deletions src/features/Statistic.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import React, { useEffect, useState } from "react";
import { Box, Flex, Button, Heading, Responsive } from "@radix-ui/themes";
import { RefactTableData } from "../services/refact";
import { Table } from "../components/Table/Table";
import { Chart } from "../components/Chart/Chart";
import { Spinner } from "../components/Spinner";
import React from "react";
import { Flex, Button, Responsive } from "@radix-ui/themes";
import { ArrowLeftIcon } from "@radix-ui/react-icons";
import { useConfig } from "../contexts/config-context";
import { ScrollArea } from "../components/ScrollArea";
import { TABLE } from "../__fixtures__";
import { useEventBusForStatistic } from "../hooks";
import { StatisticView } from "../components/StatisticView/StatisticView";

export const Statistic: React.FC<{
onCloseStatistic?: () => void;
}> = ({ onCloseStatistic }) => {
const [isLoaded, setIsLoaded] = useState<boolean>(false);
const [refactTable, setRefactTable] = useState<RefactTableData | null>(null);
const { host, tabbed } = useConfig();
const { backFromStatistic } = useEventBusForStatistic();
const { backFromStatistic, state } = useEventBusForStatistic();
const LeftRightPadding: Responsive<
"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
> =
Expand All @@ -37,13 +31,6 @@ export const Statistic: React.FC<{
initial: "5",
};

useEffect(() => {
if (TABLE.data) {
setRefactTable(JSON.parse(TABLE.data) as RefactTableData);
setIsLoaded(true);
}
}, []);

return (
<Flex
direction="column"
Expand Down Expand Up @@ -78,38 +65,11 @@ export const Statistic: React.FC<{
width: "inherit",
}}
>
{isLoaded ? (
<Box
style={{
width: "inherit",
}}
>
<Flex
direction="column"
style={{
width: "inherit",
}}
>
<Heading as="h3" align="center" mb="1">
Statistics
</Heading>
{refactTable !== null && (
<Flex align="center" justify="center" direction="column">
<Table
refactImpactTable={refactTable.table_refact_impact.data}
/>
<Chart
refactImpactDatesWeekly={
refactTable.refact_impact_dates.data.weekly
}
/>
</Flex>
)}
</Flex>
</Box>
) : (
<Spinner />
)}
<StatisticView
statisticData={state.statisticData}
isLoading={state.isLoading}
error={state.error}
/>
</Flex>
</ScrollArea>
</Flex>
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useEventBusForHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { useChatHistory } from "./useChatHistory";
import {
EVENT_NAMES_TO_CHAT,
EVENT_NAMES_TO_STATISTIC,
ChatThread,
isQuestionFromChat,
isSaveChatFromChat,
Expand All @@ -17,8 +18,10 @@ import {
isRequestAtCommandCompletion,
ReceiveAtCommandCompletion,
ReceiveAtCommandPreview,
isRequestDataForStatistic,
} from "../events";
import { useConfig } from "../contexts/config-context";
import { getStatisticData } from "../services/refact";

export function useEventBusForHost() {
const { lspUrl } = useConfig();
Expand Down Expand Up @@ -116,6 +119,24 @@ export function useEventBusForHost() {
console.error(error);
});
}

if (isRequestDataForStatistic(event.data)) {
getStatisticData(lspUrl)
.then((data) => {
window.postMessage({
type: EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA,
payload: data,
});
})
.catch((error: Error) => {
window.postMessage({
type: EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA_ERROR,
payload: {
message: error.message,
},
});
});
}
};

window.addEventListener("message", listener);
Expand Down
Loading

0 comments on commit 9120a7e

Please sign in to comment.