<commit_before>(ns swarmpit.component.node.info
  (:require [material.icon :as icon]
            [material.components :as comp]
            [material.component.form :as form]
            [material.component.label :as label]
            [material.component.list.basic :as list]
            [swarmpit.component.state :as state]
            [swarmpit.component.mixin :as mixin]
            [swarmpit.component.progress :as progress]
            [swarmpit.component.task.list :as tasks]
            [swarmpit.url :refer [dispatch!]]
            [swarmpit.ajax :as ajax]
            [swarmpit.routes :as routes]
            [clojure.contrib.humanize :as humanize]
            [sablono.core :refer-macros [html]]
            [rum.core :as rum]))

(enable-console-print!)

(defn resources
  [node]
  (let [cpu (-> node :resources :cpu (int))
        memory-bytes (-> node :resources :memory (* 1024 1024))
        disk-bytes (-> node :stats :disk :total)]
    (let [core-stats (str cpu " " (clojure.contrib.inflect/pluralize-noun cpu "core") ", "
                          (humanize/filesize memory-bytes :binary true) " memory")]
      (if (some? disk-bytes)
        (str core-stats ", " (humanize/filesize disk-bytes :binary true) " disk")
        core-stats))))

(defn- node-item-state [value]
  (case value
    "ready" (label/green value)
    "down" (label/red value)))

(rum/defc form-general < rum/static [node]
  (comp/card
    {:className "Swarmpit-form-card"
     :key       "ngc"}
    (comp/card-header
      {:title     (:nodeName node)
       :className "Swarmpit-form-card-header"
       :key       "ngch"
       :subheader (:address node)
       :action    (comp/tooltip
                    {:title "Edit node"
                     :key   "ngchaet"}
                    (comp/icon-button
                      {:aria-label "Edit"
                       :href       (routes/path-for-frontend :node-edit {:id (:id node)})}
                      (comp/svg icon/edit-path)))})
    (comp/card-content
      {:key "ngcc"}
      (html
        [:div {:key "ngccd"}
         [:span [:b "ENGINE: "] (str "docker " (:engine node))]
         [:br]
         [:span [:b "OS: "] [(:os node) " " (:arch node)]]
         [:br]
         [:span [:b "RESOURCES: "] (resources node)]]))
    (comp/card-content
      {:key "ngccl"}
      (form/item-labels
        [(node-item-state (:state node))
         (when (:leader node)
           (label/primary "Leader"))
         (label/grey (:role node))
         (if (= "active" (:availability node))
           (label/green "active")
           (label/grey (:availability node)))]))
    (comp/divider
      {:key "ncd"})
    (comp/card-content
      {:style {:paddingBottom "16px"}
       :key   "ngccf"}
      (form/item-id (:id node)))))

(def render-labels-metadata
  {:primary   (fn [item] (:name item))
   :secondary (fn [item] (:value item))})

(rum/defc form-labels < rum/static [labels id]
  (comp/card
    {:className "Swarmpit-card"
     :key       "nlc"}
    (comp/card-header
      {:className "Swarmpit-table-card-header"
       :key       "nlch"
       :title     (comp/typography
                    {:variant "h6"
                     :key     "labels-title"} "Labels")
       :action    (comp/icon-button
                    {:aria-label "Edit"
                     :href       (routes/path-for-frontend
                                   :node-edit {:id id}
                                   {:section "Labels"})}
                    (comp/svg icon/edit-path))})
    (comp/card-content
      {:className "Swarmpit-table-card-content"
       :key       "nlcc"}
      (rum/with-key
        (list/list
          render-labels-metadata
          labels
          nil) "nlccrl"))))

(rum/defc form-plugins < rum/static [networks volumes]
  (comp/card
    {:className "Swarmpit-card"
     :key       "npc"}
    (comp/card-header
      {:className "Swarmpit-form-card-header"
       :key       "npch"
       :title     (comp/typography
                    {:variant "h6"
                     :key     "plugins-title"} "Plugins")})
    (comp/card-content
      {:className "Swarmpit-form-card-content"
       :key       "npccn"}
      (form/subsection "Network")
      (map #(comp/chip
              {:label %
               :key   (str "np-" %)
               :style {:marginRight "5px"}}) networks))
    (comp/card-content
      {:className "Swarmpit-form-card-content"
       :key       "npccv"}
      (form/subsection "Volume")
      (map #(comp/chip
              {:label %
               :key   (str "vp-" %)
               :style {:marginRight "5px"}}) volumes))))

