Skip to content

Commit

Permalink
queries: add org stars API (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini256 authored Sep 6, 2023
1 parent 4387190 commit b38c2b1
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 0 deletions.
30 changes: 30 additions & 0 deletions configs/queries/orgs/stars/locations/params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"cacheHours": 1,
"engine": "liquid",
"params": [
{
"name": "ownerId",
"replaces": "11855343",
"type": "integer"
},
{
"name": "repoIds",
"replaces": "41986369",
"type": "array",
"default": [],
"itemType": "integer",
"maxArrayLength": 50
},
{
"name": "period",
"type": "string",
"enums": ["past_7_days", "past_28_days", "past_90_days", "past_12_months"],
"default": "past_28_days"
},
{
"name": "n",
"type": "integer",
"default": 10
}
]
}
28 changes: 28 additions & 0 deletions configs/queries/orgs/stars/locations/template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
WITH repos AS (
SELECT
gr.repo_id, gr.repo_name
FROM github_repos gr
WHERE
gr.owner_id = {{ownerId}}
{% if repoIds.size > 0 %}
AND gr.repo_id IN ({{ repoIds | join: ',' }})
{% endif %}
)
SELECT
IF(gu.country_code IN ('', 'N/A', 'UND'), 'UND', gu.country_code) AS country_code,
COUNT(*) AS stars
FROM github_events ge
JOIN github_users gu ON ge.actor_login = gu.login
WHERE
ge.repo_id IN (SELECT repo_id FROM repos)
AND ge.type = 'WatchEvent'
AND ge.action = 'started'
{% case period %}
{% when 'past_7_days' %} AND ge.created_at > (NOW() - INTERVAL 7 DAY)
{% when 'past_28_days' %} AND ge.created_at > (NOW() - INTERVAL 28 DAY)
{% when 'past_90_days' %} AND ge.created_at > (NOW() - INTERVAL 90 DAY)
{% when 'past_12_months' %} AND ge.created_at > (NOW() - INTERVAL 12 MONTH)
{% endcase %}
GROUP BY gu.country_code
ORDER BY stars DESC
LIMIT {{ n }}
30 changes: 30 additions & 0 deletions configs/queries/orgs/stars/organizations/params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"cacheHours": 1,
"engine": "liquid",
"params": [
{
"name": "ownerId",
"replaces": "11855343",
"type": "integer"
},
{
"name": "repoIds",
"replaces": "41986369",
"type": "array",
"default": [],
"itemType": "integer",
"maxArrayLength": 50
},
{
"name": "period",
"type": "string",
"enums": ["past_7_days", "past_28_days", "past_90_days", "past_12_months"],
"default": "past_28_days"
},
{
"name": "n",
"type": "integer",
"default": 10
}
]
}
31 changes: 31 additions & 0 deletions configs/queries/orgs/stars/organizations/template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
WITH repos AS (
SELECT
gr.repo_id, gr.repo_name
FROM github_repos gr
WHERE
gr.owner_id = {{ownerId}}
{% if repoIds.size > 0 %}
AND gr.repo_id IN ({{ repoIds | join: ',' }})
{% endif %}
)
SELECT
TRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(gu.organization, ',', ''), '-', ''), '@', ''), 'www.', ''), 'inc', ''), '.com', ''), '.cn', ''), '.', '')) AS organization_name,
CASE WHEN
COUNT(DISTINCT actor_login) AS stars
FROM github_events ge
JOIN github_users gu ON ge.actor_login = gu.login
WHERE
ge.repo_id IN (SELECT repo_id FROM repos)
AND ge.type = 'WatchEvent'
AND ge.action = 'started'
{% case period %}
{% when 'past_7_days' %} AND ge.created_at > (NOW() - INTERVAL 7 DAY)
{% when 'past_28_days' %} AND ge.created_at > (NOW() - INTERVAL 28 DAY)
{% when 'past_90_days' %} AND ge.created_at > (NOW() - INTERVAL 90 DAY)
{% when 'past_12_months' %} AND ge.created_at > (NOW() - INTERVAL 12 MONTH)
{% endcase %}
AND LENGTH(gu.organization) != 0
AND gu.organization NOT IN ('', '-', 'none', 'no', 'home', 'n/a', 'null', 'unknown')
GROUP BY organization_name
ORDER BY stars DESC
LIMIT {{ n }}
25 changes: 25 additions & 0 deletions configs/queries/orgs/stars/top-repos/params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cacheHours": 1,
"engine": "liquid",
"params": [
{
"name": "ownerId",
"replaces": "11855343",
"type": "integer"
},
{
"name": "repoIds",
"replaces": "41986369",
"type": "array",
"default": [],
"itemType": "integer",
"maxArrayLength": 50
},
{
"name": "period",
"type": "string",
"enums": ["past_7_days", "past_28_days", "past_90_days", "past_12_months"],
"default": "past_28_days"
}
]
}
40 changes: 40 additions & 0 deletions configs/queries/orgs/stars/top-repos/template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
WITH repos AS (
SELECT
gr.repo_id, gr.repo_name
FROM github_repos gr
WHERE
gr.owner_id = {{ownerId}}
{% if repoIds.size > 0 %}
AND gr.repo_id IN ({{ repoIds | join: ',' }})
{% endif %}
), repos_with_stars AS (
SELECT
repo_id,
COUNT(*) AS stars
FROM github_events ge
WHERE
ge.repo_id IN (SELECT repo_id FROM repos)
AND ge.type = 'WatchEvent'
AND ge.action = 'started'
{% case period %}
{% when 'past_7_days' %} AND created_at > (NOW() - INTERVAL 7 DAY)
{% when 'past_28_days' %} AND created_at > (NOW() - INTERVAL 28 DAY)
{% when 'past_90_days' %} AND created_at > (NOW() - INTERVAL 90 DAY)
{% when 'past_12_months' %} AND created_at > (NOW() - INTERVAL 12 MONTH)
{% endcase %}
GROUP BY repo_id
ORDER BY stars DESC
LIMIT 10
)
SELECT
gr.repo_id,
gr.repo_name,
rws.stars
FROM repos_with_stars rws
JOIN github_repos gr USING (repo_id)
ORDER BY stars DESC
LIMIT 10




