diff --git a/cache/getGitActivity b/cache/getGitActivity new file mode 100644 index 0000000..9afe52c --- /dev/null +++ b/cache/getGitActivity @@ -0,0 +1 @@ +{"data":{"2020-04-05":0,"2020-04-06":0,"2020-04-07":2,"2020-04-08":0,"2020-04-09":0,"2020-04-10":0,"2020-04-11":0,"2020-04-12":0,"2020-04-13":0,"2020-04-14":1,"2020-04-15":0,"2020-04-16":4,"2020-04-17":0,"2020-04-18":0,"2020-04-19":0,"2020-04-20":2,"2020-04-21":10,"2020-04-22":0,"2020-04-23":0,"2020-04-24":3,"2020-04-25":0,"2020-04-26":0,"2020-04-27":0,"2020-04-28":0,"2020-04-29":0,"2020-04-30":2,"2020-05-01":0,"2020-05-02":0,"2020-05-03":6,"2020-05-04":18,"2020-05-05":3,"2020-05-06":1,"2020-05-07":0,"2020-05-08":0,"2020-05-09":1,"2020-05-10":0,"2020-05-11":1,"2020-05-12":0,"2020-05-13":0,"2020-05-14":1,"2020-05-15":1,"2020-05-16":0,"2020-05-17":1,"2020-05-18":0,"2020-05-19":0,"2020-05-20":0,"2020-05-21":4,"2020-05-22":0,"2020-05-23":0,"2020-05-24":0,"2020-05-25":0,"2020-05-26":5,"2020-05-27":0,"2020-05-28":0,"2020-05-29":3,"2020-05-30":2,"2020-05-31":1,"2020-06-01":3,"2020-06-02":0,"2020-06-03":2,"2020-06-04":1,"2020-06-05":0,"2020-06-06":0,"2020-06-07":0,"2020-06-08":0,"2020-06-09":0,"2020-06-10":0,"2020-06-11":7,"2020-06-12":2,"2020-06-13":3,"2020-06-14":5,"2020-06-15":1,"2020-06-16":1,"2020-06-17":0,"2020-06-18":1,"2020-06-19":11,"2020-06-20":1,"2020-06-21":5,"2020-06-22":1,"2020-06-23":6,"2020-06-24":1,"2020-06-25":5,"2020-06-26":1,"2020-06-27":10,"2020-06-28":6,"2020-06-29":4,"2020-06-30":13,"2020-07-01":0,"2020-07-02":0,"2020-07-03":14,"2020-07-04":3,"2020-07-05":2,"2020-07-06":6,"2020-07-07":9,"2020-07-08":0,"2020-07-09":0,"2020-07-10":0,"2020-07-11":0,"2020-07-12":4,"2020-07-13":0,"2020-07-14":0,"2020-07-15":0,"2020-07-16":1,"2020-07-17":0,"2020-07-18":0,"2020-07-19":0,"2020-07-20":1,"2020-07-21":1,"2020-07-22":0,"2020-07-23":0,"2020-07-24":0,"2020-07-25":0,"2020-07-26":4,"2020-07-27":0,"2020-07-28":0,"2020-07-29":3,"2020-07-30":0,"2020-07-31":0,"2020-08-01":0,"2020-08-02":0,"2020-08-03":0,"2020-08-04":0,"2020-08-05":0,"2020-08-06":0,"2020-08-07":0,"2020-08-08":0,"2020-08-09":0,"2020-08-10":0,"2020-08-11":0,"2020-08-12":0,"2020-08-13":0,"2020-08-14":0,"2020-08-15":0,"2020-08-16":0,"2020-08-17":0,"2020-08-18":4,"2020-08-19":0,"2020-08-20":1,"2020-08-21":0,"2020-08-22":0,"2020-08-23":3,"2020-08-24":2,"2020-08-25":6,"2020-08-26":0,"2020-08-27":11,"2020-08-28":0,"2020-08-29":0,"2020-08-30":20,"2020-08-31":3,"2020-09-01":5,"2020-09-02":4,"2020-09-03":21,"2020-09-04":1,"2020-09-05":1,"2020-09-06":9,"2020-09-07":9,"2020-09-08":2,"2020-09-09":0,"2020-09-10":0,"2020-09-11":0,"2020-09-12":3,"2020-09-13":6,"2020-09-14":1,"2020-09-15":2,"2020-09-16":4,"2020-09-17":0,"2020-09-18":9,"2020-09-19":35,"2020-09-20":20,"2020-09-21":0,"2020-09-22":3,"2020-09-23":0,"2020-09-24":0,"2020-09-25":32,"2020-09-26":19,"2020-09-27":51,"2020-09-28":3,"2020-09-29":0,"2020-09-30":2,"2020-10-01":2,"2020-10-02":15,"2020-10-03":10,"2020-10-04":3,"2020-10-05":7,"2020-10-06":1,"2020-10-07":3,"2020-10-08":10,"2020-10-09":2,"2020-10-10":35,"2020-10-11":15,"2020-10-12":12,"2020-10-13":1,"2020-10-14":0,"2020-10-15":5,"2020-10-16":6,"2020-10-17":20,"2020-10-18":2,"2020-10-19":19,"2020-10-20":5,"2020-10-21":3,"2020-10-22":0,"2020-10-23":13,"2020-10-24":3,"2020-10-25":15,"2020-10-26":2,"2020-10-27":0,"2020-10-28":0,"2020-10-29":3,"2020-10-30":1,"2020-10-31":3,"2020-11-01":1,"2020-11-02":2,"2020-11-03":4,"2020-11-04":16,"2020-11-05":28,"2020-11-06":17,"2020-11-07":22,"2020-11-08":15,"2020-11-09":14,"2020-11-10":25,"2020-11-11":1,"2020-11-12":0,"2020-11-13":7,"2020-11-14":11,"2020-11-15":3,"2020-11-16":0,"2020-11-17":4,"2020-11-18":9,"2020-11-19":5,"2020-11-20":6,"2020-11-21":7,"2020-11-22":34,"2020-11-23":41,"2020-11-24":17,"2020-11-25":11,"2020-11-26":17,"2020-11-27":9,"2020-11-28":42,"2020-11-29":33,"2020-11-30":25,"2020-12-01":23,"2020-12-02":22,"2020-12-03":15,"2020-12-04":21,"2020-12-05":8,"2020-12-06":32,"2020-12-07":10,"2020-12-08":13,"2020-12-09":15,"2020-12-10":7,"2020-12-11":26,"2020-12-12":16,"2020-12-13":26,"2020-12-14":9,"2020-12-15":9,"2020-12-16":11,"2020-12-17":21,"2020-12-18":9,"2020-12-19":38,"2020-12-20":31,"2020-12-21":1,"2020-12-22":7,"2020-12-23":13,"2020-12-24":1,"2020-12-25":14,"2020-12-26":6,"2020-12-27":3,"2020-12-28":1,"2020-12-29":5,"2020-12-30":56,"2020-12-31":34,"2021-01-01":12,"2021-01-02":25,"2021-01-03":55,"2021-01-04":14,"2021-01-05":31,"2021-01-06":39,"2021-01-07":15,"2021-01-08":1,"2021-01-09":23,"2021-01-10":7,"2021-01-11":0,"2021-01-12":4,"2021-01-13":1,"2021-01-14":10,"2021-01-15":2,"2021-01-16":2,"2021-01-17":9,"2021-01-18":0,"2021-01-19":0,"2021-01-20":4,"2021-01-21":5,"2021-01-22":8,"2021-01-23":2,"2021-01-24":0,"2021-01-25":0,"2021-01-26":0,"2021-01-27":3,"2021-01-28":2,"2021-01-29":13,"2021-01-30":4,"2021-01-31":8,"2021-02-01":23,"2021-02-02":0,"2021-02-03":3,"2021-02-04":3,"2021-02-05":13,"2021-02-06":10,"2021-02-07":18,"2021-02-08":1,"2021-02-09":21,"2021-02-10":0,"2021-02-11":12,"2021-02-12":27,"2021-02-13":25,"2021-02-14":6,"2021-02-15":25,"2021-02-16":7,"2021-02-17":12,"2021-02-18":4,"2021-02-19":27,"2021-02-20":5,"2021-02-21":5,"2021-02-22":11,"2021-02-23":15,"2021-02-24":9,"2021-02-25":4,"2021-02-26":1,"2021-02-27":2,"2021-02-28":9,"2021-03-01":32,"2021-03-02":31,"2021-03-03":29,"2021-03-04":72,"2021-03-05":67,"2021-03-06":4,"2021-03-07":0,"2021-03-08":5,"2021-03-09":4,"2021-03-10":3,"2021-03-11":1,"2021-03-12":10,"2021-03-13":18,"2021-03-14":5,"2021-03-15":3,"2021-03-16":11,"2021-03-17":8,"2021-03-18":17,"2021-03-19":18,"2021-03-20":5,"2021-03-21":10,"2021-03-22":4,"2021-03-23":2,"2021-03-24":5,"2021-03-25":11,"2021-03-26":23,"2021-03-27":8,"2021-03-28":3,"2021-03-29":27,"2021-03-30":3,"2021-03-31":11,"2021-04-01":13,"2021-04-02":2,"2021-04-03":0,"2021-04-04":2},"lastRefresh":1617561360980} \ No newline at end of file diff --git a/package.json b/package.json index c37c701..ceb9a7d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/components/GitActivity/GitActivity.stories.tsx b/src/components/GitActivity/GitActivity.stories.tsx new file mode 100644 index 0000000..fad74c5 --- /dev/null +++ b/src/components/GitActivity/GitActivity.stories.tsx @@ -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 = (args) => + +// Default scenario +export const Default = Template.bind({}) +Default.args = {} diff --git a/src/components/GitActivity/index.tsx b/src/components/GitActivity/index.tsx new file mode 100644 index 0000000..ab95004 --- /dev/null +++ b/src/components/GitActivity/index.tsx @@ -0,0 +1,13 @@ +import React from "react"; + +export interface GitActivityProps { + +} + +export const GitActivity: React.FC = () => { + + return ( +
+
+ ) +} diff --git a/src/pages/api/getGitActivity.ts b/src/pages/api/getGitActivity.ts new file mode 100644 index 0000000..a3e7060 --- /dev/null +++ b/src/pages/api/getGitActivity.ts @@ -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, + 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 { + 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; +} \ No newline at end of file diff --git a/src/pages/api/hello.js b/src/pages/api/hello.js deleted file mode 100644 index 5b77ec0..0000000 --- a/src/pages/api/hello.js +++ /dev/null @@ -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' }) -} diff --git a/yarn.lock b/yarn.lock index 6b6ed08..2a30e30 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"