Skip to content

Commit

Permalink
优化结果信息输出
Browse files Browse the repository at this point in the history
  • Loading branch information
tanganyu1114 committed Dec 28, 2020
1 parent 723fa65 commit f4b8f9d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 32 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"FileCleaner/record"
"FileCleaner/write"
"flag"
"fmt"
"time"
)

Expand Down Expand Up @@ -35,6 +34,7 @@ func main() {
read.Read(basePath, casCade)
// 写文件
write.Write(delMethod)
// 累积耗时
fmt.Printf("本次去重总共耗时: %s\n", time.Since(startTime).String())
// 输出统计结果信息
record.Report(startTime)

}
7 changes: 4 additions & 3 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type writeRes struct {
}

type linkRes struct {
ErrNum int
ErrMap map[string][]string // 源文件 目标文件
TotalNum int
ErrNum int
ErrMap map[string][]string // 源文件 目标文件
}

type Record interface {
Expand Down Expand Up @@ -135,11 +136,11 @@ type link struct {
}

func (l link) Record(res *Result) {
res.Link.TotalNum += 1
if !l.stat {
res.Link.ErrNum += 1
res.Link.ErrMap[l.src] = append(res.Link.ErrMap[l.src], l.dist)
}

}

func NewLink(stat bool, src, dist string) *link {
Expand Down
11 changes: 11 additions & 0 deletions read/readdir_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package read

import (
"fmt"
"testing"
)

Expand All @@ -11,3 +12,13 @@ func TestReadDir(t *testing.T) {
}
t.Log("path:",path)*/
}

func TestEchoFloat(t *testing.T) {
fmt.Printf("%.2f ", 0.15)
var aa int = 1024
var bb int = 1010
fmt.Println(float64(aa) / 1024 / 1024)
fmt.Println(float64(aa / bb))
numPct := fmt.Sprintf("%.2f %%", float64(aa)/float64(bb))
fmt.Println(numPct)
}
78 changes: 54 additions & 24 deletions record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,77 @@ package record
import (
"FileCleaner/model"
"fmt"
"time"
)

// 操作结果集
var result model.Result

// 后台记录记录文件信息
func Record() {
// 操作结果集
var result model.Result
LOOP:
for {
select {
// 记录读文件信息
case record := <-model.RecordCH:
record.Record(&result)
case <-model.SignalCH:
break LOOP
return
}
}
}

func Report(t time.Time) {
totalSize := fmt.Sprintf("%.2f GB", float64(result.Read.TotalSize)/1024/1024/1024)
removeSize := fmt.Sprintf("%.2f GB", float64(result.Write.TotalSize)/1024/1024/1024)
saveSize := fmt.Sprintf("%.2f GB", float64(result.Read.TotalSize-result.Write.TotalSize)/1024/1024/1024)
numPct := fmt.Sprintf("%.2f %%", float64(result.Write.TotalNum)/float64(result.Read.TotalNum))
sizePct := fmt.Sprintf("%.2f %%", float64(result.Write.TotalSize)/float64(result.Read.TotalSize))
// 输出结果
fmt.Printf(`
结果概览:
本次操作总计耗时: %s
结果概览:
总目录数量: %d 总文件数量: %d 重复文件数量: %d
总文件大小: %s 去重后文件大小: %s 重复文件大小: %s 去重空间比: %d
总文件大小: %s 重复文件大小: %s 剩余文件大小: %s
读取文件目录成功: %d 失败: %d
读取文件数量成功: %d 失败: %d
删除文件数量成功: %d 失败: %d
创建文件链接成功: %d 失败: %d
文件去重比: %s 空间优化比: %s
`)
}
读取文件目录 成功: %d 失败: %d
读取文件数量 成功: %d 失败: %d
删除文件数量 成功: %d 失败: %d
创建文件链接 成功: %d 失败: %d
/*
输出结果信息
总计:
总文件大小: 去重后文件大小: 去重大小: 去重空间占比:
读取文件目录: 成功: 失败:
读取文件数量: 成功: 失败:
删除文件数量: 成功: 失败:
创建文件链接: 成功: 失败:
if 失败> 0 {
失败详情:
* * *
`, time.Since(t).String(),
result.Path.TotalNum, result.Read.TotalNum, result.Write.TotalNum,
totalSize, removeSize, saveSize,
numPct, sizePct,
result.Path.TotalNum-result.Path.ErrNum, result.Path.ErrNum,
result.Read.TotalNum-result.Read.ErrNum, result.Read.ErrNum,
result.Write.TotalNum-result.Write.ErrNum, result.Write.ErrNum,
result.Link.TotalNum-result.Link.ErrNum, result.Link.ErrNum)

if result.Path.ErrNum != 0 {
fmt.Printf("读取目录失败列表:\n")
for _, path := range result.Path.Errs {
fmt.Println(path)
}
}
if result.Read.ErrNum != 0 {
fmt.Printf("读取文件失败列表:\n")
for _, path := range result.Read.Errs {
fmt.Println(path)
}
}
if result.Write.ErrNum != 0 {
fmt.Printf("删除文件失败列表:\n")
for _, path := range result.Write.Errs {
fmt.Println(path)
}
}
if result.Link.ErrNum != 0 {
fmt.Printf("创建文件硬链接失败列表:\n")
for src, dist := range result.Link.ErrMap {
fmt.Printf("源文件:%s --> 目标文件:%s\n", src, dist)
}
}
}
*/
3 changes: 1 addition & 2 deletions write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ func Write(dm string) {
RemoveFile()
if dm == "ln" {
CreateLink()
} else {
close(model.ControlCH)
}
model.SignalCH <- true
}

func RemoveFile() {
Expand Down

0 comments on commit f4b8f9d

Please sign in to comment.