genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add freshness

This commit is contained in:
mlabusch 2017-05-12 10:54:05 +02:00
parent 73038ca70a
commit 2ea9033ba1
9 changed files with 62 additions and 6 deletions

View File

@ -35,6 +35,7 @@ func main() {
// Config packages: // Config packages:
web.GoodAvailablityTemplate = config.GoodAvailablityTemplate web.GoodAvailablityTemplate = config.GoodAvailablityTemplate
web.GoodFreshnessTemplate = config.GoodFreshnessTemplate
runtime.CacheConfig = config.CacheClean runtime.CacheConfig = config.CacheClean
runtime.ProductURL = config.MicroserviceDependencies.Product runtime.ProductURL = config.MicroserviceDependencies.Product
runtime.PermissionURL = config.MicroserviceDependencies.Permission runtime.PermissionURL = config.MicroserviceDependencies.Permission

View File

@ -2,6 +2,7 @@ webserver_bind = ":8080"
webroot = "webroot" webroot = "webroot"
good_availablity_template = "contrib/good_availablity.svg" good_availablity_template = "contrib/good_availablity.svg"
good_freshness_template = "contrib/good_freshness.svg"
[database] [database]
type = "sqlite3" type = "sqlite3"

View File

@ -1,7 +1,7 @@
<!-- SVG to show the current freshness of goods with a traffic light food labeling system --> <!-- SVG to show the current freshness of goods with a traffic light food labeling system -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<g class="arcs"> <g class="arcs">
{{if gt .Count 4}} {{if eq .Fresh false}}
<circle cx="50" cy="50" r="40" fill="#b02" stroke="#aaa" stroke-width="5"/> <circle cx="50" cy="50" r="40" fill="#b02" stroke="#aaa" stroke-width="5"/>
{{else}} {{else}}
<circle cx="50" cy="50" r="40" fill="#fff" stroke="#6a4" stroke-width="5"/> <circle cx="50" cy="50" r="40" fill="#fff" stroke="#6a4" stroke-width="5"/>

Before

Width:  |  Height:  |  Size: 438 B

After

Width:  |  Height:  |  Size: 442 B

View File

@ -46,7 +46,7 @@ Die übergeordnete Aufgabe dieses Microservice ist die Speicherung der Waren mit
\end{itemize} \end{itemize}
\item \textbf{Optionale Zusatzfunktionen} \item \textbf{Optionale Zusatzfunktionen}
\begin{itemize} \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) \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}
\end{itemize} \end{itemize}

View File

@ -13,6 +13,7 @@ import (
func BindAPI(router *goji.Mux) { func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/good/:productid"), listGoods) 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)) router.HandleFunc(pat.Post("/api/good/:productid"), http.PermissionHandler(addGood, runtime.HasPermission, runtime.PermissionCreateGood))
} }

View File

@ -13,6 +13,7 @@ import (
logger "github.com/genofire/hs_master-kss-monolith/lib/log" 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/models"
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
"time"
) )
// Function to list all goods // 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 // 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) count, log := getGoodAvailablityCount(w, r)
if count < 0 { if count < 0 {
return return
@ -77,3 +78,36 @@ func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
} }
log.Info("done") 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")
}

View File

@ -12,6 +12,7 @@ import (
// Path to the svg image template, that shows the availablity of a given good // Path to the svg image template, that shows the availablity of a given good
// with a traffic light food labeling system // with a traffic light food labeling system
var GoodAvailablityTemplate string var GoodAvailablityTemplate string
var GoodFreshnessTemplate string
// Function to calculate a percent value from a given value and an maximum value // Function to calculate a percent value from a given value and an maximum value
func tempPercent(value, max int) int { 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 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) { func getGoodAvailablitySVG(w http.ResponseWriter, count int) {
t := template.New("some") t := template.New("some")
@ -41,3 +42,20 @@ func getGoodAvailablitySVG(w http.ResponseWriter, count int) {
w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Content-Type", "image/svg+xml")
t.Execute(w, map[string]interface{}{"Count": count}) 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})
}

View File

@ -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 // 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"` GoodAvailablityTemplate string `toml:"good_availablity_template"`
GoodFreshnessTemplate string `toml:"good_freshness_template"`
// URLs to other microservices that this services uses // URLs to other microservices that this services uses
MicroserviceDependencies struct { MicroserviceDependencies struct {

View File

@ -6,7 +6,7 @@
{{obj.good.count}} {{obj.good.count}}
</div> </div>
<div class="label"> <div class="label">
Count of Goods Total Count of Goods
</div> </div>
</div> </div>
<div class="statistic"> <div class="statistic">