display commit at recipe

This commit is contained in:
pakintada@gmail.com 2023-12-06 10:05:16 +07:00
parent 820557a268
commit f2ec0ed5fa
6 changed files with 640 additions and 555 deletions

View file

@ -1,194 +1,224 @@
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, tap } from 'rxjs'; import { Observable, tap } from 'rxjs';
import { import {
Recipe, Recipe,
Recipe01, Recipe01,
RecipeDetail, RecipeDetail,
RecipeDetailMat, RecipeDetailMat,
RecipeOverview, RecipeOverview,
RecipeOverviewList, RecipeOverviewList,
RecipesDashboard, RecipesDashboard,
} from '../models/recipe.model'; } from '../models/recipe.model';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { RecipeMetaData } from 'src/app/shared/types/recipe'; import { RecipeMetaData } from 'src/app/shared/types/recipe';
type RecipeOverviewParams = { type RecipeOverviewParams = {
filename: string; filename: string;
country: string; country: string;
materialIds: number[]; materialIds: number[];
offset: number; offset: number;
take: number; take: number;
search: string; search: string;
}; };
type RecipeDashboardParams = { type RecipeDashboardParams = {
filename: string; filename: string;
country: string; country: string;
}; };
interface RecipeFiles { interface RecipeFiles {
[key: string]: string[]; [key: string]: string[];
} }
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class RecipeService { export class RecipeService {
private countries: string[] = []; private countries: string[] = [];
private recipeFiles: RecipeFiles = {}; private recipeFiles: RecipeFiles = {};
constructor(private _httpClient: HttpClient) {} private tmp_files: string[] = [];
getRecipesDashboard( private get tmpfiles(): string[] {
params: RecipeDashboardParams = { return this.tmp_files;
country: this.getCurrentCountry(), }
filename: this.getCurrentFile(),
} constructor(private _httpClient: HttpClient) {}
): Observable<RecipesDashboard> {
return this._httpClient.get<RecipesDashboard>( getRecipesDashboard(
environment.api + '/recipes/dashboard', params: RecipeDashboardParams = {
{ country: this.getCurrentCountry(),
params: { filename: this.getCurrentFile(),
country: params.country, }
filename: params.filename, ): Observable<RecipesDashboard> {
}, return this._httpClient.get<RecipesDashboard>(
withCredentials: true, environment.api + '/recipes/dashboard',
responseType: 'json', {
} params: {
); country: params.country,
} filename: params.filename,
},
getRecipeOverview( withCredentials: true,
params: RecipeOverviewParams = { responseType: 'json',
country: this.getCurrentCountry(), }
filename: this.getCurrentFile(), );
materialIds: [], }
offset: 0,
take: 20, getRecipeOverview(
search: '', params: RecipeOverviewParams = {
} country: this.getCurrentCountry(),
): Observable<RecipeOverviewList> { filename: this.getCurrentFile(),
return this._httpClient.get<RecipeOverviewList>( materialIds: [],
environment.api + '/recipes/overview', offset: 0,
{ take: 20,
params: { search: '',
country: params.country, }
filename: params.filename, ): Observable<RecipeOverviewList> {
materialIds: params.materialIds.join(','), return this._httpClient.get<RecipeOverviewList>(
offset: params.offset.toString(), environment.api + '/recipes/overview',
take: params.take.toString(), {
search: params.search, params: {
}, country: params.country,
withCredentials: true, filename: params.filename,
responseType: 'json', materialIds: params.materialIds.join(','),
} offset: params.offset.toString(),
); take: params.take.toString(),
} search: params.search,
},
getRecipeDetail(productCode: string): Observable<RecipeDetail> { withCredentials: true,
return this._httpClient.get<RecipeDetail>( responseType: 'json',
environment.api + '/recipes/' + productCode, }
{ );
params: { }
filename: this.getCurrentFile(),
country: this.getCurrentCountry(), getRecipeDetail(productCode: string): Observable<RecipeDetail> {
}, return this._httpClient.get<RecipeDetail>(
withCredentials: true, environment.api + '/recipes/' + productCode,
responseType: 'json', {
} params: {
); filename: this.getCurrentFile(),
} country: this.getCurrentCountry(),
},
getRecipeDetailMat( withCredentials: true,
productCode: string responseType: 'json',
): Observable<{ result: RecipeDetailMat[] }> { }
return this._httpClient.get<{ result: RecipeDetailMat[] }>( );
environment.api + '/recipes/' + productCode + '/mat', }
{
params: { getRecipeDetailMat(
filename: this.getCurrentFile(), productCode: string
country: this.getCurrentCountry(), ): Observable<{ result: RecipeDetailMat[] }> {
}, return this._httpClient.get<{ result: RecipeDetailMat[] }>(
withCredentials: true, environment.api + '/recipes/' + productCode + '/mat',
responseType: 'json', {
} params: {
); filename: this.getCurrentFile(),
} country: this.getCurrentCountry(),
},
getCurrentFile(): string { withCredentials: true,
const currentRecipeFile = localStorage.getItem('currentRecipeFile'); responseType: 'json',
if (currentRecipeFile) { }
return currentRecipeFile; );
} }
return 'coffeethai02_580.json'; getCurrentFile(): string {
} const currentRecipeFile = localStorage.getItem('currentRecipeFile');
if (currentRecipeFile) {
getCurrentCountry(): string { return currentRecipeFile;
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry'); }
if (currentRecipeCountry) {
return currentRecipeCountry; return 'coffeethai02_580.json';
} }
return 'Thailand'; getCurrentCountry(): string {
} const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
if (currentRecipeCountry) {
getRecipesById(id: string): Observable<{ return currentRecipeCountry;
recipe: Recipe01; }
recipeMetaData: RecipeMetaData;
}> { return 'Thailand';
return this._httpClient.get<{ }
recipe: Recipe01;
recipeMetaData: RecipeMetaData; getRecipesById(id: string): Observable<{
}>(environment.api + '/recipes/' + id, { recipe: Recipe01;
withCredentials: true, recipeMetaData: RecipeMetaData;
responseType: 'json', }> {
}); return this._httpClient.get<{
} recipe: Recipe01;
recipeMetaData: RecipeMetaData;
getRecipeCountries(): Observable<string[]> { }>(environment.api + '/recipes/' + id, {
return this._httpClient withCredentials: true,
.get<string[]>(environment.api + '/recipes/versions', { responseType: 'json',
withCredentials: true, });
responseType: 'json', }
})
.pipe(tap((countries) => (this.countries = countries))); getRecipeCountries(): Observable<string[]> {
} return this._httpClient
.get<string[]>(environment.api + '/recipes/versions', {
getRecipeFiles(country: string): Observable<string[]> { withCredentials: true,
return this._httpClient responseType: 'json',
.get<string[]>(environment.api + '/recipes/versions/' + country, { })
withCredentials: true, .pipe(tap((countries) => (this.countries = countries)));
responseType: 'json', }
})
.pipe(tap((files) => (this.recipeFiles[country] = files))); getRecipeFiles(country: string): Observable<string[]> {
} return this._httpClient
.get<string[]>(environment.api + '/recipes/versions/' + country, {
getRecipeFileCountries(): string[] { withCredentials: true,
return this.countries; responseType: 'json',
} })
.pipe(tap((files) => (this.recipeFiles[country] = files)));
getRecipeFileNames(country: string): string[] | null { }
return this.recipeFiles[country] ?? null;
} getRecipeFileCountries(): string[] {
return this.countries;
editChanges(country: string, filename: string, change: any) { }
console.log('target version = ', filename);
console.log('change in edit: ', change.value); getRecipeFileNames(country: string): string[] | null {
return this._httpClient return this.recipeFiles[country] ?? null;
.post<{ }
status: string;
}>( editChanges(country: string, filename: string, change: any) {
environment.api + ('/recipes/edit/' + country + '/' + filename), console.log('target version = ', filename);
change.value, console.log('change in edit: ', change);
{ return this._httpClient
withCredentials: true, .post<{
responseType: 'json', status: string;
} }>(
) environment.api + ('/recipes/edit/' + country + '/' + filename),
.subscribe({ JSON.stringify(change),
next(value) { {
console.log(value, change.value); withCredentials: true,
}, responseType: 'json',
}); }
} )
} .subscribe({
next(value) {
console.log( value);
},
});
}
getSavedTmp(country: string, filename:string) {
console.log("loading saved .tmp* file", country, filename)
// do split filename
filename = filename.split('_')[1];
// this._user.getCurrentUser().subscribe((user) => {
// this.user = user.user;
// })
return this._httpClient
.get<string[]>(
environment.api + ("/recipes/saved/"+ country + "/" + filename),
{
withCredentials: true,
}
);
// .subscribe({
// next(value) {
// console.log( value);
// },
// });
}
}

View file

@ -112,17 +112,20 @@
</button> </button>
<!-- todo: add modal --> <!-- todo: add modal -->
<dialog id="select_savefile_modal" class="modal"> <dialog id="select_savefile_modal" class="modal">
<div class="modal-box max-w-[600px] overflow-visible"> <div class="modal-box max-w-[1000px] overflow-visible">
<p class="font-bold text-lg m-2">Saved Files</p> <p class="font-bold text-lg m-2">Saved Files</p>
<table class="table"> <table class="table">
<tr class="bg-primary "> <tr class="bg-primary ">
<th>Name</th> <th>Commit ID</th>
<th>Comment</th> <th>Comment</th>
<!-- <th></th> --> <th>Editor</th>
<th>Date</th>
</tr> </tr>
<tr class="row hover:bg-secondary" *ngFor="let file of savedTmpfiles"> <tr class="row hover:bg-secondary" *ngFor="let file of savedTmpfiles">
<td>{{ file }}</td> <td>{{ file.Id }}</td>
<td>"-"</td> <td>{{ file.Msg }}</td>
<td>{{ file.Editor }}</td>
<td>{{ file.Created_at }}</td>
<button class="btn bg-blue-400">Select</button> <button class="btn bg-blue-400">Select</button>
</tr> </tr>
</table> </table>

View file

@ -65,7 +65,7 @@ export class RecipesComponent implements OnInit, OnDestroy {
private searchStr = ''; private searchStr = '';
private oldSearchStr = ''; private oldSearchStr = '';
savedTmpfiles: string[] = []; savedTmpfiles: any[] = [];
tableCtx?: ElementRef; tableCtx?: ElementRef;
@ -146,7 +146,7 @@ export class RecipesComponent implements OnInit, OnDestroy {
this._recipeService.getCurrentFile() this._recipeService.getCurrentFile()
).subscribe({ ).subscribe({
next: (files:any) => { next: (files:any) => {
console.log("Obtain saves: ", typeof files); console.log("Obtain saves: ", typeof files, files);
if(files != undefined && typeof files === 'object'){ if(files != undefined && typeof files === 'object'){
// console.log("Obtain saves object: ", files.files[0], typeof files); // console.log("Obtain saves object: ", files.files[0], typeof files);
this.savedTmpfiles = files.files; this.savedTmpfiles = files.files;

View file

@ -1,6 +1,8 @@
package data package data
import ( import (
"strings"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"go.uber.org/zap" "go.uber.org/zap"
@ -60,9 +62,46 @@ func Insert(c *CommitLog) error {
return nil return nil
} }
// func GetCommitLogOfFilename(filename string) ([]CommitLog, error) { func GetCommitLogOfFilename(countryId string, filename string) ([]CommitLog, error) {
//}
// cut .json, split then get pos 2, check `change_file` startwith "filename" then return all quries filename = strings.TrimSuffix(filename, ".json")
// try split and get pos 1 and 2
filenameParts := strings.Split(filename, "_")
if len(filenameParts) == 2 {
filename = filenameParts[1]
} else if len(filenameParts) > 2 {
filename = filenameParts[1] + "_" + filenameParts[2]
}
Log.Debug("CommitDB", zap.Any("lookup", filename))
commitDB, err := sqlx.Connect("sqlite3", "./data/database.db")
if err != nil {
Log.Fatal("Error when connecting to database", zap.Error(err))
}
var commits []CommitLog
// get contains
// err = commitDB.Get(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
err = commitDB.Select(&commits, "SELECT * FROM commit_log WHERE change_file LIKE ?", "%"+filename+"%")
var commitsByCountryID []CommitLog
for _, v := range commits {
if strings.Contains(v.Change_file, countryId) {
commitsByCountryID = append(commitsByCountryID, v)
}
}
if err != nil {
Log.Error("Error when get commit log", zap.Error(err))
return nil, err
}
return commitsByCountryID, nil
}
func GetCommitLogs() ([]CommitLog, error) { func GetCommitLogs() ([]CommitLog, error) {
commit_db, err := sqlx.Connect("sqlite3", "./data/database.db") commit_db, err := sqlx.Connect("sqlite3", "./data/database.db")

View file

@ -1,338 +1,344 @@
package data package data
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"recipe-manager/helpers" "recipe-manager/helpers"
"recipe-manager/models" "recipe-manager/models"
"recipe-manager/services/logger" "recipe-manager/services/logger"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
) )
var ( var (
Log = logger.GetInstance() Log = logger.GetInstance()
) )
type RecipeWithTimeStamps struct { type RecipeWithTimeStamps struct {
Recipe models.Recipe Recipe models.Recipe
TimeStamps int64 TimeStamps int64
} }
type Data struct { type Data struct {
CurrentFile string CurrentFile string
CurrentCountryID string CurrentCountryID string
AllRecipeFiles map[string][]helpers.RecipePath AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName Countries []helpers.CountryName
} }
func NewData() *Data { func NewData() *Data {
countries := []helpers.CountryName{{ countries := []helpers.CountryName{{
CountryID: "tha", CountryID: "tha",
CountryName: "Thailand", CountryName: "Thailand",
}, { }, {
CountryID: "mys", CountryID: "mys",
CountryName: "Malaysia", CountryName: "Malaysia",
}, { }, {
CountryID: "aus", CountryID: "aus",
CountryName: "Australia", CountryName: "Australia",
}, },
} }
allRecipeFiles := helpers.ScanRecipeFiles(countries) allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultFile := "coffeethai02_600.json" defaultFile := "coffeethai02_600.json"
defaultCountry := "tha" defaultCountry := "tha"
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile) defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
if err != nil { if err != nil {
log.Panic("Error when read default recipe file:", err) log.Panic("Error when read default recipe file:", err)
} }
return &Data{ return &Data{
CurrentFile: defaultFile, CurrentFile: defaultFile,
CurrentCountryID: defaultCountry, CurrentCountryID: defaultCountry,
AllRecipeFiles: allRecipeFiles, AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe, currentRecipe: defaultRecipe,
recipeMap: map[string]RecipeWithTimeStamps{ recipeMap: map[string]RecipeWithTimeStamps{
defaultFile: { defaultFile: {
Recipe: *defaultRecipe, Recipe: *defaultRecipe,
TimeStamps: time.Now().Unix(), TimeStamps: time.Now().Unix(),
}, },
}, },
Countries: countries, Countries: countries,
} }
} }
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe { func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
if countryID == "" { if countryID == "" {
return d.currentRecipe return d.currentRecipe
} }
if filename == "" || filename == d.CurrentFile { if filename == "" || filename == d.CurrentFile {
return d.currentRecipe return d.currentRecipe
} }
if recipe, ok := d.recipeMap[filename]; ok { if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename d.CurrentFile = filename
d.CurrentCountryID = countryID d.CurrentCountryID = countryID
return &recipe.Recipe return &recipe.Recipe
} }
// change current version and read new recipe // change current version and read new recipe
d.CurrentFile = filename d.CurrentFile = filename
d.CurrentCountryID = countryID d.CurrentCountryID = countryID
recipe, err := helpers.ReadRecipeFile(countryID, filename) recipe, err := helpers.ReadRecipeFile(countryID, filename)
if err != nil { if err != nil {
logger.GetInstance().Error("Error when read recipe file", zap.Error(err)) logger.GetInstance().Error("Error when read recipe file [GetRecipe]", zap.Error(err))
return d.currentRecipe return d.currentRecipe
} }
d.currentRecipe = recipe d.currentRecipe = recipe
// save to map // save to map
if len(d.recipeMap) > 5 { // limit keep in memory 5 version if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version // remove oldest version
var oldestVersion string var oldestVersion string
var oldestTime int64 var oldestTime int64
for k, v := range d.recipeMap { for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime { if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps oldestTime = v.TimeStamps
oldestVersion = k oldestVersion = k
} }
} }
delete(d.recipeMap, oldestVersion) delete(d.recipeMap, oldestVersion)
} }
d.recipeMap[filename] = RecipeWithTimeStamps{ d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe, Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(), TimeStamps: time.Now().Unix(),
} }
return d.currentRecipe return d.currentRecipe
} }
func (d *Data) GetRecipe01() []models.Recipe01 { func (d *Data) GetRecipe01() []models.Recipe01 {
return d.currentRecipe.Recipe01 return d.currentRecipe.Recipe01
} }
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) { func (d *Data) GetCurrentRecipe() *models.Recipe {
return d.currentRecipe
if filename == "" || filename == d.CurrentFile { }
for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == productCode { func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
return v, nil
} if filename == "" || filename == d.CurrentFile {
} for _, v := range d.currentRecipe.Recipe01 {
} else if recipe, ok := d.recipeMap[filename]; ok { if v.ProductCode == productCode {
for _, v := range recipe.Recipe.Recipe01 { return v, nil
if v.ProductCode == productCode { }
return v, nil }
} } else if recipe, ok := d.recipeMap[filename]; ok {
} for _, v := range recipe.Recipe.Recipe01 {
} if v.ProductCode == productCode {
return v, nil
d.CurrentFile = filename }
d.CurrentCountryID = countryID }
recipe, err := helpers.ReadRecipeFile(countryID, filename) }
if err != nil { d.CurrentFile = filename
logger.GetInstance().Error("Error when read recipe file", zap.Error(err)) d.CurrentCountryID = countryID
for _, v := range d.currentRecipe.Recipe01 { recipe, err := helpers.ReadRecipeFile(countryID, filename)
if v.ProductCode == productCode {
return v, nil if err != nil {
} logger.GetInstance().Error("Error when read recipe file [GetRecipe01ByProductCode]", zap.Error(err))
} for _, v := range d.currentRecipe.Recipe01 {
} if v.ProductCode == productCode {
return v, nil
d.currentRecipe = recipe }
}
// save to map }
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
// remove oldest version d.currentRecipe = recipe
var oldestVersion string
var oldestTime int64 // save to map
for k, v := range d.recipeMap { if len(d.recipeMap) > 5 { // limit keep in memory 5 version
if oldestTime == 0 || v.TimeStamps < oldestTime { // remove oldest version
oldestTime = v.TimeStamps var oldestVersion string
oldestVersion = k var oldestTime int64
} for k, v := range d.recipeMap {
} if oldestTime == 0 || v.TimeStamps < oldestTime {
delete(d.recipeMap, oldestVersion) oldestTime = v.TimeStamps
} oldestVersion = k
}
d.recipeMap[filename] = RecipeWithTimeStamps{ }
Recipe: *d.currentRecipe, delete(d.recipeMap, oldestVersion)
TimeStamps: time.Now().Unix(), }
}
d.recipeMap[filename] = RecipeWithTimeStamps{
for _, v := range d.currentRecipe.Recipe01 { Recipe: *d.currentRecipe,
if v.ProductCode == productCode { TimeStamps: time.Now().Unix(),
return v, nil }
}
} for _, v := range d.currentRecipe.Recipe01 {
if v.ProductCode == productCode {
return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode) return v, nil
} }
}
func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
for _, v := range d.currentRecipe.Recipe01 { return models.Recipe01{}, fmt.Errorf("product code: %s not found", productCode)
if v.ProductCode == recipe.ProductCode { }
v = recipe
break func (d *Data) SetValuesToRecipe(recipe models.Recipe01) {
} for index, v := range d.currentRecipe.Recipe01 {
} if v.ProductCode == recipe.ProductCode {
} // Log.Debug("SetValuesToRecipe", zap.Any("old", v), zap.Any("new", recipe))
// v = recipe
func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting { d.currentRecipe.Recipe01[index] = recipe
result := make([]models.MaterialSetting, 0) break
}
if countryID == "" { }
copy(result, d.currentRecipe.MaterialSetting) }
return result
} func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialSetting {
result := make([]models.MaterialSetting, 0)
if filename == "" || filename == d.CurrentFile {
copy(result, d.currentRecipe.MaterialSetting) if countryID == "" {
return result copy(result, d.currentRecipe.MaterialSetting)
} return result
}
if recipe, ok := d.recipeMap[filename]; ok {
copy(result, recipe.Recipe.MaterialSetting) if filename == "" || filename == d.CurrentFile {
d.CurrentFile = filename copy(result, d.currentRecipe.MaterialSetting)
d.CurrentCountryID = countryID return result
return result }
}
if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename copy(result, recipe.Recipe.MaterialSetting)
d.CurrentCountryID = countryID d.CurrentFile = filename
recipe, err := helpers.ReadRecipeFile(countryID, filename) d.CurrentCountryID = countryID
return result
if err != nil { }
logger.GetInstance().Error("Error when read recipe file", zap.Error(err))
copy(result, d.currentRecipe.MaterialSetting) d.CurrentFile = filename
return result d.CurrentCountryID = countryID
} recipe, err := helpers.ReadRecipeFile(countryID, filename)
d.currentRecipe = recipe if err != nil {
logger.GetInstance().Error("Error when read recipe file [GetMaterialSetting]", zap.Error(err))
// save to map copy(result, d.currentRecipe.MaterialSetting)
if len(d.recipeMap) > 5 { // limit keep in memory 5 version return result
// remove oldest version }
var oldestVersion string
var oldestTime int64 d.currentRecipe = recipe
for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime { // save to map
oldestTime = v.TimeStamps if len(d.recipeMap) > 5 { // limit keep in memory 5 version
oldestVersion = k // remove oldest version
} var oldestVersion string
} var oldestTime int64
delete(d.recipeMap, oldestVersion) for k, v := range d.recipeMap {
} if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps
d.recipeMap[filename] = RecipeWithTimeStamps{ oldestVersion = k
Recipe: *d.currentRecipe, }
TimeStamps: time.Now().Unix(), }
} delete(d.recipeMap, oldestVersion)
}
copy(result, d.currentRecipe.MaterialSetting)
return result d.recipeMap[filename] = RecipeWithTimeStamps{
} Recipe: *d.currentRecipe,
TimeStamps: time.Now().Unix(),
func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode { }
var result []models.MaterialCode
copy(result, d.currentRecipe.MaterialSetting)
if filename == "" || filename == d.CurrentFile { return result
result = d.currentRecipe.MaterialCode }
} else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []models.MaterialCode {
return recipe.Recipe.MaterialCode var result []models.MaterialCode
} else {
d.CurrentFile = filename if filename == "" || filename == d.CurrentFile {
d.CurrentCountryID = countryID result = d.currentRecipe.MaterialCode
recipe, err := helpers.ReadRecipeFile(countryID, filename) } else if recipe, ok := d.recipeMap[filename]; ok {
d.CurrentFile = filename
if err != nil { return recipe.Recipe.MaterialCode
logger.GetInstance().Error("Error when read recipe file", zap.Error(err)) } else {
return d.currentRecipe.MaterialCode d.CurrentFile = filename
} d.CurrentCountryID = countryID
recipe, err := helpers.ReadRecipeFile(countryID, filename)
d.currentRecipe = recipe
if err != nil {
// save to map logger.GetInstance().Error("Error when read recipe file [GetMaterialCode]", zap.Error(err))
if len(d.recipeMap) > 5 { // limit keep in memory 5 version return d.currentRecipe.MaterialCode
// remove oldest version }
var oldestVersion string
var oldestTime int64 d.currentRecipe = recipe
for k, v := range d.recipeMap {
if oldestTime == 0 || v.TimeStamps < oldestTime { // save to map
oldestTime = v.TimeStamps if len(d.recipeMap) > 5 { // limit keep in memory 5 version
oldestVersion = k // remove oldest version
} var oldestVersion string
} var oldestTime int64
delete(d.recipeMap, oldestVersion) for k, v := range d.recipeMap {
} if oldestTime == 0 || v.TimeStamps < oldestTime {
oldestTime = v.TimeStamps
d.recipeMap[filename] = RecipeWithTimeStamps{ oldestVersion = k
Recipe: *d.currentRecipe, }
TimeStamps: time.Now().Unix(), }
} delete(d.recipeMap, oldestVersion)
}
result = d.currentRecipe.MaterialCode
} d.recipeMap[filename] = RecipeWithTimeStamps{
Recipe: *d.currentRecipe,
if len(ids) == 0 { TimeStamps: time.Now().Unix(),
return result }
}
result = d.currentRecipe.MaterialCode
resultFilter := make([]models.MaterialCode, len(ids)) }
for _, id := range ids {
if id == 0 { if len(ids) == 0 {
continue return result
} }
for _, m := range result { resultFilter := make([]models.MaterialCode, len(ids))
if m.MaterialID == id { for _, id := range ids {
resultFilter = append(resultFilter, m) if id == 0 {
break continue
} }
}
} for _, m := range result {
if m.MaterialID == id {
return resultFilter resultFilter = append(resultFilter, m)
} break
}
func (d *Data) GetCountryNameByID(countryID string) (string, error) { }
for _, country := range d.Countries { }
if country.CountryID == countryID {
return country.CountryName, nil return resultFilter
} }
}
return "", fmt.Errorf("country ID: %s not found", countryID) func (d *Data) GetCountryNameByID(countryID string) (string, error) {
} for _, country := range d.Countries {
if country.CountryID == countryID {
func (d *Data) GetCountryIDByName(countryName string) (string, error) { return country.CountryName, nil
for _, country := range d.Countries { }
if country.CountryName == countryName { }
return country.CountryID, nil return "", fmt.Errorf("country ID: %s not found", countryID)
} }
}
return "", fmt.Errorf("country name: %s not found", countryName) func (d *Data) GetCountryIDByName(countryName string) (string, error) {
} for _, country := range d.Countries {
if country.CountryName == countryName {
func (d *Data) ExportToJSON() []byte { return country.CountryID, nil
b_recipe, err := json.Marshal(d.currentRecipe) }
if err != nil { }
Log.Error("Error when marshal recipe", zap.Error(err)) return "", fmt.Errorf("country name: %s not found", countryName)
return nil }
}
func (d *Data) ExportToJSON() []byte {
return b_recipe b_recipe, err := json.Marshal(d.currentRecipe)
} if err != nil {
Log.Error("Error when marshal recipe", zap.Error(err))
return nil
}
return b_recipe
}

