Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves utils from Browse repo into core #3906

Merged
merged 9 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/scrape/scrape_manager_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (bm *BlazeManager) report(nBlocks, perChunk, nChunks, nAppsNow, nAppsFound,
chunksStr = fmt.Sprintf(" %s", bm.chain)
}

report := `%7.7s @#% 9d}: {% 8d}/{% 8d} ({%0.1f apps/addr}) stage {% 8d} need {% 8d} (@%5.5s})%s`
report := `%7.7s @#% 11d}: {% 8d}/{% 8d} ({%0.1f apps/addr}) stage {% 8d} need {% 8d} (@%5.5s})%s`
msg := fmt.Sprintf(report,
bm.chain,
bm.EndBlock()-1,
Expand Down
4 changes: 4 additions & 0 deletions src/apps/chifra/pkg/configtypes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (s *Config) String() string {
return string(bytes)
}

func (s *Config) ShallowCopy() Config {
return *s
}

func NewConfig(cachePath, indexPath, defaultIpfs string) Config {
ret := defaultConfig
ret.Settings.CachePath = cachePath
Expand Down
12 changes: 12 additions & 0 deletions src/apps/chifra/pkg/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ const (
Autoname Operation = "autoname"
)

func OpFromString(op string) Operation {
m := map[string]Operation{
"create": Create,
"update": Update,
"delete": Delete,
"undelete": Undelete,
"remove": Remove,
"autoname": Autoname,
}
return m[op]
}

func (cd *NameCrud) Validate(requireName bool) error {
if cd.Address.Value.IsZero() {
return errors.New("address is required in crud.Validate")
Expand Down
47 changes: 39 additions & 8 deletions src/apps/chifra/pkg/file/modtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,40 @@ import (
"io/fs"
"os"
"path/filepath"
"time"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
)

func MustGetLatestFileTime(folders ...string) time.Time {
if len(folders) == 0 {
return time.Now()
}

getTimeOfNewestFile := func(folder string) time.Time {
if info, err := GetNewestInDirectory(folder); err != nil || info == nil {
if info == nil {
logger.Warn("latest file time skipped", "error", err)
} else {
logger.Error("error getting latest file time:", err)
}
return time.Now()
} else {
return info.ModTime()
}
}

var newestTime time.Time
for _, folder := range folders {
fileTime := getTimeOfNewestFile(folder)
if fileTime.After(newestTime) {
newestTime = fileTime
}
}

return newestTime
}

func GetNewestInDirectory(directory string) (fileInfo os.FileInfo, err error) {
err = filepath.WalkDir(directory, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
Expand Down Expand Up @@ -42,11 +74,10 @@ func IsLaterThan(fn1, fn2 string) (bool, error) {
return info1.ModTime().After(info2.ModTime()), nil
}

// func getLastModTs(fileName string) (int64, error) {
// info, err := os.Stat(fileName)
// if err != nil {
// return 0, err
// }
// modTime := info.ModTime()
// return modTime.Unix(), nil
// }
func GetModTime(fn string) (time.Time, error) {
if info, err := os.Stat(fn); err != nil {
return time.Time{}, err
} else {
return info.ModTime(), nil
}
}
67 changes: 67 additions & 0 deletions src/apps/chifra/pkg/logger/colored.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package logger

import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
)

func toColored(s severity, color string, v ...any) {
args := append([]interface{}{color}, v...)
args = append(args, colors.Off)
toLog(s, args...)
}

func InfoR(v ...any) {
toColored(info, colors.Red, v...)
}

func InfoG(v ...any) {
toColored(info, colors.Green, v...)
}

func InfoY(v ...any) {
toColored(info, colors.Yellow, v...)
}

func InfoB(v ...any) {
toColored(info, colors.Blue, v...)
}

func InfoM(v ...any) {
toColored(info, colors.Magenta, v...)
}

func InfoC(v ...any) {
toColored(info, colors.Cyan, v...)
}

func InfoW(v ...any) {
toColored(info, colors.White, v...)
}

func InfoBR(v ...any) {
toColored(info, colors.BrightRed, v...)
}

func InfoBG(v ...any) {
toColored(info, colors.BrightGreen, v...)
}

func InfoBY(v ...any) {
toColored(info, colors.BrightYellow, v...)
}

func InfoBB(v ...any) {
toColored(info, colors.BrightBlue, v...)
}

func InfoBM(v ...any) {
toColored(info, colors.BrightMagenta, v...)
}

func InfoBC(v ...any) {
toColored(info, colors.BrightCyan, v...)
}

func InfoBW(v ...any) {
toColored(info, colors.BrightWhite, v...)
}
4 changes: 4 additions & 0 deletions src/apps/chifra/pkg/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,7 @@ func (mon *Monitor) MoveToProduction() error {

return err
}

func PathToMonitorFile(chain string, address base.Address) string {
return filepath.Join(config.PathToCache(chain), "monitors", address.Hex()+".mon.bin")
}
1 change: 1 addition & 0 deletions src/apps/chifra/pkg/types/types_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const (
MatchCase
Expanded
Tags
All = Regular | Custom | Prefund | Baddress
)

type SortBy int
Expand Down
Loading
Loading