Skip to content
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

Add library size to main stats page #427

Merged
merged 5 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions graphql/documents/queries/misc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ query ValidGalleriesForScene($scene_id: ID!) {
query Stats {
stats {
scene_count,
scene_size_count,
gallery_count,
performer_count,
studio_count,
Expand Down
3 changes: 2 additions & 1 deletion graphql/schema/types/stats.graphql
Original file line number Diff line number Diff line change
@@ -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!
}
}
2 changes: 2 additions & 0 deletions pkg/api/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions pkg/models/querybuilder_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

"github.com/dustin/go-humanize"
"github.com/jmoiron/sqlx"
"github.com/stashapp/stash/pkg/database"
)
Expand Down Expand Up @@ -191,6 +192,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)
Expand Down
12 changes: 12 additions & 0 deletions pkg/models/querybuilder_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions ui/v2.5/src/components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export const Stats: React.FC = () => {
return (
<div className="mt-5">
<div className="col col-sm-8 m-sm-auto row stats">
<div className="stats-element">
<p className="title">
<FormattedMessage id="size" defaultMessage={data.stats.scene_size_count} />
</p>
<p className="heading">
<FormattedMessage id="library" defaultMessage="Library size" />
</p>
</div>
<div className="stats-element">
<p className="title">
<FormattedNumber value={data.stats.scene_count} />
Expand Down
6 changes: 6 additions & 0 deletions ui/v2/src/components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export const Stats: FunctionComponent = () => {
if (!data || !data.stats) { return; }
return (
<nav id="details-container" className="level stats">
<div className="level-item has-text-centered">
<div>
<p className="title">{data.stats.scene_size_count}</p>
<p className="heading">Library size</p>
</div>
</div>
<div className="level-item has-text-centered">
<div>
<p className="title">{data.stats.scene_count}</p>
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions vendor/github.com/dustin/go-humanize/README.markdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/dustin/go-humanize/big.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading