Here is some job data I receive from the backend. I'm writing a frontend in…
- User
Here is some job data I receive from the backend. I'm writing a frontend in ClojureScript, react, and reframe to handle it:
[{:wip-journaled nil, :wip-expensed nil, :job-id “58b04b8b-e481-459d-88a6-96525edd3262”, :uses-wip false, :total-draft-invoice-amount 0, :completed false, :total-approved-invoice-amount 0, :customer “City Limousines”, :actual-cost {:profession-cost 8684, :inventory-cost 1496, :asset-cost 1875}, :reference “J00001”, :update-ts #inst “2024-03-25T01:23:40.178-00:00”, :quote {:approved? false, :job-id “58b04b8b-e481-459d-88a6-96525edd3262”, :org-id “fc3433bb-c881-493c-89b2-dc36e7a5f6cc”, :currency-code “AUD”, :reference “1 - J00001 - Quote 1”, :total 58300, :id “597a798d-8f16-49ba-87df-a1f9a1b605bc”, :create-ts #inst “2024-03-25T00:41:32.285-00:00”, :sent? false}, :create-ts #inst “2024-03-25T01:23:40.178-00:00”, :estimated-cost 27251, :latest-invoice-ts nil} {:wip-journaled nil, :wip-expensed nil, :job-id “3e6e6c76-7dfd-48eb-afdc-6f496906a929”, :uses-wip false, :total-draft-invoice-amount 0, :completed false, :total-approved-invoice-amount 33000, :customer “Bayside Club”, :actual-cost {:profession-cost 0, :inventory-cost 8000, :asset-cost 2500}, :reference “J00002”, :update-ts #inst “2024-04-05T03:08:56.065-00:00”, :quote {:approved? false, :job-id “3e6e6c76-7dfd-48eb-afdc-6f496906a929”, :org-id “fc3433bb-c881-493c-89b2-dc36e7a5f6cc”, :currency-code “AUD”, :reference “3 - J00002”, :total 59520, :id “335193f9-b433-4231-ba5e-833d0614c391”, :create-ts #inst “2024-04-05T00:55:44.673-00:00”, :sent? false}, :create-ts #inst “2024-04-03T23:05:44.656-00:00”, :estimated-cost 28380, :latest-invoice-ts #inst “2024-04-05T00:59:53.357-00:00”} {:wip-journaled nil, :wip-expensed nil, :job-id “a743e4c6-23a6-4c6f-b827-c972bd9df5c2”, :uses-wip false, :total-draft-invoice-amount 70548, :completed false, :total-approved-invoice-amount 0, :customer “Abby & Wells”, :actual-cost {:profession-cost 0, :inventory-cost 8000, :asset-cost 0}, :reference “J00006”, :update-ts #inst “2024-04-05T03:15:25.939-00:00”, :quote nil, :create-ts #inst “2024-04-05T03:15:07.994-00:00”, :estimated-cost 22800, :latest-invoice-ts #inst “2024-04-05T03:18:30.983-00:00”}]
I’m able to extract the month numbers for each job with (util/job-month-numbers job-list). For the above data, it gives me (3 4 4). I want to sum the :total-approved-invoice-amount for each job in a given month. I want it formatted like [20323 85112]. Can you finish the function for me?
- Assistant
Sure! Here is a function that takes the job data and returns the total approved invoice amounts for each month:
(defn sum-approved-invoice-amounts-by-month [job-list] (->> job-list (map (fn [job] [(-> job :update-ts #(.getMonth %)) (-> job :total-approved-invoice-amount)])) (group-by first) (map (fn [[month amounts]] (reduce + (map second amounts)))))You can use this function by passing in your job data like this:
(sum-approved-invoice-amounts-by-month job-data)This will give you the total approved invoice amounts for each month in the format [20323 85112].
- User
- Assistant
It looks like the issue is that your
get-monthfunction is returning a Clojure#objectinstead of the expected integer value for the month.Here is a modified version of your code that properly extracts the month numbers and maps them with the estimated prices:
(defn get-month [ts] (.getMonth ts)) (map #(assoc {} (get-month (get % :update-ts)) (:estimated-price %)) foo)This code snippet should now give you the expected output with the keys as integers representing the month numbers.
- User
Using the new get-values-per-month, how would I rewrite the second function to work properly?
(defn get-values-per-month [job-list key] (map #(assoc {} (.getMonth (get % :update-ts)) (key %)) job-list))
(defn sum-amount-by-month2 [job-list key] (let [month-sums (->> job-list (group-by #(get-month (:update-ts %))) ; Group by month (reduce-kv (fn [acc k v] ; Sum amounts for each month (assoc acc k (reduce + 0 (map key v)))) {})) current-month-idx (get-current-month-index) nums-rotated-backwards (rotate-vector (vec (repeat 12 0)) (- 12 current-month-idx))] ; Rotate empty sums (reduce (fn [acc [k v]] ; Place sums into rotated vector (assoc acc (mod (+ k (- 12 1) current-month-idx) 12) v)) nums-rotated-backwards month-sums)))
- Assistant
To rewrite the
sum-amount-by-month2function using theget-values-per-monthfunction with the modifiedget-monthfunction, you can make the following changes:(defn sum-amount-by-month2 [job-list key] (let [month-sums (->> job-list (get-values-per-month key) (group-by key) (reduce-kv (fn [acc k v] (assoc acc k (reduce + 0 (map #(key %) v))) {})) current-month-idx (get-current-month-index) nums-rotated-backwards (rotate-vector (vec (repeat 12 0)) (- 12 current-month-idx))] ; Rotate empty sums (reduce (fn [acc [k v]] ; Place sums into rotated vector (assoc acc (mod (+ k (- 12 1) current-month-idx) 12) v)) nums-rotated-backwards month-sums)))With this modification, the
sum-amount-by-month2function now uses theget-values-per-monthfunction to extract the values for a specific key for each month and then sums those values for each month as before. This should work properly with your job data.