Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): compute log period starts from the current date (#45360)

Juhyuk Choi0efd3c1cc09b
diff --git a/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/__tests__/long-time-range-picker.spec.tsx b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/__tests__/long-time-range-picker.spec.tsx
new file mode 100644
index 00000000000..bf9afe3dc82
--- /dev/null
+++ b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/__tests__/long-time-range-picker.spec.tsx
@@ -0,0 +1,32 @@
+import { render, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { TIME_PERIOD_MAPPING } from '@/app/components/app/log/filter'
+import LongTimeRangePicker from '../long-time-range-picker'
+
+describe('LongTimeRangePicker', () => {
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('starts the selected period on the current date after the page stays open past midnight', async () => {
+ vi.useFakeTimers({ toFake: ['Date'], shouldAdvanceTime: true })
+ const user = userEvent.setup()
+ const onSelect = vi.fn()
+ render(
+ <LongTimeRangePicker
+ periodMapping={TIME_PERIOD_MAPPING}
+ onSelect={onSelect}
+ queryDateFormat="YYYY-MM-DD HH:mm"
+ />,
+ )
+
+ vi.setSystemTime(new Date(2026, 11, 1, 9))
+ await user.click(screen.getByRole('combobox'))
+ await user.click(await screen.findByText('appLog.filter.period.monthToDate'))
+
+ expect(onSelect).toHaveBeenLastCalledWith({
+ name: 'appLog.filter.period.monthToDate',
+ query: { start: '2026-12-01 00:00', end: '2026-12-01 23:59' },
+ })
+ })
+})
diff --git a/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/long-time-range-picker.tsx b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/long-time-range-picker.tsx
index 8310ffccc51..2bd2f5915c6 100644
--- a/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/long-time-range-picker.tsx
+++ b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/long-time-range-picker.tsx
@@ -26,8 +26,6 @@ type Props = Readonly<{
queryDateFormat: string
}>
-const today = dayjs()
-
const LongTimeRangePicker: FC<Props> = ({ periodMapping, onSelect, queryDateFormat }) => {
const { t } = useTranslation(['appLog', 'common'])
const items = React.useMemo<TimePeriodOption[]>(() => {
@@ -46,6 +44,7 @@ const LongTimeRangePicker: FC<Props> = ({ periodMapping, onSelect, queryDateForm
const id = item.value
const value = periodMapping[id]?.value ?? '-1'
const name = item.name || t(($) => $['filter.period.allTime'], { ns: 'appLog' })
+ const today = dayjs()
if (value === -1) {
onSelect({ name: t(($) => $['filter.period.allTime'], { ns: 'appLog' }), query: undefined })
} else if (value === 0) {
diff --git a/web/app/components/app/log/__tests__/index.spec.tsx b/web/app/components/app/log/__tests__/index.spec.tsx
index 3af8ecc29fd..473be7fad01 100644
--- a/web/app/components/app/log/__tests__/index.spec.tsx
+++ b/web/app/components/app/log/__tests__/index.spec.tsx
@@ -1,5 +1,5 @@
import type { CloudSandboxPlanState } from '../cloud-sandbox-retention'
-import { screen } from '@testing-library/react'
+import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import dayjs from 'dayjs'
import { APP_PAGE_LIMIT } from '@/config'
@@ -172,6 +172,59 @@ describe('Logs', () => {
)
})
+ it('should start each period on the current date after the page stays open past midnight', async () => {
+ vi.useFakeTimers({ toFake: ['Date'], shouldAdvanceTime: true })
+ try {
+ const user = userEvent.setup()
+ getChatConversations.mockResolvedValue({
+ total: 0,
+ data: [],
+ page: 1,
+ limit: APP_PAGE_LIMIT,
+ has_more: false,
+ })
+ render(
+ <Logs
+ appDetail={createAppDetailFixture({ id: 'app-past-midnight', mode: 'chat' as const })}
+ />,
+ )
+ const selectPeriod = async (current: string, next: string) => {
+ await user.click(
+ await screen.findByRole('combobox', {
+ name: new RegExp(`appLog\\.filter\\.period\\.${current}`),
+ }),
+ )
+ await user.click(await screen.findByText(new RegExp(`appLog\\.filter\\.period\\.${next}`)))
+ }
+ const expectStart = (start: string) =>
+ waitFor(() =>
+ expect(getChatConversations.mock.calls.at(-1)?.[0]).toEqual(
+ expect.objectContaining({ query: expect.objectContaining({ start }) }),
+ ),
+ )
+
+ // The page has been open since earlier days; each request uses the date of the request.
+ vi.setSystemTime(new Date(2026, 11, 1, 9))
+ await selectPeriod('last7days', 'monthToDate')
+ await expectStart('2026-12-01 00:00')
+ await selectPeriod('monthToDate', 'quarterToDate')
+ await expectStart('2026-10-01 00:00')
+ await selectPeriod('quarterToDate', 'last3months')
+ await expectStart('2026-09-01 00:00')
+
+ vi.setSystemTime(new Date(2027, 0, 1, 9))
+ await selectPeriod('last3months', 'yearToDate')
+ await expectStart('2027-01-01 00:00')
+
+ // Twelve months before February 29 is the last day of the shorter February.
+ vi.setSystemTime(new Date(2028, 1, 29, 9))
+ await selectPeriod('yearToDate', 'last12months')
+ await expectStart('2027-02-28 00:00')
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('should use today when a cached period is unavailable to a Sandbox workspace', async () => {
const user = userEvent.setup()
const appDetail = createAppDetailFixture({
diff --git a/web/app/components/app/log/filter.tsx b/web/app/components/app/log/filter.tsx
index d07e03da134..5a0735e7447 100644
--- a/web/app/components/app/log/filter.tsx
+++ b/web/app/components/app/log/filter.tsx
@@ -1,4 +1,5 @@
'use client'
+import type { Dayjs } from 'dayjs'
import type { FC } from 'react'
import type { QueryParam } from './index'
import type { I18nKeysByPrefix } from '@/types/i18n'
@@ -22,7 +23,12 @@ import {
dayjs.extend(quarterOfYear)
-const today = dayjs()
+// Count the days when the value is read, so a page left open past midnight still uses the current date.
+// Round the result, because a daylight saving change makes some days 23 or 25 hours long.
+const daysSince = (getStart: (today: Dayjs) => Dayjs) => {
+ const today = dayjs().startOf('day')
+ return Math.round(today.diff(getStart(today).startOf('day'), 'day', true))
+}
type TimePeriodName = I18nKeysByPrefix<'appLog', 'filter.period.'>
@@ -30,11 +36,36 @@ export const TIME_PERIOD_MAPPING: { [key: string]: { value: number; name: TimePe
1: { value: 0, name: 'today' },
2: { value: 7, name: 'last7days' },
3: { value: 28, name: 'last4weeks' },
- 4: { value: today.diff(today.subtract(3, 'month'), 'day'), name: 'last3months' },
- 5: { value: today.diff(today.subtract(12, 'month'), 'day'), name: 'last12months' },
- 6: { value: today.diff(today.startOf('month'), 'day'), name: 'monthToDate' },
- 7: { value: today.diff(today.startOf('quarter'), 'day'), name: 'quarterToDate' },
- 8: { value: today.diff(today.startOf('year'), 'day'), name: 'yearToDate' },
+ 4: {
+ get value() {
+ return daysSince((today) => today.subtract(3, 'month'))
+ },
+ name: 'last3months',
+ },
+ 5: {
+ get value() {
+ return daysSince((today) => today.subtract(12, 'month'))
+ },
+ name: 'last12months',
+ },
+ 6: {
+ get value() {
+ return daysSince((today) => today.startOf('month'))
+ },
+ name: 'monthToDate',
+ },
+ 7: {
+ get value() {
+ return daysSince((today) => today.startOf('quarter'))
+ },
+ name: 'quarterToDate',
+ },
+ 8: {
+ get value() {
+ return daysSince((today) => today.startOf('year'))
+ },
+ name: 'yearToDate',
+ },
9: { value: -1, name: 'allTime' },
}
diff --git a/web/app/components/app/workflow-log/__tests__/index.spec.tsx b/web/app/components/app/workflow-log/__tests__/index.spec.tsx
index 66dfbb1eaf5..193150a3559 100644
--- a/web/app/components/app/workflow-log/__tests__/index.spec.tsx
+++ b/web/app/components/app/workflow-log/__tests__/index.spec.tsx
@@ -224,6 +224,47 @@ describe('Logs Container', () => {
})
})
+ it('should start each period on the current date after the page stays open past midnight', async () => {
+ vi.useFakeTimers({ toFake: ['Date'], shouldAdvanceTime: true })
+ try {
+ const user = userEvent.setup()
+ mockedUseWorkflowLogs.mockResolvedValue(createMockLogsResponse([], 0))
+ renderWithQueryClient(<Logs {...defaultProps} />)
+ const selectPeriod = async (current: string, next: string) => {
+ await user.click(
+ screen.getByRole('combobox', { name: `appLog.filter.period.${current}` }),
+ )
+ await user.click(await screen.findByText(`appLog.filter.period.${next}`))
+ }
+ const expectStart = (date: Date) =>
+ waitFor(() =>
+ expect(dayjs(String(getMockCallParams()?.params?.created_at__after)).valueOf()).toBe(
+ date.getTime(),
+ ),
+ )
+
+ // The page has been open since earlier days; each request uses the date of the request.
+ vi.setSystemTime(new Date(2026, 11, 1, 9))
+ await selectPeriod('last7days', 'monthToDate')
+ await expectStart(new Date(2026, 11, 1))
+ await selectPeriod('monthToDate', 'quarterToDate')
+ await expectStart(new Date(2026, 9, 1))
+ await selectPeriod('quarterToDate', 'last3months')
+ await expectStart(new Date(2026, 8, 1))
+
+ vi.setSystemTime(new Date(2027, 0, 1, 9))
+ await selectPeriod('last3months', 'yearToDate')
+ await expectStart(new Date(2027, 0, 1))
+
+ // Twelve months before February 29 is the last day of the shorter February.
+ vi.setSystemTime(new Date(2028, 1, 29, 9))
+ await selectPeriod('yearToDate', 'last12months')
+ await expectStart(new Date(2027, 1, 28))
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('should query the last 30 days when a Sandbox user selects the longest period', async () => {
const user = userEvent.setup()
mockPlanState.value = 'sandbox'
diff --git a/web/app/components/app/workflow-log/filter.tsx b/web/app/components/app/workflow-log/filter.tsx
index 28a626b99b0..f26f9ada1cb 100644
--- a/web/app/components/app/workflow-log/filter.tsx
+++ b/web/app/components/app/workflow-log/filter.tsx
@@ -1,4 +1,5 @@
'use client'
+import type { Dayjs } from 'dayjs'
import type { FC } from 'react'
import type { QueryParam } from './index'
import type { I18nKeysByPrefix } from '@/types/i18n'
@@ -20,7 +21,12 @@ import {
dayjs.extend(quarterOfYear)
-const today = dayjs()
+// Count the days when the value is read, so a page left open past midnight still uses the current date.
+// Round the result, because a daylight saving change makes some days 23 or 25 hours long.
+const daysSince = (getStart: (today: Dayjs) => Dayjs) => {
+ const today = dayjs().startOf('day')
+ return Math.round(today.diff(getStart(today).startOf('day'), 'day', true))
+}
type TimePeriodName = I18nKeysByPrefix<'appLog', 'filter.period.'>
@@ -28,11 +34,36 @@ export const TIME_PERIOD_MAPPING: { [key: string]: { value: number; name: TimePe
1: { value: 0, name: 'today' },
2: { value: 7, name: 'last7days' },
3: { value: 28, name: 'last4weeks' },
- 4: { value: today.diff(today.subtract(3, 'month'), 'day'), name: 'last3months' },
- 5: { value: today.diff(today.subtract(12, 'month'), 'day'), name: 'last12months' },
- 6: { value: today.diff(today.startOf('month'), 'day'), name: 'monthToDate' },
- 7: { value: today.diff(today.startOf('quarter'), 'day'), name: 'quarterToDate' },
- 8: { value: today.diff(today.startOf('year'), 'day'), name: 'yearToDate' },
+ 4: {
+ get value() {
+ return daysSince((today) => today.subtract(3, 'month'))
+ },
+ name: 'last3months',
+ },
+ 5: {
+ get value() {
+ return daysSince((today) => today.subtract(12, 'month'))
+ },
+ name: 'last12months',
+ },
+ 6: {
+ get value() {
+ return daysSince((today) => today.startOf('month'))
+ },
+ name: 'monthToDate',
+ },
+ 7: {
+ get value() {
+ return daysSince((today) => today.startOf('quarter'))
+ },
+ name: 'quarterToDate',
+ },
+ 8: {
+ get value() {
+ return daysSince((today) => today.startOf('year'))
+ },
+ name: 'yearToDate',
+ },
9: { value: -1, name: 'allTime' },
}