-
Notifications
You must be signed in to change notification settings - Fork 60
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
statsMgr information get for studio #194
Changes from 1 commit
9ced538
8a6d7ef
031d009
7d27704
ca82699
b15671b
96a5e78
51d0359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ nebula-importer | |
|
||
# IDE | ||
.vscode/ | ||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import ( | |
type Runner struct { | ||
errs []error | ||
Readers []*reader.FileReader | ||
stataMgr *stats.StatsMgr | ||
NumFailed int64 | ||
} | ||
|
||
|
@@ -28,6 +29,10 @@ func (r *Runner) Error() error { | |
return r.errs[0] | ||
} | ||
|
||
func (r *Runner) Errors() []error { | ||
return r.errs | ||
} | ||
|
||
func (r *Runner) Run(yaml *config.YAMLConfig) { | ||
defer func() { | ||
if re := recover(); re != nil { | ||
|
@@ -39,7 +44,8 @@ func (r *Runner) Run(yaml *config.YAMLConfig) { | |
logger.Init(*yaml.LogPath) | ||
} | ||
|
||
statsMgr := stats.NewStatsMgr(len(yaml.Files)) | ||
statsMgr := stats.NewStatsMgr(yaml.Files) | ||
r.stataMgr = statsMgr | ||
defer statsMgr.Close() | ||
|
||
clientMgr, err := client.NewNebulaClientMgr(yaml.NebulaClientSettings, statsMgr.StatsCh) | ||
|
@@ -68,7 +74,7 @@ func (r *Runner) Run(yaml *config.YAMLConfig) { | |
} else { | ||
go func(fr *reader.FileReader, filename string) { | ||
numReadFailed, err := fr.Read() | ||
statsMgr.NumReadFailed += numReadFailed | ||
statsMgr.Stats.NumReadFailed += numReadFailed | ||
if err != nil { | ||
r.errs = append(r.errs, err) | ||
statsMgr.StatsCh <- base.NewFileDoneStats(filename) | ||
|
@@ -83,10 +89,28 @@ func (r *Runner) Run(yaml *config.YAMLConfig) { | |
<-statsMgr.DoneCh | ||
|
||
r.Readers = nil | ||
r.NumFailed = statsMgr.NumFailed | ||
r.NumFailed = statsMgr.Stats.NumFailed | ||
|
||
if statsMgr.NumFailed > 0 { | ||
if statsMgr.Stats.NumFailed > 0 { | ||
r.errs = append(r.errs, errors.Wrap(errors.NotCompleteError, | ||
fmt.Errorf("Total %d lines fail to insert into nebula graph database", statsMgr.NumFailed))) | ||
fmt.Errorf("Total %d lines fail to insert into nebula graph database", statsMgr.Stats.NumFailed))) | ||
} | ||
} | ||
|
||
func (r *Runner) QueryStats() *stats.Stats { | ||
if r.stataMgr != nil { | ||
if r.Readers == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why need to check the |
||
return &r.stataMgr.Stats | ||
} | ||
r.stataMgr.StatsCh <- base.NewOutputStats() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add chan when it can be read directly? |
||
select { | ||
case stats, ok := <-r.stataMgr.OutputStatsCh: | ||
if !ok { | ||
return nil | ||
} | ||
return &stats | ||
} | ||
} else { | ||
return nil | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ import ( | |
"bufio" | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"os" | ||
"reflect" | ||
"unsafe" | ||
|
||
"github.com/vesoft-inc/nebula-importer/pkg/base" | ||
"github.com/vesoft-inc/nebula-importer/pkg/config" | ||
|
@@ -15,17 +18,37 @@ type CSVReader struct { | |
CSVConfig *config.CSVConfig | ||
reader *csv.Reader | ||
lineNum uint64 | ||
rr *recordReader | ||
br *bufio.Reader | ||
} | ||
|
||
type recordReader struct { | ||
io.Reader | ||
remainingBytes int | ||
} | ||
|
||
func (r *recordReader) Read(p []byte) (n int, err error) { | ||
n, err = r.Reader.Read(p) | ||
r.remainingBytes += n | ||
return | ||
} | ||
|
||
func (r *CSVReader) InitReader(file *os.File) { | ||
r.reader = csv.NewReader(bufio.NewReader(file)) | ||
r.rr = &recordReader{ | ||
Reader: bufio.NewReader(file), | ||
} | ||
r.reader = csv.NewReader(r.rr) | ||
if r.CSVConfig.Delimiter != nil { | ||
d := []rune(*r.CSVConfig.Delimiter) | ||
if len(d) > 0 { | ||
r.reader.Comma = d[0] | ||
logger.Infof("The delimiter of %s is %#U", file.Name(), r.reader.Comma) | ||
} | ||
} | ||
rf := reflect.ValueOf(r.reader).Elem().FieldByName("r") | ||
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem() | ||
br := rf.Interface().(*bufio.Reader) | ||
r.br = br | ||
} | ||
|
||
func (r *CSVReader) ReadLine() (base.Data, error) { | ||
|
@@ -36,25 +59,45 @@ func (r *CSVReader) ReadLine() (base.Data, error) { | |
} | ||
|
||
r.lineNum++ | ||
n := r.rr.remainingBytes - r.br.Buffered() | ||
r.rr.remainingBytes -= n | ||
|
||
if *r.CSVConfig.WithHeader && r.lineNum == 1 { | ||
if *r.CSVConfig.WithLabel { | ||
return base.HeaderData(line[1:]), nil | ||
return base.HeaderData(line[1:], n), nil | ||
} else { | ||
return base.HeaderData(line), nil | ||
return base.HeaderData(line, n), nil | ||
} | ||
} | ||
|
||
if *r.CSVConfig.WithLabel { | ||
switch line[0] { | ||
case "+": | ||
return base.InsertData(line[1:]), nil | ||
return base.InsertData(line[1:], n), nil | ||
case "-": | ||
return base.DeleteData(line[1:]), nil | ||
return base.DeleteData(line[1:], n), nil | ||
default: | ||
return base.Data{}, fmt.Errorf("Invalid label: %s", line[0]) | ||
return base.Data{ | ||
Bytes: n, | ||
}, fmt.Errorf("Invalid label: %s", line[0]) | ||
} | ||
} else { | ||
return base.InsertData(line), nil | ||
return base.InsertData(line, n), nil | ||
} | ||
} | ||
|
||
func CountFileBytes(path string) (int64, error) { | ||
file, err := os.Open(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The statistical methods of the two functions are different.
You can take statistics bytes and don't care about headers.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var _ io.Reader = (*recordReader)(nil)
type recordReader struct {
io.Reader
lastReadBytes int
}
func (r *recordReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
r.lastReadBytes = n
return
}
func (r *CSVReader) InitReader(file *os.File) {
r.rr = &recordReader{Reader: bufio.NewReader(file)}
r.reader = csv.NewReader(r.rr)
...
}
func (r *CSVReader) ReadLine() (base.Data, error) {
line, err := r.reader.Read()
// r.rr.lastReadBytes // get the last read bytes
...
} |
||
defer file.Close() | ||
if err != nil { | ||
logger.Errorf("count bytes fail: %s", path) | ||
return 0, err | ||
} | ||
stat, err := file.Stat() | ||
if err != nil { | ||
logger.Errorf("count bytes fail: %s", path) | ||
return 0, err | ||
} | ||
bytesCount := stat.Size() | ||
return bytesCount, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about return
stats.Stats
nor*stats.Stats
?