Skip to content

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

28.7 KB
Raw Blame
(ns metabase-enterprise.stale.impl-test
  {:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.stale.impl-test]}}}}}}
  (:require
   [clojure.test :refer [deftest is are testing]]
   [metabase-enterprise.stale.impl :as stale]
   [metabase-enterprise.test :as met]
   [metabase.collections.models.collection :as collection]
   [metabase.content-verification.models.moderation-review :as moderation-review]
   [metabase.stale-test :refer [with-stale-items
                                stale-dashboard
                                stale-card
                                stale-document
                                date-months-ago
                                datetime-months-ago]]
   [metabase.test :as mt]
   [metabase.util :as u]
   [toucan2.core :as t2])
  (:import (java.time LocalDate)))

(set! *warn-on-reflection* true)

(deftest can-find-stale-dashboards
  (mt/with-temp [:model/Dashboard {id :id} (stale-dashboard
                                            {:name "My Stale Dashboard"
                                             :collection_id nil})]
    (is (= [{:id id :model :model/Dashboard}]
           (:rows
            (stale/find-candidates
             {:collection-ids #{nil}
              :cutoff-date    (date-months-ago 6)
              :limit          10
              :offset         0
              :sort-column    :name
              :sort-direction :asc}))))))

(deftest can-find-stale-cards
  (with-stale-items [:model/Card {id :id} {:name "My Stale Card"
                                           :collection_id nil}]
    (is (= [{:id id :model :model/Card}]
           (:rows
            (stale/find-candidates
             {:collection-ids #{nil}
              :cutoff-date    (date-months-ago 6)
              :limit          10
              :offset         0
              :sort-column    :name
              :sort-direction :asc}))))))

(deftest can-find-stale-documents
  (mt/with-temp [:model/Document {id :id} (stale-document
                                           {:name "My Stale Document"
                                            :collection_id nil})]
    (let [base {:collection-ids #{nil}
                :cutoff-date    (date-months-ago 6)
                :limit          10
                :offset         0
                :sort-column    :name
                :sort-direction :asc}]
      (testing "a stale document is found when documents are searched"
        (is (= [{:id id :model :model/Document}]
               (:rows (stale/find-candidates (assoc base :models #{:model/Document}))))))
      (testing "documents are not part of the default model set"
        (is (not-any? #(= (:id %) id)
                      (:rows (stale/find-candidates base))))))))

(deftest publicly-shared-documents-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Document {doc-id1 :id} (stale-document {:name          "A"
                                                                :collection_id col-id
                                                                :public_uuid   (str (random-uuid))})
                 :model/Document {doc-id2 :id} (stale-document {:name          "B"
                                                                :collection_id col-id})]
    (let [rows #(:rows (stale/find-candidates {:collection-ids #{col-id}
                                               :cutoff-date    (date-months-ago 6)
                                               :limit          10
                                               :offset         0
                                               :sort-column    :name
                                               :sort-direction :asc
                                               :models         #{:model/Document}}))]
      (testing "when public sharing is disabled, publicly shared documents are still returned"
        (mt/with-temporary-setting-values [enable-public-sharing false]
          (is (= [{:id doc-id1 :model :model/Document}
                  {:id doc-id2 :model :model/Document}]
                 (rows)))))
      (testing "when public sharing is enabled, publicly shared documents are excluded"
        (mt/with-temporary-setting-values [enable-public-sharing true]
          (is (= [{:id doc-id2 :model :model/Document}]
                 (rows))))))))

(deftest never-viewed-documents-created-before-cutoff-are-stale
  (mt/with-temp [:model/Document {never-viewed-id :id} {:name           "Never viewed, created before cutoff"
                                                        :collection_id  nil
                                                        :view_count     0
                                                        :last_viewed_at (datetime-months-ago 1)
                                                        :created_at     (datetime-months-ago 7)}
                 :model/Document {viewed-id :id} {:name           "Viewed recently, created before cutoff"
                                                  :collection_id  nil
                                                  :view_count     1
                                                  :last_viewed_at (datetime-months-ago 1)
                                                  :created_at     (datetime-months-ago 7)}]
    (let [ids (into #{}
                    (map :id)
                    (:rows (stale/find-candidates
                            {:collection-ids #{nil}
                             :cutoff-date    (date-months-ago 6)
                             :limit          100
                             :offset         0
                             :sort-column    :name
                             :sort-direction :asc
                             :models         #{:model/Document}})))]
      (testing "a never-viewed document created before the cutoff is stale despite a recent last_viewed_at"
        (is (contains? ids never-viewed-id)))
      (testing "a document viewed after the cutoff is not stale"
        (is (not (contains? ids viewed-id)))))))

(deftest documents-in-non-standard-collection-types-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {:type "instance-analytics"}
                 :model/Document _ (stale-document {:name "Doc" :collection_id col-id})]
    (is (= {:rows [] :total 0}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc
             :models         #{:model/Document}}))
        "should not include documents in non-standard collections")))

(deftest results-can-be-sorted
  (mt/with-temp [:model/Dashboard {id1 :id} {:name "Z"
                                             :last_viewed_at (datetime-months-ago 10)}
                 :model/Dashboard {id2 :id} {:name "Y"
                                             :last_viewed_at (datetime-months-ago 11)}
                 :model/Dashboard {id3 :id} {:name "X"
                                             :last_viewed_at (datetime-months-ago 12)}]
    (testing "by name"
      (testing "ascending"
        (is (= [{:id id3 :model :model/Dashboard}
                {:id id2 :model :model/Dashboard}
                {:id id1 :model :model/Dashboard}]
               (:rows
                (stale/find-candidates
                 {:collection-ids #{nil}
                  :cutoff-date    (date-months-ago 6)
                  :limit          10
                  :offset         0
                  :sort-column    :name
                  :sort-direction :asc})))))
      (testing "descending"
        (is (= [{:id id1 :model :model/Dashboard}
                {:id id2 :model :model/Dashboard}
                {:id id3 :model :model/Dashboard}]
               (:rows
                (stale/find-candidates
                 {:collection-ids #{nil}
                  :cutoff-date    (date-months-ago 6)
                  :limit          10
                  :offset         0
                  :sort-column    :name
                  :sort-direction :desc}))))))
    (testing "by last_used_at"
      (testing "ascending"
        (is (= [{:id id3 :model :model/Dashboard}
                {:id id2 :model :model/Dashboard}
                {:id id1 :model :model/Dashboard}]
               (:rows
                (stale/find-candidates
                 {:collection-ids #{nil}
                  :cutoff-date    (date-months-ago 6)
                  :limit          10
                  :offset         0
                  :sort-column    :last_used_at
                  :sort-direction :asc})))))
      (testing "descending"
        (is (= [{:id id1 :model :model/Dashboard}
                {:id id2 :model :model/Dashboard}
                {:id id3 :model :model/Dashboard}]
               (:rows
                (stale/find-candidates
                 {:collection-ids #{nil}
                  :cutoff-date    (date-months-ago 6)
                  :limit          10
                  :offset         0
                  :sort-column    :last_used_at
                  :sort-direction :desc}))))))))

(deftest limits-and-offset-work
  (with-stale-items [:model/Dashboard {id1 :id} {:name "A"}
                     :model/Dashboard {id2 :id} {:name "B"}]
    (testing "limits"
      (is (= [{:id id1 :model :model/Dashboard}]
             (:rows
              (stale/find-candidates
               {:collection-ids #{nil}
                :cutoff-date    (date-months-ago 6)
                :limit          1
                :offset         0
                :sort-column    :name
                :sort-direction :asc})))))
    (testing "offsets"
      (is (= [{:id id2 :model :model/Dashboard}]
             (:rows
              (stale/find-candidates
               {:collection-ids #{nil}
                :cutoff-date    (date-months-ago 6)
                :limit          1
                :offset         1
                :sort-column    :name
                :sort-direction :asc})))))
    (testing "total"
      (is (= 2 (:total
                (stale/find-candidates
                 {:collection-ids #{nil}
                  :cutoff-date    (date-months-ago 6)
                  :limit          1
                  :offset         0
                  :sort-column    :name
                  :sort-direction :asc})))))))

(deftest collection-set-is-taken-into-account
  (mt/with-temp [:model/Collection {col-id-1 :id :as col-1} {}
                 :model/Collection {col-id-2 :id} {:location (collection/children-location col-1)}
                 :model/Dashboard {id-1 :id} (stale-dashboard {:collection_id col-id-1
                                                               :name "A"})
                 :model/Dashboard {id-2 :id} (stale-dashboard {:collection_id col-id-2
                                                               :name "B"})]
    (is (= []
           (:rows
            (stale/find-candidates
             {:collection-ids #{nil}
              :cutoff-date    (date-months-ago 6)
              :limit          10
              :offset         0
              :sort-column    :name
              :sort-direction :asc})))
        "should not include dashboards from the collection")
    (is (= {:rows [{:model :model/Dashboard :id id-1}]
            :total 1}
           (stale/find-candidates
            {:collection-ids #{col-id-1}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc})))
    (is (= {:rows [{:model :model/Dashboard :id id-2}]
            :total 1}
           (stale/find-candidates
            {:collection-ids #{col-id-2}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc})))
    (is (= {:rows [{:model :model/Dashboard :id id-1}
                   {:model :model/Dashboard :id id-2}]
            :total 2}
           (stale/find-candidates
            {:collection-ids #{col-id-1 col-id-2}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc})))))

(deftest include-columns-adds-union-columns-to-rows
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Dashboard {id :id} (stale-dashboard {:name          "My Stale Dashboard"
                                                             :collection_id col-id})]
    (let [base {:collection-ids #{col-id}
                :cutoff-date    (date-months-ago 6)
                :limit          10
                :offset         0
                :sort-column    :name
                :sort-direction :asc}]
      (testing "rows carry only :id and :model by default"
        (is (= [{:id id :model :model/Dashboard}]
               (:rows (stale/find-candidates base)))))
      (testing ":include-columns adds the requested union columns"
        (is (=? [{:id           id
                  :model        :model/Dashboard
                  :name         "My Stale Dashboard"
                  :last_used_at some?}]
                (:rows (stale/find-candidates (assoc base :include-columns #{:name :last_used_at})))))))))

(deftest collection-ids-all-searches-instance-wide
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Dashboard {in-coll-id :id} (stale-dashboard {:name "A" :collection_id col-id})
                 :model/Dashboard {at-root-id :id} (stale-dashboard {:name "B" :collection_id nil})
                 :model/Dashboard {fresh-id :id} {:name           "C"
                                                  :collection_id  col-id
                                                  :last_viewed_at (datetime-months-ago 1)}]
    (let [ids (into #{}
                    (map :id)
                    (:rows (stale/find-candidates
                            {:collection-ids :all
                             :cutoff-date    (date-months-ago 6)
                             :limit          nil
                             :offset         nil
                             :sort-column    :name
                             :sort-direction :asc
                             :models         #{:model/Dashboard}})))]
      (testing ":all finds stale content both in collections and at the root"
        (is (contains? ids in-coll-id))
        (is (contains? ids at-root-id)))
      (testing "content used since the cutoff is still excluded"
        (is (not (contains? ids fresh-id)))))))

(deftest collection-set-with-nil-member-includes-root-content
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Collection {other-col-id :id} {}
                 :model/Dashboard {in-coll-id :id} (stale-dashboard {:name "A" :collection_id col-id})
                 :model/Dashboard {at-root-id :id} (stale-dashboard {:name "B" :collection_id nil})
                 :model/Dashboard {elsewhere-id :id} (stale-dashboard {:name "C" :collection_id other-col-id})]
    (let [ids (into #{}
                    (map :id)
                    (:rows (stale/find-candidates
                            {:collection-ids #{col-id nil}
                             :cutoff-date    (date-months-ago 6)
                             :limit          nil
                             :offset         nil
                             :sort-column    :name
                             :sort-direction :asc
                             :models         #{:model/Dashboard}})))]
      (testing "a nil member selects root-level content alongside the named collection"
        (is (contains? ids in-coll-id))
        (is (contains? ids at-root-id)))
      (testing "collections outside the set are excluded"
        (is (not (contains? ids elsewhere-id)))))))

(deftest cutoff-date-is-taken-into-account
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Dashboard _ {:name           "A"
                                     :collection_id  col-id
                                     :last_viewed_at #t "2024-02-15T00:01:00Z"}
                 :model/Card _      {:collection_id col-id
                                     :last_used_at  #t "2024-02-15T00:01:00Z"}
                 :model/Dashboard _ {:name           "B"
                                     :collection_id  col-id
                                     :last_viewed_at #t "2024-02-14T23:59:00Z"}
                 :model/Card _      {:collection_id col-id
                                     :last_used_at  #t "2024-02-14T23:59:00Z"}]
    (testing "Items viewed after midnight on the morning of the cutoff date are returned."
      (are [expected cutoff-date]
           (= expected
              (:total
               (stale/find-candidates
                {:collection-ids #{col-id}
                 :cutoff-date    (LocalDate/parse cutoff-date)
                 :limit          10
                 :offset         0
                 :sort-column    :name
                 :sort-direction :asc})))
        ;; At midnight the morning of 2-16, all items are included
        4 "2024-02-16"
        ;; At midnight the morning of 2-15, only the items viewed just before midnight are included
        2 "2024-02-15"
        ;; at midnight the morning of 2-14, no items are included
        0 "2024-02-14"))))

(deftest verified-cards-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/User {user-id :id} {}
                 :model/Card {id1 :id} (stale-card {:name "A" :collection_id col-id})
                 :model/Card {id2 :id} (stale-card {:name "B" :collection_id col-id})]
    ;; card 1 is verified, then unverified - it should be included
    (moderation-review/create-review! {:moderated_item_id   id1
                                       :moderator_id        user-id
                                       :moderated_item_type "card"
                                       :status              "verified"})
    (moderation-review/create-review! {:moderated_item_id   id1
                                       :moderator_id        user-id
                                       :moderated_item_type "card"
                                       :status              nil})
    ;; card 2 is unverified, then verified - it should NOT be included.
    (moderation-review/create-review! {:moderated_item_id   id2
                                       :moderator_id        user-id
                                       :moderated_item_type "card"
                                       :status              nil})
    (moderation-review/create-review! {:moderated_item_id   id2
                                       :moderator_id        user-id
                                       :moderated_item_type "card"
                                       :status              "verified"})
    (is (= {:rows  [{:id id1 :model :model/Card}]
            :total 1}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc}))
        "should not include verified dashboards or cards")))

(deftest cards-in-non-standard-collection-types-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {:type "instance-analytics"}
                 :model/Card _ (stale-card {:name "A" :collection_id col-id})
                 :model/Dashboard _ (stale-dashboard {:name "Dash" :collection_id col-id})]
    (is (= {:rows  []
            :total 0}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc}))
        "should not include cards or dashboard in non-standard collections")))

(deftest public-and-embedded-content-is-excluded
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Card {card-id1 :id} (stale-card {:name          "Acard"
                                                         :collection_id col-id
                                                         :public_uuid   (str (random-uuid))
                                                         :creator_id (mt/user->id :rasta)})
                 :model/Card {card-id2 :id} (stale-card {:name             "Bcard"
                                                         :collection_id    col-id
                                                         :enable_embedding true
                                                         :creator_id       (mt/user->id :rasta)})
                 :model/Card {card-id3 :id} (stale-card {:name          "Ccard"
                                                         :collection_id col-id
                                                         :creator_id   (mt/user->id :rasta)})
                 :model/Dashboard {dash-id1 :id} (stale-dashboard {:name          "Adash"
                                                                   :collection_id col-id
                                                                   :public_uuid   (str (random-uuid))
                                                                   :creator_id    (mt/user->id :rasta)})
                 :model/Dashboard {dash-id2 :id} (stale-dashboard {:name             "Bdash"
                                                                   :collection_id    col-id
                                                                   :enable_embedding true
                                                                   :creator_id       (mt/user->id :rasta)})
                 :model/Dashboard {dash-id3 :id} (stale-dashboard {:name          "Cdash"
                                                                   :collection_id col-id
                                                                   :creator_id    (mt/user->id :rasta)})]
    (testing "If public sharing and embedding are disabled, everything is returned"
      (mt/with-temporary-setting-values [enable-public-sharing false
                                         enable-embedding      false]
        (is (= {:rows  [{:id card-id1 :model :model/Card}
                        {:id dash-id1 :model :model/Dashboard}
                        {:id card-id2 :model :model/Card}
                        {:id dash-id2 :model :model/Dashboard}
                        {:id card-id3 :model :model/Card}
                        {:id dash-id3 :model :model/Dashboard}]
                :total 6}
               (stale/find-candidates
                {:collection-ids #{col-id}
                 :cutoff-date    (date-months-ago 6)
                 :limit          10
                 :offset         0
                 :sort-column    :name
                 :sort-direction :asc})))))
    (testing "If they are enabled, publicy shared or embedded content is excluded"
      (mt/with-temporary-setting-values [enable-public-sharing true
                                         enable-embedding      true]
        (is (= {:rows [{:id card-id3 :model :model/Card}
                       {:id dash-id3 :model :model/Dashboard}]
                :total 2}
               (stale/find-candidates
                {:collection-ids #{col-id}
                 :cutoff-date    (date-months-ago 6)
                 :limit          10
                 :offset         0
                 :sort-column    :name
                 :sort-direction :asc})))))))

(deftest questions-with-alerts-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Card {card-id :id} (stale-card {:name          "A"
                                                        :collection_id col-id})
                 :model/Card {card-2-id :id} (stale-card {:name "B"
                                                          :collection_id col-id})
                 :model/Pulse pulse {:alert_condition  "rows"
                                     :alert_first_only false
                                     :creator_id       (mt/user->id :rasta)
                                     :name             nil}
                 :model/PulseCard _ {:pulse_id (u/the-id pulse)
                                     :card_id  card-id
                                     :position 0}]
    (is (= {:rows  [{:model :model/Card :id card-2-id}]
            :total 1}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc}))
        "should not include cards with alerts")))

(deftest dashboards-with-subscriptions-are-excluded
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Dashboard {dash-id :id} (stale-dashboard {:name          "A"
                                                                  :collection_id col-id})
                 :model/Pulse _ {:alert_condition "rows"
                                 :alert_first_only false
                                 :creator_id (mt/user->id :rasta)
                                 :name nil
                                 :dashboard_id dash-id}]
    (is (= {:rows []
            :total 0}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc})))))

(deftest questions-used-as-data-source-for-sandboxes-are-excluded
  (met/with-gtaps! {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:< $id 3]})}}}
    (let [gtap-card-id (:id (t2/query-one {:select [:c.id]
                                           :from   [[:report_card :c]]
                                           :left-join [[:sandboxes :s] [:= :s.card_id :c.id]]
                                           :where     [:= :s.group_id (:id &group)]}))]
      (t2/update! :model/Card :id gtap-card-id {:last_used_at (datetime-months-ago 7)})
      (is (= {:rows []
              :total 0}
             (stale/find-candidates
              {:collection-ids #{nil}
               :cutoff-date    (date-months-ago 6)
               :limit          10
               :offset         0
               :sort-column    :name
               :sort-direction :asc})))
      (t2/delete! :model/Sandbox :card_id gtap-card-id)
      (is (= {:rows [{:model :model/Card :id gtap-card-id}]
              :total 1}
             (stale/find-candidates
              {:collection-ids #{nil}
               :cutoff-date    (date-months-ago 6)
               :limit          10
               :offset         0
               :sort-column    :name
               :sort-direction :asc}))))))

(deftest models-arg-selects-which-models-are-searched
  (with-stale-items [:model/Card {card-id :id} {:name "A card" :collection_id nil}
                     :model/Dashboard {dash-id :id} {:name "B dash" :collection_id nil}]
    (let [base {:collection-ids #{nil}
                :cutoff-date    (date-months-ago 6)
                :limit          10
                :offset         0
                :sort-column    :name
                :sort-direction :asc}
          rows #(:rows (stale/find-candidates %))]
      (testing "default (no :models) searches Card + Dashboard"
        (is (= [{:id card-id :model :model/Card}
                {:id dash-id :model :model/Dashboard}]
               (rows base))))
      (testing "explicit :models restricts the search to the given models"
        (is (= [{:id card-id :model :model/Card}]
               (rows (assoc base :models #{:model/Card}))))
        (is (= [{:id dash-id :model :model/Dashboard}]
               (rows (assoc base :models #{:model/Dashboard})))))
      (testing "a model with no registered staleness method fails loud (no silent drop)"
        (is (thrown-with-msg? clojure.lang.ExceptionInfo
                              #"No staleness method registered for"
                              (rows (assoc base :models #{:model/Card :model/Collection}))))))))

(deftest things-that-are-already-archived-do-not-appear
  (mt/with-temp [:model/Collection {col-id :id} {}
                 :model/Dashboard _ (stale-dashboard {:name          "A"
                                                      :collection_id col-id
                                                      :archived       true})
                 :model/Card _ (stale-card {:name          "B"
                                            :collection_id col-id
                                            :archived       true})]
    (is (= {:rows []
            :total 0}
           (stale/find-candidates
            {:collection-ids #{col-id}
             :cutoff-date    (date-months-ago 6)
             :limit          10
             :offset         0
             :sort-column    :name
             :sort-direction :asc})))))