Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
feat: all request page with API and UI integration (#32)
Aman Agarwalc4f6ef21ba42
diff --git a/src/client/src/app/(playground)/dashboard/page.tsx b/src/client/src/app/(playground)/dashboard/page.tsxindex 77e1ae1f..3372196c 100644--- a/src/client/src/app/(playground)/dashboard/page.tsx+++ b/src/client/src/app/(playground)/dashboard/page.tsx@@ -1,5 +1,5 @@"use client";-import Filter from "./filter";+import Filter from "../filter";import ModelsCategories from "./models-categories";import NumberStats from "./number-stats";import RequestsPerTime from "./requests-per-time";@@ -8,7 +8,7 @@ export default function PlaygroundPage() {return (<div className="flex flex-col grow w-full h-full rounded overflow-hidden"><Filter />- <div className="flex flex-col p-2 grow w-full h-full rounded overflow-y-auto">+ <div className="flex flex-col p-2 grow w-full h-full rounded overflow-y-auto mt-2"><NumberStats /><div className="flex flex-col gap-6"><RequestsPerTime />diff --git a/src/client/src/app/(playground)/filter-context.tsx b/src/client/src/app/(playground)/filter-context.tsxindex 82af6488..f4071b76 100644--- a/src/client/src/app/(playground)/filter-context.tsx+++ b/src/client/src/app/(playground)/filter-context.tsx@@ -19,10 +19,12 @@ export interface FilterType {end?: Date;type: keyof typeof TIME_RANGE_TYPE;};+ limit: number;+ offset: number;}const FilterContext = createContext<- [FilterType, (key: string, value: string) => void] | undefined+ [FilterType, (key: string, value: any) => void] | undefined>(undefined);function getTimeLimitObject(value: string, keyPrefix: string) {@@ -53,22 +55,28 @@ const INITIAL_FILTER: FilterType = {type: DEFAULT_TIME_RANGE,...getTimeLimitObject(DEFAULT_TIME_RANGE, ""),},+ limit: 10,+ offset: 0,};export function FilterProvider({ children }: { children: ReactNode }) {const [filter, setFilter] = useState(INITIAL_FILTER);- const updateFilter = (key: string, value: string) => {+ const updateFilter = (key: string, value: any) => {let object = {};switch (key) {case "timeLimit.type":object = getTimeLimitObject(value, "timeLimit.");- set(object, key, value);-+ break;+ case "limit":+ set(object, "offset", 0);+ break;+ case "offset":break;default:break;}+ set(object, key, value);setFilter((e) => merge({}, e, object));};diff --git a/src/client/src/app/(playground)/dashboard/filter.tsx b/src/client/src/app/(playground)/filter.tsxsimilarity index 93%rename from src/client/src/app/(playground)/dashboard/filter.tsxrename to src/client/src/app/(playground)/filter.tsxindex 3bd2c254..cab8972a 100644--- a/src/client/src/app/(playground)/dashboard/filter.tsx+++ b/src/client/src/app/(playground)/filter.tsx@@ -3,7 +3,7 @@ import {DEFAULT_TIME_RANGE,TIME_RANGE_TYPE,useFilter,-} from "../filter-context";+} from "./filter-context";const TIME_RANGE_TABS: { key: string; label: string }[] = Object.keys(TIME_RANGE_TYPE@@ -24,7 +24,7 @@ const Filter = () => {};return (- <div className="flex mb-2 p-2">+ <div className="flex p-2"><TabGrouponIndexChange={handleChange}defaultIndex={DEFAULT_CHECKED_INDEX}diff --git a/src/client/src/app/(playground)/layout.tsx b/src/client/src/app/(playground)/layout.tsxindex fc7979d0..ca5f46e7 100644--- a/src/client/src/app/(playground)/layout.tsx+++ b/src/client/src/app/(playground)/layout.tsx@@ -11,7 +11,7 @@ export default function PlaygroundLayout({<div className="h-screen w-screen overflow-hidden flex"><Sidebar /><FilterProvider>- <main className="flex flex-col p-3 w-full h-full">+ <main className="flex flex-col p-3 w-full h-full overflow-hidden"><Header />{children}</main>diff --git a/src/client/src/app/(playground)/requests/page.tsx b/src/client/src/app/(playground)/requests/page.tsxnew file mode 100644index 00000000..4b0e615b--- /dev/null+++ b/src/client/src/app/(playground)/requests/page.tsx@@ -0,0 +1,47 @@+"use client";+import RequestTable from "./request-table";+import { useFilter } from "../filter-context";+import { useCallback, useEffect, useState } from "react";+import { getData } from "@/utils/api";+import RequestFilter, { FilterConfigProps } from "./request-filter";+import { RequestProvider } from "./request-context";++export default function RequestPage() {+ const [filter] = useFilter();+ const [data, setData] = useState<Array<any>>([]);+ const [config, setConfig] = useState<FilterConfigProps | undefined>();+ const fetchData = useCallback(async () => {+ const res = await getData({+ body: JSON.stringify({+ timeLimit: filter.timeLimit,+ // TODO: send config true based on if the config has not already been fetched or when the timeLimit is changed+ config: {+ endpoints: true,+ maxUsageCost: true,+ models: true,+ totalRows: true,+ },+ limit: filter.limit,+ offset: filter.offset,+ }),+ method: "POST",+ url: "/api/metrics/request",+ });++ if (res?.config) setConfig(res.config);+ setData(res?.records || []);+ }, [filter]);++ useEffect(() => {+ if (filter.timeLimit.start && filter.timeLimit.end) fetchData();+ }, [filter, fetchData]);++ return (+ <RequestProvider>+ <div className="flex flex-col grow w-full h-full rounded overflow-hidden">+ <RequestFilter config={config} />+ <RequestTable data={data} />+ </div>+ </RequestProvider>+ );+}diff --git a/src/client/src/app/(playground)/requests/request-context.tsx b/src/client/src/app/(playground)/requests/request-context.tsxnew file mode 100644index 00000000..38966896--- /dev/null+++ b/src/client/src/app/(playground)/requests/request-context.tsx@@ -0,0 +1,129 @@+import { noop } from "@/utils/noop";+import { ReactNode, createContext, useContext, useState } from "react";++export const RequestMappings = {+ applicationname: {+ label: "Application Name",+ type: "string",+ },+ audiovoice: {+ label: "Audio Voice",+ type: "string",+ },+ completiontokens: {+ label: "Completion Tokens",+ type: "string",+ },+ endpoint: {+ label: "Endpoint",+ type: "string",+ },+ environment: {+ label: "Environment",+ type: "string",+ },+ finetunejobid: {+ label: "Fine Tune Job Id",+ type: "string",+ },+ finetunejobstatus: {+ label: "Fine Tune Job Status",+ type: "string",+ },+ finishreason: {+ label: "Finish Reason",+ type: "string",+ },+ image: {+ label: "Image",+ type: "string",+ },+ imagesize: {+ label: "Image Size",+ type: "string",+ },+ model: {+ label: "Model",+ type: "string",+ },+ name: {+ label: "Name",+ type: "string",+ },+ prompt: {+ label: "Prompt",+ type: "string",+ },+ prompttokens: {+ label: "Prompt Tokens",+ type: "string",+ },+ requestduration: {+ label: "Request Duration",+ type: "string",+ },+ response: {+ label: "Response",+ type: "string",+ },+ sourcelanguage: {+ label: "Source Language",+ type: "string",+ },+ time: {+ label: "Time",+ type: "string",+ },+ totaltokens: {+ label: "Total Tokens",+ type: "string",+ },+ usagecost: {+ label: "Usage Cost",+ type: "string",+ },+};++export const DisplayDataRequestMappingKeys: Array<+ keyof typeof RequestMappings+> = [+ "applicationname",+ "endpoint",+ "model",+ "requestduration",+ "prompttokens",+ "totaltokens",+ "usagecost",+ "time",+ "sourcelanguage",+];++type RequestProps = Record<keyof typeof RequestMappings, any> | null;++type RequestUpdateProps = (value: any) => void | typeof noop;++const RequestContext = createContext<[RequestProps, RequestUpdateProps]>([+ null,+ noop,+]);++export function RequestProvider({ children }: { children: ReactNode }) {+ const [request, setRequest] = useState<RequestProps>(null);+ const updateRequest = (value: RequestProps) => {+ setRequest(value);+ };++ return (+ <RequestContext.Provider value={[request, updateRequest]}>+ {children}+ </RequestContext.Provider>+ );+}++export function useRequest() {+ const context = useContext(RequestContext);+ if (context === undefined) {+ throw new Error("useRequest must be used within a RequestProvider");+ }+ return context;+}diff --git a/src/client/src/app/(playground)/requests/request-filter.tsx b/src/client/src/app/(playground)/requests/request-filter.tsxnew file mode 100644index 00000000..b5916856--- /dev/null+++ b/src/client/src/app/(playground)/requests/request-filter.tsx@@ -0,0 +1,43 @@+import Pagination from "@/components/common/pagination";+import Filter from "../filter";+import { useFilter } from "../filter-context";+import { ceil } from "lodash";++export type FilterConfigProps = {+ endpoints: string[];+ maxusagecost: number;+ models: string[];+ totalrows: number;+};++export default function RequestFilter({+ config,+}: {+ config: FilterConfigProps | undefined;+}) {+ const [filter, updateFilter] = useFilter();+ const onClickPageAction = (dir: -1 | 1) => {+ updateFilter("offset", filter.offset + dir * filter.limit);+ };++ const onClickPageLimit = (size: number) => {+ updateFilter("limit", size);+ };++ return (+ <div className="flex items-center w-full">+ <Filter />+ {config && (+ <div className="flex flex-grow justify-end pr-2">+ <Pagination+ currentPage={filter.offset / filter.limit + 1}+ currentSize={filter.limit}+ totalPage={ceil(config.totalrows / filter.limit)}+ onClickPageAction={onClickPageAction}+ onClickPageLimit={onClickPageLimit}+ />+ </div>+ )}+ </div>+ );+}diff --git a/src/client/src/app/(playground)/requests/request-table.tsx b/src/client/src/app/(playground)/requests/request-table.tsxnew file mode 100644index 00000000..432eef43--- /dev/null+++ b/src/client/src/app/(playground)/requests/request-table.tsx@@ -0,0 +1,101 @@+import {+ BeakerIcon,+ CalendarDaysIcon,+ ClipboardDocumentCheckIcon,+ ClipboardDocumentListIcon,+ ClockIcon,+ CogIcon,+ CurrencyDollarIcon,+ EyeIcon,+} from "@heroicons/react/24/outline";+import { format } from "date-fns";+import { round } from "lodash";+import { DisplayDataRequestMappingKeys, useRequest } from "./request-context";++type RenderRowProps = {+ item: Record<(typeof DisplayDataRequestMappingKeys)[number], any>;+};++const RenderRow = ({ item }: RenderRowProps) => {+ const [, updateRequest] = useRequest();++ return (+ <div className="flex flex-col mb-4">+ <div className="flex items-center border rounded-t border-b-0 py-1 px-3 z-0 self-start bg-gray-200">+ <div className="flex items-center pr-3">+ <CalendarDaysIcon className="w-4" />+ <p className="text-xs leading-none text-gray-600 ml-2">+ {format(item.time, "MMM do, Y")}+ </p>+ </div>+ <div className="flex items-center pl-3 border-l border-gray-500">+ <ClockIcon className="w-4" />+ <p className="text-xs leading-none ml-2">+ {round(item.requestduration, 4)}s+ </p>+ </div>+ </div>+ <div className="flex items-stretch h-16 border relative items-center px-3 rounded-b">+ <div className="flex items-center w-2/12">+ <p className="text-base leading-none">{item.applicationname}</p>+ </div>+ <div className="flex items-center w-3/12 px-2">+ <BeakerIcon className="w-4 mr-3" />+ <p className="text-sm leading-none mb-1">{item.endpoint}</p>+ </div>+ <div className="flex items-center w-2/12 px-2">+ <CogIcon className="w-4" />+ <p className="text-sm leading-none text-gray-600 ml-2">+ {item.model}+ </p>+ </div>+ <div className="flex items-center justify-center w-2/12 relative">+ <CurrencyDollarIcon className="w-4" />+ <p className="text-sm leading-none text-gray-600 ml-2">+ {round(item.usagecost, 6)}+ </p>+ <p className="text-xs absolute bottom-1 left-0 opacity-30 w-full text-center">+ Usage Cost+ </p>+ </div>+ <div className="flex items-center justify-center w-2/12 relative">+ <ClipboardDocumentCheckIcon className="w-4" />+ <p className="text-sm leading-none text-gray-600 ml-2">+ {item.prompttokens}+ </p>+ <p className="text-xs absolute bottom-1 left-0 opacity-30 w-full text-center">+ Prompt Tokens+ </p>+ </div>+ <div className="flex items-center justify-center w-2/12 relative">+ <ClipboardDocumentListIcon className="w-4" />+ <p className="text-sm leading-none text-gray-600 ml-2">+ {item.totaltokens}+ </p>+ <p className="text-xs absolute bottom-1 left-0 opacity-30 w-full text-center">+ Total Tokens+ </p>+ </div>+ <div className="flex items-center pl-3">+ <button type="button" onClick={() => updateRequest(item)}>+ <EyeIcon className="w-4" />+ </button>+ </div>+ </div>+ </div>+ );+};++export default function RequestTable({ data }: { data: any[] }) {+ return (+ <div className="flex flex-col p-2 w-full relative sm:rounded-lg overflow-hidden mt-3">+ <div className="overflow-auto">+ <div className="flex flex-col w-full text-sm text-left relative">+ {data.map((item, index) => (+ <RenderRow key={`item-${index}`} item={item} />+ ))}+ </div>+ </div>+ </div>+ );+}diff --git a/src/client/src/app/api/metrics/request/route.ts b/src/client/src/app/api/metrics/request/route.tsnew file mode 100644index 00000000..2ddff95a--- /dev/null+++ b/src/client/src/app/api/metrics/request/route.ts@@ -0,0 +1,37 @@+import { DokuRequestParams } from "@/lib/doku/common";+import { getRequests } from "@/lib/doku/request";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/utils/doku";++export async function POST(request: Request) {+ const formData = await request.json();+ const timeLimit = formData.timeLimit;+ const config = formData.config || {};+ const limit = formData.limit || 10;+ const offset = formData.offset || 0;++ const params: DokuRequestParams = {+ timeLimit: {+ start: timeLimit.start,+ end: timeLimit.end,+ },+ config,+ limit,+ offset,+ };++ const validationParam = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );++ if (!validationParam.success)+ return Response.json(validationParam.err, {+ status: 400,+ });++ const res: any = await getRequests(params);+ return Response.json(res);+}diff --git a/src/client/src/components/(playground)/sidebar.tsx b/src/client/src/components/(playground)/sidebar.tsxindex 4744a496..0bd8a8c6 100644--- a/src/client/src/components/(playground)/sidebar.tsx+++ b/src/client/src/components/(playground)/sidebar.tsx@@ -3,7 +3,8 @@import { signOut } from "next-auth/react";import { ReactElement } from "react";import {- ArrowLeftEndOnRectangleIcon,+ ArrowLeftCircleIcon,+ CircleStackIcon,HomeModernIcon,} from "@heroicons/react/24/solid";import Image from "next/image";@@ -17,7 +18,8 @@ type SidebarItem = {onClick?: any;};-const ICON_CLASSES = "flex-shrink-0 w-6 h-6 transition duration-75 transition duration-75";+const ICON_CLASSES =+ "flex-shrink-0 w-6 h-6 transition duration-75 transition duration-75";const SIDEBAR_ITEMS = [{@@ -26,7 +28,12 @@ const SIDEBAR_ITEMS = [link: "/dashboard",},{- icon: <ArrowLeftEndOnRectangleIcon className={ICON_CLASSES} />,+ icon: <CircleStackIcon className={ICON_CLASSES} />,+ text: "Requests",+ link: "/requests",+ },+ {+ icon: <ArrowLeftCircleIcon className={ICON_CLASSES} />,text: "Signout",onClick: signOut,},diff --git a/src/client/src/components/common/pagination.tsx b/src/client/src/components/common/pagination.tsxnew file mode 100644index 00000000..fa256569--- /dev/null+++ b/src/client/src/components/common/pagination.tsx@@ -0,0 +1,107 @@+import {+ ChevronDownIcon,+ ChevronLeftIcon,+ ChevronRightIcon,+} from "@heroicons/react/24/outline";+import { MouseEventHandler, useEffect, useRef, useState } from "react";++type PaginationProps = {+ currentPage: number;+ currentSize: number;+ totalPage: number;+ onClickPageAction: (dir: -1 | 1) => void;+ onClickPageLimit: (size: number) => void;+};++const PageSizes = [10, 25, 50];++export default function Pagination(props: PaginationProps) {+ const dropdownRef = useRef<HTMLDivElement>(null);+ const [isMenuOpen, setIsMenuOpen] = useState<boolean>(false);+ const onClickAction: MouseEventHandler = (ev) => {+ const { action } = (ev.currentTarget as HTMLButtonElement).dataset;+ if (action === "previous") {+ props.onClickPageAction(-1);+ } else {+ props.onClickPageAction(1);+ }+ };++ const onSizeChange: MouseEventHandler = (ev) => {+ const { size = "10" } = (ev.currentTarget as HTMLButtonElement).dataset;+ props.onClickPageLimit(parseInt(size, 10));+ setIsMenuOpen(false);+ };++ const handleClickOutside = (event: MouseEvent) => {+ if (+ dropdownRef.current &&+ !dropdownRef.current.contains(event.target as Node)+ ) {+ setIsMenuOpen(false);+ }+ };++ useEffect(() => {+ document.addEventListener("click", handleClickOutside);++ return () => {+ document.removeEventListener("click", handleClickOutside);+ };+ }, []);++ const firstPage = props.currentPage === 1;+ const lastPage = props.currentPage === props.totalPage;++ return (+ <div className="flex items-center">+ <p className="text-xs shrink-0 opacity-50 mr-3">Size : </p>+ <div className="relative flex items-center mr-5" ref={dropdownRef}>+ <button+ type="button"+ className="inline-flex justify-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-xs ring-1 ring-inset ring-gray-300"+ onClick={() => setIsMenuOpen((e) => !e)}+ >+ {props.currentSize}+ <ChevronDownIcon className="w-4" />+ </button>+ <div+ className={`absolute right-0 z-10 mt-2 top-full rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none w-full text-center ${+ isMenuOpen ? "flex flex-col" : "hidden"+ }`}+ >+ {PageSizes.map((size: number) => (+ <button+ key={`size-${size}`}+ className={`text-gray-700 block px-4 py-2 text-sm hover:bg-gray-50 ${+ props.currentSize === size ? "bg-gray-50" : ""+ }`}+ data-size={size}+ onClick={onSizeChange}+ >+ {size}+ </button>+ ))}+ </div>+ </div>+ <div className="flex rounded-md shadow-sm" aria-label="Pagination">+ <button+ className="relative inline-flex items-center rounded-l-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-40 disabled:cursor-not-allowed"+ data-action="previous"+ disabled={firstPage}+ onClick={onClickAction}+ >+ <ChevronLeftIcon className="w-4" />+ </button>+ <button+ className="relative inline-flex items-center rounded-r-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 disabled:opacity-40 disabled:cursor-not-allowed"+ data-action="next"+ disabled={lastPage}+ onClick={onClickAction}+ >+ <ChevronRightIcon className="w-4" />+ </button>+ </div>+ </div>+ );+}diff --git a/src/client/src/lib/doku/common.ts b/src/client/src/lib/doku/common.tsindex 214c1396..fc591142 100644--- a/src/client/src/lib/doku/common.ts+++ b/src/client/src/lib/doku/common.ts@@ -11,8 +11,21 @@ export interface DokuParams {timeLimit: TimeLimit;}+export type DokuRequestParams = DokuParams & {+ config?: {+ endpoints?: boolean;+ maxUsageCost?: boolean;+ models?: boolean;+ totalRows?: boolean;+ };+ offset?: number;+ limit?: number;+};+const prisma = new PrismaClient();-export async function dataCollector(query: string) {++export type DataCollectorType = { err?: unknown; data: unknown };+export async function dataCollector(query: string): Promise<DataCollectorType> {try {const response = await prisma.$queryRaw`${Prisma.raw(query)}`;return { data: response };diff --git a/src/client/src/lib/doku/request.ts b/src/client/src/lib/doku/request.tsindex 70f91840..3d0ea5f3 100644--- a/src/client/src/lib/doku/request.ts+++ b/src/client/src/lib/doku/request.ts@@ -1,4 +1,10 @@-import { TABLE_NAME, DokuParams, dataCollector } from "./common";+import {+ TABLE_NAME,+ DokuParams,+ dataCollector,+ DokuRequestParams,+ DataCollectorType,+} from "./common";import { differenceInDays, differenceInYears } from "date-fns";export async function getRequestPerTime(params: DokuParams) {@@ -42,3 +48,49 @@ export async function getAverageRequestDuration(params: DokuParams) {return dataCollector(query);}++export async function getRequestsConfig(params: DokuRequestParams) {+ const { start, end } = params.timeLimit;+ const { endpoints, maxUsageCost, models, totalRows } = params.config || {};++ const select = [+ endpoints && "ARRAY_AGG(DISTINCT endpoint) AS endpoints",+ maxUsageCost && "MAX(usagecost) AS maxUsageCost",+ models && "ARRAY_AGG(DISTINCT model) AS models",+ totalRows && "CAST(COUNT(*) AS INTEGER) AS totalRows",+ ]+ .filter(Boolean)+ .join(" , ");++ if (select.length === 0) return [];++ const query = `SELECT ${select} FROM ${TABLE_NAME}+ WHERE time >= '${start}' AND time <= '${end}'`;++ return dataCollector(query);+}++export async function getRequests(params: DokuRequestParams) {+ const { start, end } = params.timeLimit;+ const { limit = 10, offset = 0 } = params;+ let config: unknown = {};++ if (params.config) {+ const configValues = await getRequestsConfig(params);+ config =+ ((configValues as DataCollectorType)?.data as Array<any>)?.[0] || {};+ }++ const query = `SELECT * FROM ${TABLE_NAME}+ WHERE time >= '${start}' AND time <= '${end}'+ ORDER BY time+ LIMIT ${limit}+ OFFSET ${offset}`;++ const { data, err } = await dataCollector(query);+ return {+ err,+ config,+ records: data,+ };+}diff --git a/src/client/src/utils/doku.ts b/src/client/src/utils/doku.tsindex 4fbfe7d7..393404f3 100644--- a/src/client/src/utils/doku.ts+++ b/src/client/src/utils/doku.ts@@ -5,6 +5,7 @@ export const validateMetricsRequestType = {REQUEST_PER_TIME: "REQUEST_PER_TIME",TOTAL_REQUESTS: "TOTAL_REQUESTS",AVERAGE_REQUEST_DURATION: "AVERAGE_REQUEST_DURATION",+ GET_ALL: "GET_ALL",// CostTOTAL_COST: "TOTAL_COST",AVERAGE_REQUEST_COST: "AVERAGE_REQUEST_COST",@@ -30,6 +31,14 @@ export const validateMetricsRequest = (};}break;+ case validateMetricsRequestType.GET_ALL:+ if (!params.timeLimit?.start || !params.timeLimit?.end) {+ return {+ success: false,+ err: "Start date or End date missing!",+ };+ }+ break;// Costcase validateMetricsRequestType.TOTAL_COST:case validateMetricsRequestType.AVERAGE_REQUEST_COST:diff --git a/src/client/src/utils/noop.ts b/src/client/src/utils/noop.tsnew file mode 100644index 00000000..177804c7--- /dev/null+++ b/src/client/src/utils/noop.ts@@ -0,0 +1 @@+export function noop() {}