From e0b3e08d147bc31bff389740ad053a7c392333dd Mon Sep 17 00:00:00 2001 From: Noteworthy Date: Sat, 11 May 2024 10:11:45 +1000 Subject: [PATCH] include screenshot counts in GET/ summary (#60) --- db/file-summary.n1ql | 1 + internal/behavior/repostitory.go | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/db/file-summary.n1ql b/db/file-summary.n1ql index 157fd49..e66f8ec 100644 --- a/db/file-summary.n1ql +++ b/db/file-summary.n1ql @@ -32,6 +32,7 @@ SELECT }, "pe_meta": f.pe.meta, "default_behavior_id": f.default_behavior_id, + "screenshots_count": f.screenshots_count, "liked": CASE WHEN ARRAY_LENGTH(user_likes) = 0 THEN false ELSE ARRAY_BINARY_SEARCH(ARRAY_SORT((user_likes)[0]), f.sha256) >= 0 END }.* diff --git a/internal/behavior/repostitory.go b/internal/behavior/repostitory.go index 5df6a76..573d0c0 100644 --- a/internal/behavior/repostitory.go +++ b/internal/behavior/repostitory.go @@ -163,9 +163,28 @@ func (r repository) CountArtifacts(ctx context.Context, id string) (int, error) var statement string params := make(map[string]interface{}, 1) params["id"] = id - statement = + + filters, ok := ctx.Value(filtersKey).(map[string][]string) + if ok { + statement = + "SELECT RAW COUNT(artifacts) AS count FROM `" + r.db.Bucket.Name() + "` d" + + " USE KEYS $id UNNEST d.artifacts as artifacts" + i := 0 + for k, v := range filters { + if i == 0 { + statement += " WHERE" + } else { + statement += " AND" + } + i++ + statement += fmt.Sprintf(" artifacts.%s IN $%s", k, k) + params[k] = v + } + } else { + statement = "SELECT RAW ARRAY_LENGTH(d.artifacts) AS count FROM `" + r.db.Bucket.Name() + "` d" + " USE KEYS $id" + } err := r.db.Count(ctx, statement, params, &count) return count, err