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

Commit 1095365

Browse files
reidmitBrian Cunnie
andcommitted
V3: GET /v3/usage_events/:guid
Usage events are a synthesis of app usage events and service usage events. We combine both `AppUsageEvent` and `ServiceUsageEvent` in a model `UsageEvent` backed by a union of the two tables. Usage Events and their models do **not** have an `updated_at` field, for events are created but not updated; however, we present an `updated_at` field to the user for completeness's sake. The `updated_at` is the same as the `created_at` value. [#172985853] [finishes #169237831] Co-authored-by: Reid Mitchell <rmitchell@pivotal.io> Co-authored-by: Brian Cunnie <bcunnie@pivotal.io>
1 parent 7b12373 commit 1095365

13 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'presenters/v3/usage_event_presenter'
2+
3+
class UsageEventsController < ApplicationController
4+
def show
5+
usage_event_not_found! unless permission_queryer.can_read_globally?
6+
usage_event = UsageEvent.first(guid: hashed_params[:guid])
7+
usage_event_not_found! unless usage_event
8+
9+
render status: :ok, json: Presenters::V3::UsageEventPresenter.new(usage_event)
10+
end
11+
12+
private
13+
14+
def usage_event_not_found!
15+
resource_not_found!(:usage_event)
16+
end
17+
end

app/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,4 @@
141141
require 'models/runtime/organization_manager'
142142
require 'models/runtime/organization_billing_manager'
143143
require 'models/runtime/role'
144+
require 'models/runtime/usage_event'

app/models/runtime/usage_event.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module VCAP::CloudController
2+
class UsageEvent < Sequel::Model(
3+
AppUsageEvent.select(
4+
Sequel.as('app', :type),
5+
:guid,
6+
:created_at,
7+
Sequel.as(:created_at, :updated_at)
8+
).union(
9+
ServiceUsageEvent.select(
10+
Sequel.as('service', :type),
11+
:guid,
12+
:created_at,
13+
Sequel.as(:created_at, :updated_at)),
14+
all: true,
15+
from_self: false
16+
).from_self
17+
)
18+
end
19+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'presenters/v3/base_presenter'
2+
3+
module VCAP::CloudController::Presenters::V3
4+
class UsageEventPresenter < BasePresenter
5+
def to_hash
6+
{
7+
guid: usage_event.guid,
8+
created_at: usage_event.created_at,
9+
updated_at: usage_event.updated_at,
10+
type: usage_event.type,
11+
}
12+
end
13+
14+
private
15+
16+
def usage_event
17+
@resource
18+
end
19+
end
20+
end

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@
291291
get '/audit_events', to: 'events#index'
292292
get '/audit_events/:guid', to: 'events#show'
293293

294+
# usage events
295+
get '/usage_events/:guid', to: 'usage_events#show'
296+
294297
# environment variable groups
295298
get '/environment_variable_groups/:name', to: 'environment_variable_groups#show'
296299
patch '/environment_variable_groups/:name', to: 'environment_variable_groups#update'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% content_for :single_usage_event do %>
2+
{
3+
"guid": "a595fe2f-01ff-4965-a50c-290258ab8582",
4+
"created_at": "2020-05-28T16:41:23Z",
5+
"updated_at": "2020-05-28T16:41:26Z",
6+
"type": "app"
7+
}
8+
<% end %>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
### Get a usage event
2+
3+
```
4+
Example Request
5+
```
6+
7+
```shell
8+
curl "https://api.example.org/v3/usage_events/[guid]" \
9+
-X GET \
10+
-H "Authorization: bearer [token]"
11+
```
12+
13+
```
14+
Example Response
15+
```
16+
17+
```http
18+
HTTP/1.1 200 OK
19+
Content-Type: application/json
20+
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+
}
27+
```
28+
29+
Retrieve a usage event.
30+
31+
#### Definition
32+
33+
`GET /v3/usage_events/:guid`
34+
35+
#### Permitted Roles
36+
|
37+
--- | ---
38+
Admin |
39+
Admin Read-Only |
40+
Global Auditor |
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Usage Events
2+
3+
Usage events are a record of changes in the usage of apps, services, and tasks.
4+
Examples include starting an application, scaling an application (from, say, one
5+
to three instances), and stopping an application.
6+
7+
Usage events are typically used by billing and chargeback applications.
8+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### The usage event object
2+
3+
```
4+
Example Usage Event object
5+
```
6+
```json
7+
<%= yield_content :single_usage_event %>
8+
```
9+
10+
Name | Type | Description
11+
---- | ---- | -----------
12+
**guid** | _uuid_ | Unique identifier for the event.
13+
**type** | _string_ | The type of the event, either "app" (application event) or "service" (service event)
14+
**created_at** | _datetime_ | The time with zone when the event occurred
15+
**updated_at** | _datetime_ | Identical to `created_at` (events are created, never updated)

docs/v3/source/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ includes:
3737
- api_resources/space_quotas
3838
- api_resources/stacks
3939
- api_resources/tasks
40+
- api_resources/usage_events
4041
- api_resources/users
4142
- introduction/introduction
4243
- concepts/concepts
@@ -351,6 +352,9 @@ includes:
351352
- experimental_resources/sidecars/list_for_app
352353
- experimental_resources/sidecars/list_for_process
353354
- experimental_resources/sidecars/delete
355+
- experimental_resources/usage_events/header
356+
- experimental_resources/usage_events/object
357+
- experimental_resources/usage_events/get
354358
- upgrade_guide/upgrade_guide
355359

356360
---

0 commit comments

Comments
 (0)