Added /api/getGitActivity, added boilerplate for GitActivity

(yarn install is required after pull)
This commit is contained in:
Niklas Schrötler 2021-04-04 20:38:59 +02:00
parent d4d7e0d4f0
commit 8d7235bb82
7 changed files with 177 additions and 6 deletions

1
cache/getGitActivity vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -18,8 +18,10 @@
"build-storybook": "build-storybook -s public"
},
"dependencies": {
"fs": "^0.0.1-security",
"next": "10.0.5",
"react": "17.0.1",
"react-calendar-heatmap": "^1.8.1",
"react-dom": "17.0.1"
},
"devDependencies": {

View file

@ -0,0 +1,17 @@
import React from "react"
import { Story, Meta } from "@storybook/react/types-6-0"
import { GitActivity, GitActivityProps } from "."
export default {
title: "Component/GitActivity",
component: GitActivity,
argTypes: {
}
} as Meta
const Template: Story<GitActivityProps> = (args) => <GitActivity {...args} />
// Default scenario
export const Default = Template.bind({})
Default.args = {}

View file

@ -0,0 +1,13 @@
import React from "react";
export interface GitActivityProps {
}
export const GitActivity: React.FC<GitActivityProps> = () => {
return (
<div>
</div>
)
}

View file

@ -0,0 +1,126 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import * as fs from "fs";
import {number} from "prop-types";
import cachedShouldLoadAsEsm from "jest-resolve/build/shouldLoadAsEsm";
const path = "./cache/getGitActivity";
const validTime = 1000 * 60 * 30; // 30 min in milliseconds
type GitActivityCache = {
data: Map<string, number>,
lastRefresh: number
}
export default (req, res) => {
if(fs.existsSync(path)) {
try {
let data: GitActivityCache = JSON.parse(fs.readFileSync(path).toString());
if(Date.now() - data.lastRefresh < validTime) {
res.statusCode = 200;
res.send(JSON.stringify(data));
}
else {
getData()
.then((data) => {
res.statusCode = 200;
res.send(data)
})
.catch(() => {
res.statusCode = 200;
res.send(data.data);
});
}
} catch (e) {
getData()
.then((data) => {
res.statusCode = 500;
res.send(data)
})
.catch(() => {
res.statusCode = 500;
res.send("Error while fetching data.");
});
}
}
else {
getData()
.then((data) => {
res.statusCode = 200;
res.send(data)
})
.catch(() => {
res.statusCode = 500;
res.send("Error while fetching data.");
});
}
}
async function getData(): Promise<string> {
let gitData = await getGitData();
fs.writeFileSync(path, JSON.stringify({data: gitData, lastRefresh: Date.now()}));
return JSON.stringify(gitData);
}
async function getGitData(): Promise<{ [date: string]: number }> {
console.log("Refreshing Data");
let token = "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";
let username = "networkException";
const output: { [date: string]: number } = {};
const headers = {
'Authorization': `bearer ${token}`,
};
const body = {
'query': `query {
user(login: "${username}") {
name
contributionsCollection {
contributionCalendar {
totalContributions
weeks {
contributionDays {
contributionCount
date
}
}
}
}
}
}`
};
const githubResponse = await fetch('https://api.github.com/graphql', { method: 'POST', body: JSON.stringify(body), headers });
(await githubResponse.json()).data.user.contributionsCollection.contributionCalendar.weeks.forEach((week: any) => {
week.contributionDays.forEach((day: any) => {
if (day.date in output) {
output[day.date] = output[day.date] + Number(day.contributionCount);
}
else {
output[day.date] = Number(day.contributionCount);
}
});
});
const gitlabResponse: { [date: string]: number } = await (await fetch('https://gitlab.upi.li/users/networkException/calendar.json')).json();
for (const date in gitlabResponse) {
if (date in output) {
output[date] = output[date] + gitlabResponse[date];
}
else {
output[date] = gitlabResponse[date];
}
}
return output;
}

View file

@ -1,6 +0,0 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default (req, res) => {
res.statusCode = 200
res.json({ name: 'John Doe' })
}

View file

@ -6315,6 +6315,11 @@ fs.realpath@^1.0.0:
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
fs@^0.0.1-security:
version "0.0.1-security"
resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4"
integrity sha1-invTcYa23d84E/I4WLV+yq9eQdQ=
fsevents@^1.2.7:
version "1.2.13"
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.13.tgz#f325cb0455592428bcf11b383370ef70e3bfcc38"
@ -8656,6 +8661,11 @@ media-typer@0.3.0:
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
memoize-one@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0"
integrity sha512-HKeeBpWvqiVJD57ZUAsJNm71eHTykffzcLZVYWiVfQeI1rJtuEaS7hQiEpWfVVk18donPwJEcFKIkCmPJNOhHA==
memoizerific@^1.11.3:
version "1.11.3"
resolved "https://registry.yarnpkg.com/memoizerific/-/memoizerific-1.11.3.tgz#7c87a4646444c32d75438570905f2dbd1b1a805a"
@ -10345,6 +10355,14 @@ rc@^1.2.7, rc@^1.2.8:
minimist "^1.2.0"
strip-json-comments "~2.0.1"
react-calendar-heatmap@^1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/react-calendar-heatmap/-/react-calendar-heatmap-1.8.1.tgz#8370151f6cf7477d49cb3459596d6d046bdfc704"
integrity sha512-4Hbq/pDMJoCPzZnyIWFfHgokLlLXzKyGsDcMgNhYpi7zcKHcvsK9soLEPvhW2dBBqgDrQOSp/uG4wtifaDg4eQ==
dependencies:
memoize-one "^5.0.0"
prop-types "^15.6.2"
react-color@^2.17.0:
version "2.19.3"
resolved "https://registry.yarnpkg.com/react-color/-/react-color-2.19.3.tgz#ec6c6b4568312a3c6a18420ab0472e146aa5683d"