Merge pull request #7 from sijinhui/dev

进一步精简优化报表
This commit is contained in:
sijinhui 2023-12-30 23:38:23 +08:00 committed by GitHub
commit 9257e173ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 50 deletions

View File

@ -1,21 +1,11 @@
import * as echarts from "echarts"; import { NextRequest, NextResponse } from "next/server";
import { EChartsOption } from "echarts";
import dynamic from "next/dynamic";
import prisma from "@/lib/prisma"; import prisma from "@/lib/prisma";
import { addHours, subMinutes } from "date-fns"; import { addHours, subMinutes } from "date-fns";
import { log } from "util"; import { EChartsOption } from "echarts";
import { use } from "react";
// import { getTokenLength } from "@/app/utils/token";
const UsageByModelChart = dynamic(() => import("./usage-by-model-chart"), {
ssr: false,
});
interface StringKeyedObject { interface StringKeyedObject {
[key: string]: { [key: string]: number }; [key: string]: { [key: string]: number };
} }
type StringSet = Set<string>; type StringSet = Set<string>;
type StringArray = string[]; type StringArray = string[];
@ -58,9 +48,11 @@ function HandleLogData(
}; };
} }
export default async function UsageByModel() { async function handle(req: NextRequest) {
// 今天日期的开始和结束 const { searchParams } = new URL(req.url);
let currentTime = new Date(); const date = searchParams.get("date") ?? "";
// 当天日期的开始和结束
let currentTime = new Date(date);
// today = subMinutes(today, today.getTimezoneOffset()) // today = subMinutes(today, today.getTimezoneOffset())
const startOfTheDayInTimeZone = new Date( const startOfTheDayInTimeZone = new Date(
currentTime.getFullYear(), currentTime.getFullYear(),
@ -84,6 +76,7 @@ export default async function UsageByModel() {
}); });
// @ts-ignore // @ts-ignore
const log_data = HandleLogData(todayLog); const log_data = HandleLogData(todayLog);
// console.log('log_data', log_data)
const usageByModelOption: EChartsOption = { const usageByModelOption: EChartsOption = {
tooltip: { tooltip: {
@ -130,9 +123,8 @@ export default async function UsageByModel() {
}; };
}), }),
}; };
return ( return NextResponse.json(usageByModelOption);
<>
<UsageByModelChart option={usageByModelOption} />
</>
);
} }
export const GET = handle;
// export const POST = handle;

View File

@ -9,19 +9,8 @@ import { get_encoding } from "tiktoken";
import { addHours, subMinutes } from "date-fns"; import { addHours, subMinutes } from "date-fns";
function getTokenLength(input: string): number { function getTokenLength(input: string): number {
// const { Tiktoken } = require("tiktoken/lite");
// const cl100k_base = require("tiktoken/encoders/cl100k_base.json");
// const encoding = new Tiktoken(
// cl100k_base.bpe_ranks,
// cl100k_base.special_tokens,
// cl100k_base.pat_str,
// );
const encoding = get_encoding("cl100k_base"); const encoding = get_encoding("cl100k_base");
return encoding.encode(input).length;
const tokenLength = encoding.encode(input).length;
// console.log('[TOKEN],=========', input, tokenLength)
return tokenLength;
} }
async function handle( async function handle(

View File

@ -1,5 +1,13 @@
import { Grid, Col, Card, Text, AreaChart, Metric } from "@tremor/react"; import {
import UsageByModel from "./usage-by-model"; Grid,
Col,
// Card,
// Text,
// AreaChart,
// Metric,
// DatePicker,
} from "@tremor/react";
import UsageByModel from "./usage-by-model-chart";
import { getSession, ADMIN_LIST, isName } from "@/lib/auth"; import { getSession, ADMIN_LIST, isName } from "@/lib/auth";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
@ -13,11 +21,13 @@ export default async function AdminPage() {
return ( return (
<> <>
<Grid numItems={1} numItemsSm={2} numItemsLg={3} className="gap-2"> <Grid numItems={1} numItemsSm={2} numItemsLg={3} className="gap-2">
<Col numColSpan={1} numColSpanLg={1}> <Col numColSpan={1} numColSpanSm={2} numColSpanLg={3}>
{/*<UsageAnalysis />*/} {/*<UsageAnalysis />*/}
{/*<Card></Card>*/} {/*<Card></Card>*/}
{/*<DatePicker className="max-w-sm mx-auto justify-center" />*/}
{/*<DateRangePickerSpanish />*/}
</Col> </Col>
<Col numColSpan={1} numColSpanLg={2}> <Col numColSpan={1} numColSpanSm={2} numColSpanLg={3}>
<UsageByModel /> <UsageByModel />
</Col> </Col>
</Grid> </Grid>

View File

@ -1,22 +1,59 @@
"use client"; "use client";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import * as echarts from "echarts"; // 导入 echarts import * as echarts from "echarts";
import { DatePicker, DatePickerValue } from "@tremor/react";
import { zhCN } from "date-fns/locale";
import { param } from "ts-interface-checker"; // 导入 echarts
// {
// option,
// }: {
// option: echarts.EChartsOption;
// }
export default function UsageByModelChart() {
const [currentDate, setCurrentDate] = React.useState(new Date());
const [searchDate, setSearchDate] = React.useState("");
export default function UsageByModelChart({
option,
}: {
option: echarts.EChartsOption;
}) {
useEffect(() => { useEffect(() => {
var chartDom = document.getElementById("usage-by-model-chart"); console.log("init", currentDate, searchDate);
var myChart = echarts.init(chartDom); const currentDateString = currentDate.toLocaleDateString();
if (searchDate != currentDateString) {
async function fetchData() {
console.log("异步", searchDate, currentDateString);
const response = await fetch("/api/charts?date=" + currentDateString, {
method: "GET",
});
return await response.json();
}
fetchData().then((option) => {
if (option) {
let chartDom = document.getElementById("usage-by-model-chart");
let myChart = echarts.init(chartDom);
option && myChart.setOption(option); option && myChart.setOption(option);
}, [option]); // 空数组作为第二个参数,表示仅在组件挂载和卸载时执行 console.log("option计数", 1);
}
});
console.log("搜索开始计数", 1, searchDate, currentDateString);
}
return () => {
setSearchDate(currentDateString);
};
}, [currentDate, searchDate]); // 空数组作为第二个参数,表示仅在组件挂载和卸载时执行
return ( return (
<div>
<DatePicker
className="max-w-sm mx-auto justify-center"
value={currentDate}
locale={zhCN}
defaultValue={new Date()}
onValueChange={(d) => d && setCurrentDate(d)}
maxDate={new Date()}
/>
<div <div
id="usage-by-model-chart" id="usage-by-model-chart"
style={{ width: "100%", height: "400px" }} style={{ width: "100%", height: "400px" }}
></div> ></div>
</div>
); );
} }