From 26a11dc3d08a88c344c807ef67dce987aafac0bc Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 14 Dec 2022 14:47:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:art:=20=E6=95=B0=E6=8D=AE=E5=BF=AB?= =?UTF-8?q?=E7=85=A7=20-=20=E6=9C=AC=E5=9C=B0=E5=BF=AB=E7=85=A7=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8B=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/6870?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/util/history.ts | 7 ++++- kernel/go.mod | 2 +- kernel/go.sum | 4 +++ kernel/model/repository.go | 59 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/app/src/util/history.ts b/app/src/util/history.ts index a757b3fc0..e56bd767d 100644 --- a/app/src/util/history.ts +++ b/app/src/util/history.ts @@ -86,7 +86,7 @@ const renderRepoItem = (response: IWebSocketData, element: Element, type: string `; } let repoHTML = ""; - response.data.snapshots.forEach((item: { memo: string, id: string, hCreated: string, count: number, hSize: string, tag: string }) => { + response.data.snapshots.forEach((item: { memo: string, id: string, hCreated: string, count: number, hSize: string, tag: string, typesCount: { type: string, count: number }[] }) => { if (isMobile()) { repoHTML += `
  • @@ -111,6 +111,11 @@ const renderRepoItem = (response: IWebSocketData, element: Element, type: string ${item.hCreated} ${window.siyuan.languages.fileSize} ${item.hSize} ${window.siyuan.languages.fileCount} ${item.count} + + ${item.typesCount.map((type: { type: string, count: number }) => { + return `${type.type} ${type.count}`; + }).join(" ")} +
    ${actionHTML} diff --git a/kernel/go.mod b/kernel/go.mod index ae8dcf078..834506eb2 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -38,7 +38,7 @@ require ( github.com/panjf2000/ants/v2 v2.7.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/radovskyb/watcher v1.0.7 - github.com/siyuan-note/dejavu v0.0.0-20221214060041-f7b64a7eb12a + github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da github.com/siyuan-note/filelock v0.0.0-20221117095924-e1947438a35e diff --git a/kernel/go.sum b/kernel/go.sum index 8493903c7..07a208e88 100644 --- a/kernel/go.sum +++ b/kernel/go.sum @@ -363,6 +363,10 @@ github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYED github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= github.com/siyuan-note/dejavu v0.0.0-20221214060041-f7b64a7eb12a h1:9sAurbeois47ROtvJ9EcQFWfJIdL7C6fPZm29PhTBXg= github.com/siyuan-note/dejavu v0.0.0-20221214060041-f7b64a7eb12a/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= +github.com/siyuan-note/dejavu v0.0.0-20221214062510-12058008827b h1:ACydDbRC5n76G843HaW+mx86hm3CZya/VUZlB0dtFnU= +github.com/siyuan-note/dejavu v0.0.0-20221214062510-12058008827b/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= +github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57 h1:luEVy9oqVB8yAlxVdwIMFoI53mVvSRG3N1rJ/+U6p3I= +github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw= github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90= diff --git a/kernel/model/repository.go b/kernel/model/repository.go index e5fdeb53d..6243312f7 100644 --- a/kernel/model/repository.go +++ b/kernel/model/repository.go @@ -28,6 +28,7 @@ import ( "os" "path" "path/filepath" + "sort" "strings" "sync" "time" @@ -53,7 +54,18 @@ func init() { subscribeEvents() } -func GetRepoSnapshots(page int) (logs []*dejavu.Log, pageCount, totalCount int, err error) { +type Snapshot struct { + *dejavu.Log + TypesCount []*TypeCount `json:"typesCount"` +} + +type TypeCount struct { + Type string `json:"type"` + Count int `json:"count"` +} + +func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err error) { + ret = []*Snapshot{} if 1 > len(Conf.Repo.Key) { err = errors.New(Conf.Language(26)) return @@ -64,7 +76,7 @@ func GetRepoSnapshots(page int) (logs []*dejavu.Log, pageCount, totalCount int, return } - logs, pageCount, totalCount, err = repo.GetIndexLogs(page, 32) + logs, pageCount, totalCount, err := repo.GetIndexLogs(page, 32) if nil != err { if dejavu.ErrNotFoundIndex == err { logs = []*dejavu.Log{} @@ -75,6 +87,49 @@ func GetRepoSnapshots(page int) (logs []*dejavu.Log, pageCount, totalCount int, logging.LogErrorf("get data repo index logs failed: %s", err) return } + + // 数据快照 - 本地快照显示文件类型 https://github.com/siyuan-note/siyuan/issues/6870 + for _, l := range logs { + typesCount := statTypesByPath(l.Files) + ret = append(ret, &Snapshot{ + Log: l, + TypesCount: typesCount, + }) + } + return +} + +func statTypesByPath(files []*entity.File) (ret []*TypeCount) { + m := map[string]int{} + + for _, f := range files { + ext := path.Ext(f.Path) + m[ext]++ + } + + var stated []string + for ext, count := range m { + ret = append(ret, &TypeCount{ + Type: ext, + Count: count, + }) + stated = append(stated, ext) + if 10 < len(ret) { + break + } + } + sort.Slice(ret, func(i, j int) bool { return ret[i].Count > ret[j].Count }) + for _, s := range stated { + delete(m, s) + } + otherCount := 0 + for _, count := range m { + otherCount += count + } + ret = append(ret, &TypeCount{ + Type: "Other", + Count: otherCount, + }) return } From e73f38390b7395e6526f2b51439ee6bc940697cb Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 14 Dec 2022 15:39:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:art:=20=E6=98=BE=E7=A4=BA=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=95=B0=E6=8D=AE=E5=BF=AB=E7=85=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=AE=A1=E6=95=B0=20Fix=20https://github.com?= =?UTF-8?q?/siyuan-note/siyuan/issues/6870?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/util/history.ts | 13 +++++--- kernel/go.mod | 2 +- kernel/go.sum | 8 ++--- kernel/model/repository.go | 68 +++++++++++++++++++++----------------- 4 files changed, 49 insertions(+), 42 deletions(-) diff --git a/app/src/util/history.ts b/app/src/util/history.ts index e56bd767d..e0a90b950 100644 --- a/app/src/util/history.ts +++ b/app/src/util/history.ts @@ -110,11 +110,16 @@ const renderRepoItem = (response: IWebSocketData, element: Element, type: string
    ${item.hCreated} ${window.siyuan.languages.fileSize} ${item.hSize} - ${window.siyuan.languages.fileCount} ${item.count} - - ${item.typesCount.map((type: { type: string, count: number }) => { + ${window.siyuan.languages.fileCount} ${item.count}` + let statHTML = ""; + if (item.typesCount && 0 < item.typesCount.length) { + statHTML += ` + + ${item.typesCount.map((type: { type: string, count: number }) => { return `${type.type} ${type.count}`; - }).join(" ")} + }).join("  ")}` + } + repoHTML += `${statHTML}
    diff --git a/kernel/go.mod b/kernel/go.mod index 834506eb2..137140ca4 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -38,7 +38,7 @@ require ( github.com/panjf2000/ants/v2 v2.7.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/radovskyb/watcher v1.0.7 - github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57 + github.com/siyuan-note/dejavu v0.0.0-20221214071109-67333a5acbcb github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da github.com/siyuan-note/filelock v0.0.0-20221117095924-e1947438a35e diff --git a/kernel/go.sum b/kernel/go.sum index 07a208e88..974f40691 100644 --- a/kernel/go.sum +++ b/kernel/go.sum @@ -361,12 +361,8 @@ github.com/shurcooL/reactions v0.0.0-20181006231557-f2e0b4ca5b82/go.mod h1:TCR1l github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4= github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= -github.com/siyuan-note/dejavu v0.0.0-20221214060041-f7b64a7eb12a h1:9sAurbeois47ROtvJ9EcQFWfJIdL7C6fPZm29PhTBXg= -github.com/siyuan-note/dejavu v0.0.0-20221214060041-f7b64a7eb12a/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= -github.com/siyuan-note/dejavu v0.0.0-20221214062510-12058008827b h1:ACydDbRC5n76G843HaW+mx86hm3CZya/VUZlB0dtFnU= -github.com/siyuan-note/dejavu v0.0.0-20221214062510-12058008827b/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= -github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57 h1:luEVy9oqVB8yAlxVdwIMFoI53mVvSRG3N1rJ/+U6p3I= -github.com/siyuan-note/dejavu v0.0.0-20221214062838-c08423e72b57/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= +github.com/siyuan-note/dejavu v0.0.0-20221214071109-67333a5acbcb h1:fS8iB5d7x57eyfvNjqlDOTL9XglVCSBtGoa5e3nQTkY= +github.com/siyuan-note/dejavu v0.0.0-20221214071109-67333a5acbcb/go.mod h1:aarwJw3uJaqNoIGVLmyhZjMCjI+xAzwrMIg05scqmtc= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw= github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90= diff --git a/kernel/model/repository.go b/kernel/model/repository.go index 6243312f7..d2d2b9e55 100644 --- a/kernel/model/repository.go +++ b/kernel/model/repository.go @@ -88,7 +88,11 @@ func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err return } - // 数据快照 - 本地快照显示文件类型 https://github.com/siyuan-note/siyuan/issues/6870 + ret = buildSnapshots(logs) + return +} + +func buildSnapshots(logs []*dejavu.Log) (ret []*Snapshot) { for _, l := range logs { typesCount := statTypesByPath(l.Files) ret = append(ret, &Snapshot{ @@ -100,36 +104,34 @@ func GetRepoSnapshots(page int) (ret []*Snapshot, pageCount, totalCount int, err } func statTypesByPath(files []*entity.File) (ret []*TypeCount) { - m := map[string]int{} - for _, f := range files { ext := path.Ext(f.Path) - m[ext]++ - } - var stated []string - for ext, count := range m { - ret = append(ret, &TypeCount{ - Type: ext, - Count: count, - }) - stated = append(stated, ext) - if 10 < len(ret) { - break + found := false + for _, tc := range ret { + if tc.Type == ext { + tc.Count++ + found = true + break + } + } + if !found { + ret = append(ret, &TypeCount{Type: ext, Count: 1}) } } + sort.Slice(ret, func(i, j int) bool { return ret[i].Count > ret[j].Count }) - for _, s := range stated { - delete(m, s) + if 10 < len(ret) { + otherCount := 0 + for _, tc := range ret[10:] { + tc.Count += otherCount + } + other := &TypeCount{ + Type: "Other", + Count: otherCount, + } + ret = append(ret[:10], other) } - otherCount := 0 - for _, count := range m { - otherCount += count - } - ret = append(ret, &TypeCount{ - Type: "Other", - Count: otherCount, - }) return } @@ -388,6 +390,7 @@ func RemoveCloudRepoTag(tag string) (err error) { } func GetCloudRepoTagSnapshots() (ret []*dejavu.Log, err error) { + ret = []*dejavu.Log{} if 1 > len(Conf.Repo.Key) { err = errors.New(Conf.Language(26)) return @@ -398,14 +401,16 @@ func GetCloudRepoTagSnapshots() (ret []*dejavu.Log, err error) { return } - ret, err = repo.GetCloudRepoTagLogs(map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}) - if 1 > len(ret) { - ret = []*dejavu.Log{} + logs, err := repo.GetCloudRepoTagLogs(map[string]interface{}{eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBar}) + if nil != err { + return } + ret = logs return } -func GetTagSnapshots() (ret []*dejavu.Log, err error) { +func GetTagSnapshots() (ret []*Snapshot, err error) { + ret = []*Snapshot{} if 1 > len(Conf.Repo.Key) { err = errors.New(Conf.Language(26)) return @@ -416,10 +421,11 @@ func GetTagSnapshots() (ret []*dejavu.Log, err error) { return } - ret, err = repo.GetTagLogs() - if 1 > len(ret) { - ret = []*dejavu.Log{} + logs, err := repo.GetTagLogs() + if nil != err { + return } + ret = buildSnapshots(logs) return }