Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit c1fee1d

Browse files
reidmitBrian Cunnie
andcommitted
V3: client can **see data** for an **app** usage event
There are differences between the model and the data presented; some field names have changed. | Model AppUsageEvent | Presenter | |---------------------|----------------| | `parent_app_guid` | `app.guid` | | `parent_app_name` | `app.name` | | `app_guid` | `process.guid` | In other words, `app_guid` is NOT `app.guid`. For now, service usage events have `null` for their data; this will be addressed in a subsequent story. [#172985876] Co-authored-by: Reid Mitchell <rmitchell@pivotal.io> Co-authored-by: Brian Cunnie <bcunnie@pivotal.io>
1 parent 1f16a1f commit c1fee1d

6 files changed

Lines changed: 242 additions & 11 deletions

File tree

app/models/runtime/usage_event.rb

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,68 @@ module VCAP::CloudController
22
class UsageEvent < Sequel::Model(
33
AppUsageEvent.select(
44
Sequel.as('app', :type),
5-
:guid,
5+
:app_guid,
6+
:app_name,
7+
:buildpack_guid,
8+
:buildpack_name,
69
:created_at,
10+
:guid,
11+
:instance_count,
12+
:memory_in_mb_per_instance,
13+
:org_guid,
14+
:parent_app_guid,
15+
:parent_app_name,
16+
:previous_instance_count,
17+
:previous_memory_in_mb_per_instance,
18+
:previous_state,
19+
:process_type,
20+
Sequel.as(nil, :service_broker_guid),
21+
Sequel.as(nil, :service_broker_name),
22+
Sequel.as(nil, :service_guid),
23+
Sequel.as(nil, :service_instance_guid),
24+
Sequel.as(nil, :service_instance_name),
25+
Sequel.as(nil, :service_instance_type),
26+
Sequel.as(nil, :service_label),
27+
Sequel.as(nil, :service_plan_guid),
28+
Sequel.as(nil, :service_plan_name),
29+
:space_guid,
30+
:space_name,
31+
:state,
32+
:task_guid,
33+
:task_name,
734
Sequel.as(:created_at, :updated_at)
835
).union(
936
ServiceUsageEvent.select(
1037
Sequel.as('service', :type),
11-
:guid,
38+
Sequel.as(nil, :app_guid),
39+
Sequel.as(nil, :app_name),
40+
Sequel.as(nil, :buildpack_guid),
41+
Sequel.as(nil, :buildpack_name),
1242
:created_at,
43+
:guid,
44+
Sequel.as(nil, :instance_count),
45+
Sequel.as(nil, :memory_in_mb_per_instance),
46+
:org_guid,
47+
Sequel.as(nil, :parent_app_guid),
48+
Sequel.as(nil, :parent_app_name),
49+
Sequel.as(nil, :previous_instance_count),
50+
Sequel.as(nil, :previous_memory_in_mb_per_instance),
51+
Sequel.as(nil, :previous_state),
52+
Sequel.as(nil, :process_type),
53+
:service_broker_guid,
54+
:service_broker_name,
55+
:service_guid,
56+
:service_instance_guid,
57+
:service_instance_name,
58+
:service_instance_type,
59+
:service_label,
60+
:service_plan_guid,
61+
:service_plan_name,
62+
:space_guid,
63+
:space_name,
64+
:state,
65+
Sequel.as(nil, :task_guid),
66+
Sequel.as(nil, :task_name),
1367
Sequel.as(:created_at, :updated_at)),
1468
all: true,
1569
from_self: false

app/presenters/v3/usage_event_presenter.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def to_hash
88
created_at: usage_event.created_at,
99
updated_at: usage_event.updated_at,
1010
type: usage_event.type,
11+
data: build_data
1112
}
1213
end
1314

@@ -16,5 +17,51 @@ def to_hash
1617
def usage_event
1718
@resource
1819
end
20+
21+
def build_data
22+
if usage_event.type == 'app'
23+
build_app_data
24+
end
25+
end
26+
27+
def build_app_data
28+
{
29+
state: {
30+
current: usage_event.state,
31+
previous: usage_event.previous_state,
32+
},
33+
app: {
34+
guid: usage_event.parent_app_guid,
35+
name: usage_event.parent_app_name,
36+
},
37+
process: {
38+
guid: usage_event.app_guid,
39+
type: usage_event.process_type,
40+
},
41+
space: {
42+
guid: usage_event.space_guid,
43+
name: usage_event.space_name,
44+
},
45+
organization: {
46+
guid: usage_event.org_guid,
47+
},
48+
buildpack: {
49+
guid: usage_event.buildpack_guid,
50+
name: usage_event.buildpack_name,
51+
},
52+
task: {
53+
guid: usage_event.task_guid,
54+
name: usage_event.task_name,
55+
},
56+
memory_in_mb_per_instance: {
57+
current: usage_event.memory_in_mb_per_instance,
58+
previous: usage_event.previous_memory_in_mb_per_instance
59+
},
60+
instance_count: {
61+
current: usage_event.instance_count,
62+
previous: usage_event.previous_instance_count,
63+
}
64+
}
65+
end
1966
end
2067
end

docs/v3/source/includes/api_resources/_usage_events.erb

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,53 @@
33
"guid": "a595fe2f-01ff-4965-a50c-290258ab8582",
44
"created_at": "2020-05-28T16:41:23Z",
55
"updated_at": "2020-05-28T16:41:26Z",
6-
"type": "app"
6+
"type": "app",
7+
"data": {
8+
"state": {
9+
"current": "STARTED",
10+
"previous": "STOPPED"
11+
},
12+
"app": {
13+
"guid": "guid-f93250f7-7ef5-4b02-8d33-353919ce8358",
14+
"name": "name-1982"
15+
},
16+
"process": {
17+
"guid": "guid-e9d2d5a0-69a6-46ef-bac5-43f3ed177614",
18+
"type": "type-1983"
19+
},
20+
"space": {
21+
"guid": "guid-5e28f12f-9d80-473e-b826-537b148eb338",
22+
"name": "name-1664"
23+
},
24+
"organization": {
25+
"guid": "guid-036444f4-f2f5-4ea8-a353-e73330ca0f0a",
26+
"name": "name-1665"
27+
},
28+
"buildpack": {
29+
"guid": "guid-34916716-31d7-40c1-9afd-f312996c9654",
30+
"name": "label-64"
31+
},
32+
"task": {
33+
"guid": "guid-7cc11646-bf38-4f4e-b6e0-9581916a74d9",
34+
"name": "name-2929"
35+
},
36+
"memory_in_mb_per_instance": {
37+
"current": 512,
38+
"previous": 256
39+
},
40+
"instance_count": {
41+
"current": 10,
42+
"previous": 5
43+
}
44+
}
745
}
846
<% end %>
947
<% content_for :single_service_usage_event do %>
1048
{
1149
"guid": "c9976002-96f4-435a-888e-db1e1178df62",
1250
"created_at": "2020-05-28T12:34:56Z",
1351
"updated_at": "2020-05-28T12:34:56Z",
14-
"type": "service"
52+
"type": "service",
53+
"data": null
1554
}
1655
<% end %>

docs/v3/source/includes/experimental_resources/usage_events/_get.md renamed to docs/v3/source/includes/experimental_resources/usage_events/_get.md.erb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ Example Response
1818
HTTP/1.1 200 OK
1919
Content-Type: application/json
2020

21-
{
22-
"guid": "a595fe2f-01ff-4965-a50c-290258ab8582",
23-
"created_at": "2020-05-28T16:41:23Z",
24-
"updated_at": "2020-05-28T16:41:26Z",
25-
"type": "app"
26-
}
21+
<%= yield_content :single_app_usage_event %>
2722
```
2823

2924
Retrieve a usage event.

spec/request/usage_events_spec.rb

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let(:org) { space.organization }
1010
let(:api_call) { lambda { |user_headers| get "/v3/usage_events/#{usage_event.guid}", nil, user_headers } }
1111

12-
context 'when the audit_event exists' do
12+
context 'for an app usage event' do
1313
let(:usage_event) {
1414
VCAP::CloudController::AppUsageEvent.make
1515
}
@@ -20,6 +20,82 @@
2020
'created_at' => iso8601,
2121
'updated_at' => iso8601,
2222
'type' => 'app',
23+
'data' => {
24+
'state' => {
25+
'current' => usage_event.state,
26+
'previous' => nil
27+
},
28+
'app' => {
29+
'guid' => usage_event.parent_app_guid,
30+
'name' => usage_event.parent_app_name
31+
},
32+
'process' => {
33+
'guid' => usage_event.app_guid,
34+
'type' => usage_event.process_type,
35+
},
36+
'space' => {
37+
'guid' => usage_event.space_guid,
38+
'name' => usage_event.space_name
39+
},
40+
'organization' => {
41+
'guid' => usage_event.org_guid
42+
},
43+
'buildpack' => {
44+
'guid' => usage_event.buildpack_guid,
45+
'name' => usage_event.buildpack_name
46+
},
47+
'task' => {
48+
'guid' => nil,
49+
'name' => nil
50+
},
51+
'memory_in_mb_per_instance' => {
52+
'current' => usage_event.memory_in_mb_per_instance,
53+
'previous' => nil
54+
},
55+
'instance_count' => {
56+
'current' => usage_event.instance_count,
57+
'previous' => nil
58+
}
59+
}
60+
61+
}
62+
end
63+
64+
let(:expected_codes_and_responses) do
65+
h = Hash.new(
66+
code: 404,
67+
response_object: []
68+
)
69+
h['admin'] = {
70+
code: 200,
71+
response_object: usage_event_json
72+
}
73+
h['admin_read_only'] = {
74+
code: 200,
75+
response_object: usage_event_json
76+
}
77+
h['global_auditor'] = {
78+
code: 200,
79+
response_object: usage_event_json
80+
}
81+
h.freeze
82+
end
83+
84+
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS
85+
end
86+
87+
context 'for a service usage event' do
88+
let(:usage_event) {
89+
VCAP::CloudController::ServiceUsageEvent.make
90+
}
91+
92+
let(:usage_event_json) do
93+
{
94+
'guid' => usage_event.guid,
95+
'created_at' => iso8601,
96+
'updated_at' => iso8601,
97+
'type' => 'service',
98+
'data' => nil
2399
}
24100
end
25101

spec/unit/presenters/v3/usage_event_presenter_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,27 @@
1616
expect(result[:created_at]).to eq(usage_event.created_at)
1717
expect(result[:updated_at]).to eq(usage_event.updated_at)
1818
expect(result[:type]).to eq('app')
19+
expect(result[:data][:state][:current]).to eq usage_event.state
20+
expect(result[:data][:state][:previous]).to eq nil
21+
expect(result[:data][:app][:guid]).to eq usage_event.parent_app_guid
22+
expect(result[:data][:app][:name]).to eq usage_event.parent_app_name
23+
expect(result[:data][:process][:guid]).to eq usage_event.app_guid
24+
expect(result[:data][:process][:type]).to eq usage_event.process_type
25+
expect(result[:data][:space][:guid]).to eq usage_event.space_guid
26+
expect(result[:data][:space][:name]).to eq usage_event.space_name
27+
expect(result[:data][:organization][:guid]).to eq usage_event.org_guid
28+
expect(result[:data][:organization][:name]).to eq nil
29+
expect(result[:data][:buildpack][:guid]).to eq usage_event.buildpack_guid
30+
expect(result[:data][:buildpack][:name]).to eq usage_event.buildpack_name
31+
expect(result[:data][:task][:guid]).to eq nil
32+
expect(result[:data][:task][:name]).to eq nil
33+
expect(result[:data][:memory_in_mb_per_instance][:current]).to eq usage_event.memory_in_mb_per_instance
34+
expect(result[:data][:memory_in_mb_per_instance][:previous]).to eq nil
35+
expect(result[:data][:instance_count][:current]).to eq usage_event.instance_count
36+
expect(result[:data][:instance_count][:previous]).to eq nil
1937
end
2038
end
39+
2140
context "when it's a service usage event" do
2241
let(:usage_event) { VCAP::CloudController::UsageEvent.find(guid: service_usage_event.guid) }
2342

@@ -26,6 +45,7 @@
2645
expect(result[:created_at]).to eq(usage_event.created_at)
2746
expect(result[:updated_at]).to eq(usage_event.updated_at)
2847
expect(result[:type]).to eq('service')
48+
expect(result[:data]).to be_nil
2949
end
3050
end
3151
end

0 commit comments

Comments
 (0)