Profile API

This commit is contained in:
Valentin Ivanov 2018-04-20 15:39:33 -04:00 committed by Juha Ristolainen
parent 454fa33ce1
commit 35ce1e244f
5 changed files with 3007 additions and 67 deletions

2854
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -34,8 +34,13 @@
},
"codestats.apiurl": {
"type": "string",
"default": "https://codestats.net/api/my/pulses",
"default": "https://codestats.net/api/",
"description": "Code::Stats API URL. Only change if you know what you are doing!"
},
"codestats.username": {
"type": "string",
"default": "",
"description": "Code::Stats User Name"
}
}
},

View File

@ -4,13 +4,16 @@ import * as axios from "axios";
export class CodeStatsAPI {
private API_KEY = null;
private UPDATE_URL = "https://codestats.net/api/my/pulses";
private PROFILE_URL = "https://codestats.net/api/users";
private USER_NAME = null;
private UPDATE_URL = "https://codestats.net/api/";
//private PROFILE_URL = "https://codestats.net/api/users";
private axios = null;
constructor(apiKey: string, apiURL: string) {
constructor(apiKey: string, apiURL: string, userName: string) {
this.API_KEY = apiKey;
this.UPDATE_URL = apiURL;
this.USER_NAME = userName;
if (
this.API_KEY === null ||
this.API_KEY === undefined ||
@ -48,7 +51,7 @@ export class CodeStatsAPI {
console.log(`JSON: ${json}`);
return this.axios
.post(this.UPDATE_URL, json)
.post("my/pulses", json)
.then(response => {
console.log(response);
})
@ -59,6 +62,18 @@ export class CodeStatsAPI {
console.log(error);
});
}
public getProfile(): axios.AxiosPromise {
return this.axios
.get(`users/${this.USER_NAME}`)
.then(response => {
console.log("Got Response\n");
return response.data;
})
.catch(error => {
console.log(error);
});
}
}
class ApiJSON {

116
src/profileHtmlProvider.ts Normal file
View File

@ -0,0 +1,116 @@
import {
Uri,
Event,
CancellationToken,
TextDocumentContentProvider
} from "vscode";
import { CodeStatsAPI } from "./code-stats-api";
export class ProfileHtmlProvider implements TextDocumentContentProvider {
onDidChange?: Event<Uri>;
api: CodeStatsAPI;
constructor(api: CodeStatsAPI) {
this.api = api;
}
provideTextDocumentContent(uri: Uri, token: CancellationToken): string | Thenable<string> {
const LEVEL_FACTOR = 0.025;
function getLevel(xp: number): number {
return Math.floor(Math.sqrt(xp) * LEVEL_FACTOR);
}
function getNextLevelXp(level: number): number {
return Math.pow(Math.ceil((level+1)/LEVEL_FACTOR) ,2);
}
function getLevelProgress(xp: number): number {
let level = getLevel(xp);
let curLevelXp = getNextLevelXp(level-1);
let nextLevelXp = getNextLevelXp(level);
let haveXp = xp - curLevelXp;
let needXp = nextLevelXp - curLevelXp;
return Math.round(haveXp * 100.0 / needXp);
}
function getLanguages(languages: any): string {
let ret = '';
for( let lang in languages )
{
ret+=`<div class="language">${lang}<span style="display:block;">${languages[lang]["xps"]}</span></div>`;
}
return ret;
}
function getHeader(profile: any): string {
let userName = profile["user"];
let totalXp = profile["total_xp"];
let newXp = profile["new_xp"];
let currentLevel = getLevel(totalXp);
return `<h3> ${userName}'s Profile Level ${currentLevel} (${totalXp} XP) ${newXp > 0 ? '<sup>(+' + newXp +')</sup>' :'' }</h3>`;
}
return this.api.getProfile().then( profile => {
console.log(profile);
return `
<style>
.language {
float: left;
width: 6rem;
height: 6rem;
padding-top: 2rem;
text-align: center;
background: rgb(37,37,38);
margin: 0.1rem;
border-radius: 50%;
border-style: solid 2px;
border-color: black;
font-size: 0.9em;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.language span {
font-weight: bold;
font-size: 1.1em;
}
.vs-dark .language {
color: #ddca7e;
}
.vs .language {
color: blue;
}
sup {
top: -.5em;
font-size: 75%;
}
h3 {
text-align: center;
}
</style>
${getHeader(profile)}
${getLanguages(profile["languages"])}
`;
});
}
}

View File

@ -6,7 +6,6 @@ import {
Uri,
ViewColumn,
commands,
TextDocumentContentProvider,
Event,
CancellationToken,
StatusBarItem,
@ -18,61 +17,7 @@ import {
} from "vscode";
import { Pulse } from "./pulse";
import { CodeStatsAPI } from "./code-stats-api";
class HtmlProvider implements TextDocumentContentProvider {
onDidChange?: Event<Uri>;
provideTextDocumentContent(uri: Uri, token: CancellationToken): string | Thenable<string> {
return `
<style>
.tile {
float: left;
width: 7rem;
height: 7rem;
padding-top: 2.5rem;
text-align: center;
background: gray;
margin: 0.1rem;
border-radius: 50%;
border-style: solid 2px;
border-color: black;
color: #ddca7e;
font-size: 0.8em;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.tile span {
font-weight: bold;
font-size: 1.2em;
}
.tile:nth-child(even) {
background: gray;
}
</style>
<div class="tile">C#<span style="display:block;">10</span></div>
<div class="tile">Elixir</div>
<div class="tile">TypeScript</div>
<div class="tile">Rust</div>
<div class="tile">Html</div>
<div class="tile">PlainText</div>
<div class="tile">C++</div>
<div class="tile">VB</div>
<div class="tile">F#</div>
<div class="tile">C#</div>
<div class="tile">Elixir</div>
<div class="tile">TypeScript</div>
<div class="tile">Rust</div>
<div class="tile">Html</div>
<div class="tile">PlainText</div>
<div class="tile">C++</div>
<div class="tile">VB</div>
<div class="tile">F#</div>
`;
}
}
import { ProfileHtmlProvider } from "./profileHtmlProvider";
export class XpCounter {
private combinedDisposable: Disposable;
@ -107,7 +52,8 @@ export class XpCounter {
this.statusBarItem.command = "code-stats.profile";
}
let provider = new HtmlProvider();
let provider = new ProfileHtmlProvider(this.api);
let registration = workspace.registerTextDocumentContentProvider('code-stats', provider);
subscriptions.push(registration);
@ -190,12 +136,16 @@ export class XpCounter {
const apiKey: string = config.get("apikey");
const apiURL: string = config.get("apiurl");
const userName: string = config.get("username");
console.log(
"code-stats-vscode setting up with API URL",
apiURL,
"and key",
apiKey
`code-stats-vscode setting up:
API URL: ${apiURL}
NAME: ${userName}
KEY: ${apiKey}
`
);
this.api = new CodeStatsAPI(apiKey, apiURL);
this.api = new CodeStatsAPI(apiKey, apiURL, userName);
}
}