Skip to content
Snippets Groups Projects
Commit e5232b13 authored by Cam Saul's avatar Cam Saul
Browse files

Code to load sample dataset on launch

parent 582850d7
No related merge requests found
......@@ -22,4 +22,4 @@ profiles.clj
/node_modules/
/.babel_cache
/coverage
/resources/sample-dataset-*
/resources/sample-dataset*
#! /bin/bash
echo "Running 'npm install' to download javascript dependencies..." &&
npm install &&
echo "Running 'webpack -p' to assemble and minify frontend assets..." &&
if [ -n "$CI_DISABLE_WEBPACK_MINIFICATION" ]; then
./node_modules/webpack/bin/webpack.js
else
./node_modules/webpack/bin/webpack.js -p
fi &&
echo "Running 'lein generate-sample-dataset' to generate the sample dataset..." &&
lein generate-sample-dataset &&
echo "Running 'lein uberjar'..." &&
lein uberjar
......@@ -11,6 +11,6 @@ test:
# 2) runs Eastwood linter
# 3) Bikeshed linter
# 4) runs JS linter + JS test
# 5) runs lein uberjar
- case $CIRCLE_NODE_INDEX in 0) MB_TEST_DATASETS=h2,mongo,postgres lein test ;; 1) MB_DB_TYPE=postgres MB_DB_DBNAME=circle_test MB_DB_PORT=5432 MB_DB_USER=ubuntu MB_DB_HOST=localhost lein test ;; 2) lein eastwood ;; 3) lein bikeshed --max-line-length 240 ;; 4) npm run lint && npm run build && npm run test ;; 5) CI_DISABLE_WEBPACK_MINIFICATION=1 lein uberjar ;; esac:
# 5) runs ./build-uberjar
- case $CIRCLE_NODE_INDEX in 0) MB_TEST_DATASETS=h2,mongo,postgres lein test ;; 1) MB_DB_TYPE=postgres MB_DB_DBNAME=circle_test MB_DB_PORT=5432 MB_DB_USER=ubuntu MB_DB_HOST=localhost lein test ;; 2) lein eastwood ;; 3) lein bikeshed --max-line-length 240 ;; 4) npm run lint && npm run build && npm run test ;; 5) CI_DISABLE_WEBPACK_MINIFICATION=1 ./build-uberjar ;; esac:
parallel: true
......@@ -36,7 +36,7 @@ Then run the HTTP server with
Check that the project can compile successfully with
lein uberjar
./build-uberjar
Run the linters with
......
(ns leiningen.npm
(:use clojure.java.shell))
(defn npm [projects & args]
;; TODO - some better validations such as checking if `npm` is available
(println "Running `npm install` to download javascript dependencies")
(let [result (sh "npm" "install")]
(if (= 0 (:exit result))
(println (:out result))
(println (:err result)))))
\ No newline at end of file
(ns leiningen.webpack
(:require [clojure.java.shell :refer :all]))
;; Set the CI_DISABLE_WEBPACK_MINIFICATION environment variable to skip minification which takes ~6+ minutes on CircleCI
(defn webpack [projects & args]
;; TODO - some better validations such as checking that we have webpack available
(println "Running `webpack -p` to assemble and minify frontend assets")
(let [result (sh (str (:root projects) "/node_modules/webpack/bin/webpack.js") (if (System/getenv "CI_DISABLE_WEBPACK_MINIFICATION") ""
"-p"))]
(if (= 0 (:exit result))
(println (:out result))
(println (:err result)))))
......@@ -90,8 +90,7 @@
"-Dmb.jetty.port=3010"
"-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" "generate-sample-dataset" "javac" "compile"]}
:uberjar {:aot :all}
: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"]
......
......@@ -11,10 +11,8 @@
[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)))
(str (System/getProperty "user.dir") "/resources/sample-dataset.db"))
(defn- normal-distribution-rand [mean median]
(dist/draw (dist/normal-distribution mean median)))
......
......@@ -16,6 +16,7 @@
[medley.core :as medley]
(metabase [config :as config]
[db :as db]
[driver :as driver]
[routes :as routes]
[setup :as setup]
[task :as task])
......@@ -23,6 +24,7 @@
[log-api-call :refer :all]
[format :refer :all])
(metabase.models [setting :refer [defsetting]]
[database :refer [Database]]
[user :refer [User]])))
;; ## CONFIG
......@@ -134,6 +136,30 @@
(.stop ^org.eclipse.jetty.server.Server @jetty-instance)
(reset! jetty-instance nil)))
(def ^:private ^:const sample-dataset-name "Sample Dataset")
(def ^:private ^:const sample-dataset-filename "sample-dataset.db.mv.db")
(defn- add-sample-dataset! []
(db/cascade-delete Database :name sample-dataset-name)
(when-not (db/exists? Database :name sample-dataset-name)
(try
(log/info "Loading sample dataset...")
(let [resource (-> (Thread/currentThread) ; hunt down the sample dataset DB file inside the current JAR
.getContextClassLoader
(.getResource sample-dataset-filename))]
(if-not resource
(log/error (format "Can't load sample dataset: the DB file '%s' can't be found by the ClassLoader." sample-dataset-filename))
(let [h2-file (-> (.getPath resource)
(s/replace #"^file:" "zip:") ; to connect to an H2 DB inside a JAR just replace file: with zip:
(s/replace #"\.mv\.db$" "") ; strip the .mv.db suffix from the path
(str ";USER=GUEST;PASSWORD=guest"))] ; specify the GUEST user account created for the DB
(driver/sync-database! (db/ins Database
:name sample-dataset-name
:details {:db h2-file}
:engine :h2)))))
(catch Throwable e
(log/error (format "Failed to load sample dataset: %s" (.getMessage e)))))))
(defn -main
"Launch Metabase in standalone mode."
......@@ -142,7 +168,10 @@
(try
;; run our initialization process
(init)
;; add the sample dataset DB if applicable
(add-sample-dataset!)
;; launch embedded webserver
(start-jetty)
(catch Exception e
(.printStackTrace e)
(log/error "Metabase Initialization FAILED: " (.getMessage e)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment