forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily_commitments.sql
More file actions
90 lines (87 loc) · 3.58 KB
/
Copy pathdaily_commitments.sql
File metadata and controls
90 lines (87 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* It is assumed that the following query will be run in the administration
* project where the reservations were created. If this is not the case,
* prepend the project id to the table name as follows:
* `{project_id}`.`region-{region_name}`.INFORMATION_SCHEMA.{table}
*/
/*
* Daily Utilization Report: Returns the slot count for active monthly and
* annual capacity commitments for each day since the first commitment
* was purchased.
*/
-- 'commitments' extracts the commitment history and computes the cumulative slot count
-- 'results' takes the most recent entry and slot count for a given day
-- 'days' generates an entry for each day between two entries
WITH
commitments AS (
SELECT
change_timestamp,
commitment_plan,
action,
EXTRACT(DATE FROM change_timestamp) AS start_date,
-- Compute the stop date of a commitment by looking at the following
-- change_timestamp and subtracting one day. This works because monthly
-- and yearly commitments cannot be deleted on the same day
-- they were created.
IFNULL(
LEAD(DATE_SUB(EXTRACT(DATE FROM change_timestamp), INTERVAL 1 DAY))
OVER (PARTITION BY state ORDER BY change_timestamp),
CURRENT_DATE()) AS stop_date,
-- In order to calculate the cumulative slots up to this point, add
-- the slot count of new commitments (indicated by a CREATE or UPDATE action)
-- and subtract the slot count of deleted commitments
SUM(CASE WHEN cccp.action IN ('CREATE', 'UPDATE') THEN cccp.slot_count ELSE cccp.slot_count * -1 END)
-- Cumulative slots are tracked by their state and carried over from
-- previous rows
OVER (
PARTITION BY state
ORDER BY change_timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS slot_cummulative,
-- In the event that multiple changes occurred in one day, we keep track
-- of the most recent cumulative value by using row numbers
ROW_NUMBER()
OVER (
PARTITION BY EXTRACT(DATE FROM change_timestamp)
ORDER BY change_timestamp DESC
) AS rn
FROM
`region-{region_name}`.INFORMATION_SCHEMA.CAPACITY_COMMITMENT_CHANGES_BY_PROJECT AS cccp
-- In this case, we only want to look at active commitments that are
-- monthly or annual, not flex
WHERE
state = 'ACTIVE'
AND commitment_plan != 'FLEX'
ORDER BY change_timestamp
),
results AS (SELECT * FROM commitments WHERE rn = 1),
days AS (
-- This subquery is used to fill in the missing days between a commitment
-- starting and ending so that it can be graphed properly.
SELECT day
FROM (
SELECT
start_date,
stop_date
FROM results
), UNNEST(GENERATE_DATE_ARRAY(start_date, stop_date)) day
)
SELECT TIMESTAMP(day) as date, LAST_VALUE(slot_cummulative IGNORE NULLS) OVER(ORDER BY day) slots,
FROM days
-- Join these results with the cumulative slot count values for each day
LEFT JOIN results
ON day = DATE(change_timestamp)