Skip to content

Commit

Permalink
Merge pull request #1 from pmbrull/feature/competitors
Browse files Browse the repository at this point in the history
Add competitor commit data
  • Loading branch information
pmbrull authored Feb 7, 2022
2 parents f419915 + 2bd55ab commit 028cf73
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
9 changes: 8 additions & 1 deletion openstats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ social: "
[![Github](https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white)](https://github.com/open-metadata/OpenMetadata)
[![Slack](https://img.shields.io/badge/Slack-4A154B?style=for-the-badge&logo=slack&logoColor=white)](https://slack.open-metadata.org/)
"
"

# Compare commit activity
competitors:
- repo: datahub
owner: linkedin
- repo: amundsen
owner: amundsen-io
2 changes: 1 addition & 1 deletion openstats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Discover and share your OpenSource KPIs"""

__version__ = "0.1.6"
__version__ = "0.1.7"
4 changes: 3 additions & 1 deletion openstats/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def stats(cfg: Config):
builder.contributors_component()
st.markdown("---")
builder.traffic_component()
st.markdown("---")
builder.competitors_component()


def run():
config = Config.read_file("openstats.yaml")
config = Config.read_file("openstats.yaml", list_id="repo")
stats(config)
65 changes: 65 additions & 0 deletions openstats/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
components to show on the app
"""

from datetime import datetime

import altair as alt
import streamlit as st
from levy.config import Config
Expand Down Expand Up @@ -189,3 +191,66 @@ def sidebar(self):
st.markdown(
"Powered by [OpenStats](https://github.com/pmbrull/open-stats) 🚀"
)

def competitors_component(self):
"""
Prepare a bar chart with commit activity
if we are comparing competitors
"""

if self.config("competitors", None):

activity = self.data.competitors_data()

last_month = activity[-4:].sum().to_frame(name="commits")
last_month["repo"] = last_month.index

with st.container():
st.subheader("Competitors")

bars = (
alt.Chart(
last_month,
title="Last month commit activity",
)
.mark_bar()
.encode(
x=alt.X(
"repo",
title="Repository",
sort=alt.EncodingSortField(
field="commits", op="count", order="descending"
),
axis=alt.Axis(labelAngle=-45),
),
y=alt.Y("commits", title="#Commits"),
color=alt.value(self.color),
)
)

text = bars.mark_text(
align="center",
baseline="middle",
fontSize=13,
dy=-8, # Nudges text to top so it doesn't appear on top of the bar
).encode(
text="commits:Q",
)

chart = (bars + text).properties(
width=650,
height=350,
)

st.altair_chart(chart)

button_col, desc_col = st.columns(2)
button_col.download_button(
label="Download CSV",
data=activity.to_csv(index=False).encode("utf-8"),
file_name=f"commit_activity_{datetime.now().strftime('%Y%m%d')}",
mime="text/csv",
)
desc_col.write(
"Download the last 52 weeks of commit activity as a CSV file."
)
32 changes: 32 additions & 0 deletions openstats/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Data:
def __init__(self, client: Client):
self.client = client

# Use client's Levy config
self.config = self.client.config

def stars_data(self) -> Optional[DataFrame]:
"""
Extract information from stargazers.
Expand Down Expand Up @@ -180,3 +183,32 @@ def traffic_data(self):
).get("uniques")

return clones, views

def get_participation(self, owner: str, repo: str):
"""
Get all participation data for the last 52 weeks
as a reversed list
"""
return self.client.get(
self.client.root / "repos" / owner / repo / "stats" / "participation"
).json()["all"]

def competitors_data(self) -> DataFrame:
"""
Compare your project stats vs. a list
of competitors.
Return my activity a list of competitor's activity
"""
my_activity = {
self.client.repo: self.get_participation(
self.client.owner, self.client.repo
)
}

activity = {
competitor.repo: self.get_participation(competitor.owner, competitor.repo)
for competitor in self.config.competitors
}

return pd.DataFrame({**my_activity, **activity})

0 comments on commit 028cf73

Please sign in to comment.