Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

add started-at filter to task runs page (#68279)

Nicola Mometto240acb478228
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index efef3b82a95..a9f8b9a6110 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1456,6 +1456,7 @@
models
permissions
premium-features
+ query-processor
request
task
util}}
diff --git a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
index 6aaba172474..74f3616f6d2 100644
--- a/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
+++ b/e2e/test/scenarios/admin/tools/tools.cy.spec.ts
@@ -139,17 +139,14 @@ describe("issue 14636", () => {
"have.value",
"field values scanning",
);
- cy.findByPlaceholderText("Filter by status").should(
- "have.value",
- "Success",
- );
+ getFilterByStatus().should("have.value", "Success");
cy.findAllByTestId("task").should("have.length", 1);
cy.findByTestId("task")
.should("contain.text", "field values scanning")
.and("contain.text", "Sample Database")
.and("contain.text", "Success");
- cy.findByPlaceholderText("Filter by status").click();
+ getFilterByStatus().click();
H.popover().findByText("Failed").click();
cy.location("search").should(
"eq",
@@ -161,12 +158,9 @@ describe("issue 14636", () => {
"No results",
);
- cy.findByPlaceholderText("Filter by status")
- .parent()
- .findByLabelText("Clear")
- .click();
+ getFilterByStatus().parent().findByLabelText("Clear").click();
cy.location("search").should("eq", "?task=field+values+scanning");
- cy.findByPlaceholderText("Filter by status").should("have.value", "");
+ getFilterByStatus().should("have.value", "");
cy.findAllByTestId("task").should("have.length", 1);
cy.findByTestId("task")
.should("contain.text", "field values scanning")
@@ -184,14 +178,14 @@ describe("issue 14636", () => {
cy.log("should reset pagination when changing filters");
cy.visit("/admin/tools/tasks/list?page=1");
- cy.findByPlaceholderText("Filter by status").click();
+ getFilterByStatus().click();
H.popover().findByText("Success").click();
cy.location("search").should("eq", "?status=success");
cy.log("should remove invalid query params");
cy.visit("/admin/tools/tasks/list?status=foobar");
cy.location("search").should("eq", "");
- cy.findByPlaceholderText("Filter by status").should("have.value", "");
+ getFilterByStatus().should("have.value", "");
});
});
@@ -832,54 +826,40 @@ describe("scenarios > admin > tools > task runs filtering", () => {
cy.wait("@getTaskRuns");
cy.log("Filter by run type");
- cy.findByPlaceholderText("Filter by run type").click();
+ getFilterByRun().click();
H.popover().findByText("Sync").click();
cy.location("search").should("contain", "run-type=sync");
cy.wait("@getTaskRuns");
+ cy.log("Filter by started at");
+ getFilterByStartedAt().click();
+ H.popover().findByText("Previous 30 days").click();
+ cy.location("search").should("contain", "started-at=past30days");
+ cy.wait("@getTaskRuns");
cy.wait("@getEntities");
cy.log("Filter by entity");
- cy.findByPlaceholderText("Filter by entity").click();
+ getFilterByEntity().click();
H.popover().findByText("Sample Database").click();
cy.location("search").should("contain", "entity-type=database");
cy.location("search").should("contain", "entity-id=1");
cy.wait("@getTaskRuns");
cy.log("Filter by status");
- cy.findByPlaceholderText("Filter by status").click();
+ getFilterByStatus().click();
H.popover().findByText("Success").click();
cy.location("search").should("contain", "status=success");
cy.wait("@getTaskRuns");
cy.log("Clear all filters");
- cy.findByPlaceholderText("Filter by run type")
- .parent()
- .findByLabelText("Clear")
- .click();
- cy.findByPlaceholderText("Filter by status")
- .parent()
- .findByLabelText("Clear")
- .click();
+ getFilterByRun().parent().findByLabelText("Clear").click();
+ getFilterByStartedAt().parent().findByLabelText("Clear").click();
+ getFilterByStatus().parent().findByLabelText("Clear").click();
cy.location("search").should("eq", "");
});
- it("entity picker should be disabled/enabled based on run type and entities availability", () => {
+ it("entity picker should be disabled/enabled based on run type, started at and entities availability", () => {
cy.visit("/admin/tools/tasks/runs");
cy.wait("@getTaskRuns");
-
- cy.log("Should be disabled when no run type is selected");
- cy.findByPlaceholderText("Filter by entity").should("be.disabled");
-
- cy.log("Should show tooltip 'Select a run type first' when hovering");
- cy.findByPlaceholderText("Filter by entity").trigger("mouseenter", {
- force: true,
- });
- cy.findByRole("tooltip").should("have.text", "Select a run type first");
- cy.findByPlaceholderText("Filter by entity").trigger("mouseleave", {
- force: true,
- });
-
- cy.log("Should show loader while loading entities");
cy.intercept("GET", "/api/task/runs/entities?*", {
body: [
{
@@ -891,10 +871,24 @@ describe("scenarios > admin > tools > task runs filtering", () => {
delay: 500,
}).as("getEntitiesDelayed");
- cy.findByPlaceholderText("Filter by run type").click();
+ cy.log("Should be disabled when no run type is selected");
+ getFilterByEntity().should("be.disabled");
+ assertFilterByEntityTooltipText("Select a run type first");
+
+ getFilterByRun().click();
H.popover().findByText("Sync").click();
- cy.findByPlaceholderText("Filter by entity")
+ cy.log("Should be still disabled until started at is selected");
+ getFilterByEntity().should("be.disabled");
+
+ cy.log("Should show tooltip 'Select a start time' when hovering");
+ assertFilterByEntityTooltipText("Select a start time first");
+
+ getFilterByStartedAt().click();
+ H.popover().findByText("Previous 30 days").click();
+
+ cy.log("Should show loader while loading entities");
+ getFilterByEntity()
.should("be.disabled")
.closest(".mb-mantine-Select-root")
.find(".mb-mantine-Loader-root")
@@ -903,30 +897,56 @@ describe("scenarios > admin > tools > task runs filtering", () => {
cy.wait("@getEntitiesDelayed");
cy.log("Should be enabled after entities are loaded");
- cy.findByPlaceholderText("Filter by entity").should("not.be.disabled");
+ getFilterByEntity().should("not.be.disabled");
cy.log("Should clear and disable entity filter when run type is cleared");
- cy.findByPlaceholderText("Filter by run type")
- .parent()
- .findByLabelText("Clear")
- .click();
+ getFilterByRun().parent().findByLabelText("Clear").click();
- cy.findByPlaceholderText("Filter by entity").should("be.disabled");
- cy.findByPlaceholderText("Filter by entity").should("have.value", "");
+ getFilterByEntity().should("be.disabled");
+ getFilterByEntity().should("have.value", "");
+
+ cy.log("Should clear and disable entity filter when started at is cleared");
+ getFilterByRun().click();
+ H.popover().findByText("Sync").click();
+ getFilterByStartedAt().parent().findByLabelText("Clear").click();
+
+ getFilterByEntity().should("be.disabled");
+ getFilterByEntity().should("have.value", "");
cy.log("Should show tooltip 'No entities available' when no entities");
cy.intercept("GET", "/api/task/runs/entities?*", {
body: [],
}).as("getEmptyEntities");
- cy.findByPlaceholderText("Filter by run type").click();
+ getFilterByRun().click();
H.popover().findByText("Alert").click();
+ getFilterByStartedAt().click();
+ H.popover().findByText("Previous 30 days").click();
cy.wait("@getEmptyEntities");
- cy.findByPlaceholderText("Filter by entity").should("be.disabled");
- cy.findByPlaceholderText("Filter by entity").trigger("mouseenter", {
- force: true,
- });
- cy.findByRole("tooltip").should("have.text", "No entities available");
+ getFilterByEntity().should("be.disabled");
+ assertFilterByEntityTooltipText("No entities available");
});
});
+
+function getFilterByRun() {
+ return cy.findByPlaceholderText("Filter by run type");
+}
+function getFilterByStartedAt() {
+ return cy.findByPlaceholderText("Filter by started at");
+}
+
+function getFilterByEntity() {
+ return cy.findByPlaceholderText("Filter by entity");
+}
+
+function getFilterByStatus() {
+ return cy.findByPlaceholderText("Filter by status");
+}
+
+function assertFilterByEntityTooltipText(text: string) {
+ getFilterByEntity().trigger("mouseenter", {
+ force: true,
+ });
+ cy.findByRole("tooltip").should("have.text", text);
+}
diff --git a/frontend/src/metabase-types/api/task.ts b/frontend/src/metabase-types/api/task.ts
index 0b33c129480..cbf8bbfc639 100644
--- a/frontend/src/metabase-types/api/task.ts
+++ b/frontend/src/metabase-types/api/task.ts
@@ -67,6 +67,15 @@ export type TaskInfo = {
export type TaskRunType = "subscription" | "alert" | "sync" | "fingerprint";
export type TaskRunEntityType = "database" | "card" | "dashboard";
export type TaskRunStatus = "started" | "success" | "failed" | "abandoned";
+export type TaskRunDateFilterOption =
+ | "thisday"
+ | "past1days~"
+ | "past1weeks~"
+ | "past7days~"
+ | "past30days~"
+ | "past1months~"
+ | "past3months~"
+ | "past12months~";
export interface TaskRun {
id: number;
@@ -97,6 +106,7 @@ export type ListTaskRunsRequest = {
"entity-type"?: TaskRunEntityType;
"entity-id"?: number;
status?: TaskRunStatus;
+ "started-at"?: TaskRunDateFilterOption;
} & PaginationRequest;
export type ListTaskRunsResponse = {
@@ -105,4 +115,5 @@ export type ListTaskRunsResponse = {
export type ListTaskRunEntitiesRequest = {
"run-type": TaskRunType;
+ "started-at": TaskRunDateFilterOption;
};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx
index 4d2adfea31e..5f139d69d11 100644
--- a/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx
+++ b/frontend/src/metabase/admin/tools/components/TaskRunEntityPicker/TaskRunEntityPicker.tsx
@@ -3,7 +3,7 @@ import { t } from "ttag";
import { skipToken, useListTaskRunEntitiesQuery } from "metabase/api";
import { Loader, Select, type SelectProps, Tooltip } from "metabase/ui";
-import type { TaskRunType } from "metabase-types/api";
+import type { TaskRunDateFilterOption, TaskRunType } from "metabase-types/api";
import type { EntityValue } from "./types";
import {
@@ -17,18 +17,26 @@ type TaskRunEntityPickerProps = Omit<
"data" | "value" | "onChange"
> & {
runType: TaskRunType | null;
+ startedAt: TaskRunDateFilterOption | null;
value: EntityValue | null;
onChange: (value: EntityValue | null) => void;
};
export const TaskRunEntityPicker = ({
runType,
+ startedAt,
value,
onChange,
...props
}: TaskRunEntityPickerProps) => {
+ const hasAllRequiredParams = runType && startedAt;
const { data: entities, isLoading } = useListTaskRunEntitiesQuery(
- runType ? { "run-type": runType } : skipToken,
+ hasAllRequiredParams
+ ? {
+ "run-type": runType,
+ "started-at": startedAt,
+ }
+ : skipToken,
);
const data = useMemo(
@@ -47,7 +55,8 @@ export const TaskRunEntityPicker = ({
onChange(parsed);
};
- const isDisabled = isLoading || !runType || (!isLoading && data.length === 0);
+ const isDisabled =
+ isLoading || !hasAllRequiredParams || (!isLoading && data.length === 0);
const select = (
<Select
@@ -77,6 +86,10 @@ export const TaskRunEntityPicker = ({
return <Tooltip label={t`Select a run type first`}>{select}</Tooltip>;
}
+ if (!startedAt) {
+ return <Tooltip label={t`Select a start time first`}>{select}</Tooltip>;
+ }
+
if (!isLoading && data.length === 0) {
return <Tooltip label={t`No entities available`}>{select}</Tooltip>;
}
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/TaskRunDatePicker.tsx b/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/TaskRunDatePicker.tsx
new file mode 100644
index 00000000000..57bb97817c7
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/TaskRunDatePicker.tsx
@@ -0,0 +1,54 @@
+import { t } from "ttag";
+
+import { Select, type SelectProps } from "metabase/ui";
+import type { SelectData } from "metabase/ui/components/inputs/Select/Select";
+import type { TaskRunDateFilterOption } from "metabase-types/api";
+
+type TaskRunDatePickerProps = Omit<
+ SelectProps,
+ "data" | "value" | "onChange"
+> & {
+ value: TaskRunDateFilterOption | null;
+ onChange: (value: TaskRunDateFilterOption | null) => void;
+};
+
+export const TaskRunDatePicker = ({
+ value,
+ onChange,
+ ...props
+}: TaskRunDatePickerProps) => {
+ /**
+ * Using a simple version of TimeFilterWidget here in order to align with a design of admin page.
+ * That means no custom date ranges.
+ */
+ const data: SelectData<TaskRunDateFilterOption> = [
+ { label: t`Today`, value: "thisday" },
+ { label: t`Yesterday`, value: "past1days~" },
+ { label: t`Previous week`, value: "past1weeks~" },
+ { label: t`Previous 7 days`, value: "past7days~" },
+ { label: t`Previous 30 days`, value: "past30days~" },
+ { label: t`Previous month`, value: "past1months~" },
+ { label: t`Previous 3 months`, value: "past3months~" },
+ { label: t`Previous 12 months`, value: "past12months~" },
+ ];
+
+ return (
+ <Select
+ comboboxProps={{
+ middlewares: {
+ flip: true,
+ size: {
+ padding: 6,
+ },
+ },
+ position: "bottom-start",
+ width: 300,
+ }}
+ clearable
+ data={data}
+ value={value}
+ onChange={onChange}
+ {...props}
+ />
+ );
+};
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/index.ts b/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/index.ts
new file mode 100644
index 00000000000..dea7716fc89
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/components/TaskRunStartedAtPicker/index.ts
@@ -0,0 +1 @@
+export { TaskRunDatePicker } from "./TaskRunDatePicker";
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx
index 4997a8aef2a..c1ab0e0698e 100644
--- a/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/TaskRunsPage.tsx
@@ -1,4 +1,5 @@
import { type WithRouterProps, withRouter } from "react-router";
+import { t } from "ttag";
import { useListTaskRunsQuery } from "metabase/api";
import { PaginationControls } from "metabase/common/components/PaginationControls";
@@ -7,6 +8,7 @@ import { Flex } from "metabase/ui";
import { TaskRunTypePicker } from "../RunTypePicker";
import { TaskRunEntityPicker } from "../TaskRunEntityPicker";
+import { TaskRunDatePicker } from "../TaskRunStartedAtPicker";
import { TaskRunStatusPicker } from "../TaskRunStatusPicker";
import { TasksTabs } from "../TasksTabs";
@@ -21,6 +23,7 @@ const TaskRunsPageBase = ({ location }: WithRouterProps) => {
"run-type": runType,
"entity-type": entityType,
"entity-id": entityId,
+ "started-at": startedAt,
status,
},
{ patchUrlState },
@@ -37,6 +40,7 @@ const TaskRunsPageBase = ({ location }: WithRouterProps) => {
"run-type": runType ?? undefined,
"entity-type": entityType ?? undefined,
"entity-id": entityId ?? undefined,
+ "started-at": startedAt ?? undefined,
status: status ?? undefined,
},
{
@@ -65,8 +69,22 @@ const TaskRunsPageBase = ({ location }: WithRouterProps) => {
}
/>
+ <TaskRunDatePicker
+ value={startedAt}
+ placeholder={t`Filter by started at`}
+ onChange={(startedAt) =>
+ patchUrlState({
+ "started-at": startedAt,
+ "entity-type": null,
+ "entity-id": null,
+ page: 0,
+ })
+ }
+ />
+
<TaskRunEntityPicker
runType={runType}
+ startedAt={startedAt}
value={entityValue}
onChange={(entity) =>
patchUrlState({
diff --git a/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts b/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts
index d5262f645f5..fff4afbebc5 100644
--- a/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts
+++ b/frontend/src/metabase/admin/tools/components/TaskRunsPage/utils.ts
@@ -4,6 +4,7 @@ import {
getFirstParamValue,
} from "metabase/common/hooks/use-url-state";
import type {
+ TaskRunDateFilterOption,
TaskRunEntityType,
TaskRunStatus,
TaskRunType,
@@ -12,6 +13,7 @@ import type {
import {
guardTaskRunEntityType,
guardTaskRunRunType,
+ guardTaskRunStartedAtRange,
guardTaskRunStatus,
} from "../../utils";
@@ -21,6 +23,7 @@ type UrlState = {
"entity-type": TaskRunEntityType | null;
"entity-id": number | null;
status: TaskRunStatus | null;
+ "started-at": TaskRunDateFilterOption | null;
};
export const urlStateConfig: UrlStateConfig<UrlState> = {
@@ -30,6 +33,7 @@ export const urlStateConfig: UrlStateConfig<UrlState> = {
"entity-type": parseTaskRunEntityType(query["entity-type"]),
"entity-id": parseTaskRunEntityId(query["entity-id"]),
status: parseTaskRunStatus(query.status),
+ "started-at": parseTaskRunStartedAt(query["started-at"]),
}),
serialize: ({
page,
@@ -37,12 +41,14 @@ export const urlStateConfig: UrlStateConfig<UrlState> = {
"entity-type": entityType,
"entity-id": entityId,
status,
+ "started-at": startedAt,
}) => ({
page: page === 0 ? undefined : String(page),
"run-type": runType === null ? undefined : runType,
"entity-type": entityType === null ? undefined : entityType,
"entity-id": entityId === null ? undefined : String(entityId),
status: status === null ? undefined : status,
+ "started-at": startedAt === null ? undefined : startedAt,
}),
};
@@ -75,3 +81,8 @@ const parseTaskRunStatus = (param: QueryParam): UrlState["status"] => {
const value = getFirstParamValue(param);
return value && guardTaskRunStatus(value) ? value : null;
};
+
+const parseTaskRunStartedAt = (param: QueryParam): UrlState["started-at"] => {
+ const value = getFirstParamValue(param);
+ return value && guardTaskRunStartedAtRange(value) ? value : null;
+};
diff --git a/frontend/src/metabase/admin/tools/utils.ts b/frontend/src/metabase/admin/tools/utils.ts
index 3c9cb49cdd5..91442991354 100644
--- a/frontend/src/metabase/admin/tools/utils.ts
+++ b/frontend/src/metabase/admin/tools/utils.ts
@@ -5,6 +5,7 @@ import * as Urls from "metabase/lib/urls";
import type {
Task,
TaskRun,
+ TaskRunDateFilterOption,
TaskRunEntityType,
TaskRunStatus,
TaskRunType,
@@ -103,3 +104,19 @@ export const guardTaskRunStatus = (value: string): value is TaskRunStatus =>
(
["started", "success", "failed", "abandoned"] satisfies TaskRunStatus[]
).includes(value as TaskRunStatus);
+
+export const guardTaskRunStartedAtRange = (
+ value: string,
+): value is TaskRunDateFilterOption =>
+ (
+ [
+ "thisday",
+ "past1days~",
+ "past1weeks~",
+ "past7days~",
+ "past30days~",
+ "past1months~",
+ "past3months~",
+ "past12months~",
+ ] satisfies TaskRunDateFilterOption[]
+ ).includes(value as TaskRunDateFilterOption);
diff --git a/src/metabase/task_history/api.clj b/src/metabase/task_history/api.clj
index d2d797054fa..10917cdb45e 100644
--- a/src/metabase/task_history/api.clj
+++ b/src/metabase/task_history/api.clj
@@ -4,10 +4,13 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.permissions.core :as perms]
+ [metabase.query-processor.parameters.dates :as params.dates]
[metabase.request.core :as request]
[metabase.task-history.models.task-history :as task-history]
[metabase.task-history.models.task-run :as task-run]
[metabase.task.core :as task]
+ [metabase.util.date-2 :as u.date]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -70,7 +73,8 @@
[:run-type {:optional true} (into [:enum] (map name task-run/run-types))]
[:entity-type {:optional true} (into [:enum] (map name task-run/entity-types))]
[:entity-id {:optional true} ms/PositiveInt]
- [:status {:optional true} [:enum "started" "success" "failed" "abandoned"]]])
+ [:status {:optional true} [:enum "started" "success" "failed" "abandoned"]]
+ [:started-at {:optional true} ms/NonBlankString]])
(mr/def ::TaskRun
[:map
@@ -160,15 +164,33 @@
(get counts-by-id (:id %)))
runs))))
+(defn- timestamp-constraint
+ [field-name date-string]
+ (let [{:keys [start end]}
+ (try
+ (params.dates/date-string->range date-string {:inclusive-end? false})
+ (catch Exception e
+ (throw (ex-info (tru "Failed to parse datetime value: {0}" date-string)
+ {:status-code 400}
+ e))))
+ start (some-> start u.date/parse)
+ end (some-> end u.date/parse)]
+ (into [:and] (remove nil?)
+ [(when start
+ [:>= field-name start])
+ (when end
+ [:< field-name end])])))
+
(defn- build-run-where-clause
- [{:keys [run-type entity-type entity-id status]}]
- (let [clause (cond-> {}
- run-type (assoc :run_type run-type)
- entity-type (assoc :entity_type entity-type)
- entity-id (assoc :entity_id entity-id)
- status (assoc :status status))]
- (if (seq clause)
- {:where (into [:and] (map (fn [[k v]] [:= k v])) clause)}
+ [{:keys [run-type entity-type entity-id status started-at]}]
+ (let [conditions (cond-> []
+ run-type (conj [:= :run_type run-type])
+ entity-type (conj [:= :entity_type entity-type])
+ entity-id (conj [:= :entity_id entity-id])
+ status (conj [:= :status status])
+ started-at (conj (timestamp-constraint :started_at started-at)))]
+ (if (seq conditions)
+ {:where (into [:and] conditions)}
{})))
(api.macros/defendpoint :get "/runs" :- ::TaskRunsResponse
@@ -203,10 +225,14 @@
(api.macros/defendpoint :get "/runs/entities" :- [:sequential ::RunEntity]
"Get distinct entities that have task runs for a given run type. Used for populating entity filter picker."
[_
- params :- [:map [:run-type (into [:enum] (map name task-run/run-types))]]]
+ params :- [:map
+ [:run-type (into [:enum] (map name task-run/run-types))]
+ [:started-at ms/NonBlankString]]]
(perms/check-has-application-permission :monitoring)
- (->> (t2/query {:select-distinct [:entity_type :entity_id]
- :from :task_run
- :where [:= :run_type (:run-type params)]})
- (map #(update % :entity_type keyword))
- hydrate-entity-names))
+ (let [where-conditions [[:= :run_type (:run-type params)]
+ (timestamp-constraint :started_at (:started-at params))]]
+ (->> (t2/query {:select-distinct [:entity_type :entity_id]
+ :from :task_run
+ :where (into [:and] where-conditions)})
+ (map #(update % :entity_type keyword))
+ hydrate-entity-names)))
diff --git a/test/metabase/task_history/api_test.clj b/test/metabase/task_history/api_test.clj
index 20e55e1fd68..ff9b1ad879f 100644
--- a/test/metabase/task_history/api_test.clj
+++ b/test/metabase/task_history/api_test.clj
@@ -4,6 +4,7 @@
[java-time.api :as t]
[metabase.test :as mt]
[metabase.util :as u]
+ [metabase.util.date-2 :as u.date]
[toucan2.core :as t2]))
(def ^:private default-task-history
@@ -612,7 +613,9 @@
(deftest runs-entities-perms-test
(testing "non-superuser cannot access runs/entities"
(is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :get 403 "task/runs/entities" :run-type "sync")))))
+ (mt/user-http-request :rasta :get 403 "task/runs/entities"
+ :run-type "sync"
+ :started-at "past30days")))))
(deftest ^:synchronized runs-task-counts-test
(t2/delete! :model/TaskRun)
@@ -674,12 +677,140 @@
:status :success
:started_at (t/zoned-date-time)}]
(testing "sync entities"
- (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities" :run-type "sync")]
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities"
+ :run-type "sync"
+ :started-at "past30days~")]
(is (= 2 (count response)))
(is (every? #(= "database" (:entity_type %)) response))
(is (= #{"DB1" "DB2"} (set (map :entity_name response))))))
(testing "subscription entities"
- (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities" :run-type "subscription")]
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities"
+ :run-type "subscription"
+ :started-at "past30days~")]
(is (= 1 (count response)))
(is (= "dashboard" (-> response first :entity_type)))
(is (= "My Dashboard" (-> response first :entity_name))))))))
+
+(deftest ^:synchronized runs-started-at-filter-test
+ (t2/delete! :model/TaskRun)
+ (testing "filtering by started-at date range"
+ (let [now (t/zoned-date-time)
+ old-date (t/minus now (t/days 10))
+ recent-date (t/minus now (t/days 2))]
+ (mt/with-temp [:model/Database db {:name "Test DB"}
+ :model/Database db2 {:name "Test DB 2"}
+ :model/Database db3 {:name "Test DB 3"}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db3)
+ :status :success
+ :started_at old-date
+ :ended_at old-date}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db2)
+ :status :success
+ :started_at recent-date
+ :ended_at recent-date}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :success
+ :started_at now
+ :ended_at now}]
+ (testing "past7days returns only recent runs"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs" :started-at "past7days~")]
+ (is (= 2 (:total response)))))
+ (testing "past30days returns all runs"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs" :started-at "past30days~")]
+ (is (= 3 (:total response)))))
+ (testing "absolute date range filtering"
+ (let [start-date (u.date/format (t/local-date (t/minus now (t/days 3))))
+ end-date (u.date/format (t/local-date now))
+ response (mt/user-http-request :crowberto :get 200 "task/runs"
+ :started-at (str start-date "~" end-date))]
+ (is (= 2 (:total response)))))))))
+
+(deftest ^:synchronized runs-started-at-combined-filters-test
+ (t2/delete! :model/TaskRun)
+ (testing "combining started-at with other filters"
+ (let [now (t/zoned-date-time)
+ old-date (t/minus now (t/days 10))
+ recent-date (t/minus now (t/days 2))]
+ (mt/with-temp [:model/Database db {:name "Test DB"}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :success
+ :started_at old-date
+ :ended_at old-date}
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db)
+ :status :failed
+ :started_at recent-date
+ :ended_at recent-date}
+ :model/TaskRun _ {:run_type :fingerprint
+ :entity_type :database
+ :entity_id (:id db)
+ :status :success
+ :started_at recent-date
+ :ended_at recent-date}]
+ (testing "started-at combined with run-type"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs"
+ :started-at "past7days"
+ :run-type "sync")]
+ (is (= 1 (:total response)))))
+ (testing "started-at combined with status"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs"
+ :started-at "past7days"
+ :status "success")]
+ (is (= 1 (:total response)))))
+ (testing "started-at combined with multiple filters"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs"
+ :started-at "past7days"
+ :run-type "sync"
+ :status "failed")]
+ (is (= 1 (:total response)))))))))
+
+(deftest runs-started-at-invalid-date-test
+ (testing "invalid date string returns 400 error"
+ (is (re-matches #"Failed to parse datetime value.*"
+ (mt/user-http-request :crowberto :get 400 "task/runs" :started-at "invalid-date")))))
+
+(deftest ^:synchronized runs-entities-started-at-filter-test
+ (t2/delete! :model/TaskRun)
+ (testing "/runs/entities with started-at filter"
+ (let [now (t/zoned-date-time)
+ old-date (t/minus now (t/days 10))
+ recent-date (t/minus now (t/days 2))]
+ (mt/with-temp [:model/Database db1 {:name "DB1"}
+ :model/Database db2 {:name "DB2"}
+ ;; Old run for db1
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db1)
+ :status :success
+ :started_at old-date
+ :ended_at old-date}
+ ;; Recent run for db2
+ :model/TaskRun _ {:run_type :sync
+ :entity_type :database
+ :entity_id (:id db2)
+ :status :success
+ :started_at recent-date
+ :ended_at recent-date}]
+ (testing "started-at is required"
+ (is (=? {:errors {:started-at some?}}
+ (mt/user-http-request :crowberto :get 400 "task/runs/entities" :run-type "sync"))))
+ (testing "with past30days filter returns all entities"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities"
+ :run-type "sync"
+ :started-at "past30days")]
+ (is (= 2 (count response)))))
+ (testing "with past7days filter returns only recent entities"
+ (let [response (mt/user-http-request :crowberto :get 200 "task/runs/entities"
+ :run-type "sync"
+ :started-at "past7days")]
+ (is (= 1 (count response)))
+ (is (= "DB2" (-> response first :entity_name)))))))))