From ed7810cded372fd4669af2ee112c8bb10533393d Mon Sep 17 00:00:00 2001 From: "pakintada@gmail.com" Date: Fri, 23 Feb 2024 16:48:23 +0700 Subject: [PATCH] change ignore fresh updated, do update after merge instead. --- server/data/data.go | 23 ++++++++++++++++ server/routers/recipe.go | 55 +++++++++++++++++++++------------------ server/routers/topping.go | 3 +-- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/server/data/data.go b/server/data/data.go index afb69b6..c5e7a03 100644 --- a/server/data/data.go +++ b/server/data/data.go @@ -221,6 +221,16 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { if currentConfig != requestedConfig { d.taoLogger.Log.Debug("GetRecipe", zap.Any("ActualFileNotMatch", "Skip this!")) } else { + + // detect patches, return original + var cached_original_recipe models.Recipe + err := d.redisClient.GetKeyTo(filename, &cached_original_recipe) + + if err == nil && d.redisClient.HealthCheck() == nil { + d.taoLogger.Log.Debug("GetRecipe.NoReturnUpdated", zap.Any("target", filename)) + return &cached_original_recipe + } + // if equal, OK return d.CurrentRecipe[countryID] } @@ -239,6 +249,19 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { if currentConfig != requestedConfig { d.taoLogger.Log.Debug("GetRecipe", zap.Any("InvalidOnStored", "Skip this!")) } else { + + // detect patches, return original + + var cached_original_recipe models.Recipe + err := d.redisClient.GetKeyTo(filename, &cached_original_recipe) + + if err == nil && d.redisClient.HealthCheck() == nil { + d.taoLogger.Log.Debug("GetRecipe.NoReturnUpdated", zap.Any("target", filename)) + return &cached_original_recipe + } + + d.taoLogger.Log.Debug("GetRecipe.ReturnDefault", zap.Any("error_cache_recipe", err)) + // if equal, OK return recipe.Recipe[countryID] } diff --git a/server/routers/recipe.go b/server/routers/recipe.go index f12cb63..d7733e4 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -448,6 +448,8 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) { // Apply changes tempRecipe := models.Recipe01{} tempRecipe = tempRecipe.FromMap(changeMap) + + // Disable temp! Was updated the master by patch. Too fast rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe) rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed")) @@ -502,36 +504,35 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) { return } - } else { + } - // this following codes do need users to pass update to each other - // otherwise, the changes will diverge + // this following codes do need users to pass update to each other + // otherwise, the changes will diverge - file, _ := os.Create(temp_file_name) - if err != nil { - rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to create file"))) - http.Error(w, "Internal Error", http.StatusInternalServerError) - return - } + file, _ := os.Create(temp_file_name) + if err != nil { + rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to create file"))) + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - // write to local if cannot connect - encoder := json.NewEncoder(file) - encoder.SetIndent("", " ") - // full - err = encoder.Encode(targetRecipe) + // write to local if cannot connect + encoder := json.NewEncoder(file) + encoder.SetIndent("", " ") + // full + err = encoder.Encode(tempRecipe) - // ------------------------------------------------------------- + // ------------------------------------------------------------- - // partial - // err = encoder.Encode(changes) + // partial + // err = encoder.Encode(changes) - // put changes to redis + // put changes to redis - if err != nil { - rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when write file"))) - http.Error(w, "Internal Error", http.StatusInternalServerError) - return - } + if err != nil { + rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when write file"))) + http.Error(w, "Internal Error", http.StatusInternalServerError) + return } // add to commit @@ -624,7 +625,7 @@ func (rr *RecipeRouter) getSavedAsPatches(w http.ResponseWriter, r *http.Request return } - rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("targetPatchOf", countryID+"."+filename+":patchList"), zap.Any("patchList", patchList)) + // rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("targetPatchOf", countryID+"."+filename+":patchList"), zap.Any("patchList", patchList)) // find patch content from patch list keys, err := rr.cache_db.KeyList() @@ -662,7 +663,11 @@ func (rr *RecipeRouter) getSavedAsPatches(w http.ResponseWriter, r *http.Request w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(patchMap) - rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("patchMap", patchMap)) + // The above code is using the Zap logging library in Go to log a debug message with two fields. The + // first field is "targetPatchOf" with the value of countryID + "." + filename + ":patchList", and the + // second field is "patchList" with the value of the variable patchList. This log message is related + // to the "RecipeRouter.getSavedAsPatches" method. + // rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("patchMap", patchMap)) } func (rr *RecipeRouter) getToppings(w http.ResponseWriter, r *http.Request) { diff --git a/server/routers/topping.go b/server/routers/topping.go index 7a96f97..8dbafb8 100644 --- a/server/routers/topping.go +++ b/server/routers/topping.go @@ -2,7 +2,6 @@ package routers import ( "encoding/json" - "fmt" "net/http" "recipe-manager/data" "recipe-manager/services/logger" @@ -28,7 +27,7 @@ func NewToppingRouter(data *data.Data, taoLogger *logger.TaoLogger) *ToppingRout func (tr *ToppingRouter) Route(r chi.Router) { // check incoming request - fmt.Println("topping router", r.Routes()) + // fmt.Println("topping router", r.Routes()) r.Route("/toppover", func(r chi.Router) {