Skip to content

Commit 083f01b

Browse files
committed
add product/package leaderboards
1 parent fbfa5e7 commit 083f01b

5 files changed

Lines changed: 385 additions & 0 deletions

File tree

app/models/analytics/leaderboard.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Leaderboard
1313
COUNTERS = {
1414
ips: Ips,
1515
licenses: Licenses,
16+
packages: Packages,
17+
products: Products,
1618
urls: Urls,
1719
user_agents: UserAgents,
1820
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Analytics
4+
class Leaderboard
5+
class Packages
6+
def initialize(account:, environment:)
7+
@account = account
8+
@environment = environment
9+
end
10+
11+
def count(start_date:, end_date:, limit:)
12+
ReleaseDownloadSpark.for_account(account)
13+
.for_environment(environment)
14+
.where(created_date: start_date..end_date)
15+
.where.not(package_id: nil)
16+
.group(:package_id)
17+
.order(Arel.sql('sum_count DESC'))
18+
.limit(limit)
19+
.sum(:count)
20+
end
21+
22+
private
23+
24+
attr_reader :account, :environment
25+
end
26+
end
27+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
module Analytics
4+
class Leaderboard
5+
class Products
6+
def initialize(account:, environment:)
7+
@account = account
8+
@environment = environment
9+
end
10+
11+
def count(start_date:, end_date:, limit:)
12+
ReleaseDownloadSpark.for_account(account)
13+
.for_environment(environment)
14+
.where(created_date: start_date..end_date)
15+
.group(:product_id)
16+
.order(Arel.sql('sum_count DESC'))
17+
.limit(limit)
18+
.sum(:count)
19+
end
20+
21+
private
22+
23+
attr_reader :account, :environment
24+
end
25+
end
26+
end

features/priv/analytics/leaderboards.feature

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,83 @@ Feature: Leaderboard analytics
147147
And sidekiq should have 0 "event-log" jobs
148148
And time is unfrozen
149149

150+
Scenario: Admin retrieves products leaderboard
151+
Given I am an admin of account "test1"
152+
And the current account is "test1"
153+
And time is frozen at "2100-08-30T00:00:00.000Z"
154+
And the current account has the following "product" rows:
155+
| id | name |
156+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | Product 1 |
157+
| fa48996c-9c98-41c1-a2c3-21de98aefafe | Product 2 |
158+
| 0aef7c4a-953e-4824-9e16-9be2361afcf4 | Product 3 |
159+
And the current account has the following "release" rows:
160+
| id | product_id | version | channel |
161+
| bf9b523f-dd65-48a2-9512-fb66ba6c3714 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 1.0.0 | stable |
162+
| a499bb93-9902-4b52-8a04-76944ad7f660 | fa48996c-9c98-41c1-a2c3-21de98aefafe | 2.0.0 | stable |
163+
| 7559899f-2761-4b9c-a43e-2d919efa9b04 | 0aef7c4a-953e-4824-9e16-9be2361afcf4 | 3.0.0 | stable |
164+
And the current account has the following "release_download_spark" rows:
165+
| product_id | release_id | count | created_date | created_at |
166+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | bf9b523f-dd65-48a2-9512-fb66ba6c3714 | 5 | 2100-08-23 | 2100-08-23T00:00:00.000Z |
167+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | bf9b523f-dd65-48a2-9512-fb66ba6c3714 | 3 | 2100-08-24 | 2100-08-24T00:00:00.000Z |
168+
| fa48996c-9c98-41c1-a2c3-21de98aefafe | a499bb93-9902-4b52-8a04-76944ad7f660 | 4 | 2100-08-23 | 2100-08-23T00:00:00.000Z |
169+
| 0aef7c4a-953e-4824-9e16-9be2361afcf4 | 7559899f-2761-4b9c-a43e-2d919efa9b04 | 2 | 2100-08-25 | 2100-08-25T00:00:00.000Z |
170+
And I use an authentication token
171+
When I send a GET request to "/accounts/test1/analytics/leaderboards/products?date[start]=2100-08-20&date[end]=2100-08-27"
172+
Then the response status should be "200"
173+
And the response body should be a JSON document with the following content:
174+
"""
175+
{
176+
"data": [
177+
{ "discriminator": "c9e2cd2e-2543-4d3f-8563-d0bf0b11e233", "count": 8 },
178+
{ "discriminator": "fa48996c-9c98-41c1-a2c3-21de98aefafe", "count": 4 },
179+
{ "discriminator": "0aef7c4a-953e-4824-9e16-9be2361afcf4", "count": 2 }
180+
]
181+
}
182+
"""
183+
And sidekiq should have 0 "request-log" jobs
184+
And sidekiq should have 0 "event-log" jobs
185+
And time is unfrozen
186+
187+
Scenario: Admin retrieves packages leaderboard
188+
Given I am an admin of account "test1"
189+
And the current account is "test1"
190+
And time is frozen at "2100-08-30T00:00:00.000Z"
191+
And the current account has the following "product" rows:
192+
| id | name |
193+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | Product 1 |
194+
And the current account has the following "package" rows:
195+
| id | product_id | name | key |
196+
| 46e034e3-1c8e-4e3b-8a6b-76c2e2ec3694 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | Package 1 | pkg1 |
197+
| f6cac50e-7153-4b0d-897d-3f1a79a13304 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | Package 2 | pkg2 |
198+
| 8fec17e8-17f1-4869-aeb1-19e050cf4dea | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | Package 3 | pkg3 |
199+
And the current account has the following "release" rows:
200+
| id | product_id | package_id | version | channel |
201+
| bf9b523f-dd65-48a2-9512-fb66ba6c3714 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 46e034e3-1c8e-4e3b-8a6b-76c2e2ec3694 | 1.0.0 | stable |
202+
| a499bb93-9902-4b52-8a04-76944ad7f660 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | f6cac50e-7153-4b0d-897d-3f1a79a13304 | 2.0.0 | stable |
203+
| 7559899f-2761-4b9c-a43e-2d919efa9b04 | c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 8fec17e8-17f1-4869-aeb1-19e050cf4dea | 3.0.0 | stable |
204+
And the current account has the following "release_download_spark" rows:
205+
| product_id | package_id | release_id | count | created_date | created_at |
206+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 46e034e3-1c8e-4e3b-8a6b-76c2e2ec3694 | bf9b523f-dd65-48a2-9512-fb66ba6c3714 | 5 | 2100-08-23 | 2100-08-23T00:00:00.000Z |
207+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 46e034e3-1c8e-4e3b-8a6b-76c2e2ec3694 | bf9b523f-dd65-48a2-9512-fb66ba6c3714 | 3 | 2100-08-24 | 2100-08-24T00:00:00.000Z |
208+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | f6cac50e-7153-4b0d-897d-3f1a79a13304 | a499bb93-9902-4b52-8a04-76944ad7f660 | 4 | 2100-08-23 | 2100-08-23T00:00:00.000Z |
209+
| c9e2cd2e-2543-4d3f-8563-d0bf0b11e233 | 8fec17e8-17f1-4869-aeb1-19e050cf4dea | 7559899f-2761-4b9c-a43e-2d919efa9b04 | 2 | 2100-08-25 | 2100-08-25T00:00:00.000Z |
210+
And I use an authentication token
211+
When I send a GET request to "/accounts/test1/analytics/leaderboards/packages?date[start]=2100-08-20&date[end]=2100-08-27"
212+
Then the response status should be "200"
213+
And the response body should be a JSON document with the following content:
214+
"""
215+
{
216+
"data": [
217+
{ "discriminator": "46e034e3-1c8e-4e3b-8a6b-76c2e2ec3694", "count": 8 },
218+
{ "discriminator": "f6cac50e-7153-4b0d-897d-3f1a79a13304", "count": 4 },
219+
{ "discriminator": "8fec17e8-17f1-4869-aeb1-19e050cf4dea", "count": 2 }
220+
]
221+
}
222+
"""
223+
And sidekiq should have 0 "request-log" jobs
224+
And sidekiq should have 0 "event-log" jobs
225+
And time is unfrozen
226+
150227
Scenario: Admin retrieves leaderboard with limit
151228
Given I am an admin of account "test1"
152229
And the current account is "test1"

0 commit comments

Comments
 (0)