Skip to content

Commit

Permalink
fix: final refactor of all models
Browse files Browse the repository at this point in the history
  • Loading branch information
ccerv1 committed Dec 3, 2024
1 parent 9dce10f commit 8eb2adb
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 206 deletions.
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,51 @@ oso_projects as (
wallets.artifact_type = 'WALLET'
and repos.artifact_source = 'GITHUB'
and wallets.project_id = repos.project_id
)
),

wallet_matches as (
select distinct
gitcoin_projects.gitcoin_project_id,
gitcoin_projects.latest_project_github,
gitcoin_projects.latest_project_recipient_address,
oso_projects.oso_project_id as oso_wallet_match
from gitcoin_projects
left join oso_projects
on gitcoin_projects.latest_project_recipient_address = oso_projects.address
),

select distinct
oso_projects.oso_project_id,
gitcoin_projects.gitcoin_project_id,
gitcoin_projects.latest_project_github,
gitcoin_projects.latest_project_recipient_address
from gitcoin_projects
inner join oso_projects on (
gitcoin_projects.latest_project_github = oso_projects.repo_owner
and gitcoin_projects.latest_project_recipient_address = oso_projects.address
repo_matches as (
select distinct
wm.*,
oso_projects.oso_project_id as oso_repo_match
from wallet_matches as wm
left join oso_projects
on wm.latest_project_github = oso_projects.repo_owner
),

final_matches as (
select distinct
gitcoin_project_id,
latest_project_github,
latest_project_recipient_address,
oso_wallet_match,
oso_repo_match,
case
when oso_wallet_match is not null then oso_wallet_match
when oso_repo_match is not null then oso_repo_match
end as oso_project_id
from repo_matches
)

select
final_matches.gitcoin_project_id,
final_matches.latest_project_github,
final_matches.latest_project_recipient_address,
final_matches.oso_wallet_match,
final_matches.oso_repo_match,
final_matches.oso_project_id,
projects.project_name as oso_project_name,
projects.display_name as oso_display_name
from final_matches
left join {{ ref('projects_v1') }} as projects
on final_matches.oso_project_id = projects.project_id
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

with oss_funding_data as (
select -- noqa: ST06
CAST(funding_date as timestamp) as `time`,
cast(funding_date as timestamp) as `time`,
'GRANT_RECEIVED_USD' as event_type,
'{{ oss_funding_url }}' as event_source_id,
'OSS_FUNDING' as event_source,
LOWER(to_project_name) as to_project_name,
LOWER(from_funder_name) as from_project_name,
COALESCE(amount, 0) as amount
lower(to_project_name) as to_project_name,
lower(from_funder_name) as from_project_name,
coalesce(amount, 0) as amount
from {{ source('static_data_sources', 'oss_funding_v1') }}
where
to_project_name is not null
Expand All @@ -22,21 +22,17 @@ with oss_funding_data as (

gitcoin_data as (
select -- noqa: ST06
events.event_time as `time`,
event_time as `time`,
'GRANT_RECEIVED_USD' as event_type,
COALESCE(
events.transaction_hash,
CAST(events.gitcoin_project_id as string)
coalesce(
transaction_hash,
cast(gitcoin_project_id as string)
) as event_source_id,
CONCAT('GITCOIN', '_', funding_type) as event_source,
projects.project_name as to_project_name,
concat('GITCOIN', '_', upper(gitcoin_data_source)) as event_source,
oso_project_name as to_project_name,
'gitcoin' as from_project_name,
events.amount_in_usd as amount
from {{ ref('int_gitcoin_funding_events') }} as events
inner join {{ ref('int_gitcoin_project_directory') }} as project_directory
on events.gitcoin_project_id = project_directory.gitcoin_project_id
inner join {{ ref('projects_v1') }} as projects
on project_directory.oso_project_id = projects.project_id
amount_in_usd as amount
from {{ ref('int_gitcoin_funding_events') }}
),

grants as (
Expand Down
Loading

0 comments on commit 8eb2adb

Please sign in to comment.