Skip to content

Commit

Permalink
feat(db) no progress when --log-json option (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadayuki-matsuno authored Jul 4, 2024
1 parent ee94366 commit 1d9c13d
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 @@ -209,7 +210,12 @@ func (r *RDBDriver) deleteAndInsertExploit(exploitType models.ExploitType, explo

if len(oldIDs) > 0 {
log15.Info("Deleting old Exploits")
bar := pb.StartNew(len(oldIDs))
bar := pb.StartNew(len(oldIDs)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(oldIDs), batchSize) {
osIDs := []models.OffensiveSecurity{}
if err := tx.Model(&models.OffensiveSecurity{}).Select("id").Where("exploit_id IN ?", oldIDs[idx.From:idx.To]).Find(&osIDs).Error; err != nil {
Expand Down Expand Up @@ -251,7 +257,12 @@ func (r *RDBDriver) deleteAndInsertExploit(exploitType models.ExploitType, explo
}

log15.Info("Inserting new Exploits")
bar := pb.StartNew(len(exploits))
bar := pb.StartNew(len(exploits)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
for idx := range chunkSlice(len(exploits), batchSize) {
if err = tx.Create(exploits[idx.From:idx.To]).Error; err != nil {
return xerrors.Errorf("Failed to insert. 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"
"time"

Expand Down Expand Up @@ -324,7 +326,12 @@ func (r *RedisDriver) InsertExploit(exploitType models.ExploitType, exploits []m
return xerrors.Errorf("Failed to unmarshal JSON. err: %w", err)
}

bar := pb.StartNew(len(exploits))
bar := pb.StartNew(len(exploits)).SetWriter(func() io.Writer {
if viper.GetBool("log-json") {
return io.Discard
}
return os.Stderr
}())
var noCveIDExploitCount, cveIDExploitCount int
for idx := range chunkSlice(len(exploits), batchSize) {
pipe := r.conn.Pipeline()
Expand Down

0 comments on commit 1d9c13d

Please sign in to comment.