Skip to content

Commit

Permalink
Fix images total megapixels and filesize queries (#4203)
Browse files Browse the repository at this point in the history
  • Loading branch information
bob123491234 authored Oct 16, 2023
1 parent 2ec948a commit bdf705f
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions pkg/sqlite/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,33 +801,52 @@ func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.Ima
aggregateQuery.addColumn("COUNT(DISTINCT temp.id) as total")
}

// TODO - this doesn't work yet
// if options.Megapixels {
// query.addColumn("COALESCE(images.width, 0) * COALESCE(images.height, 0) / 1000000 as megapixels")
// aggregateQuery.addColumn("COALESCE(SUM(temp.megapixels), 0) as megapixels")
// }
if options.Megapixels {
query.addJoins(
join{
table: imagesFilesTable,
onClause: "images_files.image_id = images.id",
},
join{
table: imageFileTable,
onClause: "images_files.file_id = image_files.file_id",
},
)
query.addColumn("COALESCE(image_files.width, 0) * COALESCE(image_files.height, 0) as megapixels")
aggregateQuery.addColumn("COALESCE(SUM(temp.megapixels), 0) / 1000000 as megapixels")
}

// if options.TotalSize {
// query.addColumn("COALESCE(images.size, 0) as size")
// aggregateQuery.addColumn("COALESCE(SUM(temp.size), 0) as size")
// }
if options.TotalSize {
query.addJoins(
join{
table: imagesFilesTable,
onClause: "images_files.image_id = images.id",
},
join{
table: fileTable,
onClause: "images_files.file_id = files.id",
},
)
query.addColumn("COALESCE(files.size, 0) as size")
aggregateQuery.addColumn("SUM(temp.size) as size")
}

const includeSortPagination = false
aggregateQuery.from = fmt.Sprintf("(%s) as temp", query.toSQL(includeSortPagination))

out := struct {
Total int
Megapixels float64
Size float64
Megapixels null.Float
Size null.Float
}{}
if err := qb.repository.queryStruct(ctx, aggregateQuery.toSQL(includeSortPagination), query.args, &out); err != nil {
return nil, err
}

ret := models.NewImageQueryResult(qb)
ret.Count = out.Total
ret.Megapixels = out.Megapixels
ret.TotalSize = out.Size
ret.Megapixels = out.Megapixels.Float64
ret.TotalSize = out.Size.Float64
return ret, nil
}

Expand Down

0 comments on commit bdf705f

Please sign in to comment.