Browse Source

Improving the status page so that it shows some stats about the db

Jim Barritt 11 months ago
parent
commit
b8f278cff8

+ 2 - 1
apps/petitions/decode-app/ops/local/_lib.sh

@@ -3,6 +3,7 @@
3 3
 # https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
4 4
 # https://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
5 5
 # http://mywiki.wooledge.org/BashFAQ/031
6
+# http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html - redirections
6 7
 
7 8
 # TODO: make it so you have to prefix the commands with cmd_ or something so not all functions are availbale
8 9
 function lib/process_commands {
@@ -13,7 +14,7 @@ function lib/process_commands {
13 14
 
14 15
 function lib/ps {
15 16
     PROCESS_PATTERN=$1
16
-    echo $(ps aux | grep ${PROCESS_PATTERN} | grep -v grep)
17
+    echo $(ps aux | grep ${PROCESS_PATTERN} | grep -v grep | cut -c1-80)
17 18
 }
18 19
 
19 20
 # https://stackoverflow.com/questions/11231937/bash-ignoring-error-for-a-particular-command

+ 15 - 8
apps/petitions/decode-app/ops/local/decode-app.sh

@@ -12,7 +12,7 @@ source ${SCRIPT_HOME}/_lib.sh
12 12
 
13 13
 source ${SCRIPT_HOME}/mongo.lib.sh
14 14
 
15
-DECODE_APP_PORT=3040
15
+DECODE_APP_PORT=40300
16 16
 DECODE_APP_URL="http://localhost:${DECODE_APP_PORT}"
17 17
 DECODE_APP_DB_NAME="decode_sample"
18 18
 
@@ -59,19 +59,25 @@ function package {
59 59
 }
60 60
 
61 61
 function up {
62
-    echo -e  "\nBringing up local decode-app in background, to access. To kill 'do decode-app down'.\n"
63
-
64
-    java -jar target/decode-app-0.1.0-SNAPSHOT-standalone.jar &> ${DECODE_APP_LOG_FILE} &
62
+    DEBUG=${1:-}
63
+    if [[ ${DEBUG} == "debug" ]]; then
64
+        echo -e "\nBringing up local decode-app in foreground in debug mode (you can edit and live reload the changes). To kill. CTRL+C."
65
+        lein ring server-headless
66
+        echo -e "\nExited the server..\n"
67
+    else
68
+        echo -e  "\nBringing up local decode-app in background, to access. To kill 'do decode-app down'.\n"
69
+        java -jar target/decode-app-0.1.0-SNAPSHOT-standalone.jar &> ${DECODE_APP_LOG_FILE} &
70
+        echo $! > .decode-app.pid
71
+        lib/wait_for_service ${DECODE_APP_URL}
72
+        status
73
+    fi
65 74
 
66
-    echo $! > .decode-app.pid
67 75
 
68
-    lib/wait_for_service ${DECODE_APP_URL}
69
-    status
70 76
 }
71 77
 
72 78
 # https://www.tecmint.com/how-to-kill-a-process-in-linux/
73 79
 function down {
74
-    echo -e "\nTaking down local decode-app...\n"
80
+    echo -e "\nTaking down local decode-app..."
75 81
 
76 82
 
77 83
     if [[ ! -e .decode-app.pid ]]; then
@@ -79,6 +85,7 @@ function down {
79 85
     else
80 86
         PID=$(cat .decode-app.pid)
81 87
         kill -15 ${PID}
88
+        sleep 1s
82 89
         status
83 90
     fi
84 91
 

+ 1 - 1
apps/petitions/decode-app/project.clj

@@ -26,7 +26,7 @@
26 26
   :sass {:src "resources/stylesheets/"
27 27
          :output-directory "resources/public/stylesheets/"
28 28
          :command :sass}
29
-  :ring {:handler decode-app.handler/app  :port 3040 :host "0.0.0.0"}
29
+  :ring {:handler decode-app.handler/app  :port 40300 :host "0.0.0.0"}
30 30
   :profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
31 31
                                   [ring/ring-mock "0.3.0"]
32 32
                                   [danlentz/clj-uuid "0.1.7"]]}})

+ 58 - 40
apps/petitions/decode-app/src/decode_app/database.clj

@@ -1,71 +1,89 @@
1 1
 (ns decode-app.database
2
-    (:require [monger.core :as mg]
3
-      [monger.collection :as mc]
4
-      [monger.operators :refer :all]
5
-      [monger.result :refer [updated-existing?]]
6
-      [decode-app.config :as config])
7
-    (:import org.bson.types.ObjectId))
2
+  (:require [monger.core :as mg]
3
+            [monger.collection :as mc]
4
+            [monger.operators :refer :all]
5
+            [monger.result :refer [updated-existing?]]
6
+            [decode-app.config :as config])
7
+  (:import org.bson.types.ObjectId))
8 8
 
