Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(defn generate-report
"take query-results and vectors each with the following info:
[header-display [key(s)] manipulation-function]
key(s) is a vector of nested keywords. i.e. [:operator :first-name]
which is passed to a get-in call
manipulation-function is the function to apply to the item.
returns a map with :headers and :rows"
[query-results & report-items]
(let [headers (vec (map first report-items))
process (fn [ks f]
#(f (get-in % ks)))
columns (map #(process (% 1) (% 2)) report-items)
rows (for [r query-results]
((apply juxt columns) r))]
{:headers headers
:rows rows}))
;;;;===============
;;==== display html
;;;;===============
(defn html-report
[headers rows]
[:table.report
[:tr.report-header
(for [h headers]
[:td h])]
(for [row rows]
[:tr.report-row
(for [item row]
item)])])
(defn scorecard-table
[cards]
(let [{:keys [headers rows]}
(generate-report
cards
["Operator" [:operator] employee-td]
["Evaluator" [:evaluator] employee-td]
["Client" [:client] client-td]
["Entry Time" [:timestamp] date-td]
["Call Time" [:call-date] date-td]
["Score" [:grade] grade-td])]
(html-report headers rows)))
;;;;================
;;==== export to xls
;;;;================
(defn xls-report
[headers rows]
(str
(join "\t" headers)
(apply str
(for [r rows]
(str "\n"
(join "\t" r))))))
(defn employee-display [emp]
(entity-display emp [:last-name ", " :first-name " (" :user-name ")"]))
(defn spreadsheet
[cards]
(let [{:keys [headers rows]}
(generate-report
cards
["Operator" [:operator] employee-display]
["Evaluator" [:evaluator] employee-display]
["Client Number" [:client :account-number] identity]
["Client Name" [:client :account-name] identity]
["Entry Time" [:timestamp] html/display-date]
["Call Time" [:call-date] html/display-date]
["Scored" [:grade :scored] html/display-number]
["Possible" [:grade :possible] html/display-number]
["Percentage" [:grade :percentage] #(* 100.0 %)]
["ID" [:_id] identity])]
(xls-report headers rows)))
Owner

bhenry commented Nov 14, 2010

the query results to generate-report can be any collection of maps i think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment