(defn calculate-frequencies [xs]
(reduce
(fn [xs x] (assoc xs x (inc (get xs x 0))))
{}
xs))
(defn greater-then [m n]
(filter #(> (val %) n) m))
(defproject spa-tutorial "0.1.0-SNAPSHOT"
:description "FIXME: write this!"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.521"]
[org.clojure/core.async "0.3.442"]
[org.omcljs/om "0.9.0"]
[prismatic/om-tools "0.4.0"]
[http-kit "2.3.0-alpha2"]
[secretary "1.2.3"]]
:plugins [[lein-cljsbuild "1.1.6"]]
:source-paths ["src" "target/classes"]
:clean-targets ["out/spa_tutorial" "out/spa_tutorial.js"]
:cljsbuild {
:builds [{:id "spa-tutorial"
:source-paths ["src"]
:compiler {
:main spa-tutorial.core
:output-to "out/spa_tutorial.js"
:output-dir "out"
:optimizations :none
:verbose true}}]})
(ns su.gornostaev.rstatfr2csv
(:use [clojure.xml :only (parse)])
(:use [clojure.string :only [index-of join split]])
(:use [clojure.java.io])
(:import java.io.File))
(defn parse-cmd
[args]
(when-let [file-name (first args)]
file-name))
(defn parse-file
[file-name]
(if (nil? file-name) nil (parse (File. file-name))))
(defn format-datetime
[dt]
(let [[date time] (split dt #"\s")]
(str (join "." (reverse (split date #"\."))) " " (subs time 0 (index-of time \:)) ":00")))
(defn merge-counters
[counters]
(map
(fn [item]
(let [[k & v] item]
(assoc (reduce (fn [acc val] (merge-with + acc val)) (first v)) :dt k)))
(reduce
(fn [acc val]
(let [{:keys [dt in out]} val]
(update-in acc [dt] (fnil #(conj % {:in in :out out}) []))))
{} counters)))
(defn parse-counters
[sensor]
(map
(fn [counter]
{:dt (format-datetime (:datetime (:attrs counter)))
:in (read-string (:realin (:attrs counter)))
:out (read-string (:realout (:attrs counter)))})
sensor))
(defn parse-sensors
[shop]
(map
(fn [sensor]
{:name (:name (:attrs sensor)) :counters (merge-counters (parse-counters (:content sensor)))})
shop))
(defn parse-shops
[dom]
(map
(fn [shop]
{:name (:name (:attrs shop)) :sensors (seq (parse-sensors (:content shop)))})
dom))
(defn parse-doc
[doc]
(parse-shops
(->>
doc
:content
first
:content)))
(defn save-csv
[data]
(with-open [w (writer (file "output.csv"))]
(doseq [shop data]
(doseq [sensor (:sensors shop)]
(doseq [counter (:counters sensor)]
(.write w (str (join "\t" (concat [(:name shop) (:name sensor)] (vals (into (sorted-map) counter)))) "\n")))))))
(save-csv (parse-doc (parse-file (parse-cmd *command-line-args*))))