From e58c311dddcd2a25b5a14cd9ec8fea5104f3adcf Mon Sep 17 00:00:00 2001 From: bnkai <48220860+bnkai@users.noreply.github.com> Date: Fri, 3 Apr 2020 05:44:17 +0300 Subject: [PATCH] Add library size to main stats page (#427) --- go.mod | 1 + go.sum | 1 + graphql/documents/queries/misc.graphql | 1 + graphql/schema/types/stats.graphql | 3 +- pkg/api/resolver.go | 2 + pkg/models/querybuilder_scene.go | 9 + pkg/models/querybuilder_sql.go | 12 ++ ui/v2.5/src/components/Stats.tsx | 8 + ui/v2/src/components/Stats.tsx | 6 + .../github.com/dustin/go-humanize/.travis.yml | 21 ++ vendor/github.com/dustin/go-humanize/LICENSE | 21 ++ .../dustin/go-humanize/README.markdown | 124 +++++++++++ vendor/github.com/dustin/go-humanize/big.go | 31 +++ .../github.com/dustin/go-humanize/bigbytes.go | 173 ++++++++++++++++ vendor/github.com/dustin/go-humanize/bytes.go | 143 +++++++++++++ vendor/github.com/dustin/go-humanize/comma.go | 116 +++++++++++ .../github.com/dustin/go-humanize/commaf.go | 40 ++++ vendor/github.com/dustin/go-humanize/ftoa.go | 46 +++++ .../github.com/dustin/go-humanize/humanize.go | 8 + .../github.com/dustin/go-humanize/number.go | 192 ++++++++++++++++++ .../github.com/dustin/go-humanize/ordinals.go | 25 +++ vendor/github.com/dustin/go-humanize/si.go | 123 +++++++++++ vendor/github.com/dustin/go-humanize/times.go | 117 +++++++++++ vendor/modules.txt | 2 + 24 files changed, 1224 insertions(+), 1 deletion(-) create mode 100644 vendor/github.com/dustin/go-humanize/.travis.yml create mode 100644 vendor/github.com/dustin/go-humanize/LICENSE create mode 100644 vendor/github.com/dustin/go-humanize/README.markdown create mode 100644 vendor/github.com/dustin/go-humanize/big.go create mode 100644 vendor/github.com/dustin/go-humanize/bigbytes.go create mode 100644 vendor/github.com/dustin/go-humanize/bytes.go create mode 100644 vendor/github.com/dustin/go-humanize/comma.go create mode 100644 vendor/github.com/dustin/go-humanize/commaf.go create mode 100644 vendor/github.com/dustin/go-humanize/ftoa.go create mode 100644 vendor/github.com/dustin/go-humanize/humanize.go create mode 100644 vendor/github.com/dustin/go-humanize/number.go create mode 100644 vendor/github.com/dustin/go-humanize/ordinals.go create mode 100644 vendor/github.com/dustin/go-humanize/si.go create mode 100644 vendor/github.com/dustin/go-humanize/times.go diff --git a/go.mod b/go.mod index 28f66064eae..99905964152 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/antchfx/xpath v1.1.2 // indirect github.com/bmatcuk/doublestar v1.1.5 github.com/disintegration/imaging v1.6.0 + github.com/dustin/go-humanize v1.0.0 github.com/go-chi/chi v4.0.2+incompatible github.com/gobuffalo/packr/v2 v2.0.2 github.com/golang-migrate/migrate/v4 v4.3.1 diff --git a/go.sum b/go.sum index 9e465eac320..be485c64cd7 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,7 @@ github.com/docker/docker v0.7.3-0.20190108045446-77df18c24acf/go.mod h1:eEKB0N0r github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= diff --git a/graphql/documents/queries/misc.graphql b/graphql/documents/queries/misc.graphql index f6bdcbd78bf..295532eefdc 100644 --- a/graphql/documents/queries/misc.graphql +++ b/graphql/documents/queries/misc.graphql @@ -52,6 +52,7 @@ query ValidGalleriesForScene($scene_id: ID!) { query Stats { stats { scene_count, + scene_size_count, gallery_count, performer_count, studio_count, diff --git a/graphql/schema/types/stats.graphql b/graphql/schema/types/stats.graphql index 11e7d139328..d9408630884 100644 --- a/graphql/schema/types/stats.graphql +++ b/graphql/schema/types/stats.graphql @@ -1,8 +1,9 @@ type StatsResultType { scene_count: Int! + scene_size_count: String! gallery_count: Int! performer_count: Int! studio_count: Int! movie_count: Int! tag_count: Int! -} \ No newline at end of file +} diff --git a/pkg/api/resolver.go b/pkg/api/resolver.go index a14bd677f63..5713e0b2222 100644 --- a/pkg/api/resolver.go +++ b/pkg/api/resolver.go @@ -93,6 +93,7 @@ func (r *queryResolver) ValidGalleriesForScene(ctx context.Context, scene_id *st func (r *queryResolver) Stats(ctx context.Context) (*models.StatsResultType, error) { scenesQB := models.NewSceneQueryBuilder() scenesCount, _ := scenesQB.Count() + scenesSizeCount, _ := scenesQB.SizeCount() galleryQB := models.NewGalleryQueryBuilder() galleryCount, _ := galleryQB.Count() performersQB := models.NewPerformerQueryBuilder() @@ -105,6 +106,7 @@ func (r *queryResolver) Stats(ctx context.Context) (*models.StatsResultType, err tagsCount, _ := tagsQB.Count() return &models.StatsResultType{ SceneCount: scenesCount, + SceneSizeCount: scenesSizeCount, GalleryCount: galleryCount, PerformerCount: performersCount, StudioCount: studiosCount, diff --git a/pkg/models/querybuilder_scene.go b/pkg/models/querybuilder_scene.go index 8a127749b27..413930f0c28 100644 --- a/pkg/models/querybuilder_scene.go +++ b/pkg/models/querybuilder_scene.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" + "github.com/dustin/go-humanize" "github.com/jmoiron/sqlx" "github.com/stashapp/stash/pkg/database" ) @@ -195,6 +196,14 @@ func (qb *SceneQueryBuilder) Count() (int, error) { return runCountQuery(buildCountQuery("SELECT scenes.id FROM scenes"), nil) } +func (qb *SceneQueryBuilder) SizeCount() (string, error) { + sum, err := runSumQuery("SELECT SUM(size) as sum FROM scenes", nil) + if err != nil { + return "0", err + } + return humanize.Bytes(sum), err +} + func (qb *SceneQueryBuilder) CountByStudioID(studioID int) (int, error) { args := []interface{}{studioID} return runCountQuery(buildCountQuery(scenesForStudioQuery), args) diff --git a/pkg/models/querybuilder_sql.go b/pkg/models/querybuilder_sql.go index 4fc63c0313f..3acbd7d441f 100644 --- a/pkg/models/querybuilder_sql.go +++ b/pkg/models/querybuilder_sql.go @@ -258,6 +258,18 @@ func runCountQuery(query string, args []interface{}) (int, error) { return result.Int, nil } +func runSumQuery(query string, args []interface{}) (uint64, error) { + // Perform query and fetch result + result := struct { + Uint uint64 `db:"sum"` + }{0} + if err := database.DB.Get(&result, query, args...); err != nil && err != sql.ErrNoRows { + return 0, err + } + + return result.Uint, nil +} + func executeFindQuery(tableName string, body string, args []interface{}, sortAndPagination string, whereClauses []string, havingClauses []string) ([]int, int) { if len(whereClauses) > 0 { body = body + " WHERE " + strings.Join(whereClauses, " AND ") // TODO handle AND or OR diff --git a/ui/v2.5/src/components/Stats.tsx b/ui/v2.5/src/components/Stats.tsx index e200dc01477..0e23763c7a9 100644 --- a/ui/v2.5/src/components/Stats.tsx +++ b/ui/v2.5/src/components/Stats.tsx @@ -13,6 +13,14 @@ export const Stats: React.FC = () => { return (
+
+

+ +

+

+ +

+

diff --git a/ui/v2/src/components/Stats.tsx b/ui/v2/src/components/Stats.tsx index ea4f3ce9e8c..28cb114f22f 100644 --- a/ui/v2/src/components/Stats.tsx +++ b/ui/v2/src/components/Stats.tsx @@ -9,6 +9,12 @@ export const Stats: FunctionComponent = () => { if (!data || !data.stats) { return; } return (