finish core
This commit is contained in:
@@ -40,7 +40,7 @@ export default function MainList({
|
||||
const disableLogging = async (): Promise<void> => {
|
||||
dispatch(setLoggingEnabled(false));
|
||||
await browser.storage.sync.set({ loggingEnabled: false });
|
||||
await changeExtensionState('notLogging');
|
||||
await changeExtensionState('trackingDisabled');
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -29,7 +29,7 @@ describe('wakatime config', () => {
|
||||
"colors": {
|
||||
"allGood": "",
|
||||
"lightTheme": "white",
|
||||
"notLogging": "gray",
|
||||
"trackingDisabled": "gray",
|
||||
"notSignedIn": "red",
|
||||
},
|
||||
"currentUserApiEndPoint": "/users/current",
|
||||
@@ -46,7 +46,7 @@ describe('wakatime config', () => {
|
||||
"udemy.com",
|
||||
"w3schools.com",
|
||||
],
|
||||
"heartbeatApiEndPoint": "/users/current/heartbeats",
|
||||
"heartbeatApiEndPoint": "/users/current/heartbeats.bulk",
|
||||
"hostname": "",
|
||||
"loggingEnabled": true,
|
||||
"loggingStyle": "deny",
|
||||
@@ -71,7 +71,7 @@ describe('wakatime config', () => {
|
||||
],
|
||||
"states": [
|
||||
"allGood",
|
||||
"notLogging",
|
||||
"trackingDisabled",
|
||||
"notSignedIn",
|
||||
"ignored",
|
||||
],
|
||||
@@ -80,7 +80,7 @@ describe('wakatime config', () => {
|
||||
"tooltips": {
|
||||
"allGood": "",
|
||||
"ignored": "This URL is ignored",
|
||||
"notLogging": "Not logging",
|
||||
"trackingDisabled": "Not logging",
|
||||
"notSignedIn": "Not signed In",
|
||||
},
|
||||
"trackSocialMedia": true,
|
||||
|
||||
@@ -3,7 +3,7 @@ import browser from 'webextension-polyfill';
|
||||
/**
|
||||
* Logging
|
||||
*/
|
||||
export type ExtensionStatus = 'allGood' | 'notLogging' | 'notSignedIn' | 'ignored';
|
||||
export type ExtensionStatus = 'allGood' | 'trackingDisabled' | 'notSignedIn' | 'ignored';
|
||||
/**
|
||||
* Supported logging style
|
||||
*/
|
||||
@@ -35,7 +35,7 @@ interface SuccessOrFailAlert {
|
||||
interface Colors {
|
||||
allGood: '';
|
||||
lightTheme: 'white';
|
||||
notLogging: 'gray';
|
||||
trackingDisabled: 'gray';
|
||||
notSignedIn: 'red';
|
||||
}
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ interface Colors {
|
||||
interface Tooltips {
|
||||
allGood: string;
|
||||
ignored: string;
|
||||
notLogging: string;
|
||||
trackingDisabled: string;
|
||||
notSignedIn: string;
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ const config: Config = {
|
||||
colors: {
|
||||
allGood: '',
|
||||
lightTheme: 'white',
|
||||
notLogging: 'gray',
|
||||
trackingDisabled: 'gray',
|
||||
notSignedIn: 'red',
|
||||
},
|
||||
|
||||
@@ -176,7 +176,7 @@ const config: Config = {
|
||||
'youtube.com',
|
||||
],
|
||||
|
||||
states: ['allGood', 'notLogging', 'notSignedIn', 'ignored'],
|
||||
states: ['allGood', 'trackingDisabled', 'notSignedIn', 'ignored'],
|
||||
|
||||
summariesApiEndPoint: process.env.SUMMARIES_API_URL ?? '/users/current/summaries',
|
||||
|
||||
@@ -185,7 +185,7 @@ const config: Config = {
|
||||
tooltips: {
|
||||
allGood: '',
|
||||
ignored: 'This URL is ignored',
|
||||
notLogging: 'Not logging',
|
||||
trackingDisabled: 'Not logging',
|
||||
notSignedIn: 'Not signed In',
|
||||
},
|
||||
trackSocialMedia: true,
|
||||
|
||||
@@ -3,16 +3,15 @@ import browser, { Tabs } from 'webextension-polyfill';
|
||||
/* eslint-disable no-fallthrough */
|
||||
/* eslint-disable default-case */
|
||||
import moment from 'moment';
|
||||
import { OptionalHeartbeat } from 'src/types/sites';
|
||||
import { getOperatingSystem } from '../utils';
|
||||
import { v4 as uuid4 } from 'uuid';
|
||||
import { OptionalHeartbeat } from '../types/sites';
|
||||
import { getOperatingSystem, IS_EDGE, IS_FIREFOX } from '../utils';
|
||||
import { changeExtensionStatus } from '../utils/changeExtensionStatus';
|
||||
import getDomainFromUrl, { getDomain } from '../utils/getDomainFromUrl';
|
||||
import { getSettings, Settings } from '../utils/settings';
|
||||
|
||||
import config, { ExtensionStatus } from '../config/config';
|
||||
import { EntityType, Heartbeat } from '../types/heartbeats';
|
||||
import { getApiKey } from '../utils/apiKey';
|
||||
import { getLoggingType } from '../utils/logging';
|
||||
import { EntityType, Heartbeat, HeartbeatsBulkResponse } from '../types/heartbeats';
|
||||
|
||||
class WakaTimeCore {
|
||||
tabsWithDevtoolsOpen: Tabs.Tab[];
|
||||
@@ -32,17 +31,15 @@ class WakaTimeCore {
|
||||
const dbConnection = await openDB('wakatime', 1, {
|
||||
upgrade(db, oldVersion) {
|
||||
// Create a store of objects
|
||||
const store = db.createObjectStore('cacheHeartbeats', {
|
||||
// The `time` property of the object will be the key, and be incremented automatically
|
||||
keyPath: 'time',
|
||||
const store = db.createObjectStore('heartbeatQueue', {
|
||||
keyPath: 'id',
|
||||
});
|
||||
// Switch over the oldVersion, *without breaks*, to allow the database to be incrementally upgraded.
|
||||
switch (oldVersion) {
|
||||
case 0:
|
||||
// Placeholder to execute when database is created (oldVersion is 0)
|
||||
case 1:
|
||||
// Create an index called `type` based on the `type` property of objects in the store
|
||||
store.createIndex('time', 'time');
|
||||
store.createIndex('id', 'id');
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -95,7 +92,7 @@ class WakaTimeCore {
|
||||
async handleActivity(tabId: number) {
|
||||
const settings = await getSettings();
|
||||
if (!settings.loggingEnabled) {
|
||||
await changeExtensionStatus('notLogging');
|
||||
await changeExtensionStatus('trackingDisabled');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -118,7 +115,9 @@ class WakaTimeCore {
|
||||
if (!this.shouldSendHeartbeat(heartbeat)) return;
|
||||
|
||||
// append heartbeat to queue
|
||||
await this.db?.add('cacheHeartbeats', heartbeat);
|
||||
await this.db?.add('heartbeatQueue', heartbeat);
|
||||
|
||||
await this.sendHeartbeats();
|
||||
}
|
||||
|
||||
async getCurrentTab(tabId: number): Promise<browser.Tabs.Tab | undefined> {
|
||||
@@ -145,63 +144,112 @@ class WakaTimeCore {
|
||||
|
||||
const entity = settings.loggingType === 'domain' ? getDomainFromUrl(url) : url;
|
||||
return {
|
||||
branch: heartbeat?.branch,
|
||||
branch: heartbeat?.branch ?? '<<LAST_BRANCH>>',
|
||||
category: heartbeat?.category,
|
||||
entity: heartbeat?.entity ?? entity,
|
||||
entityType: heartbeat?.entityType ?? (settings.loggingType as EntityType),
|
||||
id: uuid4(),
|
||||
language: heartbeat?.language,
|
||||
project: heartbeat?.project,
|
||||
project: heartbeat?.project ?? '<<LAST_PROJECT>>',
|
||||
time: this.getCurrentTime(),
|
||||
type: heartbeat?.entityType ?? (settings.loggingType as EntityType),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the heartbeat and logging type it creates a payload and
|
||||
* sends an ajax post request to the API.
|
||||
*
|
||||
* @param heartbeat
|
||||
* @param debug
|
||||
*/
|
||||
async sendHeartbeat(
|
||||
heartbeat: Heartbeat,
|
||||
apiKey: string,
|
||||
navigationPayload: Record<string, unknown>,
|
||||
): Promise<void> {
|
||||
console.log('Sending Heartbeat', heartbeat);
|
||||
let payload;
|
||||
getCurrentTime(): string {
|
||||
const m = moment();
|
||||
return `${m.format('x').slice(0, -3)}.${m.format('x').slice(-3)}`;
|
||||
}
|
||||
|
||||
const loggingType = await getLoggingType();
|
||||
// Get only the domain from the entity.
|
||||
// And send that in heartbeat
|
||||
if (loggingType == 'domain') {
|
||||
heartbeat.url = getDomainFromUrl(heartbeat.url);
|
||||
payload = await this.preparePayload(heartbeat, 'domain');
|
||||
await this.sendPostRequestToApi(
|
||||
{ ...payload, ...navigationPayload },
|
||||
apiKey,
|
||||
heartbeat.hostname,
|
||||
);
|
||||
async sendHeartbeats(): Promise<void> {
|
||||
const settings = await browser.storage.sync.get({
|
||||
apiKey: config.apiKey,
|
||||
apiUrl: config.apiUrl,
|
||||
heartbeatApiEndPoint: config.heartbeatApiEndPoint,
|
||||
hostname: '',
|
||||
});
|
||||
if (!settings.apiKey) {
|
||||
await changeExtensionStatus('notSignedIn');
|
||||
return;
|
||||
}
|
||||
// Send entity in heartbeat
|
||||
else if (loggingType == 'url') {
|
||||
payload = await this.preparePayload(heartbeat, 'url');
|
||||
await this.sendPostRequestToApi(
|
||||
{ ...payload, ...navigationPayload },
|
||||
apiKey,
|
||||
heartbeat.hostname,
|
||||
);
|
||||
|
||||
const heartbeats = (await this.db?.getAll('heartbeatQueue', undefined, 50)) as
|
||||
| Heartbeat[]
|
||||
| undefined;
|
||||
if (!heartbeats || heartbeats.length === 0) return;
|
||||
|
||||
await this.db?.delete(
|
||||
'heartbeatQueue',
|
||||
heartbeats.map((heartbeat) => heartbeat.id),
|
||||
);
|
||||
|
||||
const userAgent = await this.getUserAgent();
|
||||
|
||||
try {
|
||||
const request: RequestInit = {
|
||||
body: JSON.stringify(
|
||||
heartbeats.map((heartbeat) => {
|
||||
return { ...heartbeat, userAgent };
|
||||
}),
|
||||
),
|
||||
credentials: 'omit',
|
||||
method: 'POST',
|
||||
};
|
||||
if (typeof settings.hostname === 'string' && settings.hostname) {
|
||||
request.headers = {
|
||||
'X-Machine-Name': settings.hostname,
|
||||
};
|
||||
}
|
||||
|
||||
const url = `${settings.apiUrl}${settings.heartbeatApiEndPoint}?api_key=${settings.apiKey}`;
|
||||
const response = await fetch(url, request);
|
||||
if (response.status === 401) {
|
||||
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||
await changeExtensionStatus('notSignedIn');
|
||||
return;
|
||||
}
|
||||
const data = (await response.json()) as HeartbeatsBulkResponse;
|
||||
if (data.error) {
|
||||
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||
console.error(data.error);
|
||||
return;
|
||||
}
|
||||
if (response.status === 201) {
|
||||
await Promise.all(
|
||||
(data.responses ?? []).map(async (resp, respNumber) => {
|
||||
if (resp[0].error) {
|
||||
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
||||
console.error(resp[0].error);
|
||||
} else if (resp[1] === 201 && resp[0].data?.id) {
|
||||
await changeExtensionStatus('allGood');
|
||||
// await this.db?.delete('heartbeatQueue', resp[0].data.id);
|
||||
} else {
|
||||
if (resp[1] !== 400) {
|
||||
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
|
||||
}
|
||||
console.error(
|
||||
`Heartbeat ${resp[0].data?.id ?? respNumber} returned status: ${resp[1]}`,
|
||||
);
|
||||
}
|
||||
return resp;
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||
console.error(`Heartbeat response status: ${response.status}`);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
console.error(err);
|
||||
await this.putHeartbeatsBackInQueue(heartbeats);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates payload for the heartbeat and returns it as JSON.
|
||||
*
|
||||
* @param heartbeat
|
||||
* @param type
|
||||
* @param debug
|
||||
* @returns {*}
|
||||
* @private
|
||||
*/
|
||||
async preparePayload(heartbeat: Heartbeat, type: string): Promise<Record<string, unknown>> {
|
||||
async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> {
|
||||
await Promise.all(
|
||||
heartbeats.map(async (heartbeat) => this.db?.add('heartbeatQueue', heartbeat)),
|
||||
);
|
||||
}
|
||||
|
||||
async getUserAgent(): Promise<string> {
|
||||
const os = await getOperatingSystem();
|
||||
let browserName = 'chrome';
|
||||
let userAgent;
|
||||
@@ -214,88 +262,7 @@ class WakaTimeCore {
|
||||
} else {
|
||||
userAgent = navigator.userAgent.match(/Chrome\/\S+/g)?.[0];
|
||||
}
|
||||
const payload: Record<string, unknown> = {
|
||||
entity: heartbeat.url,
|
||||
time: moment().format('X'),
|
||||
type: type,
|
||||
user_agent: `${userAgent} ${os} ${browserName}-wakatime/${config.version}`,
|
||||
};
|
||||
|
||||
payload.project = heartbeat.project ?? '<<LAST_PROJECT>>';
|
||||
payload.branch = heartbeat.branch ?? '<<LAST_BRANCH>>';
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends AJAX request with payload to the heartbeat API as JSON.
|
||||
*
|
||||
* @param payload
|
||||
* @param method
|
||||
* @returns {*}
|
||||
*/
|
||||
async sendPostRequestToApi(
|
||||
payload: Record<string, unknown>,
|
||||
apiKey = '',
|
||||
hostname = '',
|
||||
): Promise<void> {
|
||||
try {
|
||||
const items = await browser.storage.sync.get({
|
||||
apiUrl: config.apiUrl,
|
||||
heartbeatApiEndPoint: config.heartbeatApiEndPoint,
|
||||
});
|
||||
|
||||
const request: RequestInit = {
|
||||
body: JSON.stringify(payload),
|
||||
credentials: 'omit',
|
||||
method: 'POST',
|
||||
};
|
||||
if (hostname) {
|
||||
request.headers = {
|
||||
'X-Machine-Name': hostname,
|
||||
};
|
||||
}
|
||||
const response = await fetch(
|
||||
`${items.apiUrl}${items.heartbeatApiEndPoint}?api_key=${apiKey}`,
|
||||
request,
|
||||
);
|
||||
await response.json();
|
||||
} catch (err: unknown) {
|
||||
if (this.db) {
|
||||
await this.db.add('cacheHeartbeats', payload);
|
||||
}
|
||||
|
||||
await changeExtensionStatus('notSignedIn');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends cached heartbeats request to wakatime api
|
||||
* @param requests
|
||||
*/
|
||||
async sendCachedHeartbeatsRequest(): Promise<void> {
|
||||
const apiKey = await getApiKey();
|
||||
if (!apiKey) {
|
||||
return changeExtensionStatus('notLogging');
|
||||
}
|
||||
|
||||
if (this.db) {
|
||||
const requests = await this.db.getAll('cacheHeartbeats');
|
||||
await this.db.clear('cacheHeartbeats');
|
||||
const chunkSize = 50; // Create batches of max 50 request
|
||||
for (let i = 0; i < requests.length; i += chunkSize) {
|
||||
const chunk = requests.slice(i, i + chunkSize);
|
||||
const requestsPromises: Promise<void>[] = [];
|
||||
chunk.forEach((request: Record<string, unknown>) =>
|
||||
requestsPromises.push(this.sendPostRequestToApi(request, apiKey)),
|
||||
);
|
||||
try {
|
||||
await Promise.all(requestsPromises);
|
||||
} catch (error: unknown) {
|
||||
console.log('Error sending heartbeats');
|
||||
}
|
||||
}
|
||||
}
|
||||
return `${userAgent} ${os} ${browserName}-wakatime/${config.version}`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ export interface Heartbeat {
|
||||
branch?: string | null;
|
||||
category?: Category | null;
|
||||
entity: string;
|
||||
entityType: EntityType;
|
||||
id: string;
|
||||
language?: string | null;
|
||||
project?: string | null;
|
||||
time: string;
|
||||
type: EntityType;
|
||||
}
|
||||
|
||||
export enum Category {
|
||||
@@ -30,3 +32,7 @@ export interface ProjectDetails {
|
||||
language: string;
|
||||
project: string;
|
||||
}
|
||||
|
||||
export type HeartbeatsBulkResponse = { error?: string; responses?: HeartbeatResponse[] };
|
||||
|
||||
export type HeartbeatResponse = [{ data?: { id: string }; error?: string }, number];
|
||||
|
||||
@@ -8,6 +8,7 @@ export enum KnownSite {
|
||||
github = 'github',
|
||||
gitlab = 'gitlab',
|
||||
googlemeet = 'googlemeet',
|
||||
slack = 'slack',
|
||||
stackoverflow = 'stackoverflow',
|
||||
travisci = 'travisci',
|
||||
vercel = 'vercel',
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import browser from 'webextension-polyfill';
|
||||
import config from '../config/config';
|
||||
|
||||
export default function apiKeyInvalid(key?: string): string {
|
||||
const err = 'Invalid api key... check https://wakatime.com/settings for your key';
|
||||
if (!key) return err;
|
||||
@@ -11,11 +8,3 @@ export default function apiKeyInvalid(key?: string): string {
|
||||
if (!re.test(key)) return err;
|
||||
return '';
|
||||
}
|
||||
|
||||
export async function getApiKey(): Promise<string> {
|
||||
const storage = await browser.storage.sync.get({
|
||||
apiKey: config.apiKey,
|
||||
});
|
||||
const apiKey = storage.apiKey as string;
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
@@ -13,16 +13,16 @@ export async function changeExtensionStatus(status: ExtensionStatus): Promise<vo
|
||||
await changeExtensionIcon(config.colors.allGood);
|
||||
await changeExtensionTooltip(config.tooltips.allGood);
|
||||
break;
|
||||
case 'notLogging':
|
||||
await changeExtensionIcon(config.colors.notLogging);
|
||||
await changeExtensionTooltip(config.tooltips.notLogging);
|
||||
case 'trackingDisabled':
|
||||
await changeExtensionIcon(config.colors.trackingDisabled);
|
||||
await changeExtensionTooltip(config.tooltips.trackingDisabled);
|
||||
break;
|
||||
case 'notSignedIn':
|
||||
await changeExtensionIcon(config.colors.notSignedIn);
|
||||
await changeExtensionTooltip(config.tooltips.notSignedIn);
|
||||
break;
|
||||
case 'ignored':
|
||||
await changeExtensionIcon(config.colors.notLogging);
|
||||
await changeExtensionIcon(config.colors.trackingDisabled);
|
||||
await changeExtensionTooltip(config.tooltips.ignored);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -39,7 +39,7 @@ export const logUserIn = async (apiKey: string): Promise<void> => {
|
||||
if (items.loggingEnabled === true) {
|
||||
await changeExtensionState('allGood');
|
||||
} else {
|
||||
await changeExtensionState('notLogging');
|
||||
await changeExtensionState('trackingDisabled');
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
await changeExtensionState('notSignedIn');
|
||||
@@ -121,7 +121,7 @@ export const fetchUserData = async (
|
||||
if (items.loggingEnabled === true) {
|
||||
await changeExtensionState('allGood');
|
||||
} else {
|
||||
await changeExtensionState('notLogging');
|
||||
await changeExtensionState('trackingDisabled');
|
||||
}
|
||||
|
||||
dispatch(setLoggingEnabled(items.loggingEnabled as boolean));
|
||||
|
||||
Reference in New Issue
Block a user