⚠️ WIP migrating tmp to patch

This commit is contained in:
pakintada@gmail.com 2024-02-22 16:04:34 +07:00
parent 89ce1f361c
commit fed315367a
13 changed files with 317 additions and 270 deletions

View file

@ -135,3 +135,39 @@ func (r *RedisCli) SetKeyTimeout(key string, value interface{}, timeout int) err
fmt.Println("error on EXPIRE ", err)
return err
}
func (r *RedisCli) KeyList() ([]string, error) {
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> KEYS error ", err)
return nil, err
}
keys := r.Client.Keys(context.Background(), "*")
return keys.Result()
}
// list operations
func (r *RedisCli) GetList(key string) ([]string, error) {
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> List.GET error ", err)
return nil, err
}
return r.Client.LRange(context.Background(), key, 0, -1).Result()
}
func (r *RedisCli) Add(key string, value interface{}) error {
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> List.ADD error ", err)
return err
}
err := r.Client.RPush(context.Background(), key, value)
return err.Err()
}

View file

@ -101,6 +101,8 @@ func (rr *RecipeRouter) Route(r chi.Router) {
r.Get("/saved/{country}/{filename_version_only}", rr.getSavedRecipes)
r.Get("/patch/get/{country}/{filename}", rr.getSavedAsPatches)
r.Get("/departments", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
@ -447,6 +449,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
tempRecipe := models.Recipe01{}
tempRecipe = tempRecipe.FromMap(changeMap)
rr.data.SetValuesToRecipe(targetRecipe.Recipe01, tempRecipe)
rr.taoLogger.Log.Debug("ApplyChange", zap.Any("status", "passed"))
// check if changed
@ -462,7 +465,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
// gen hash
commit_hash, err := data.HashCommit(8)
rr.cache_db.SetToKey(commit_hash, targetRecipe)
// rr.cache_db.SetToKey(commit_hash, targetRecipe)
commit := data.CommitLog{
@ -474,31 +477,68 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
Relation: filename,
}
// ------------------------ SKIP THIS ------------------------
// TODO: save only changes.
// get new tempfile name if redis is connected;
productCodeNoSpl := strings.ReplaceAll(changes.ProductCode, "-", "")
patchName := "Recipe_" + productCodeNoSpl + "-" + commit_hash + "_" + filename + ".patch"
// if cache service online
if rr.cache_db.HealthCheck() == nil {
// do change mode
commit.Change_file = patchName
commit.Relation = commit.Relation + "/patch"
// add to patch list of that filename
// filename:patchlist
err := rr.cache_db.Add(countryID+"."+filename+":patchList", commit.Id)
// add failed
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipe", zap.Error(errors.WithMessage(err, "Error when tried to add to patch list")))
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
} else {
// 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
}
// write to local if cannot connect
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
// full
err = encoder.Encode(targetRecipe)
// -------------------------------------------------------------
// partial
// err = encoder.Encode(changes)
// 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
}
}
// add to commit
err = data.Insert(&commit)
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
}
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
// full
err = encoder.Encode(targetRecipe)
// partial
// err = encoder.Encode(changes)
// 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
}
err = rr.cache_db.SetToKey(commit_hash+"_"+filename, changes)
err = rr.cache_db.SetToKey(patchName, changes)
// TODO: ^----- change this to patch
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipeCache", zap.Error(errors.WithMessage(err, "Error when write file")))
@ -567,6 +607,64 @@ func (rr *RecipeRouter) getSavedRecipes(w http.ResponseWriter, r *http.Request)
json.NewEncoder(w).Encode(map[string]interface{}{"files": commits})
}
func (rr *RecipeRouter) getSavedAsPatches(w http.ResponseWriter, r *http.Request) {
filename := chi.URLParam(r, "filename")
country := chi.URLParam(r, "country")
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", country), http.StatusNotFound)
return
}
patchList, err := rr.cache_db.GetList(countryID + "." + filename + ":patchList")
if err != nil {
// silent return, no patch
return
}
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()
if err != nil {
// keys found nothing
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
// loop through keys, if contain patch id
patchMap := map[string]models.Recipe01{}
for _, key := range keys {
if strings.Contains(key, filename) && strings.Contains(key, "patch") {
// check if legit saved file from patchList
for _, patchID := range patchList {
if strings.Contains(key, patchID) {
// get patch content
var recipePatch models.Recipe01
err := rr.cache_db.GetKeyTo(key, &recipePatch)
if err != nil {
// silent return, no patch
return
}
// append to patch list
// patchContents = append(patchContents, recipePatch)
patchMap[patchID] = recipePatch
}
}
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(patchMap)
rr.taoLogger.Log.Debug("RecipeRouter.getSavedAsPatches", zap.Any("patchMap", patchMap))
}
func (rr *RecipeRouter) getToppings(w http.ResponseWriter, r *http.Request) {
countryID := chi.URLParam(r, "country")
@ -626,7 +724,7 @@ func (rr *RecipeRouter) getRawRecipeOfProductCode(w http.ResponseWriter, r *http
productCode := chi.URLParam(r, "product_code")
// debug
rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("countryID", countryID), zap.Any("filename", filename), zap.Any("productCode", productCode))
// rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("countryID", countryID), zap.Any("filename", filename), zap.Any("productCode", productCode))
w.Header().Add("Content-Type", "application/json")
@ -638,7 +736,7 @@ func (rr *RecipeRouter) getRawRecipeOfProductCode(w http.ResponseWriter, r *http
}
// return recipe
rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("recipe", recipe))
// rr.taoLogger.Log.Debug("RecipeRouter.getRawRecipeOfProductCode", zap.Any("recipe", recipe))
json.NewEncoder(w).Encode(recipe)
}