-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
206 deletions.
There are no files selected for viewing
156 changes: 59 additions & 97 deletions
156
warehouse/dbt/models/intermediate/funding/int_gitcoin_funding_events.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,69 @@ | ||
{# | ||
This model combines the donations and matching grants data to create a single table of funding events. | ||
The `funding_type` column is used to differentiate between donations and matching grants. | ||
#} | ||
|
||
|
||
with donations as ( | ||
select | ||
transaction_hash, | ||
donation_timestamp as event_time, | ||
gitcoin_round_id, | ||
coalesce(round_number, -1) as round_number, | ||
chain_id, | ||
gitcoin_project_id, | ||
project_application_title, | ||
project_recipient_address, | ||
donor_address, | ||
amount_in_usd, | ||
'DONATIONS' as funding_type | ||
from {{ ref('stg_gitcoin__donations') }} | ||
), | ||
|
||
matching as ( | ||
select | ||
cast(null as string) as transaction_hash, | ||
funding_rounds.date_round_ended as event_time, | ||
matching_data.gitcoin_round_id, | ||
matching_data.round_number, | ||
matching_data.chain_id, | ||
matching_data.gitcoin_project_id, | ||
matching_data.project_application_title, | ||
matching_data.project_recipient_address, | ||
cast(null as string) as donor_address, | ||
matching_data.amount_in_usd, | ||
'MATCHING' as funding_type | ||
from ( | ||
select | ||
gitcoin_round_id, | ||
coalesce(round_number, -1) as round_number, | ||
chain_id, | ||
gitcoin_project_id, | ||
project_application_title, | ||
project_recipient_address, | ||
amount_in_usd | ||
from {{ ref('stg_gitcoin__matching') }} | ||
) as matching_data | ||
left join {{ ref('int_gitcoin_funding_rounds') }} as funding_rounds | ||
on | ||
matching_data.gitcoin_round_id = funding_rounds.gitcoin_round_id | ||
and matching_data.round_number = funding_rounds.round_number | ||
), | ||
|
||
unioned_events as ( | ||
select * from donations | ||
with unioned_funding_events as ( | ||
select * from {{ ref('stg_gitcoin__donations') }} | ||
union all | ||
select * from matching | ||
select * from {{ ref('stg_gitcoin__matching') }} | ||
), | ||
|
||
project_directory_joined as ( | ||
labeled_funding_events as ( | ||
select | ||
unioned_events.*, | ||
project_directory.oso_project_id | ||
from unioned_events | ||
left join {{ ref('int_gitcoin_project_directory') }} as project_directory | ||
on unioned_events.gitcoin_project_id = project_directory.gitcoin_project_id | ||
where amount_in_usd > 0 | ||
*, | ||
case | ||
when ( | ||
gitcoin_data_source in ('MatchFunding', 'CGrants') | ||
and round_number between 1 and 15 | ||
) then concat('GG-', lpad(cast(round_number as string), 2, '0')) | ||
when ( | ||
gitcoin_data_source in ('MatchFunding', 'Alpha') | ||
and round_number = 16 | ||
) then 'GG-16' | ||
when ( | ||
gitcoin_data_source in ('MatchFunding', 'GrantsStack') | ||
and round_number is not null | ||
) then concat('GG-', lpad(cast(round_number as string), 2, '0')) | ||
end as main_round_label, | ||
case | ||
when ( | ||
gitcoin_data_source = 'CGrants' | ||
and round_number is null | ||
) then 'DirectDonations' | ||
when ( | ||
gitcoin_data_source in ('MatchFunding', 'GrantsStack') | ||
and round_number is null | ||
) then 'PartnerRound' | ||
else 'MainRound' | ||
end as round_type | ||
from unioned_funding_events | ||
), | ||
|
||
events as ( | ||
joined_events as ( | ||
select | ||
{{ oso_id("'GITCOIN'", 'gitcoin_round_id', 'round_number') }} | ||
as funding_round_id, | ||
gitcoin_round_id, | ||
round_number, | ||
chain_id, | ||
gitcoin_project_id, | ||
project_application_title, | ||
oso_project_id, | ||
donor_address, | ||
amount_in_usd, | ||
funding_type, | ||
event_time, | ||
transaction_hash | ||
from project_directory_joined | ||
labeled_funding_events.*, | ||
directory.oso_project_id, | ||
projects.project_name as oso_project_name, | ||
projects.display_name as oso_display_name | ||
from labeled_funding_events | ||
left join {{ ref('int_gitcoin_project_directory') }} as directory | ||
on labeled_funding_events.gitcoin_project_id = directory.gitcoin_project_id | ||
left join {{ ref('projects_v1') }} as projects | ||
on directory.oso_project_id = projects.project_id | ||
where amount_in_usd > 0 | ||
) | ||
|
||
select | ||
events.funding_round_id, | ||
events.gitcoin_round_id, | ||
events.round_number, | ||
funding_rounds.round_name, | ||
events.chain_id, | ||
events.gitcoin_project_id, | ||
events.project_application_title, | ||
events.oso_project_id, | ||
projects.project_name as oso_project_name, | ||
projects.display_name as oso_display_name, | ||
events.donor_address, | ||
events.amount_in_usd, | ||
events.funding_type, | ||
events.event_time, | ||
events.transaction_hash | ||
from events | ||
left join {{ ref('projects_v1') }} as projects | ||
on events.oso_project_id = projects.project_id | ||
left join {{ ref('int_gitcoin_funding_rounds') }} as funding_rounds | ||
on events.funding_round_id = funding_rounds.funding_round_id | ||
event_time, | ||
gitcoin_data_source, | ||
gitcoin_round_id, | ||
round_number, | ||
round_type, | ||
main_round_label, | ||
round_name, | ||
chain_id, | ||
gitcoin_project_id, | ||
project_application_title, | ||
oso_project_id, | ||
oso_project_name, | ||
oso_display_name, | ||
donor_address, | ||
amount_in_usd, | ||
transaction_hash | ||
from joined_events |
102 changes: 27 additions & 75 deletions
102
warehouse/dbt/models/intermediate/funding/int_gitcoin_funding_rounds.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,37 @@ | ||
-- Hardcode round dates for legacy cGrants rounds | ||
with round_dates as ( | ||
select | ||
round_number, | ||
timestamp(date_round_started) as date_round_started, | ||
timestamp(date_round_ended) as date_round_ended | ||
from unnest([ | ||
struct( | ||
1 as round_number, | ||
'2019-02-01T00:00:00Z' as date_round_started, | ||
'2019-02-15T00:00:00Z' as date_round_ended | ||
), | ||
struct(2, '2019-03-26T00:00:00Z', '2019-04-19T00:00:00Z'), | ||
struct(3, '2019-09-15T00:00:00Z', '2019-10-04T00:00:00Z'), | ||
struct(4, '2020-01-06T00:00:00Z', '2020-01-21T00:00:00Z'), | ||
struct(5, '2020-03-23T00:00:00Z', '2020-04-05T00:00:00Z'), | ||
struct(6, '2020-06-16T00:00:00Z', '2020-07-03T00:00:00Z'), | ||
struct(7, '2020-09-14T00:00:00Z', '2020-10-02T18:00:00Z'), | ||
struct(8, '2020-12-02T00:00:00Z', '2020-12-18T00:00:00Z'), | ||
struct(9, '2021-03-10T00:00:00Z', '2021-03-26T00:00:00Z'), | ||
struct(10, '2021-06-16T15:00:00Z', '2021-07-02T00:00:00Z'), | ||
struct(11, '2021-09-08T15:00:00Z', '2021-09-24T00:00:00Z'), | ||
struct(12, '2021-12-01T15:00:00Z', '2021-12-17T00:00:00Z'), | ||
struct(13, '2022-03-09T15:00:00Z', '2022-03-25T00:00:00Z'), | ||
struct(14, '2022-06-08T15:00:00Z', '2022-06-24T00:00:00Z'), | ||
struct(15, '2022-09-07T15:00:00Z', '2022-09-23T00:00:00Z'), | ||
struct(16, '2023-01-17T00:00:00Z', '2023-01-31T00:00:00Z') | ||
]) as v | ||
with partner_rounds as ( | ||
select * | ||
from {{ ref('int_gitcoin_funding_events') }} | ||
where | ||
round_type = 'PartnerRound' | ||
and round_name is not null | ||
), | ||
|
||
round_details as ( | ||
select | ||
gitcoin_round_id, | ||
round_name, | ||
coalesce(round_number, -1) as round_number, | ||
min(donation_timestamp) as date_round_started, | ||
max(donation_timestamp) as date_round_ended | ||
from {{ ref('stg_gitcoin__donations') }} | ||
group by gitcoin_round_id, round_number, round_name | ||
main_rounds as ( | ||
select * | ||
from {{ ref('int_gitcoin_funding_events') }} | ||
where round_type = 'MainRound' | ||
), | ||
|
||
matching as ( | ||
select | ||
gitcoin_round_id, | ||
chain_id, | ||
coalesce(round_number, -1) as round_number, | ||
sum(amount_in_usd) as matching_amount_in_usd | ||
from {{ ref('stg_gitcoin__matching') }} | ||
group by gitcoin_round_id, round_number, chain_id | ||
), | ||
|
||
combined as ( | ||
select | ||
matching.gitcoin_round_id, | ||
matching.round_number, | ||
matching.chain_id, | ||
matching.matching_amount_in_usd, | ||
coalesce(round_details.round_name, matching.gitcoin_round_id) | ||
as round_name, | ||
coalesce(round_details.date_round_started, round_dates.date_round_started) | ||
as date_round_started, | ||
coalesce(round_details.date_round_ended, round_dates.date_round_ended) | ||
as date_round_ended | ||
from matching | ||
left join round_details | ||
on | ||
matching.gitcoin_round_id = round_details.gitcoin_round_id | ||
and matching.round_number = round_details.round_number | ||
left join round_dates | ||
on matching.round_number = round_dates.round_number | ||
order by date_round_ended | ||
unioned as ( | ||
select * from partner_rounds | ||
union all | ||
select * from main_rounds | ||
) | ||
|
||
select | ||
{{ oso_id("'GITCOIN'", 'gitcoin_round_id', 'round_number') }} | ||
as funding_round_id, | ||
gitcoin_data_source, | ||
gitcoin_round_id, | ||
round_number, | ||
round_name, | ||
chain_id, | ||
date_round_started, | ||
date_round_ended, | ||
matching_amount_in_usd | ||
from combined | ||
order by date_round_ended desc | ||
round_type, | ||
main_round_label, | ||
array_agg(distinct chain_id) as chain_ids, | ||
sum(amount_in_usd) as total_amount_in_usd, | ||
count(distinct gitcoin_project_id) as count_projects | ||
from unioned | ||
group by 1, 2, 3, 4, 5, 6 | ||
order by | ||
round_type asc, | ||
main_round_label desc, | ||
gitcoin_round_id asc | ||
_oss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.