Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/clj/collect_earth_online/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
[triangulum.response :refer [data-response]]
[triangulum.type-conversion :as tc]
[triangulum.config :refer [get-config]]
[malli.error :as me]
[clojure.pprint :refer [pprint]]))
[malli.error :as me]))


(def Int
Expand Down Expand Up @@ -250,7 +249,7 @@
"next"
"id"]]
[:projectId Int]
[:visibleId Int]
[:visibleId :string]
[:threshold Int]
[:currentUserId [:maybe [:or :int :string]]]
[:projectType {:optional true} :string]
Expand Down Expand Up @@ -350,6 +349,7 @@
(data-response (me/humanize (m/explain schema# args#)) {:status 403})))))

(comment
(require '[clojure.pprint :refer [pprint]])
(->
(->> ~(keyword (str query))
(get validation-map)
Expand Down
2 changes: 1 addition & 1 deletion src/clj/collect_earth_online/db/geoai.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

(defonce gcs-resource
(st/init
{:project-id (get-config :gcs-integration :project-name)
#_{:project-id (get-config :gcs-integration :project-name)
:credentials (get-config :gcs-integration :credentials)}))

(defn generate-geojson [project-id]
Expand Down
145 changes: 66 additions & 79 deletions src/clj/collect_earth_online/db/plots.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@
- if no datum of coll matches pred, or if no datum exists in dir,
(ie, the first datum when dir = :prev or the last when dir = :next
returns nil."
[pred coll dir]
(let [loop-coll (into [nil] coll)]
(loop [[prev this & foll] loop-coll]
(if (pred this)
(case dir
:prev prev
:next (first foll))
(when (seq foll)
(recur (into [this] foll)))))))
([pred coll]
(pred-idx-nav pred nil coll))
([pred dir coll]
(let [loop-coll (into [nil] coll)]
(loop [[prev this & foll] loop-coll]
(if (pred this)
(case dir
:prev prev
:next (first foll)
this)
(when (seq foll)
(recur (into [this] foll))))))))

(defn filter-pred-idx
[pred coll]
Expand All @@ -58,8 +61,13 @@
(when (seq foll)
(recur (into [this] foll)))))))



(defn get-least [idx key coll]
(if (some (fn [this] (= (get this key) idx)) coll)
(filter-pred-idx (fn [this] (= (get this key) idx)) coll)
(let [least-idx (-> (filter (fn [this] (> (get this key) idx)) coll)
first
(get key))]
(filter-pred-idx (fn [this] (= (get this key) least-idx)) coll))))
;;;
;;; Project Level
;;;
Expand Down Expand Up @@ -142,11 +150,10 @@
(apply map sample-disagreement)
(average))))

(defn- filter-plot-disagreement [project-id grouped-plots threshold]
(defn- filter-plot-disagreement [project-id threshold plot-ids]
(let [survey-questions (get-survey-questions project-id)]
(filterm (fn [[_ plots]]
(let [plot-id (-> plots (first) :plot_id)
users-samples (->> (call-sql "select_qaqc_plot_samples" {:log? false} plot-id)
(filterm (fn [plot-id]
(let [users-samples (->> (call-sql "select_qaqc_plot_samples" {:log? false} plot-id)
(group-by :user_id)
(mapv (fn [[_ samples]]
(map (fn [{:keys [:saved_answers]}] (jsonb->clj-str saved_answers))
Expand All @@ -157,7 +164,7 @@
(question-disagreement)
(<= threshold)))
survey-questions)))
grouped-plots)))
plot-ids)))

(defn get-plot-disagreement
"Returns data containing the survey questions augmented with agreement
Expand Down Expand Up @@ -216,7 +223,7 @@
:visibleId visible_id})
(call-sql "select_plot_samples" {:log? false} plot-id user-id)))

(defn- build-collection-plot [plot-info user-uid review-mode? project-type]
(defn- build-collection-plot [plot-info user-uid review-mode? project-type navigation]
(let [{:keys [plot_id
flagged
confidence
Expand All @@ -242,25 +249,28 @@
(take 1 (filter (fn [s] (= 1 (:visibleId s))) samples))
samples)
:userId user_id
:email email}))
:email email
:plotNavigation navigation}))

(defn get-correct-plot-navigation
[project-id user-id current-user-id review-mode? navigation-mode project-type threshold reference-plot-id]
(if (and (= project-type "simplified")
(= navigation-mode "unanalyzed"))
(call-sql "select_simplified_project_plot" project-id)
(case navigation-mode
"unanalyzed" (call-sql "select_unanalyzed_plots" project-id user-id review-mode?)
"analyzed" (call-sql "select_analyzed_plots" project-id user-id review-mode?)
"flagged" (call-sql "select_flagged_plots" project-id user-id review-mode?)
"confidence" (call-sql "select_confidence_plots" project-id user-id review-mode? threshold)
"natural" (concat (call-sql "select_analyzed_plots" project-id user-id false)
(call-sql "select_unanalyzed_plots" project-id user-id false))
"user" (call-sql "select_analyzed_plots" project-id current-user-id false)
"qaqc" (call-sql "select_qaqc_plots" project-id)
"unanalyzed" (call-sql "select_unanalyzed_plot_ids" project-id user-id review-mode?)
"analyzed" (call-sql "select_analyzed_plot_ids" project-id user-id review-mode?)
"flagged" (call-sql "select_flagged_plot_ids" project-id user-id review-mode?)
"confidence" (call-sql "select_confidence_plot_ids" project-id user-id review-mode? threshold)
"natural" (concat (call-sql "select_analyzed_plot_ids" project-id user-id false)
(call-sql "select_unanalyzed_plot_ids" project-id user-id false))
"user" (call-sql "select_analyzed_plot_ids" project-id current-user-id false)
"qaqc" (call-sql "select_qaqc_plot_ids" project-id)
"similar" (call-sql "select_plots_by_similarity" project-id reference-plot-id)
[])))


(require '[clojure.pprint :refer [pprint]])
(defn get-collection-plot
"Gets plot information needed for the collections page. The plot
returned is based off of the navigation mode and direction. Valid
Expand All @@ -274,85 +284,62 @@
:projectType :string?
:currentUserId :int-str?
:inReviewMode :bool?}
:session {:userId :int-str?}}

"
[{:keys [params session]}]
:session {:userId :int-str?}}"
[{:keys [params session] :as this}]
;; (pprint (select-keys this [:params :session] ))
(let [navigation-mode (:navigationMode params "unanalyzed")
direction (:direction params "next")
project-id (tc/val->int (:projectId params))
old-visible-id (tc/val->int (:visibleId params))
visible-id (tc/val->int (:visibleId params))
threshold (tc/val->int (:threshold params))
ref-plot-id (tc/val->int (:referencePlotId params))
project-type (:projectType params "regular")
user-id (:userId session -1)
current-user-id (tc/val->int (:currentUserId params -1))
review-mode? (and (tc/val->bool (:inReviewMode params))
(is-proj-admin? user-id project-id nil))
proj-plots (get-correct-plot-navigation project-id
plot_ids (get-correct-plot-navigation project-id
user-id
current-user-id
review-mode?
navigation-mode
project-type
threshold
ref-plot-id)
grouped-plots (group-by :visible_id proj-plots)
sorted-plots (case navigation-mode
"qaqc" (sort-by first (filter-plot-disagreement project-id grouped-plots threshold))
"similar"
(if (some #(= (:visible_id %) old-visible-id) proj-plots)
(filter-pred-idx #(= (:visible_id %) old-visible-id) proj-plots)
(take 3 (into [nil nil] proj-plots)))
(sort-by first grouped-plots))
plots-info (case direction
"next" (case navigation-mode "similar"
(->> sorted-plots
(take-last 1)
(remove nil?)
seq)
(or
(->> sorted-plots
(some (fn [[visible-id plots]]
(and (> visible-id old-visible-id)
plots))))
(-> sorted-plots first second)))
"previous" (case navigation-mode "similar"
(->> sorted-plots
(take 1)
(remove nil?)
seq)
(or
(->> sorted-plots
(sort-by first #(compare %2 %1))
(some (fn [[visible-id plots]]
(and (< visible-id old-visible-id)
plots))))
(->> sorted-plots
(last)
(second))))
"id" (case navigation-mode "similar"
(->> sorted-plots
(take-last 2)
(remove nil?)
seq)
(some (fn [[visible-id plots]]
(and (= visible-id old-visible-id)
plots))
sorted-plots)))]
(if plots-info
ref-plot-id)
plot-ids (->> (case navigation-mode "qaqc"
(filter-plot-disagreement project-id threshold plot_ids)
(group-by :visible_id plot_ids))
(sort-by first)
(reduce (fn [acc [_ plot]] (into acc plot)) []))
[prev-plot
this-plot
next-plot] (get-least visible-id :visible_id plot-ids)
nav-plots (-> (case direction
"next" next-plot
"id" this-plot
"previous" prev-plot)
:visible_id (get-least :visible_id plot-ids))
plot-info (->> (case direction
"previous" prev-plot
this-plot)
:plot_id
(call-sql "select_plot_by_id"))]

(if (seq plot-info)
(try
(when (not= project-type "simplified")
(unlock-plots user-id)
(call-sql "lock_plot"
(:plot_id (first plots-info))
(:plot_id (first plot-info))
user-id
(time-plus-five-min)))
(data-response (map #(build-collection-plot % user-id review-mode? project-type) plots-info))
(data-response (map #(build-collection-plot % user-id review-mode? project-type nav-plots) plot-info))
(catch Exception _e
(data-response "Unable to get the requested plot. Please try again.")))
(data-response "not-found"))))



;;;
;;; Saving Plots
;;;
Expand Down
12 changes: 5 additions & 7 deletions src/js/collection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ export const Collection = ({ projectId, acceptedTerms, plotId }) => {

// GET PLOT DATA WHEN NEEDED - When getNewPlot changes to true, request plot data
useEffect(() => {
if(state.getNewPlot) {
getPlotData(state.newPlotId || -999, state.navDirection);
setState(s => ({...s, getNewPlot: false}));
}
state.getNewPlot && getPlotData(state.newPlotId, state.navDirection);
setState((prev) => ({...prev, getNewPlot: false}));
}, [state.getNewPlot]);

// UPDATE MAP WHEN STATE CHANGES — samples redraw (question/answers/visibility)
Expand Down Expand Up @@ -220,11 +218,11 @@ export const Collection = ({ projectId, acceptedTerms, plotId }) => {

useEffect(()=> {
state.navigationMode === 'similar' &&
setState((s)=> ({ ...s, referencePlotId: 1}));
setState((s)=> ({ ...s, referencePlotId: s.currentPlot.referencePlotId}));
}, [state.navigationMode]);

// API CALLS
const getPlotData = (visibleId=1, direction, forcedNavMode = null, reviewMode = null) => {
const getPlotData = (visibleId, direction, forcedNavMode = null, reviewMode = null) => {
processModal("Getting plot", () => {
return fetch(
"/get-collection-plot?" +
Expand All @@ -250,6 +248,7 @@ export const Collection = ({ projectId, acceptedTerms, plotId }) => {
} else {
setState (prev=> ({
... prev,
plotNavigation: data[0].plotNavigation,
userPlotList: data,
remainingPlotters: data,
currentPlot: data[0],
Expand Down Expand Up @@ -426,7 +425,6 @@ export const Collection = ({ projectId, acceptedTerms, plotId }) => {
}),
}).then((response) => {
if (!response.ok) {
console.log(response);
setState (s => ({...s, modal: {alert: {alertType: "Plot Lock Error", alertMessage: "Error maintaining plot lock. Your work may get overwritten. See console for details."}}}));
}
});
Expand Down
Loading