9 9
 (defn normalise-mongo-id [data]
10
-      "Swaps _id for id in the map"
11
-      (let [{:keys [_id]} data]
12
-           (->
13
-             (dissoc data :_id))))
10
+  "Swaps _id for id in the map"
11
+  (let [{:keys [_id]} data]
12
+    (->
13
+      (dissoc data :_id))))
14
+
15
+(defn get-conn [host port]
16
+    (mg/connect {:host host :port port}))
14 17
 
15 18
 (defn get-db []
16 19
   (let [config (config/get-config)
17 20
         database (:database config)
18 21
         host (:mongo-host config)
19 22
         port (:mongo-port config)]
20
-   (mg/get-db (mg/connect {:host host :port port}) database)))
23
+    (mg/get-db (get-conn host port) database)))
21 24
 
25
+(defn test-connection []
26
+  (let [db (get-db)]
27
+    (let [petition-count (mc/count db "petitions")
28
+          user-count (mc/count db "users")]
29
+      {:petition-count petition-count
30
+       :user-count user-count})))
22 31
 
32
+(defn status []
33
+  (let [config (config/get-config)
34
+        database (:database config)
35
+        host (:mongo-host config)
36
+        port (:mongo-port config)]
37
+    {:db-type "mongo"
38
+     :host host
39
+     :port port
40
+     (keyword database) (test-connection)}))
23 41
 
24
-(defn find-by-query [collection query]  
25
-  (let [db (get-db) 
42
+(defn find-by-query [collection query]
43
+  (let [db (get-db)
26 44
         result (mc/find-one-as-map db collection query)]
27
-           (if-not (empty? result)
28
-                   (normalise-mongo-id result)
29
-                   (throw (Exception. "Not found")))))
45
+    (if-not (empty? result)
46
+      (normalise-mongo-id result)
47
+      (throw (Exception. "Not found")))))
30 48
 
31 49
 (defn find-by-id [collection id]
32
-      (find-by-query collection {:id id}))
50
+  (find-by-query collection {:id id}))
33 51
 
34 52
 (defn find-all [collection]
35
-      (map normalise-mongo-id (mc/find-maps (get-db) collection)))
53
+  (map normalise-mongo-id (mc/find-maps (get-db) collection)))
36 54
 
37 55
 (defn increment-by-id [collection id incrementer]
38
-      (let [result (mc/update (get-db) collection {:id id} {$inc incrementer})]
39
-           (if (updated-existing? result)
40
-             (find-by-id collection id)
41
-             (throw (Exception. "Failed to increment by id")))))
56
+  (let [result (mc/update (get-db) collection {:id id} {$inc incrementer})]
57
+    (if (updated-existing? result)
58
+      (find-by-id collection id)
59
+      (throw (Exception. "Failed to increment by id")))))
42 60
 
43 61
 (defn push [collection id field item]
44
-      (let [result (mc/update (get-db) collection {:id id} {$push {field item}})]
45
-           (if (updated-existing? result)
46
-             (find-by-id collection id)
47
-             (throw (Exception. "Failed to push item")))))
62
+  (let [result (mc/update (get-db) collection {:id id} {$push {field item}})]
63
+    (if (updated-existing? result)
64
+      (find-by-id collection id)
65
+      (throw (Exception. "Failed to push item")))))
48 66
 
49 67
 
50 68
 
51 69
 (defn create-petition [chainspace-object-id title description]
52
-      (let [db (get-db)
53
-            object-id (ObjectId.)]
54
-           (mc/insert db "petitions"
55
-                      {:_id object-id
56
-                       :cs-id chainspace-object-id
57
-                       :title title
58
-                       :description description})
59
-           (str  object-id)))
70
+  (let [db (get-db)
71
+        object-id (ObjectId.)]
72
+    (mc/insert db "petitions"
73
+               {:_id         object-id
74
+                :cs-id       chainspace-object-id
75
+                :title       title
76
+                :description description})
77
+    (str object-id)))
60 78
 
