<commit_before>(ns frontend.components.build-head
  (:require [cljs.core.async :as async :refer [>! <! alts! chan sliding-buffer close!]]
            [clojure.string :as string]
            [frontend.async :refer [raise!]]
            [frontend.datetime :as datetime]
            [frontend.models.build :as build-model]
            [frontend.models.plan :as plan-model]
            [frontend.models.feature :as feature]
            [frontend.models.test :as test-model]
            [frontend.components.builds-table :as builds-table]
            [frontend.components.common :as common]
            [frontend.components.forms :as forms]
            [frontend.config :refer [intercom-enabled? github-endpoint env enterprise?]]
            [frontend.routes :as routes]
            [frontend.state :as state]
            [frontend.timer :as timer]
            [frontend.utils :as utils :include-macros true]
            [frontend.utils.github :as gh-utils]
            [frontend.utils.vcs-url :as vcs-url]
            [frontend.visualization.build :as viz-build]
            [goog.string :as gstring]
            [goog.string.format]
            [inflections.core :refer (pluralize)]
            [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true])
  (:require-macros [frontend.utils :refer [html defrender inspect]]))

;; This is awful, can't we just pass build-head the whole app state?
;; splitting it up this way means special purpose paths to find stuff
;; in it depending on what sub-state with special keys we have, right?
(defn has-scope [scope data]
  (scope (:scopes data)))

(defn show-additional-containers-offer? [plan build]
  (when (and plan build (not (enterprise?)))
    (let [usage-queued-ms (build-model/usage-queued-time build)
          run-queued-ms (build-model/run-queued-time build)]
      ;; more than 10 seconds waiting for other builds, and
      ;; less than 10 seconds waiting for additional containers (our fault)
      (< run-queued-ms 10000 usage-queued-ms))))

(defn additional-containers-offer [plan build]
  [:p#additional_containers_offer
   "Too much waiting? You can " [:a {:href (routes/v1-org-settings-subpage {:org (:org_name plan)
                                                                            :subpage "containers"})}
                                 "add more containers"]
   " and finish even faster."])

(defn new-additional-containers-offer [plan build]
  (let [run-phrase (if (build-model/finished? build)
                     "ran"
                     "is running")]
    [:div.additional-containers-offer
     [:p
      "This build "
      run-phrase
      " under "
      (:org_name plan)
      "'s plan which provides "
      (plan-model/usable-containers plan)
      " containers, plus 3 additional containers available for free and open source projects. "
      [:a {:href (routes/v1-org-settings-subpage {:org (:org_name plan)
                                                  :subpage "containers"})}
       "Add Containers"]
      " to run more builds concurrently."]]))

(defn build-queue [data owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (let [{:keys [build]} data
            build-id (build-model/id build)]
        (raise! owner [:usage-queue-why-showed
                       {:build-id build-id
                        :username (:username @build)
                        :reponame (:reponame @build)
                        :build_num (:build_num @build)}])))

    om/IRender
    (render [_]
      (let [{:keys [build builds]} data
            run-queued? (build-model/in-run-queue? build)
            usage-queued? (build-model/in-usage-queue? build)
            plan (:plan data)]
        (html
         (if-not builds
           [:div.loading-spinner common/spinner]
           [:div.build-queue.active
            (when (and (:queued_at build) (not usage-queued?))
              [:p "Circle " (when run-queued? "has") " spent "
               (om/build common/updating-duration {:start (:queued_at build)
                                                   :stop (or (:start_time build) (:stop_time build))})
               " acquiring containers for this build."])
            (when (< 10000 (build-model/run-queued-time build))
              [:p#circle_queued_explanation
               "We're sorry; this is our fault. Typically you should only see this when load spikes overwhelm our auto-scaling; waiting to acquire containers should be brief and infrequent."])

            (when (seq builds)
              (list
               [:p "This build " (if usage-queued? "has been" "was")
                " queued behind the following builds for "
                (om/build common/updating-duration {:start (:usage_queued_at build)
                                                    :stop (or (:queued_at build) (:stop_time build))})
                "."]

               (om/build builds-table/builds-table builds {:opts {:show-actions? true}})))
            (when (show-additional-containers-offer? plan build)
              (if (om/get-shared owner [:ab-tests :new_usage_queued_upsell])
                (new-additional-containers-offer plan build)
                (additional-containers-offer plan build)))]))))))

(defn build-queue-v2 [data owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (let [{:keys [build]} data
            build-id (build-model/id build)]
        (raise! owner [:usage-queue-why-showed
                       {:build-id build-id
                        :username (:username @build)
                        :reponame (:reponame @build)
                        :build_num (:build_num @build)}])))

    om/IRender
    (render [_]
      (let [{:keys [build builds]} data
            run-queued? (build-model/in-run-queue? build)
            usage-queued? (build-model/in-usage-queue? build)
            plan (:plan data)]
        (html
         (if-not builds
           [:div.loading-spinner common/spinner]
           [:div.build-queue.active
            [:div<commit_msg>launched the winning treatment of the useage queue upsell test<commit_after>(ns frontend.components.build-head
  (:require [cljs.core.async :as async :refer [>! <! alts! chan sliding-buffer close!]]
            [clojure.string :as string]
            [frontend.async :refer [raise!]]
            [frontend.datetime :as datetime]
            [frontend.models.build :as build-model]
            [frontend.models.plan :as plan-model]
            [frontend.models.feature :as feature]
            [frontend.models.test :as test-model]
            [frontend.components.builds-table :as builds-table]
            [frontend.components.common :as common]
            [frontend.components.forms :as forms]
            [frontend.config :refer [intercom-enabled? github-endpoint env enterprise?]]
            [frontend.routes :as routes]
            [frontend.state :as state]
            [frontend.timer :as timer]
            [frontend.utils :as utils :include-macros true]
            [frontend.utils.github :as gh-utils]
            [frontend.utils.vcs-url :as vcs-url]
            [frontend.visualization.build :as viz-build]
            [goog.string :as gstring]
            [goog.string.format]
            [inflections.core :refer (pluralize)]
            [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true])
  (:require-macros [frontend.utils :refer [html defrender inspect]]))

;; This is awful, can't we just pass build-head the whole app state?
;; splitting it up this way means special purpose paths to find stuff
;; in it depending on what sub-state with special keys we have, right?
(defn has-scope [scope data]
  (scope (:scopes data)))

(defn show-additional-containers-offer? [plan build]
  (when (and plan build (not (enterprise?)))
    (let [usage-queued-ms (build-model/usage-queued-time build)
          run-queued-ms (build-model/run-queued-time build)]
      ;; more than 10 seconds waiting for other builds, and
      ;; less than 10 seconds waiting for additional containers (our fault)
      (< run-queued-ms 10000 usage-queued-ms))))

(defn new-additional-containers-offer [plan build]
  (let [run-phrase (if (build-model/finished? build)
                     "ran"
                     "is running")]
    [:div.additional-containers-offer
     [:p
      "This build "
      run-phrase
      " under "
      (:org_name plan)
      "'s plan which provides "
      (plan-model/usable-containers plan)
      " containers, plus 3 additional containers available for free and open source projects. "
      [:a {:href (routes/v1-org-settings-subpage {:org (:org_name plan)
                                                  :subpage "containers"})}
       "Add Containers"]
      " to run more builds concurrently."]]))

(defn build-queue [data owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (let [{:keys [build]} data
            build-id (build-model/id build)]
        (raise! owner [:usage-queue-why-showed
                       {:build-id build-id
                        :username (:username @build)
                        :reponame (:reponame @build)
                        :build_num (:build_num @build)}])))

    om/IRender
    (render [_]
      (let [{:keys [build builds]} data
            run-queued? (build-model/in-run-queue? build)
            usage-queued? (build-model/in-usage-queue? build)
            plan (:plan data)]
        (html
         (if-not builds
           [:div.loading-spinner common/spinner]
           [:div.build-queue.active
            (when (and (:queued_at build) (not usage-queued?))
              [:p "Circle " (when run-queued? "has") " spent "
               (om/build common/updating-duration {:start (:queued_at build)
                                                   :stop (or (:start_time build) (:stop_time build))})
               " acquiring containers for this build."])
            (when (< 10000 (build-model/run-queued-time build))
              [:p#circle_queued_explanation
               "We're sorry; this is our fault. Typically you should only see this when load spikes overwhelm our auto-scaling; waiting to acquire containers should be brief and infrequent."])

            (when (seq builds)
              (list
               [:p "This build " (if usage-queued? "has been" "was")
                " queued behind the following builds for "
                (om/build common/updating-duration {:start (:usage_queued_at build)
                                                    :stop (or (:queued_at build) (:stop_time build))})
                "."]

               (om/build builds-table/builds-table builds {:opts {:show-actions? true}})))
            (when (show-additional-containers-offer? plan build)
                (new-additional-containers-offer plan build))]))))))

(defn build-queue-v2 [data owner]
  (reify
    om/IWillMount
    (will-mount [_]
      (let [{:keys [build]} data
            build-id (build-model/id build)]
        (raise! owner [:usage-queue-why-showed
                       {:build-id build-id
                        :username (:username @build)
                        :reponame (:reponame @build)
                        :build_num (:build_num @build)}])))

    om/IRender
    (render [_]
      (let [{:keys [build builds]} data
            run-queued? (build-model/in-run-queue? build)
            usage-queued? (build-model/in-usage-queue? build)
            plan (:plan data)]
        (html
         (if-not builds
           [:div.loading-spinner common/spinner]
           [:div.build-queue.active
            [:div
{'fraction_non_alphanumeric': 0.11980376968758069, 'fraction_numerical': 0.003012307427489457, 'mean_word_length': 1.8783750309635867, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '924101', 'n_tokens_mistral': 3435, 'n_tokens_neox': 3227, 'n_words': 903}