fix delay material fetching

This commit is contained in:
pakintada@gmail.com 2024-01-18 16:59:06 +07:00
parent db131d10c0
commit 4ece2cf30c
13 changed files with 220 additions and 143 deletions

View file

@ -18,7 +18,7 @@ import (
)
type RecipeWithTimeStamps struct {
Recipe models.Recipe
Recipe map[string]*models.Recipe
TimeStamps int64
}
@ -27,7 +27,7 @@ type Data struct {
CurrentCountryID map[string]string
DefaultCountryMap []DefaultByCountry
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
currentRecipe map[string]*models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
taoLogger *logger.TaoLogger
@ -58,7 +58,6 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultFile := "coffeethai02_600.json"
defaultCountry := "tha"
// TODO: read 'version' file by country
@ -102,6 +101,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
currentFileMap := make(map[string]string)
CurrentCountryIDMap := make(map[string]string)
currentDefaultFileForEachCountry := make(map[string]*models.Recipe)
// all default versions as string
versionsString := ""
@ -125,6 +125,13 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
versionsString = versionsString + v.CountryShortName + ":" + strconv.Itoa(current_version_iter) + ","
// do read default
defaultRecipe, err := helpers.ReadRecipeFile(v.CountryShortName, v2.Name)
if err != nil {
log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err)
}
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
break
}
}
@ -148,22 +155,24 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
// }
// }
taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
// FIXME: default file bug. do assign each default recipe model to each country
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
// taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
if err != nil {
log.Panic("Error when read default recipe file:", err)
}
// defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
// if err != nil {
// log.Panic("Error when read default recipe file:", err)
// }
return &Data{
CurrentFile: currentFileMap,
CurrentCountryID: CurrentCountryIDMap,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
currentRecipe: currentDefaultFileForEachCountry,
recipeMap: map[string]RecipeWithTimeStamps{
defaultFile: {
Recipe: *defaultRecipe,
Recipe: currentDefaultFileForEachCountry,
TimeStamps: time.Now().Unix(),
},
},
@ -176,17 +185,17 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
if countryID == "" {
return d.currentRecipe
return d.currentRecipe["tha"]
}
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe
return d.currentRecipe[countryID]
}
if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile[countryID] = filename
d.CurrentCountryID[countryID] = countryID
return &recipe.Recipe
return recipe.Recipe[countryID]
}
// change current version and read new recipe
@ -202,10 +211,10 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
if err != nil {
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe
return d.currentRecipe[countryID]
}
d.currentRecipe = recipe
d.currentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -222,34 +231,34 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
Recipe: d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
return d.currentRecipe
return d.currentRecipe[countryID]
}
func (d *Data) GetRecipe01() []models.Recipe01 {
return d.currentRecipe.Recipe01
}
// func (d *Data) GetRecipe01() []models.Recipe01 {
// return d.currentRecipe.Recipe01
// }
func (d *Data) GetCurrentRecipe() *models.Recipe {
return d.currentRecipe
}
// func (d *Data) GetCurrentRecipe() *models.Recipe {
// return d.currentRecipe
// }
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
if !strings.Contains(filename, "tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
fmt.Println("GetRecipe01ByProductCode.ReadCurrent", filename, d.CurrentFile)
for _, v := range d.currentRecipe.Recipe01 {
for _, v := range d.currentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, nil
}
}
} else if recipe, ok := d.recipeMap[filename]; ok {
fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile)
for _, v := range recipe.Recipe.Recipe01 {
for _, v := range recipe.Recipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, nil
}
@ -278,7 +287,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
if err != nil {
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
for _, v := range d.currentRecipe.Recipe01 {
for _, v := range d.currentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
return v, nil
}
@ -287,7 +296,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
d.currentRecipe = recipe
d.currentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -304,11 +313,11 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
Recipe: d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
for _, v := range d.currentRecipe.Recipe01 {
for _, v := range d.currentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
return v, nil
@ -358,22 +367,22 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
result := make([]models.MaterialSetting, 0)
if countryID == "" {
copy(result, d.currentRecipe.MaterialSetting)
copy(result, d.currentRecipe[countryID].MaterialSetting)
return result
}
if !strings.Contains(filename, "tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
copy(result, d.currentRecipe.MaterialSetting)
copy(result, d.currentRecipe[countryID].MaterialSetting)
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
return d.currentRecipe.MaterialSetting
return d.currentRecipe[countryID].MaterialSetting
}
if recipe, ok := d.recipeMap[filename]; ok {
copy(result, recipe.Recipe.MaterialSetting)
copy(result, recipe.Recipe[countryID].MaterialSetting)
d.CurrentFile[countryID] = filename
d.CurrentCountryID[countryID] = countryID
return d.currentRecipe.MaterialSetting
return d.currentRecipe[countryID].MaterialSetting
}
}
@ -389,13 +398,13 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
if err != nil {
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
copy(result, d.currentRecipe.MaterialSetting)
return d.currentRecipe.MaterialSetting
copy(result, d.currentRecipe[countryID].MaterialSetting)
return d.currentRecipe[countryID].MaterialSetting
}
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
d.currentRecipe = recipe
d.currentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -412,7 +421,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
Recipe: d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
@ -424,10 +433,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
var result []models.MaterialCode
if filename == "" || filename == d.CurrentFile[countryID] {
result = d.currentRecipe.MaterialCode
result = d.currentRecipe[countryID].MaterialCode
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile[countryID] = filename
return recipe.Recipe.MaterialCode
return recipe.Recipe[countryID].MaterialCode
} else {
if filename == "default" {
@ -440,10 +449,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
if err != nil {
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.MaterialCode
return d.currentRecipe[countryID].MaterialCode
}
d.currentRecipe = recipe
d.currentRecipe[countryID] = recipe
// save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
@ -460,11 +469,11 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
}
d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
Recipe: d.currentRecipe,
TimeStamps: time.Now().Unix(),
}
result = d.currentRecipe.MaterialCode
result = d.currentRecipe[countryID].MaterialCode
}
if len(ids) == 0 {
@ -491,10 +500,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
func (d *Data) GetToppings(countryID, filename string) models.Topping {
if filename == "" || filename == d.CurrentFile[countryID] {
return d.currentRecipe.Topping
return d.currentRecipe[countryID].Topping
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile[countryID] = filename
return recipe.Recipe.Topping
return recipe.Recipe[countryID].Topping
}
if filename == "default" {
@ -507,10 +516,10 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping {
if err != nil {
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
return d.currentRecipe.Topping
return d.currentRecipe[countryID].Topping
}
d.currentRecipe = recipe
d.currentRecipe[countryID] = recipe
return recipe.Topping
}