Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] Fix usage and count calculation

Neeraj Guptac6603fc37c28
diff --git a/server/pkg/repo/usage.go b/server/pkg/repo/usage.go
index 59fdd0b34a..6691f721ef 100644
--- a/server/pkg/repo/usage.go
+++ b/server/pkg/repo/usage.go
@@ -66,43 +66,87 @@ func (repo *UsageRepository) GetLockerUsage(ctx context.Context, userIDs []int64
return usage, nil
}
- query := `
- WITH locker_files AS (
- SELECT DISTINCT c.owner_id, cf.file_id
- FROM collection_files cf
- JOIN collections c ON c.collection_id = cf.collection_id
- WHERE c.app = 'locker'
- AND c.owner_id = ANY($1)
- AND cf.f_owner_id = c.owner_id
- AND cf.is_deleted = false
- )
- SELECT
- owner_id,
- COUNT(DISTINCT lf.file_id) AS file_count,
- COALESCE(SUM(ok.size), 0) AS total_size
- FROM locker_files lf
- LEFT JOIN object_keys ok ON ok.file_id = lf.file_id AND ok.is_deleted = false
- GROUP BY owner_id;
- `
+ // Query 1: Get file counts (non-deleted only)
+ countQuery := `
+ SELECT
+ c.owner_id,
+ COUNT(DISTINCT cf.file_id) AS file_count
+ FROM collections c
+ JOIN collection_files cf ON c.collection_id = cf.collection_id
+ WHERE c.app = 'locker'
+ AND c.owner_id = ANY($1)
+ AND cf.f_owner_id = c.owner_id
+ AND cf.is_deleted = false
+ GROUP BY c.owner_id;
+ `
- rows, err := repo.DB.QueryContext(ctx, query, pq.Array(userIDs))
+ // Query 2: Get total sizes (all files)
+ sizeQuery := `
+ SELECT
+ unique_files.owner_id,
+ COALESCE(SUM(ok.size), 0) AS total_size
+ FROM (
+ SELECT DISTINCT c.owner_id, cf.file_id
+ FROM collections c
+ JOIN collection_files cf ON c.collection_id = cf.collection_id
+ WHERE c.app = 'locker'
+ AND c.owner_id = ANY($1)
+ AND cf.f_owner_id = c.owner_id
+ ) AS unique_files
+ LEFT JOIN object_keys ok ON ok.file_id = unique_files.file_id AND ok.is_deleted = false
+ GROUP BY unique_files.owner_id;
+ `
+ // Execute both queries
+ userMap := make(map[int64]*UserLockerUsage)
+
+ // Get counts
+ rows, err := repo.DB.QueryContext(ctx, countQuery, pq.Array(userIDs))
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
defer rows.Close()
for rows.Next() {
- var ownerID, fileCount, totalSize int64
- if scanErr := rows.Scan(&ownerID, &fileCount, &totalSize); scanErr != nil {
+ var ownerID, fileCount int64
+ if scanErr := rows.Scan(&ownerID, &fileCount); scanErr != nil {
return nil, stacktrace.Propagate(scanErr, "")
}
- usage.Users = append(usage.Users, UserLockerUsage{
+ userMap[ownerID] = &UserLockerUsage{
UserID: ownerID,
FileCount: fileCount,
- Usage: totalSize,
- })
- usage.TotalFileCount += fileCount
- usage.TotalUsage += totalSize
+ Usage: 0,
+ }
+ }
+ rows.Close()
+
+ // Get sizes
+ rows, err = repo.DB.QueryContext(ctx, sizeQuery, pq.Array(userIDs))
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var ownerID, totalSize int64
+ if scanErr := rows.Scan(&ownerID, &totalSize); scanErr != nil {
+ return nil, stacktrace.Propagate(scanErr, "")
+ }
+ if user, exists := userMap[ownerID]; exists {
+ user.Usage = totalSize
+ } else {
+ // User has files but none are non-deleted
+ userMap[ownerID] = &UserLockerUsage{
+ UserID: ownerID,
+ FileCount: 0,
+ Usage: totalSize,
+ }
+ }
+ }
+ // Build result
+ for _, user := range userMap {
+ usage.Users = append(usage.Users, *user)
+ usage.TotalFileCount += user.FileCount
+ usage.TotalUsage += user.Usage
}
if err := rows.Err(); err != nil {