61 79
 (defn close-petition [id outcome]
62 80
   (let [collection "petitions"
63 81
         db (get-db)
64 82
         petition (mc/find-one-as-map db collection {:id id})
65 83
         object-id (get petition :_id)]
66
-        (mc/update-by-id db collection object-id
67
-                         {$set {
68
-                                :isOpen  false
69
-                                :outcome outcome
70
-                                }
71
-                          })))
84
+    (mc/update-by-id db collection object-id
85
+                     {$set {
86
+                            :isOpen  false
87
+                            :outcome outcome
88
+                            }
89
+                      })))

+ 62 - 61
apps/petitions/decode-app/src/decode_app/handler.clj

@@ -1,45 +1,46 @@
1 1
 (ns decode-app.handler
2
-    (:use ring.util.response
3
-      [ring.middleware.json :only [wrap-json-body]]
4
-      [ring.util.response :only [response]])
5
-    (:require [compojure.core :refer :all]
6
-      [compojure.route :as route]
7
-      [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
8
-      [ring.middleware.params :refer [wrap-params]]
9
-      [ring.util.response :refer [response]]
10
-      [ring.util.codec :refer [url-decode form-encode]]
11
-      [ring.middleware.session :refer [wrap-session]]
12
-      [ring.middleware.json :refer [wrap-json-response]]
13
-      [ring.middleware.file :refer [wrap-file]]
14
-      [ring.middleware.reload :refer [wrap-reload]]
15
-      [views.contents :as contents]
16
-      [views.layout :as layout]
17
-      [decode-app.users :as users]
18
-      [decode-app.petitions :as petitions]
19
-      [decode-app.chainspace :as chainspace]
20
-      [buddy.auth :refer [authenticated?]]
21
-      [buddy.auth.backends.session :refer [session-backend]]
22
-      [buddy.auth.middleware :refer [wrap-authentication]]
23
-      [decode-app.config :as config]))
2
+  (:use ring.util.response
3
+        [ring.middleware.json :only [wrap-json-body]]
4
+        [ring.util.response :only [response]])
5
+  (:require [compojure.core :refer :all]
6
+            [compojure.route :as route]
7
+            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
8
+            [ring.middleware.params :refer [wrap-params]]
9
+            [ring.util.response :refer [response]]
10
+            [ring.util.codec :refer [url-decode form-encode]]
11
+            [ring.middleware.session :refer [wrap-session]]
12
+            [ring.middleware.json :refer [wrap-json-response]]
13
+            [ring.middleware.file :refer [wrap-file]]
14
+            [ring.middleware.reload :refer [wrap-reload]]
15
+            [views.contents :as contents]
16
+            [views.layout :as layout]
17
+            [decode-app.users :as users]
18
+            [decode-app.petitions :as petitions]
19
+            [decode-app.chainspace :as chainspace]
20
+            [buddy.auth :refer [authenticated?]]
21
+            [buddy.auth.backends.session :refer [session-backend]]
22
+            [buddy.auth.middleware :refer [wrap-authentication]]
23
+            [decode-app.config :as config]
24
+            [decode-app.status :as status]))
24 25
 
25 26
 
26 27
 (defn base-auth-url
27
-      [query-params]
28
-      (format "/auth/in?%s" (form-encode query-params)))
28
+  [query-params]
29
+  (format "/auth/in?%s" (form-encode query-params)))
29 30
 
30 31
 (defn redirect-auth-url
31
-      ([next]
32
-        (base-auth-url {:next next}))
33
-      ([next error]
34
-        (base-auth-url {:next next :error error})))
32
+  ([next]
33
+   (base-auth-url {:next next}))
34
+  ([next error]
35
+   (base-auth-url {:next next :error error})))
35 36
 
36 37
 (defn wrap-exception-handling
37
-      [handler]
38
-      (fn [request]
39
-          (try
40
-            (handler request)
41
-            (catch Exception e
42
-              {:status 500 :body "Internal Server Error"}))))
38
+  [handler]
39
+  (fn [request]
40
+    (try
41
+      (handler request)
42
+      (catch Exception e
43
+        {:status 500 :body "Internal Server Error"}))))
43 44
 
44 45
 (defroutes app-routes
45 46
            (GET "/" [] (layout/application "Index" (contents/index)))
@@ -51,32 +52,32 @@
51 52
            (GET "/petitions/:id" [id] (header (response (petitions/get-petition id)) "Access-Control-Allow-Origin" "*"))
52 53
 
53 54
            (POST "/petitions" request (let [current-url (:uri request)]
54
-                                                        (do
55
-                                                          (println "about to post to create petition")
56
-                                                          (let [petition-object-id (chainspace/create)
57
-                                                                title (get-in request [:body :title])
58
-                                                                description (get-in request [:body :description])
59
-                                                                petition-id (petitions/add-petition petition-object-id title description)]
60
-                                                               (println "Added petition object " petition-object-id)
61
-                                                               (created (str current-url (format "/%s" petition-id)))))))
55
+                                        (do
56
+                                          (println "about to post to create petition")
57
+                                          (let [petition-object-id (chainspace/create)
58
+                                                title (get-in request [:body :title])
59
+                                                description (get-in request [:body :description])
60
+                                                petition-id (petitions/add-petition petition-object-id title description)]
61
+                                            (println "Added petition object " petition-object-id)
62
+                                            (created (str current-url (format "/%s" petition-id)))))))
62 63
 