(rum/defc form-tasks < rum/static [tasks]
  (let [table-summary (->> (get-in tasks/render-metadata [:table :summary])
                           (filter #(not= "Node" (:name %)))
                           (into []))
        custom-metadata (assoc-in tasks/render-metadata [:table :summary] table-summary)]
    (comp/card
      {:className "Swarmpit-card"
       :key       "ftc"}
      (comp/card-header
        {:className "Swarmpit-table-card-header"
         :key       "ftch"
         :title     (comp/typography
                      {:variant "h6"
                       :key     "tasks-title"} "Tasks")})
      (comp/card-content
        {:className "Swarmpit-table-card-content"
         :key       "ftcc"}
        (rum/with-key
          (list/responsive
            custom-metadata
            (filter #(not (= "shutdown" (:state %))) tasks)
            tasks/onclick-handler) "ftccrl")))))

(defn- node-tasks-handler
  [node-id]
  (ajax/get
    (routes/path-for-backend :node-tasks {:id node-id})
    {:on-success (fn [{:keys [response]}]
                   (state/update-value [:tasks] response state/form-value-cursor))}))

(defn- node-handler
  [node-id]
  (ajax/get
    (routes/path-for-backend :node {:id node-id})
    {:state      [:loading?]
     :on-success (fn [{:keys [response]}]
                   (state/update-value [:node] response state/form-value-cursor))}))

(defn- init-form-state<commit_msg>node info messages when empty linked resources<commit_after>(ns swarmpit.component.node.info
  (:require [material.icon :as icon]
            [material.components :as comp]
            [material.component.form :as form]
            [material.component.label :as label]
            [material.component.list.basic :as list]
            [swarmpit.component.state :as state]
            [swarmpit.component.mixin :as mixin]
            [swarmpit.component.progress :as progress]
            [swarmpit.component.task.list :as tasks]
            [swarmpit.url :refer [dispatch!]]
            [swarmpit.ajax :as ajax]
            [swarmpit.routes :as routes]
            [clojure.contrib.humanize :as humanize]
            [sablono.core :refer-macros [html]]
            [rum.core :as rum]))

(enable-console-print!)

(defn resources
  [node]
  (let [cpu (-> node :resources :cpu (int))
        memory-bytes (-> node :resources :memory (* 1024 1024))
        disk-bytes (-> node :stats :disk :total)]
    (let [core-stats (str cpu " " (clojure.contrib.inflect/pluralize-noun cpu "core") ", "
                          (humanize/filesize memory-bytes :binary true) " memory")]
      (if (some? disk-bytes)
        (str core-stats ", " (humanize/filesize disk-bytes :binary true) " disk")
        core-stats))))

(defn- node-item-state [value]
  (case value
    "ready" (label/green value)
    "down" (label/red value)))

(rum/defc form-general < rum/static [node]
  (comp/card
    {:className "Swarmpit-form-card"
     :key       "ngc"}
    (comp/card-header
      {:title     (:nodeName node)
       :className "Swarmpit-form-card-header"
       :key       "ngch"
       :subheader (:address node)
       :action    (comp/tooltip
                    {:title "Edit node"
                     :key   "ngchaet"}
                    (comp/icon-button
                      {:aria-label "Edit"
                       :href       (routes/path-for-frontend :node-edit {:id (:id node)})}
                      (comp/svg icon/edit-path)))})
    (comp/card-content
      {:key "ngcc"}
      (html
        [:div {:key "ngccd"}
         [:span [:b "ENGINE: "] (str "docker " (:engine node))]
         [:br]
         [:span [:b "OS: "] [(:os node) " " (:arch node)]]
         [:br]
         [:span [:b "RESOURCES: "] (resources node)]]))
    (comp/card-content
      {:key "ngccl"}
      (form/item-labels
        [(node-item-state (:state node))
         (when (:leader node)
           (label/primary "Leader"))
         (label/grey (:role node))
         (if (= "active" (:availability node))
           (label/green "active")
           (label/grey (:availability node)))]))
    (comp/divider
      {:key "ncd"})
    (comp/card-content
      {:style {:paddingBottom "16px"}
       :key   "ngccf"}
      (form/item-id (:id node)))))

(def render-labels-metadata
  {:primary   (fn [item] (:name item))
   :secondary (fn [item] (:value item))})

(rum/defc form-labels < rum/static [labels id]
  (comp/card
    {:className "Swarmpit-card"
     :key       "nlc"}
    (comp/card-header
      {:className "Swarmpit-table-card-header"
       :key       "nlch"
       :title     (comp/typography
                    {:variant "h6"
                     :key     "labels-title"} "Labels")
       :action    (comp/icon-button
                    {:aria-label "Edit"
                     :href       (routes/path-for-frontend
                                   :node-edit {:id id}
                                   {:section "Labels"})}
                    (comp/svg icon/edit-path))})
    (if (empty? labels)
      (comp/card-content
        {:key "nlcce"}
        (html [:div "No labels defined for the node."]))
      (comp/card-content
        {:className "Swarmpit-table-card-content"
         :key       "nlcc"}
        (rum/with-key
          (list/list
            render-labels-metadata
            labels
            nil) "nlcclc")))))

(rum/defc form-plugins < rum/static [networks volumes]
  (comp/card
    {:className "Swarmpit-card"
     :key       "npc"}
    (comp/card-header
      {:className "Swarmpit-form-card-header"
       :key       "npch"
       :title     (comp/typography
                    {:variant "h6"
                     :key     "plugins-title"} "Plugins")})
    (comp/card-content
      {:className "Swarmpit-form-card-content"
       :key       "npccn"}
      (form/subsection "Network")
      (map #(comp/chip
              {:label %
               :key   (str "np-" %)
               :style {:marginRight "5px"}}) networks))
    (comp/card-content
      {:className "Swarmpit-form-card-content"
       :key       "npccv"}
      (form/subsection "Volume")
      (map #(comp/chip
              {:label %
               :key   (str "vp-" %)
               :style {:marginRight "5px"}}) volumes))))

(rum/defc form-tasks < rum/static [tasks]
  (let [table-summary (->> (get-in tasks/render-metadata [:table :summary])
                           (filter #(not= "Node" (:name %)))
                           (into []))
        custom-metadata (assoc-in tasks/render-metadata [:table :summary] table-summary)]
    (comp/card
      {:className "Swarmpit-card"
       :key       "ftc"}
      (comp/card-header
        {:className "Swarmpit-table-card-header"
         :key       "ftch"
         :title     (comp/typography
                      {:variant "h6"
                       :key     "tasks-title"} "Tasks")})
      (if (empty? tasks)
        (comp/card-content
          {:key "ftcce"}
          (html [:div "No tasks running on the node."]))
        (comp/card-content
          {:className "Swarmpit-table-card-content"
           :key       "ftcc"}
          (rum/with-key
            (list/responsive
              custom-metadata
              (filter #(not (= "shutdown" (:state %))) tasks)
              tasks/onclick-handler) "ftccrl"))))))

(defn- node-tasks-handler
  [node-id]
  (ajax/get
    (routes/path-for-backend :node-tasks {:id node-id})
    {:on-success (fn [{:keys [response]}]
                   (state/update-value [:tasks] response state/form-value-cursor))}))

(defn- node-handler
  [node-id]
  (ajax/get
    (routes/path-for-backend :node {:id node-id})
    {:state      [:loading?]
     :on-success (fn [{:keys [response]}]
                   (state/update-value [:node] response state/form-value-cursor))}))

(defn- init-form-state
{'fraction_non_alphanumeric': 0.15460261163952926, 'fraction_numerical': 0.0024181847493148475, 'mean_word_length': 1.7107275507974655, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708582', 'n_tokens_mistral': 4054, 'n_tokens_neox': 3871, 'n_words': 849}