Skip to content

Commit

Permalink
Revert "Bytebase master (#97)" (#98)
Browse files Browse the repository at this point in the history
This reverts commit 710d2c7.
  • Loading branch information
boojack committed Dec 28, 2021
1 parent 710d2c7 commit c0e7aa7
Show file tree
Hide file tree
Showing 64 changed files with 19,566 additions and 4,098 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"plugins": ["@babel/plugin-transform-runtime"]
}
46 changes: 0 additions & 46 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,5 @@
node_modules
.DS_Store
dist
dist-ssr
*.local
.cache
extension.zip
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

2 changes: 2 additions & 0 deletions .upignore
@@ -0,0 +1,2 @@
*
!dist/**
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@

The MIT License

Copyright (c) 2020 Tim Qian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -14,11 +14,11 @@

**Support comparing multiple repos**

![web-demo](https://raw.githubusercontent.com/timqian/images/master/star-history.gif)
![](https://raw.githubusercontent.com/timqian/images/master/star-history.gif)

## [As an extension](https://chrome.google.com/webstore/detail/star-history/iijibbcdddbhokfepbblglfgdglnccfn)

![extension-demo](https://raw.githubusercontent.com/timqian/images/master/star-history-extension.gif)
![](https://raw.githubusercontent.com/timqian/images/master/star-history-extension.gif)

> Note: You can [load the `./extension` folder to chrome](https://superuser.com/a/247654) to install the extension too.
Expand Down
120 changes: 120 additions & 0 deletions core/getStarHistory.js
@@ -0,0 +1,120 @@
import axios from 'axios';

// number of sample requests to do
const sampleNum = 15;

// return [1,2, ..., n]
const range = n => Array.apply(null, {length: n}).map((_, i) => i + 1);

/**
* get star history
* @param {String} repo - eg: 'timqian/jsCodeStructure'
* @param {String} token - github access token
* @return {Array} history - eg: [{date: 2015-3-1,starNum: 12}, ...]
*/
async function getStarHistory(repo, token) {
const axiosGit = axios.create({
headers: {
Accept: 'application/vnd.github.v3.star+json',
Authorization: token ? `token ${token}` : undefined,
},
});

/**
* generate Urls and pageNums
* @param {sting} repo - eg: 'timqian/jsCodeStructure'
* @return {object} {sampleUrls, pageIndexes} - urls to be fatched(length <=10) and page indexes
*/
async function generateUrls(repo) {

const initUrl = `https://api.github.com/repos/${repo}/stargazers`; // used to get star info
const initRes = await axiosGit.get(initUrl);

/**
* link Sample (no link when star < 30):
* <https://api.github.com/repositories/40237624/stargazers?access_token=2e71ec1017dda2220ccba0f6922ecefd9ea44ac7&page=2>;
* rel="next",
* <https://api.github.com/repositories/40237624/stargazers?access_token=2e71ec1017dda2220ccba0f6922ecefd9ea44ac7&page=4>;
* rel="last"
*/
const link = initRes.headers.link;

const pageNum = link ? /next.*?page=(\d*).*?last/.exec(link)[1] : 1; // total page number

// used to calculate total stars for this page
const pageIndexes = pageNum <= sampleNum ?
range(pageNum).slice(1, pageNum) :
range(sampleNum).map(n => Math.round(n / sampleNum * pageNum) - 1); // for bootstrap bug

// store sampleUrls to be requested
const sampleUrls = pageIndexes.map(pageIndex => `${initUrl}?page=${pageIndex}`);

console.log("pageIndexes", pageIndexes);
return { firstPage: initRes, sampleUrls, pageIndexes };
}

const { sampleUrls, pageIndexes, firstPage } = await generateUrls(repo);

// promises to request sampleUrls

const getArray = [firstPage].concat(sampleUrls.map(url => axiosGit.get(url)));

const resArray = await Promise.all(getArray);

let starHistory = null;

if (resArray[0].data === undefined || resArray[0].data.length == 0) {
throw new Error('Repo has no star history');
}

if (pageIndexes[pageIndexes.length - 1] > sampleNum) {
starHistory = pageIndexes.map((p, i) => {
return {
date: resArray[i + 1].data[0].starred_at.slice(0, 10),
starNum: 30 * ((p === 0 ? 1 : p) - 1), // page 0 also means page 1
};
});
} else {
// we have every starredEvent: we can use them to generate 15 (sampleNum) precise points
const starredEvents = resArray.reduce((acc, r) => acc.concat(r.data), []);

const firstStarredAt = new Date(starredEvents[0].starred_at);
const daysSinceRepoCreatedAt = Math.round((new Date()) - firstStarredAt) / (1000*60*60*24);

const dates = Array.from(new Array(50)).map((_, i) => {
const firstStarredAtCopy = new Date(firstStarredAt);
firstStarredAtCopy.setDate(firstStarredAtCopy.getDate() + Math.floor((daysSinceRepoCreatedAt / 50) * (i + 1)));
return firstStarredAtCopy.toISOString().slice(0, 10);
}, []);

starHistory = dates.map((d, i) => {
let starNum = 0;
const firstStarredEventAfterDate = starredEvents.find((se, i) => {
if (se.starred_at.slice(0, 10) >= d) {
starNum = i + 1;
return true
}

return false;
})

return firstStarredEventAfterDate && {
date: firstStarredEventAfterDate.starred_at.slice(0, 10),
starNum: starNum
};
}).filter(x => x);
}

// Better view for less star repos (#28) and for repos with too much stars (>40000)
const resForStarNum = await axiosGit.get(`https://api.github.com/repos/${repo}`);
const starNumToday = resForStarNum.data.stargazers_count;
const today = new Date().toISOString().slice(0, 10);
starHistory.push({
date: today,
starNum: starNumToday
})

return starHistory;
}

export default getStarHistory;
29 changes: 29 additions & 0 deletions core/test/generateUrls_test.js
@@ -0,0 +1,29 @@
import generateUrls from '../generateUrls';

// describe('getData()', () => {
// it('should return an arg parser', () => {
(async function() {
console.log('bluebird:');
const bluebirdArray = await generateUrls('petkaantonov/bluebird')
.catch(err => {
console.log(err);
});
console.log(bluebirdArray);

console.log('jsCodeStructure:');
const jsCodeStructureArray = await generateUrls('timqian/jsCodeStructure')
.catch(err => {
console.log(err);
});
console.log(jsCodeStructureArray);

console.log('timqian/tcp_test:');
const tcp_testArray = await generateUrls('timqian/tcp_test')
.catch(err => {
console.log(err);
});
console.log(tcp_testArray);
})();

// });
// });
10 changes: 10 additions & 0 deletions core/test/getStarHistory_test.js
@@ -0,0 +1,10 @@
import getStarHistory from '../getStarHistory';

// getStarHistory('timqian/jsCodeStructure');
(async function() {
const history = await getStarHistory('twbs/bootstrap')
.catch(err => {
console.log(err);
});
console.log( history );
})();
1 change: 1 addition & 0 deletions core/test/xkct_test.js
@@ -0,0 +1 @@
import xkcd from '../xkcd';
1 change: 1 addition & 0 deletions extension/assets/cog-solid.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added extension/assets/loadinfo.net.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c0e7aa7

Please sign in to comment.