63 64
            (GET "/sign/petitions/:id" request (let [current-url (:uri request)
64 65
                                                     petition-id (get-in request [:params :id])]
65
-                                                   (if (authenticated? request)
66
-                                                     (let [email (get-in request [:session :identity])
67
-                                                           petition (petitions/get-petition petition-id)]
68
-                                                          (layout/application "Sign Petition"
69
-                                                                              (contents/sign-petition email current-url petition)))
70
-                                                     (redirect (redirect-auth-url current-url)))))
66
+                                                (if (authenticated? request)
67
+                                                  (let [email (get-in request [:session :identity])
68
+                                                        petition (petitions/get-petition petition-id)]
69
+                                                    (layout/application "Sign Petition"
70
+                                                                        (contents/sign-petition email current-url petition)))
71
+                                                  (redirect (redirect-auth-url current-url)))))
71 72
 
72 73
            (GET "/sign/petitions/:id/confirmation" request (let [current-url (:uri request)
73 74
                                                                  petition-id (get-in request [:params :id])]
74
-                                                                (if (authenticated? request)
75
-                                                                  (let [email (get-in request [:session :identity])
76
-                                                                        petition (petitions/get-petition petition-id)]
77
-                                                                       (layout/application "Petition signed"
78
-                                                                                           (contents/sign-petition-confirmation email current-url petition)))
79
-                                                                  (redirect (redirect-auth-url current-url)))))
75
+                                                             (if (authenticated? request)
76
+                                                               (let [email (get-in request [:session :identity])
77
+                                                                     petition (petitions/get-petition petition-id)]
78
+                                                                 (layout/application "Petition signed"
79
+                                                                                     (contents/sign-petition-confirmation email current-url petition)))
80
+                                                               (redirect (redirect-auth-url current-url)))))
80 81
 
81 82
            (POST "/close" request (let [petition-id "2"
82 83
                                         outcome (get-in request [:body])]
@@ -89,17 +90,17 @@
89 90
                                          error (get-in request [:query-params "error"])
90 91
                                          petition-id (re-find #"[^\/]+$" next)
91 92
                                          petition-title (:title (petitions/get-petition petition-id))]
92
-                                        (layout/application "Login" (contents/auth next error petition-title))))
93
+                                     (layout/application "Login" (contents/auth next error petition-title))))
93 94
 
94 95
            (POST "/auth/in" request (let [email (get-in request [:form-params "email"])
95 96
                                           password (get-in request [:form-params "password"])
96 97
                                           next (get-in request [:form-params "next"])
97 98
                                           session (:session request)]
98
-                                         (if (users/valid-credentials? email password)
99
-                                           (let [updated-session (assoc session :identity (keyword email))]
100
-                                                (-> (redirect next)
101
-                                                    (assoc :session updated-session)))
102
-                                           (redirect (redirect-auth-url next "invalid-credentials")))))
99
+                                      (if (users/valid-credentials? email password)
100
+                                        (let [updated-session (assoc session :identity (keyword email))]
101
+                                          (-> (redirect next)
102
+                                              (assoc :session updated-session)))
103
+                                        (redirect (redirect-auth-url next "invalid-credentials")))))
103 104
 
104 105
            (GET "/auth/out" request (-> (redirect (get-in request [:query-params "next"] "/"))
105 106
                                         (assoc :session {})))
@@ -108,7 +109,7 @@
108 109
 
109 110
 (def auth-backend (session-backend))
110 111
 
111
-(def app  
112
+(def app
112 113
   (-> app-routes
113 114
       (wrap-file "resources/public")
114 115
       (wrap-authentication auth-backend)

+ 3 - 2
apps/petitions/decode-app/src/decode_app/status.clj

@@ -1,4 +1,5 @@
1
-(ns decode-app.status)
1
+(ns decode-app.status
2
+  (:require [decode-app.database :as db]))
2 3
 
3 4
 (defn get-status []
4
-  {:mongo-connection "UP"})
5
+  {:database (db/status)})