mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(roi): verify ClickHouse aggregation and administrator access
This commit is contained in:
parent
d3b971179b
commit
69323e7292
6 changed files with 58 additions and 7 deletions
|
|
@ -2,10 +2,10 @@ import asyncio
|
|||
import base64
|
||||
import binascii
|
||||
import os
|
||||
from collections.abc import AsyncGenerator, AsyncIterator
|
||||
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated, Final, TypeAlias
|
||||
from typing import Annotated, Final, Protocol, TypeAlias
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
|
|
@ -39,6 +39,20 @@ from litellm.rust_bridge.clickhouse import spend_storage_config
|
|||
from litellm.types.roi_calculator import ROISyncStatus
|
||||
from litellm.types.utils import ModelResponse
|
||||
|
||||
|
||||
class ROIJobScheduler(Protocol):
|
||||
def add_job(
|
||||
self,
|
||||
func: Callable[[], Awaitable[None]],
|
||||
trigger: str,
|
||||
*,
|
||||
seconds: int,
|
||||
id: str,
|
||||
max_instances: int,
|
||||
replace_existing: bool,
|
||||
) -> object: ...
|
||||
|
||||
|
||||
router: Final = APIRouter(prefix="/roi/categories", tags=["ROI categories"])
|
||||
_RUNNING: Final = ROISyncStatus(
|
||||
running=True,
|
||||
|
|
|
|||
|
|
@ -11522,9 +11522,10 @@ class ProxyStartupEvent:
|
|||
prisma_client=prisma_client,
|
||||
)
|
||||
|
||||
from litellm.proxy.management_endpoints.roi_categories_endpoints import run_roi_tick
|
||||
from litellm.proxy.management_endpoints.roi_categories_endpoints import ROIJobScheduler, run_roi_tick
|
||||
|
||||
scheduler.add_job(
|
||||
roi_scheduler: Final = cast(ROIJobScheduler, scheduler) # cast-ok: APScheduler exposes this untyped job API
|
||||
_ = roi_scheduler.add_job(
|
||||
run_roi_tick, "interval", seconds=30, id="roi_categories", max_instances=1, replace_existing=True
|
||||
)
|
||||
await cls._initialize_spend_tracking_background_jobs(scheduler=scheduler)
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ class CategoryStorage:
|
|||
return await self.rows(
|
||||
"SELECT toString(toDate(start_time, 'UTC')) AS day, "
|
||||
"if(a.category_id = '', 'unclassified', a.category_id) AS category_id, "
|
||||
"model, sum(ifNull(spend, 0)) AS spend, count() AS requests, countIf(isNull(spend)) AS unknown_cost_requests "
|
||||
"model, sum(ifNull(spend_logs.spend, 0)) AS spend, count() AS requests, countIf(isNull(spend_logs.spend)) AS unknown_cost_requests "
|
||||
"FROM spend_logs FINAL "
|
||||
+ _JOIN
|
||||
+ " WHERE "
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ import type { Settings } from "./data";
|
|||
const post = vi.hoisted(() => vi.fn());
|
||||
vi.mock("@/components/networking", () => ({ apiClient: { post } }));
|
||||
|
||||
beforeEach(() => post.mockReset());
|
||||
beforeEach(() => {
|
||||
post.mockReset();
|
||||
});
|
||||
|
||||
describe("ROI category setup", () => {
|
||||
it("includes extracted business context in discovery setup", async () => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { Tabs, TabsList } from "@/components/ui/tabs";
|
||||
import { ROIUsageTrigger } from "./UsageROITab";
|
||||
|
||||
describe("ROI usage access", () => {
|
||||
it.each(["Admin", "proxy_admin"])("shows ROI for the %s administrator role", (role) => {
|
||||
render(
|
||||
<Tabs defaultValue="roi">
|
||||
<TabsList>
|
||||
<ROIUsageTrigger role={role} view="global" />
|
||||
</TabsList>
|
||||
</Tabs>,
|
||||
);
|
||||
expect(screen.getByRole("tab", { name: "ROI" })).toBeVisible();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["Admin Viewer", "global"],
|
||||
["proxy_admin_viewer", "global"],
|
||||
["internal_user", "global"],
|
||||
["proxy_admin", "team"],
|
||||
])("hides ROI for %s in %s usage", (role, view) => {
|
||||
render(
|
||||
<Tabs defaultValue="roi">
|
||||
<TabsList>
|
||||
<ROIUsageTrigger role={role} view={view} />
|
||||
</TabsList>
|
||||
</Tabs>,
|
||||
);
|
||||
expect(screen.queryByRole("tab", { name: "ROI" })).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
import { TabsContent, TabsTrigger } from "@/components/ui/tabs";
|
||||
import type { DateRangePickerValue } from "@/components/shared/date_picker_types";
|
||||
import { isProxyAdminRole } from "@/utils/roles";
|
||||
import CategoryROI from "./CategoryROI";
|
||||
|
||||
type Access = { role: string | null; view: string };
|
||||
const permitted = ({ role, view }: Access) => role === "proxy_admin" && view === "global";
|
||||
const permitted = ({ role, view }: Access) => isProxyAdminRole(role ?? "") && view === "global";
|
||||
|
||||
export function ROIUsageTrigger(props: Access) {
|
||||
if (!permitted(props)) return null;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue