Skip to content

Commit

Permalink
added events for eventBusForHost for getting data for statistic from …
Browse files Browse the repository at this point in the history
…endpoint
  • Loading branch information
ViktoriiaNakoryk committed Feb 24, 2024
1 parent b8d42a7 commit b9bdadb
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 19 deletions.
45 changes: 45 additions & 0 deletions src/events/statistic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
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",
}

interface BaseAction {
type: EVENT_NAMES_FROM_STATISTIC | EVENT_NAMES_TO_STATISTIC;
payload?: { data: string };
}

export interface ActionToStatistic extends BaseAction {
type: EVENT_NAMES_TO_STATISTIC;
}
export interface RequestDataForStatistic extends ActionToStatistic {
type: EVENT_NAMES_TO_STATISTIC.REQUEST_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;
}
26 changes: 11 additions & 15 deletions src/features/Statistic.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
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 { 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";

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

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

return (
<Flex
Expand Down Expand Up @@ -78,7 +74,9 @@ export const Statistic: React.FC<{
width: "inherit",
}}
>
{isLoaded ? (
{isLoading ? (
<Spinner />
) : (
<Box
style={{
width: "inherit",
Expand All @@ -93,22 +91,20 @@ export const Statistic: React.FC<{
<Heading as="h3" align="center" mb="1">
Statistics
</Heading>
{refactTable !== null && (
{statisticData !== null && (
<Flex align="center" justify="center" direction="column">
<Table
refactImpactTable={refactTable.table_refact_impact.data}
refactImpactTable={statisticData.table_refact_impact.data}
/>
<Chart
refactImpactDatesWeekly={
refactTable.refact_impact_dates.data.weekly
statisticData.refact_impact_dates.data.weekly
}
/>
</Flex>
)}
</Flex>
</Box>
) : (
<Spinner />
)}
</Flex>
</ScrollArea>
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useEventBusForHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { sendChat, getCaps, ChatContextFile } from "../services/refact";
import { useChatHistory } from "./useChatHistory";
import {
EVENT_NAMES_TO_CHAT,
EVENT_NAMES_TO_STATISTIC,
ChatThread,
isQuestionFromChat,
isSaveChatFromChat,
isRequestCapsFromChat,
isStopStreamingFromChat,
isRequestForFileFromChat,
isRequestDataForStatistic,
} from "../events";
import { useConfig } from "../contexts/config-context";
import { getStatisticData } from "../services/refact";

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

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
45 changes: 42 additions & 3 deletions src/hooks/useEventBusForStatistic.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
import { EVENT_NAMES_FROM_STATISTIC } from "../events";
import {
EVENT_NAMES_FROM_STATISTIC,
EVENT_NAMES_TO_STATISTIC,
isReceiveDataForStatistic,
} from "../events";
import { usePostMessage } from "./usePostMessage";
import { useEffect, useState } from "react";
import { StatisticData } from "../services/refact";

export const useEventBusForStatistic = () => {
const postMessage = usePostMessage();
const [statisticData, setStatisticData] = useState<StatisticData | null>(
null,
);

function backFromStatistic() {
const backFromStatistic = () => {
postMessage({
type: EVENT_NAMES_FROM_STATISTIC.BACK_FROM_STATISTIC,
});
}
};

useEffect(() => {
const listener = (event: MessageEvent) => {
if (isReceiveDataForStatistic(event.data)) {
if (event.data.payload !== undefined) {
const parsedData = JSON.parse(
event.data.payload.data,
) as StatisticData;
setStatisticData(parsedData);
}
}
};

window.addEventListener("message", listener);

return () => {
window.removeEventListener("message", listener);
};
}, []);

useEffect(() => {
const requestStatisticData = () => {
postMessage({
type: EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA,
});
};

requestStatisticData();
}, [postMessage]);

return {
backFromStatistic,
statisticData,
};
};
34 changes: 33 additions & 1 deletion src/services/refact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getApiKey } from "../utils/ApiKey";
const CHAT_URL = `/v1/chat`;
const CAPS_URL = `/v1/caps`;
const STATISTIC_URL = `/v1/get-dashboard-plots`;

export type ChatRole = "user" | "assistant" | "context_file" | "system";

Expand Down Expand Up @@ -154,6 +155,37 @@ export async function getCaps(lspUrl?: string): Promise<CapsResponse> {
return json;
}

export function isStatisticDataResponse(
json: unknown,
): json is { data: string } {
if (!json || typeof json !== "object") return false;
return typeof (json as { data?: unknown }).data === "string";
}

export async function getStatisticData(
lspUrl?: string,
): Promise<{ data: string }> {
const statisticDataEndpoint = lspUrl
? `${lspUrl.replace(/\/*$/, "")}${STATISTIC_URL}`
: STATISTIC_URL;
const response = await fetch(statisticDataEndpoint, {
method: "GET",
credentials: "same-origin",
headers: {
accept: "application/json",
},
});
if (!response.ok) {
throw new Error(response.statusText);
}

const json: unknown = await response.json();
if (!isStatisticDataResponse(json)) {
throw new Error("Invalid response for statistic data");
}
return json;
}

type CodeChatModel = {
default_scratchpad: string;
n_ctx: number;
Expand Down Expand Up @@ -211,7 +243,7 @@ export type RefactTableImpactDateObj = {
export type RefactTableImpactLanguagesRow = {
[key in ColumnName]: string | number;
};
export type RefactTableData = {
export type StatisticData = {
refact_impact_dates: {
data: {
daily: Record<string, RefactTableImpactDateObj>;
Expand Down

0 comments on commit b9bdadb

Please sign in to comment.