From 2ea9033ba187c0cd3239dbea27e684fbf3548ac9 Mon Sep 17 00:00:00 2001 From: mlabusch Date: Fri, 12 May 2017 10:54:05 +0200 Subject: [PATCH] [TASK] add freshness --- cmd/stock/main.go | 1 + config_example.conf | 1 + contrib/good_freshness.svg | 2 +- .../chapter/Anforderungen.tex | 2 +- http/bindapi.go | 3 +- http/good_show.go | 36 ++++++++++++++++++- http/good_temp.go | 20 ++++++++++- models/config.go | 1 + webroot/static/html/statistics.html | 2 +- 9 files changed, 62 insertions(+), 6 deletions(-) diff --git a/cmd/stock/main.go b/cmd/stock/main.go index 4114567..4355628 100644 --- a/cmd/stock/main.go +++ b/cmd/stock/main.go @@ -35,6 +35,7 @@ func main() { // Config packages: web.GoodAvailablityTemplate = config.GoodAvailablityTemplate + web.GoodFreshnessTemplate = config.GoodFreshnessTemplate runtime.CacheConfig = config.CacheClean runtime.ProductURL = config.MicroserviceDependencies.Product runtime.PermissionURL = config.MicroserviceDependencies.Permission diff --git a/config_example.conf b/config_example.conf index 217cbbd..5a8d718 100644 --- a/config_example.conf +++ b/config_example.conf @@ -2,6 +2,7 @@ webserver_bind = ":8080" webroot = "webroot" good_availablity_template = "contrib/good_availablity.svg" +good_freshness_template = "contrib/good_freshness.svg" [database] type = "sqlite3" diff --git a/contrib/good_freshness.svg b/contrib/good_freshness.svg index 71b0b20..5f807f1 100644 --- a/contrib/good_freshness.svg +++ b/contrib/good_freshness.svg @@ -1,7 +1,7 @@ - {{if gt .Count 4}} + {{if eq .Fresh false}} {{else}} diff --git a/documentation/microservice_stock/chapter/Anforderungen.tex b/documentation/microservice_stock/chapter/Anforderungen.tex index 9549a04..5d739e4 100644 --- a/documentation/microservice_stock/chapter/Anforderungen.tex +++ b/documentation/microservice_stock/chapter/Anforderungen.tex @@ -46,7 +46,7 @@ Die übergeordnete Aufgabe dieses Microservice ist die Speicherung der Waren mit \end{itemize} \item \textbf{Optionale Zusatzfunktionen} \begin{itemize} - \item Ausgabe einer Statistik, wie viele Waren im letzten Monat aus dem Warenbestand versandt und wie viele manuell entfernt wurden im Admin-Fontend + \item Ausgabe einer Statistik, wie viele Waren sich gesamt und durchschnittlich im Warenbestand befinden im Admin-Fontend \item Ampeldarstellung pro Ware, die Anzeigt ob diese bereits überaltert ist, im Admin-Frontend (ein Alter von mehr als X Tagen wird als überaltert angesehen) \end{itemize} \end{itemize} diff --git a/http/bindapi.go b/http/bindapi.go index ccc8625..6139bc9 100644 --- a/http/bindapi.go +++ b/http/bindapi.go @@ -13,6 +13,7 @@ import ( func BindAPI(router *goji.Mux) { router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/good/:productid"), listGoods) - router.HandleFunc(pat.Get("/api/good/availablity/:productid"), getGoodAvailablity) + router.HandleFunc(pat.Get("/api/good/availablity/:productid"), getGoodAvailability) + router.HandleFunc(pat.Get("/api/good/freshness/:goodid"), getGoodFreshness) router.HandleFunc(pat.Post("/api/good/:productid"), http.PermissionHandler(addGood, runtime.HasPermission, runtime.PermissionCreateGood)) } diff --git a/http/good_show.go b/http/good_show.go index 4381711..507bbfe 100644 --- a/http/good_show.go +++ b/http/good_show.go @@ -13,6 +13,7 @@ import ( logger "github.com/genofire/hs_master-kss-monolith/lib/log" "github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/runtime" + "time" ) // Function to list all goods @@ -63,7 +64,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr } // Function that returns the availability of a good -func getGoodAvailablity(w http.ResponseWriter, r *http.Request) { +func getGoodAvailability(w http.ResponseWriter, r *http.Request) { count, log := getGoodAvailablityCount(w, r) if count < 0 { return @@ -77,3 +78,36 @@ func getGoodAvailablity(w http.ResponseWriter, r *http.Request) { } log.Info("done") } + + + +// Function that returns the freshness of a good +func getGoodFreshness(w http.ResponseWriter, r *http.Request){ + log := logger.HTTP(r) + id, err := strconv.ParseInt(pat.Param(r, "goodid"), 10, 64) + if err != nil { + log.Warn("wrong goodid format") + http.Error(w, "the good id has a false format", http.StatusNotAcceptable) + return + } + log = log.WithField("goodid", id) + + var good models.Good + database.Read.Where("id = ?", id).First(&good) + if good.ProductID == 0 { + log.Warn("good not found") + http.Error(w, "the good was not found in database", http.StatusNotFound) + return + } + + fresh := good.FouledAt.Before(time.Now().Add(-time.Duration(3) * time.Hour * 24)) + + log = log.WithField("type", r.Header.Get("Content-Type")) + switch r.Header.Get("Content-Type") { + case "application/json": + lib.Write(w, fresh) + default: + getGoodFreshnessSVG(w, fresh) + } + log.Info("done") +} \ No newline at end of file diff --git a/http/good_temp.go b/http/good_temp.go index 06948af..cf3a7e0 100644 --- a/http/good_temp.go +++ b/http/good_temp.go @@ -12,6 +12,7 @@ import ( // Path to the svg image template, that shows the availablity of a given good // with a traffic light food labeling system var GoodAvailablityTemplate string +var GoodFreshnessTemplate string // Function to calculate a percent value from a given value and an maximum value func tempPercent(value, max int) int { @@ -23,7 +24,7 @@ func tempProcessRadius(value, max, radius int) float64 { return (1 - float64(value)/float64(max)) * float64(radius) * 2 * 3.14 } -// Function to get the SVG, that shows the traffic light food labeling system for a given good +// Function to get the SVG, that shows availybility with a traffic light food labeling system for a given good func getGoodAvailablitySVG(w http.ResponseWriter, count int) { t := template.New("some") @@ -41,3 +42,20 @@ func getGoodAvailablitySVG(w http.ResponseWriter, count int) { w.Header().Set("Content-Type", "image/svg+xml") t.Execute(w, map[string]interface{}{"Count": count}) } + + +// Function to get the SVG, that shows freshness with a traffic light food labeling system for a given good +func getGoodFreshnessSVG(w http.ResponseWriter, fresh bool) { + + t := template.New("some") + buf := bytes.NewBuffer(nil) + f, _ := os.Open(GoodFreshnessTemplate) // Error handling elided for brevity. + io.Copy(buf, f) // Error handling elided for brevity. + f.Close() + + s := string(buf.Bytes()) + t.Parse(s) + + w.Header().Set("Content-Type", "image/svg+xml") + t.Execute(w, map[string]interface{}{"Fresh": fresh}) +} \ No newline at end of file diff --git a/models/config.go b/models/config.go index 2eb3136..08b53b1 100644 --- a/models/config.go +++ b/models/config.go @@ -24,6 +24,7 @@ type Config struct { // path to the svg image templaes to show availablity of a given good with a traffic light food labeling system GoodAvailablityTemplate string `toml:"good_availablity_template"` + GoodFreshnessTemplate string `toml:"good_freshness_template"` // URLs to other microservices that this services uses MicroserviceDependencies struct { diff --git a/webroot/static/html/statistics.html b/webroot/static/html/statistics.html index 64bf082..b0b7a39 100644 --- a/webroot/static/html/statistics.html +++ b/webroot/static/html/statistics.html @@ -6,7 +6,7 @@ {{obj.good.count}}
- Count of Goods + Total Count of Goods