View file

@ -370,31 +370,38 @@ func (rr *RecipeRouter) Route(r chi.Router) {
return return
} }
recipe_root_path := "./cofffeemachineConfig/" // recipe_root_path := "./cofffeemachineConfig/"
// structure // structure
full_file_name_targets := []string{} // full_file_name_targets := []string{}
files, err := os.ReadDir(recipe_root_path + countryID) // files, err := os.ReadDir(recipe_root_path + countryID)
// if err != nil {
// Log.Error("Error when read directory", zap.Error(err))
// return
// }
// for _, file := range files {
// Log.Debug("File: ", zap.Any("file", file.Name()))
// if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") {
// full_file_name_targets = append(full_file_name_targets, file.Name())
// }
// }
commits, err := data.GetCommitLogOfFilename(countryID, file_version)
if err != nil { if err != nil {
Log.Error("Error when read directory", zap.Error(err)) Log.Error("Error when get commit log", zap.Error(err))
return return
} }
for _, file := range files {
Log.Debug("File: ", zap.Any("file", file.Name()))
if strings.Contains(file.Name(), file_version) && strings.Contains(file.Name(), ".tmp") {
full_file_name_targets = append(full_file_name_targets, file.Name())
}
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"files": full_file_name_targets}) json.NewEncoder(w).Encode(map[string]interface{}{"files": commits})
Log.Debug("Saved Files: ", zap.Any("files", full_file_name_targets)) Log.Debug("Saved Files: ", zap.Any("files", commits))
}) })
}) })
} }