add changes diffing modal & silence some logs

This commit is contained in:
pakintada@gmail.com 2024-03-01 00:22:28 +07:00
parent 148488e2c4
commit da353cec84
22 changed files with 1770 additions and 120 deletions

View file

@ -77,7 +77,7 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
}
commitDB, err := sqlx.Connect("sqlite3", "./data/database.db")
// fmt.Println("GetCommitLogOfFilename", err)
// //fmt.Println("GetCommitLogOfFilename", err)
if err != nil {
return nil, err
@ -89,7 +89,7 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
err = commitDB.Select(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
// fmt.Println("commits", err)
// //fmt.Println("commits", err)
if err != nil {
return nil, err
@ -112,7 +112,7 @@ func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, err
return nil, err
}
// fmt.Println("commitsByCountryID", len(commitsByCountryID) == 0)
// //fmt.Println("commitsByCountryID", len(commitsByCountryID) == 0)
if len(commitsByCountryID) == 0 {
return nil, fmt.Errorf("no commit found for %s", filename)

View file

@ -8,6 +8,7 @@ import (
"recipe-manager/helpers"
"recipe-manager/models"
"recipe-manager/services/logger"
"slices"
"strconv"
"strings"
"time"
@ -358,22 +359,22 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
// try convert
if len(countryID) != 3 {
for k, v := range d.CurrentCountryID {
fmt.Println("GetRecipe01ByProductCode.Iterate", k, v, v == countryID)
// //fmt.Println("GetRecipe01ByProductCode.Iterate", k, v, v == countryID)
if v == countryID {
countryID = k
break
}
}
}
fmt.Println("GetRecipe01ByProductCode", filename, countryID, productCode)
// //fmt.Println("GetRecipe01ByProductCode", filename, countryID, productCode)
if !strings.Contains(filename, "tmp") {
if filename == "" || filename == d.CurrentFile[countryID] {
// , d.CurrentFile, countryID, "result by country id", len(d.currentRecipe[countryID].Recipe01)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::filename", filename)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::countryID", countryID)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
// fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::filename", filename)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::countryID", countryID)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentFile", d.CurrentFile)
// //fmt.Println("GetRecipe01ByProductCode.ReadCurrent::CurrentCountryID", d.CurrentCountryID)
for _, v := range d.CurrentRecipe[countryID].Recipe01 {
if v.ProductCode == productCode {
@ -386,17 +387,17 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
}
}
}
// fmt.Println("No result in current recipe", countryID)
// //fmt.Println("No result in current recipe", countryID)
} else if recipe, ok := d.recipeMap[filename]; ok {
// fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
// //fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile, recipe.Recipe[countryID], "countryID=", countryID)
for _, v := range recipe.Recipe[countryID].Recipe01 {
if v.ProductCode == productCode {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
return v, nil
} else if len(v.SubMenu) > 0 {
for _, subMenu := range v.SubMenu {
if subMenu.ProductCode == productCode {
d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode.getSuccess", zap.Any("fromFile", filename), zap.Any("whereSource", d.recipeMap))
return subMenu, nil
}
}
@ -915,3 +916,114 @@ func (d *Data) GetCountryIDByName(countryName string) (string, error) {
}
return "", fmt.Errorf("country name: %s not found", countryName)
}
// ------ sorting ------
func (d *Data) SortRecipe(countryID, filename string, sort_by string) (error, []string) {
// Get recipe
recipe := d.GetRecipe(countryID, filename)
// error code
errorCode := 0
emptiedComparators := make([]string, 0)
// Sort
switch sort_by {
case "Product Code":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
if a.ProductCode == "" || b.ProductCode == "" {
errorCode = 1
emptiedComparators = append(emptiedComparators, a.ProductCode+" !compare! "+b.ProductCode)
}
return strings.Compare(a.ProductCode, b.ProductCode)
})
case "Name":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
if a.Name == "" || b.Name == "" {
errorCode = 2
emptiedComparators = append(emptiedComparators, a.Name+" !compare! "+b.Name)
}
return strings.Compare(a.Name, b.Name)
})
case "Other Name":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
if a.OtherName == "" || b.OtherName == "" {
errorCode = 3
emptiedComparators = append(emptiedComparators, a.OtherName+" !compare! "+b.OtherName)
}
return strings.Compare(a.OtherName, b.OtherName)
})
case "Description":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
if a.Description == "" || b.Description == "" {
errorCode = 4
emptiedComparators = append(emptiedComparators, a.Description+" !compare! "+b.Description)
}
return strings.Compare(a.Description, b.Description)
})
case "Other Description":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
if a.OtherDescription == "" || b.OtherDescription == "" {
errorCode = 5
emptiedComparators = append(emptiedComparators, a.OtherDescription+" !compare! "+b.OtherDescription)
}
return strings.Compare(a.OtherDescription, b.OtherDescription)
})
case "Last Updated":
slices.SortFunc(recipe.Recipe01, func(a, b models.Recipe01) int {
// parse date
layout := "02-Jan-2006 15:04:05"
if a.LastChange == "" || b.LastChange == "" {
errorCode = 6
emptiedComparators = append(emptiedComparators, a.ProductCode+":"+a.LastChange+" !compare! "+b.ProductCode+":"+b.LastChange)
}
timeA, err := time.Parse(layout, a.LastChange)
if err != nil {
// fmt.Println("Parse error! not in layout format: ", a.LastChange)
errorCode = 7
emptiedComparators = append(emptiedComparators, a.ProductCode+":"+a.LastChange)
}
timeB, err := time.Parse(layout, b.LastChange)
if err != nil {
// fmt.Println("Parse error! not in layout format: ", b.LastChange)
errorCode = 8
emptiedComparators = append(emptiedComparators, b.ProductCode+":"+b.LastChange)
}
if a.LastChange == "" && b.LastChange != "" {
errorCode = 0
return 1
} else if a.LastChange != "" && b.LastChange == "" {
errorCode = 0
return -1
} else if a.LastChange == "" && b.LastChange == "" {
errorCode = 0
return 0
}
return timeA.Compare(timeB)
})
}
if errorCode != 0 {
errStatus := fmt.Errorf("ERR[%v]", errorCode)
fmt.Println(errStatus)
return errStatus, emptiedComparators
}
return nil, emptiedComparators
}