25 changes: 25 additions & 0 deletions configs/queries/orgs/stars/total/params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cacheHours": 1,
"engine": "liquid",
"params": [
{
"name": "ownerId",
"replaces": "11855343",
"type": "integer"
},
{
"name": "repoIds",
"replaces": "41986369",
"type": "array",
"default": [],
"itemType": "integer",
"maxArrayLength": 50
},
{
"name": "period",
"type": "string",
"enums": ["past_7_days", "past_28_days", "past_90_days", "past_12_months"],
"default": "past_28_days"
}
]
}
43 changes: 43 additions & 0 deletions configs/queries/orgs/stars/total/template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
WITH repos AS (
SELECT gr.repo_id
FROM github_repos gr
WHERE
gr.owner_id = {{ownerId}}
{% if repoIds.size > 0 %}
AND gr.repo_id IN ({{ repoIds | join: ',' }})
{% endif %}
), stars_per_period AS (
SELECT
-- Divide periods.
{% case period %}
{% when 'past_7_days' %} TIMESTAMPDIFF(DAY, created_at, NOW()) DIV 7
{% when 'past_28_days' %} TIMESTAMPDIFF(DAY, created_at, NOW()) DIV 28
{% when 'past_90_days' %} TIMESTAMPDIFF(DAY, created_at, NOW()) DIV 90
{% when 'past_12_months' %} TIMESTAMPDIFF(MONTH, created_at, NOW()) DIV 12
{% endcase %} AS period,
COUNT(*) AS stars_total
FROM github_events ge
WHERE
ge.repo_id IN (SELECT repo_id FROM repos)
AND type = 'WatchEvent'
AND action = 'started'
{% case period %}
{% when 'past_7_days' %} AND created_at > (NOW() - INTERVAL 14 DAY)
{% when 'past_28_days' %} AND created_at > (NOW() - INTERVAL 56 DAY)
{% when 'past_90_days' %} AND created_at > (NOW() - INTERVAL 180 DAY)
{% when 'past_12_months' %} AND created_at > (NOW() - INTERVAL 24 MONTH)
{% endcase %}
GROUP BY period
), current_period_stars AS (
SELECT stars_total FROM stars_per_period WHERE period = 0
), past_period_stars AS (
SELECT stars_total FROM stars_per_period WHERE period = 1
)
SELECT
cps.stars_total AS current_period_total,
pps.stars_total AS past_period_total,
(cps.stars_total - pps.stars_total) / pps.stars_total AS growth_percentage
FROM
current_period_stars cps,
past_period_stars pps
;
25 changes: 25 additions & 0 deletions configs/queries/orgs/stars/trends/params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"cacheHours": 1,
"engine": "liquid",
"params": [
{
"name": "ownerId",
"replaces": "11855343",
"type": "integer"
},
{
"name": "repoIds",
"replaces": "41986369",
"type": "array",
"default": [],
"itemType": "integer",
"maxArrayLength": 50
},
{
"name": "period",
"type": "string",
"enums": ["past_7_days", "past_28_days", "past_90_days", "past_12_months"],
"default": "past_28_days"
}
]
}
102 changes: 102 additions & 0 deletions configs/queries/orgs/stars/trends/template.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
WITH RECURSIVE seq(idx, current_period_day, past_period_day) AS (
SELECT
1 AS idx,
{% case period %}
{% when 'past_7_days', 'past_28_days', 'past_90_days' %} DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d')
{% when 'past_12_months' %} DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')
{% endcase %} AS current_period_day,
{% case period %}
{% when 'past_7_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY), '%Y-%m-%d')
{% when 'past_28_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 28 DAY), '%Y-%m-%d')
{% when 'past_90_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY), '%Y-%m-%d')
{% when 'past_12_months' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL 12 MONTH), '%Y-%m-01')
{% endcase %} AS past_period_day
UNION ALL
SELECT
idx + 1 AS idx,
{% case period %}
{% when 'past_7_days', 'past_28_days', 'past_90_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx DAY), '%Y-%m-%d')
{% when 'past_12_months' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx MONTH), '%Y-%m-01')
{% endcase %} AS current_period_day,
{% case period %}
{% when 'past_7_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx + 7 DAY), '%Y-%m-%d')
{% when 'past_28_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx + 28 DAY), '%Y-%m-%d')
{% when 'past_90_days' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx + 90 DAY), '%Y-%m-%d')
{% when 'past_12_months' %} DATE_FORMAT(DATE_SUB(CURRENT_DATE(), INTERVAL idx + 12 MONTH), '%Y-%m-01')
{% endcase %} AS past_period_day
FROM seq
WHERE
1 = 1
{% case period %}
{% when 'past_7_days' %} AND idx < 7
{% when 'past_28_days' %} AND idx < 28
{% when 'past_90_days' %} AND idx < 90
{% when 'past_12_months' %} AND idx < 12
{% endcase %}
), repos AS (
SELECT gr.repo_id
FROM github_repos gr
WHERE
gr.owner_id = {{ownerId}}
{% if repoIds.size > 0 %}
AND gr.repo_id IN ({{ repoIds | join: ',' }})
{% endif %}
), group_by_day AS (
SELECT
{% case period %}
{% when 'past_7_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) % 7 + 1
{% when 'past_28_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) % 28 + 1
{% when 'past_90_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) % 90 + 1
{% when 'past_12_months' %} TIMESTAMPDIFF(MONTH, day, CURRENT_DATE()) % 12 + 1
{% endcase %} AS idx,
-- Divide periods.
{% case period %}
{% when 'past_7_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) DIV 7
{% when 'past_28_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) DIV 28
{% when 'past_90_days' %} TIMESTAMPDIFF(DAY, day, CURRENT_DATE()) DIV 90
{% when 'past_12_months' %} TIMESTAMPDIFF(MONTH, day, CURRENT_DATE()) DIV 12
{% endcase %} AS period,
day,
stars
FROM (
SELECT
{% case period %}
{% when 'past_7_days', 'past_28_days', 'past_90_days' %} DATE_FORMAT(created_at, '%Y-%m-%d')
{% when 'past_12_months' %} DATE_FORMAT(created_at, '%Y-%m-01')
{% endcase %} AS day,
COUNT(*) AS stars
FROM
github_events ge
WHERE
repo_id IN (SELECT repo_id FROM repos)
AND type = 'WatchEvent'
AND action = 'started'
{% case period %}
{% when 'past_7_days' %} AND created_at > (CURRENT_DATE() - INTERVAL 14 DAY)
{% when 'past_28_days' %} AND created_at > (CURRENT_DATE() - INTERVAL 56 DAY)
{% when 'past_90_days' %} AND created_at > (CURRENT_DATE() - INTERVAL 180 DAY)
{% when 'past_12_months' %} AND created_at > (CURRENT_DATE() - INTERVAL 24 MONTH)
{% endcase %}
GROUP BY day
ORDER BY day
) sub
), current_period AS (
SELECT idx, day, stars
FROM group_by_day
WHERE period = 0
), past_period AS (
SELECT idx, day, stars
FROM group_by_day
WHERE period = 1
)
SELECT
s.idx AS idx,
s.current_period_day AS current_period_day,
IFNULL(cp.stars, 0) AS current_period_day_total,
s.past_period_day AS past_period_day,
IFNULL(pp.stars, 0) AS past_period_day_total
FROM seq s
LEFT JOIN current_period cp ON s.idx = cp.idx
LEFT JOIN past_period pp ON s.idx = pp.idx
ORDER BY idx
;

0 comments on commit b38c2b1

Please sign in to comment.