Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send stat #5

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@ export class Api {
};
this.post(Endpoint.SEND_USER, 'POST', body)
.then((response: {}) => {
console.log('response', response);
console.log('send user response', response);
})
.catch((err: {}) => {
console.log('err', err);
});
}

sendLineOfCode(userId: string, lineOfCode: number): void {
const timestamp: number = moment().valueOf();
const body: RequestOptionsBody = {
userId,
timestamp,
lineOfCode
};

this.post(Endpoint.SEND_LINE_OF_CODE, 'POST', body)
.then((response: {}) => {
console.log('send line of code response', response);
})
.catch((err: {}) => {
console.log('err', err);
Expand All @@ -34,7 +51,7 @@ export class Api {

this.post(Endpoint.SEND_FILE_CHANGE, 'POST', body)
.then((response: {}) => {
console.log('response', response);
console.log('Send file change response', response);
})
.catch((err: {}) => {
console.log('err', err);
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum Endpoint {
SEND_FILE_CHANGE = 'send',
SEND_FILE_CHANGE = 'stat', //TODO: change (waiting instructions from back-team)
SEND_LINE_OF_CODE = 'stat',
SEND_USER = 'user'
}
16 changes: 11 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
'use strict';
import * as vscode from 'vscode';

import { checkFileChanges, createUserId } from './utils';
import { checkFileChanges, createUserId, handleTextDocument } from './utils';

export function activate(context: vscode.ExtensionContext) {
createUserId(context);

let disposable = vscode.window.onDidChangeActiveTextEditor(() => {
checkFileChanges(context);
});
const disposable = [
vscode.window.onDidChangeActiveTextEditor(() => {
checkFileChanges(context);
}),
vscode.workspace.onDidSaveTextDocument(textDocument => {
handleTextDocument(textDocument, context);
})
];

context.subscriptions.push(disposable);
// save number of line written between two saves or Nb of line changed on save
context.subscriptions.push(...disposable);
}

export function deactivate() {}
17 changes: 15 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as uniqid from 'uniqid';
import { ExtensionContext, window } from 'vscode';
import { ExtensionContext, window, TextDocument } from 'vscode';

import { Api } from './api';

const api = new Api();

export function checkFileChanges(context: ExtensionContext): void {
const file = window.activeTextEditor;
const userId: string = getUserId(context);
const userId = getUserId(context);

if (file && userId) {
api.sendFileChange(
Expand All @@ -17,6 +17,19 @@ export function checkFileChanges(context: ExtensionContext): void {
);
}
}
let lastLineNb: { [key: string]: number } = {};

export const handleTextDocument = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the tests ? :)

textDocument: TextDocument,
context: ExtensionContext
): void => {
const key = textDocument.fileName;
const previousLoC = lastLineNb[key] || 0;
const currentLoC = textDocument.lineCount - previousLoC;
lastLineNb[key] = textDocument.lineCount;
const userId = getUserId(context);
api.sendLineOfCode(userId, currentLoC);
};

export function createUserId(context: ExtensionContext) {
let userId = context.workspaceState.get('userId') as string | undefined;
Expand Down