Skip to content

Commit

Permalink
Statistics display
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Podivin <[email protected]>
  • Loading branch information
jpodivin committed Jan 22, 2024
1 parent 74e5f83 commit 57494f3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
6 changes: 6 additions & 0 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,9 @@ def iter_large_file(file_name: Path):
"Content-Length": str(_tar_path.stat().st_size),
},
)


@app.get("/stats")
def get_report_stats() -> dict:

return Storator3000.get_stats()
18 changes: 18 additions & 0 deletions backend/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,21 @@ def get_random(cls) -> Path:
files = cls.get_logs()

return Path(random.choice(files))

@classmethod
def get_stats(cls) -> dict:
"""Retrieve basic statistics about submitted reports.
"""
files = cls.get_logs()
stats = {
"total_reports" : len(files),
"avg_snippets" : 0.0
}
for path in files:
with open(path, encoding='utf-8') as f:
report = json.load(f)
stats['avg_snippets'] += sum([len(e['snippets']) for e in report['logs'].values()])
if stats['total_reports'] > 0:
stats['avg_snippets'] /= stats['total_reports']

return stats
15 changes: 15 additions & 0 deletions frontend/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,18 @@ a:hover {
--bs-spinner-width: 80px;
--bs-spinner-height: 80px;
}

#progressbar {
width: 100%;
background-color: var(--gray-5);
}

#progress {
height: 30px;
background-color: var(--newblue);
text-align: center;
line-height: 30px;
font-weight: 500;
color: var(--darkblue);
overflow-wrap: normal;
}
7 changes: 5 additions & 2 deletions frontend/src/app/core.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns app.core
"This namespace contains your application and is the entrypoint for 'yarn start'."
(:require [reagent.core :as r]
[app.homepage :refer [homepage]]
[app.homepage :refer [homepage fetch-stats-backend]]
[app.contribute :refer [contribute init-data]]
[app.review.core :refer [review init-data-review]]))

Expand All @@ -12,7 +12,10 @@
;; This is not the standard way of doing this, we should probably use some
;; router. But it is good enough for now.
(cond (.getElementById js/document "app-homepage")
(r/render [homepage] (.getElementById js/document "app-homepage"))
(do
(fetch-stats-backend)
(r/render [homepage] (.getElementById js/document "app-homepage"))
)

(.getElementById js/document "app-contribute")
(do
Expand Down
49 changes: 44 additions & 5 deletions frontend/src/app/homepage.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@
[clojure.string :as str]
[cljs.source-map.base64 :as base64 :refer [encode]]
[cljs.core.match :refer-macros [match]]
[app.homepage-validation :refer [validate]]))
[app.homepage-validation :refer [validate]]
[lambdaisland.fetch :as fetch]
[app.helpers :refer
[current-path
fontawesome-icon
remove-trailing-slash]]))


(def input-values (r/atom nil))
(def input-errors (r/atom []))
(def current-hash-atom (r/atom nil))

(def backend-stats (r/atom nil))
(def report-target (r/atom 1000))

(defn current-hash []
(. (. js/document -location) -hash))

(defn fetch-stats-backend []
(let [url (remove-trailing-slash (str "/stats" (current-path)))]
(-> (fetch/get url {:accept :json :content-type :json})
(.then (fn [resp]
(-> resp :body (js->clj :keywordize-keys true))))
(.then (fn [resp]
(reset! backend-stats resp))))))

(defn on-submit [event]
(.preventDefault event)
(validate current-hash-atom input-values input-errors)
Expand Down Expand Up @@ -79,6 +93,29 @@
(render-navigation-item "Upload" "#upload")
(render-navigation-item "Container" "#container")]])

(defn progress-width []
(min
(*
(float
(/
(:total_reports @backend-stats)
@report-target)) 100) 100)
)

(defn render-stats []
[:div {:id "progressbar"}
[:div
[:h5 "Are we there yet?"]
]
[:div {
:id "progress"
:style {:width (str (progress-width) "%")}
}
[:p (progress-width) "%"]
]
]
)

(defn render-card [provider url title img text inputs]
[:div {:class "card-body"}
[:div {:class "row"}
Expand Down Expand Up @@ -200,6 +237,8 @@
(defn homepage []
(reset! current-hash-atom
(if (str/blank? (current-hash)) "#copr" (current-hash)))
[:div {:class "card text-center"}
(render-navigation)
(render-cards)])
[:div {:class "card text-center"}
(render-stats)
(render-navigation)
(render-cards)]
)

0 comments on commit 57494f3

Please sign in to comment.