[TASK] add freshness
This commit is contained in:
parent
73038ca70a
commit
2ea9033ba1
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<!-- 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">
|
||||
<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"/>
|
||||
{{else}}
|
||||
<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 |
|
@ -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}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
|
@ -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})
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
{{obj.good.count}}
|
||||
</div>
|
||||
<div class="label">
|
||||
Count of Goods
|
||||
Total Count of Goods
|
||||
</div>
|
||||
</div>
|
||||
<div class="statistic">
|
||||
|
|
Reference in New Issue