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
11 changed files
with
5,399 additions
and
62 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-react"], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import format from 'date-fns/format'; | ||
import { Bar, XAxis, YAxis, Legend, Tooltip, BarChart, CartesianGrid } from 'recharts'; | ||
|
||
import { capitalize, randomColor } from './utils'; | ||
|
||
const formatter = t => format(new Date(t), 'MMM dd'); | ||
const labelFormatter = value => capitalize(value); | ||
|
||
export const InsightsBarChart = props => ( | ||
<div className="chart"> | ||
<h2>{props.title}</h2> | ||
|
||
<p>Count: {props.data.count}</p> | ||
|
||
<p>Unique: {props.data.uniques}</p> | ||
|
||
<BarChart width={700} height={300} data={props.name ? props.data[props.name] : props.data}> | ||
<CartesianGrid strokeDasharray="3 3" /> | ||
<XAxis dataKey={props.dataKey} tickFormatter={props.dataKey === 'timestamp' ? formatter : () => ''} /> | ||
<YAxis /> | ||
<Tooltip labelFormatter={props.dataKey === 'timestamp' ? formatter : t => t} /> | ||
<Legend formatter={labelFormatter} /> | ||
<Bar dataKey="count" fill={randomColor()} /> | ||
<Bar dataKey="uniques" fill={randomColor()} /> | ||
</BarChart> | ||
</div> | ||
); |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react'; | ||
|
||
import { InsightsBarChart } from './bar-chart'; | ||
import { InsightsPieChart } from './pie-chart'; | ||
|
||
export const Charts = props => ( | ||
<div className="charts"> | ||
<InsightsBarChart | ||
name="paths" | ||
data={props.data.paths.reduce( | ||
(result, path) => { | ||
result.count = (result.count || 0) + path.count; | ||
result.uniques = (result.uniques || 0) + path.uniques; | ||
|
||
return result; | ||
}, | ||
{ | ||
paths: props.data.paths | ||
} | ||
)} | ||
title="Paths" | ||
dataKey="path" | ||
/> | ||
|
||
<InsightsBarChart name="views" data={props.data.views} title="Views" dataKey="timestamp" /> | ||
|
||
<InsightsBarChart name="views" data={props.data.forks} title="Forks" dataKey="timestamp" /> | ||
|
||
<InsightsBarChart name="clones" data={props.data.clones} title="Clones" dataKey="timestamp" /> | ||
|
||
<InsightsPieChart name="referrer" data={props.data.referrers} title="Referrers" dataKey="timestamp" /> | ||
|
||
<InsightsPieChart | ||
name="author" | ||
data={props.data.contributors.map(item => ({ | ||
count: item.total, | ||
author: item.author.login | ||
}))} | ||
title="Contributors" | ||
/> | ||
</div> | ||
); |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
|
||
export const Form = props => ( | ||
<div className="form"> | ||
<h1>Github Insights</h1> | ||
|
||
<p>Get insights for your repository</p> | ||
|
||
<form onSubmit={props.onSubmit}> | ||
<input | ||
type="text" | ||
value={props.repository} | ||
placeholder=":owner/:repository" | ||
required | ||
onChange={props.onChange} | ||
/> | ||
|
||
<input type="submit" value="Go" /> | ||
</form> | ||
</div> | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import { Form } from './form'; | ||
import { Charts } from './charts'; | ||
|
||
const App = () => { | ||
const [data, setData] = React.useState(); | ||
const [error, setError] = React.useState(''); | ||
const [loading, setLoading] = React.useState(false); | ||
const [repository, setRepository] = React.useState(''); | ||
|
||
const onSubmit = event => { | ||
event.preventDefault(); | ||
|
||
setLoading(true); | ||
|
||
fetch('/insights', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: 'repository=' + repository | ||
}) | ||
.then(async res => { | ||
const [response] = await res.json(); | ||
const errors = Object.values(response) | ||
.map(value => value.status) | ||
.filter(Boolean).length; | ||
|
||
if (errors > 0) { | ||
setError(`Repository ${repository} does not exist.`); | ||
} else { | ||
setData(response); | ||
setError(''); | ||
} | ||
}) | ||
.catch(_ => setError('Something went wrong. Please try again later.')) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}; | ||
|
||
return loading ? ( | ||
<div className="loading">... fetching data for {repository}</div> | ||
) : ( | ||
<div className={`insights${data ? ' insights--fetched' : ''}`}> | ||
{error && <div className="error">{error}</div>} | ||
|
||
<Form onSubmit={onSubmit} onChange={e => setRepository(e.target.value)} repository={repository} /> | ||
|
||
{data && <Charts data={data} />} | ||
</div> | ||
); | ||
}; | ||
|
||
ReactDOM.render(<App />, document.getElementById('app')); |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import { Pie, PieChart, Tooltip } from 'recharts'; | ||
|
||
import { randomColor } from './utils'; | ||
|
||
export const InsightsPieChart = props => ( | ||
<div className="chart"> | ||
<h2>{props.title}</h2> | ||
|
||
<PieChart width={730} height={250}> | ||
<Pie | ||
cx="50%" | ||
cy="50%" | ||
data={props.data} | ||
fill={randomColor()} | ||
label | ||
dataKey="count" | ||
nameKey={props.name} | ||
outerRadius={100} | ||
/> | ||
|
||
<Tooltip /> | ||
</PieChart> | ||
</div> | ||
); |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const capitalize = ([first, ...rest]) => first.toUpperCase() + rest.map(letter => letter.toLowerCase()).join(''); | ||
|
||
export const randomColor = () => '#' + Math.floor(Math.random() * 16777215).toString(16); |
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.
21f8899
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: