Skip to content

Commit

Permalink
feat(db) no progress when --log-json option (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadayuki-matsuno authored Jul 4, 2024
1 parent 1da5dfb commit df2b721
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 13 additions & 2 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"time"
Expand Down Expand Up @@ -325,7 +326,12 @@ func (r *RDBDriver) InsertOval(root *models.Root) error {
return xerrors.Errorf("Failed to select old defs: %w", err)
}

bar := pb.StartNew(len(defs))
bar := pb.StartNew(len(defs)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(defs), 998) {
var advs []models.Advisory
if err := tx.Model(defs[idx.From:idx.To]).Association("Advisory").Find(&advs); err != nil {
Expand Down Expand Up @@ -354,7 +360,12 @@ func (r *RDBDriver) InsertOval(root *models.Root) error {
}

log15.Info("Inserting new Definitions...")
bar := pb.StartNew(len(root.Definitions))
bar := pb.StartNew(len(root.Definitions)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
if err := tx.Omit("Definitions").Create(&root).Error; err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to insert Root. err: %w", err)
Expand Down
9 changes: 8 additions & 1 deletion db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -338,7 +340,12 @@ func (r *RedisDriver) InsertOval(root *models.Root) (err error) {
return xerrors.Errorf("Failed to unmarshal JSON. err: %w", err)
}

bar := pb.StartNew(len(root.Definitions))
bar := pb.StartNew(len(root.Definitions)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(root.Definitions), batchSize) {
pipe := r.conn.Pipeline()
for _, def := range root.Definitions[idx.From:idx.To] {
Expand Down

0 comments on commit df2b721

Please sign in to comment.