feat(merge_component): Add merge from website

Merge contents from patch selected by user into newer version

merge from client feature
This commit is contained in:
pakintada@gmail.com 2024-03-04 11:19:11 +07:00
parent 292c7697a4
commit 09c21301d6
15 changed files with 343 additions and 42 deletions

View file

@ -154,8 +154,6 @@ func (rr *RecipeRouter) Route(r chi.Router) {
}
})
r.Post("/merge", rr.doMergeJson)
r.Get("/{country}/versions", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
@ -200,6 +198,8 @@ func (rr *RecipeRouter) Route(r chi.Router) {
})
r.Post("/sort/{country}/{filename}", rr.sortRecipe)
r.Post("/upgrade/{country}/{filename}", rr.upgradeVersion)
})
}
@ -419,7 +419,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
changes = changes.FromMap(ch_map)
rr.taoLogger.Log.Debug("Changes: ", zap.Any("changes", changes))
// TODO: find the matched pd
// find the matched pd
_, err = rr.data.GetRecipe01ByProductCode(filename, countryID, changes.ProductCode)
if err != nil {
@ -465,7 +465,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
// store @ temporary file
temp_file_name := helpers.GetTempFile(saved_filename, editor, 0)
// TODO: push this change, editor, commit_msg into db
// push this change, editor, commit_msg into db
// gen hash
commit_hash, err := data.HashCommit(8)
@ -482,7 +482,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
}
// ------------------------ SKIP THIS ------------------------
// TODO: save only changes.
// save only changes.
// get new tempfile name if redis is connected;
@ -541,7 +541,7 @@ func (rr *RecipeRouter) updateRecipe(w http.ResponseWriter, r *http.Request) {
err = data.Insert(&commit)
err = rr.cache_db.SetToKey(patchName, changes)
// TODO: ^----- change this to patch
// ^----- change this to patch
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.UpdateRecipeCache", zap.Error(errors.WithMessage(err, "Error when write file")))
@ -580,7 +580,7 @@ func (rr *RecipeRouter) updateMaterialSetting(w http.ResponseWriter, r *http.Req
return
}
// TODO: create commit and set change
// create commit and set change
}
@ -802,18 +802,6 @@ func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Req
png.Encode(w, thisImage)
}
func (rr *RecipeRouter) doMergeJson(w http.ResponseWriter, r *http.Request) {
// TODO: v2, change to binary instead
if !APIhandler(w, r) {
rr.taoLogger.Log.Warn("RecipeRouter.doMergeJson", zap.Error(errors.New("API is busy")))
return
} else {
rr.taoLogger.Log.Debug("RecipeRouter.doMergeJson", zap.Any("status", "ready"))
}
// TODO: add binary command here
}
func APIhandler(w http.ResponseWriter, r *http.Request) bool {
timeout := 10 * time.Second
@ -839,7 +827,8 @@ func lockThenTimeout(mutex *sync.Mutex, timeout time.Duration) bool {
}
}
// ------------------ Sorting ------------------
// ------------------ Sorting ------------------\
// FIXME: sorting not working, will look into it later.
func (rr *RecipeRouter) sortRecipe(w http.ResponseWriter, r *http.Request) {
country := chi.URLParam(r, "country")
filename := chi.URLParam(r, "filename")
@ -872,3 +861,63 @@ func (rr *RecipeRouter) sortRecipe(w http.ResponseWriter, r *http.Request) {
})
}
// TODO: apply contents from commit given by id
// this do match at server
func (rr *RecipeRouter) upgradeVersion(w http.ResponseWriter, r *http.Request) {
country := chi.URLParam(r, "country")
filename := chi.URLParam(r, "filename")
// get short version country
countryID, err := rr.data.GetCountryIDByName(country)
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get commit id and patch source from body
var commitInfo map[string]interface{}
err = json.NewDecoder(r.Body).Decode(&commitInfo)
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
mode := ""
rr.taoLogger.Log.Debug("RecipeRouter.upgradeVersion", zap.Any("commitInfo", commitInfo))
changeKey, ok := commitInfo["changeKey"].(string)
if !ok {
rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// optional AppliedMachineRecipe: if provided, use this instead
// this does mean recipe from machine has been applied on the client website
//
appliedMachineRecipe := commitInfo["appliedMachineRecipe"]
if appliedMachineRecipe != nil {
mode = "NoCache"
} else {
mode = "Recipe"
}
// upgrade version
// merge from website
result, err := rr.data.Merge(countryID, filename, mode, changeKey, nil)
if err != nil {
rr.taoLogger.Log.Error("RecipeRouter.upgradeVersion", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"result": result,
})
}