Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change startDate to the first day of this month, endDate to the current date #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ async function fetchBalance() {
return Promise.resolve(cachedBanlance.toFixed(3))

// 计算起始日期和结束日期
const startDate = new Date(now - 90 * 24 * 60 * 60 * 1000)
const endDate = new Date(now + 24 * 60 * 60 * 1000)
const [startDate, endDate] = formatDate()

const config = await getCacheConfig()
const OPENAI_API_KEY = config.apiKey
Expand All @@ -174,7 +173,7 @@ async function fetchBalance() {
// 查普通账单
// const urlBalance = `${API_BASE_URL}/dashboard/billing/credit_grants`
// 查使用量
const urlUsage = `${API_BASE_URL}/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`
const urlUsage = `${API_BASE_URL}/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`

const headers = {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
Expand Down Expand Up @@ -221,12 +220,13 @@ async function fetchBalance() {
}
}

function formatDate(date) {
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')

return `${year}-${month}-${day}`
function formatDate() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const formattedFirstDay = `${year}-${month.toString().padStart(2, '0')}-01`;
const formattedToday = `${year}-${month.toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}`;
return [formattedFirstDay, formattedToday];
}

async function chatConfig() {
Expand Down