From 582850d73492a8d550a634e4899710b948e9176c Mon Sep 17 00:00:00 2001 From: Cam Saul <cam@geotip.com> Date: Fri, 31 Jul 2015 12:35:10 -0700 Subject: [PATCH] Make sample data generation a separate lein task --- .gitignore | 1 + project.clj | 12 ++++++++---- .../metabase/sample_dataset}/generate.clj | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) rename {src/metabase/sample_data => sample_dataset/metabase/sample_dataset}/generate.clj (96%) diff --git a/.gitignore b/.gitignore index fc3739969df..21db16fd5ee 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ profiles.clj /node_modules/ /.babel_cache /coverage +/resources/sample-dataset-* diff --git a/project.clj b/project.clj index 3c01fd9fa35..18146339b7b 100644 --- a/project.clj +++ b/project.clj @@ -5,7 +5,8 @@ :description "Metabase Community Edition" :url "http://metabase.com/" :min-lein-version "2.5.0" - :aliases {"test" ["with-profile" "+expectations" "expectations"]} + :aliases {"test" ["with-profile" "+expectations" "expectations"] + "generate-sample-dataset" ["with-profile" "+generate-sample-dataset" "run"]} :dependencies [[org.clojure/clojure "1.7.0"] [org.clojure/core.logic "0.8.10"] [org.clojure/core.match "0.3.0-alpha4"] ; optimized pattern matching library for Clojure @@ -30,9 +31,7 @@ [com.novemberain/monger "3.0.0"] ; MongoDB Driver [compojure "1.4.0"] ; HTTP Routing library built on Ring [environ "1.0.0"] ; easy environment management - [faker "0.2.2"] ; Fake data generator -- port of Perl/Ruby [hiccup "1.0.5"] ; HTML templating - [incanter/incanter-core "1.5.6"] ; Satistical functions like normal distibutions [korma "0.4.2"] ; SQL lib [log4j/log4j "1.2.17" :exclusions [javax.mail/mail @@ -92,4 +91,9 @@ "-Dmb.api.key=test-api-key" "-Xverify:none"]} ; disable bytecode verification when running tests so they start slightly faster :uberjar {:aot :all - :prep-tasks ^:replace ["npm" "webpack" "javac" "compile"]}}) + :prep-tasks ^:replace ["npm" "webpack" "generate-sample-dataset" "javac" "compile"]} + :generate-sample-dataset {:dependencies [[faker "0.2.2"] ; Fake data generator -- port of Perl/Ruby + [incanter/incanter-core "1.5.6"]] ; Satistical functions like normal distibutions}}) + :source-paths ["sample_dataset"] + :global-vars {*warn-on-reflection* false} + :main ^:skip-aot metabase.sample-dataset.generate}}) diff --git a/src/metabase/sample_data/generate.clj b/sample_dataset/metabase/sample_dataset/generate.clj similarity index 96% rename from src/metabase/sample_data/generate.clj rename to sample_dataset/metabase/sample_dataset/generate.clj index 50613029595..5c1711e86eb 100644 --- a/src/metabase/sample_data/generate.clj +++ b/sample_dataset/metabase/sample_dataset/generate.clj @@ -1,4 +1,4 @@ -(ns metabase.sample-data.generate +(ns metabase.sample-dataset.generate (:require [clojure.math.numeric-tower :as math] [clojure.string :as s] (faker [address :as address] @@ -11,6 +11,11 @@ [db :as kdb])) (:import java.util.Date)) +(def ^:private ^:const sample-dataset-version "1.0.0") + +(def ^:private ^:const sample-dataset-filename + (str (System/getProperty "user.dir") (format "/resources/sample-dataset-%s.db" sample-dataset-version))) + (defn- normal-distribution-rand [mean median] (dist/draw (dist/normal-distribution mean median))) @@ -345,7 +350,7 @@ ;; Insert the data (println "Inserting data...") - (metabase.util/pdoseq [[table rows] (seq data)] + (doseq [[table rows] (seq data)] (assert (keyword? table)) (assert (sequential? rows)) (let [entity (-> (k/create-entity (s/upper-case (name table))) @@ -362,3 +367,8 @@ (k/exec-raw db (format "GRANT SELECT ON %s TO GUEST;" (s/upper-case (name table))))) (println "Done.")))) + +(defn -main [& [filename]] + (let [filename (or filename sample-dataset-filename)] + (println (format "Writing sample dataset to %s..." filename)) + (create-h2-db filename))) -- GitLab