View file

@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"
"github.com/redis/go-redis/v9"
@ -27,7 +26,7 @@ func NewRedisClient(address, password string) *RedisCli {
client := redis.NewClient(&options)
if err := client.Ping(context.Background()); err.Err() != nil {
fmt.Println("trying localhost ...", err)
// //fmt.Println("trying localhost ...", err)
client = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: password,
@ -35,13 +34,14 @@ func NewRedisClient(address, password string) *RedisCli {
})
if err_local := client.Ping(context.Background()); err_local.Err() != nil {
fmt.Println("> result ====> ", err_local)
// //fmt.Println("> result ====> ", err_local)
// do as warning
} else {
fmt.Println("\n> Localhost Redis OK!\n")
// //fmt.Println("\n> Localhost Redis OK!\n")
}
} else {
fmt.Println("\n> Redis OK! \n")
// //fmt.Println("\n> Redis OK! \n")
}
return &RedisCli{
@ -54,51 +54,51 @@ func (r *RedisCli) HealthCheck() error {
}
func (r *RedisCli) GetKeyTo(source string, dest interface{}) error {
fmt.Println("Redis> GET ", source)
// //fmt.Println("Redis> GET ", source)
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> GET error ", err)
// //fmt.Println("HS> GET error ", err)
return err
}
saved, err := r.Client.Get(context.Background(), source).Result()
// chcek EOF
// fmt.Println("GET last char ", saved[len(saved)-1:])
// //fmt.Println("GET last char ", saved[len(saved)-1:])
if saved == "" || err != nil {
fmt.Println("GET error (empty on error)|", err, "|while saved=", saved)
// //fmt.Println("GET error (empty on error)|", err, "|while saved=", saved)
return err
}
// fmt.Println("GET ", saved)
// //fmt.Println("GET ", saved)
// if err != nil {
// fmt.Println("GET error ", err)
// //fmt.Println("GET error ", err)
// return err
// }
err = json.NewDecoder(bytes.NewBufferString(saved)).Decode(dest)
if err != nil {
fmt.Println("GET error ", err)
// //fmt.Println("GET error ", err)
}
return err
}
func (r *RedisCli) SetToKey(key string, value interface{}) error {
fmt.Println("Redis> SET ", key)
// //fmt.Println("Redis> SET ", key)
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> SET error ", err)
// //fmt.Println("HS> SET error ", err)
return err
}
saved, err := json.Marshal(value)
if err != nil {
fmt.Println("SET error ", err)
//fmt.Println("SET error ", err)
return err
}
@ -106,7 +106,7 @@ func (r *RedisCli) SetToKey(key string, value interface{}) error {
err = r.Client.Set(context.Background(), key, saved, redis.KeepTTL).Err()
if err != nil {
fmt.Println("error on SET ", err)
//fmt.Println("error on SET ", err)
}
return err
@ -116,7 +116,7 @@ func (r *RedisCli) ExpireKey(key string) error {
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> EXPIRE error ", err)
//fmt.Println("HS> EXPIRE error ", err)
return err
}
@ -127,19 +127,19 @@ func (r *RedisCli) SetKeyTimeout(key string, value interface{}, timeout int) err
// if cannot pass healthcheck, return err
if err := r.HealthCheck(); err != nil {
fmt.Println("HS> EXPIRE error ", err)
//fmt.Println("HS> EXPIRE error ", err)
return err
}
err := r.Client.Expire(context.Background(), key, time.Duration(timeout)*time.Second).Err()
fmt.Println("error on EXPIRE ", 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)
//fmt.Println("HS> KEYS error ", err)
return nil, err
}
@ -153,7 +153,7 @@ func (r *RedisCli) KeyList() ([]string, error) {
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)
//fmt.Println("HS> List.GET error ", err)
return nil, err
}
@ -163,7 +163,7 @@ func (r *RedisCli) GetList(key string) ([]string, error) {
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)
//fmt.Println("HS> List.ADD error ", err)
return err
}

View file

@ -11,11 +11,9 @@ import (
func NewSqliteDatabase() *sqlx.DB {
// ensure that database exists
info, err := os.Stat("./data/database.db")
_, err := os.Stat("./data/database.db")
if os.IsNotExist(err) {
fmt.Println("No database found. Check path: ", err)
} else {
fmt.Println("Database existed. ", info)
fmt.Errorf("No database found. Check path: ", err)
}
db := sqlx.MustConnect("sqlite3", "./data/database.db")