Fix sending heartbeats and parsing GitHub repo

This commit is contained in:
Alan Hamlett
2024-08-30 10:33:13 +02:00
parent fb29f07e5e
commit 872161f655
5 changed files with 26 additions and 28 deletions

View File

@@ -53,7 +53,7 @@ export interface Config {
/** /**
* API key use to query wakatime api * API key use to query wakatime api
*/ */
apiKey: ''; apiKey: string;
apiUrl: string; apiUrl: string;
colors: Colors; colors: Colors;
/** /**
@@ -66,14 +66,14 @@ export interface Config {
* no activity in the browser for x second * no activity in the browser for x second
*/ */
detectionIntervalInSeconds: number; detectionIntervalInSeconds: number;
devSites: string[]; devSites: string[];
/** /**
* Url to which to send the heartbeat * Url to which to send the heartbeat
*/ */
heartbeatApiEndPoint: string; heartbeatApiEndPoint: string;
hostname: string; hostname: string;
/** /**
* Is logging enabled * Is logging enabled
*/ */
@@ -89,6 +89,7 @@ export interface Config {
*/ */
name: string; name: string;
nonTrackableSites: string[]; nonTrackableSites: string[];
queueName: string;
socialMediaSites: string[]; socialMediaSites: string[];
states: ExtensionStatus[]; states: ExtensionStatus[];
/** /**
@@ -163,6 +164,8 @@ const config: Config = {
nonTrackableSites: ['chrome://', 'about:'], nonTrackableSites: ['chrome://', 'about:'],
queueName: 'heartbeatQueue',
socialMediaSites: [ socialMediaSites: [
'facebook.com', 'facebook.com',
'instagram.com', 'instagram.com',

View File

@@ -29,18 +29,10 @@ class WakaTimeCore {
*/ */
async createDB() { async createDB() {
const dbConnection = await openDB('wakatime', 1, { const dbConnection = await openDB('wakatime', 1, {
upgrade(db, oldVersion) { upgrade(db) {
// Create a store of objects db.createObjectStore(config.queueName, {
const store = db.createObjectStore('heartbeatQueue', {
keyPath: 'id', 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:
store.createIndex('id', 'id');
}
}, },
}); });
this.db = dbConnection; this.db = dbConnection;
@@ -115,7 +107,7 @@ class WakaTimeCore {
if (!this.shouldSendHeartbeat(heartbeat)) return; if (!this.shouldSendHeartbeat(heartbeat)) return;
// append heartbeat to queue // append heartbeat to queue
await this.db?.add('heartbeatQueue', heartbeat); await this.db?.add(config.queueName, heartbeat);
await this.sendHeartbeats(); await this.sendHeartbeats();
} }
@@ -173,14 +165,15 @@ class WakaTimeCore {
return; return;
} }
const heartbeats = (await this.db?.getAll('heartbeatQueue', undefined, 50)) as const heartbeats = (await this.db?.getAll(config.queueName, undefined, 50)) as
| Heartbeat[] | Heartbeat[]
| undefined; | undefined;
if (!heartbeats || heartbeats.length === 0) return; if (!heartbeats || heartbeats.length === 0) return;
await this.db?.delete( await Promise.all(
'heartbeatQueue', heartbeats.map((heartbeat) => {
heartbeats.map((heartbeat) => heartbeat.id), return this.db?.delete(config.queueName, heartbeat.id);
}),
); );
const userAgent = await this.getUserAgent(); const userAgent = await this.getUserAgent();
@@ -214,7 +207,7 @@ class WakaTimeCore {
console.error(data.error); console.error(data.error);
return; return;
} }
if (response.status === 201) { if (response.status === 202) {
await Promise.all( await Promise.all(
(data.responses ?? []).map(async (resp, respNumber) => { (data.responses ?? []).map(async (resp, respNumber) => {
if (resp[0].error) { if (resp[0].error) {
@@ -222,7 +215,7 @@ class WakaTimeCore {
console.error(resp[0].error); console.error(resp[0].error);
} else if (resp[1] === 201 && resp[0].data?.id) { } else if (resp[1] === 201 && resp[0].data?.id) {
await changeExtensionStatus('allGood'); await changeExtensionStatus('allGood');
// await this.db?.delete('heartbeatQueue', resp[0].data.id); // await this.db?.delete(config.queueName, resp[0].data.id);
} else { } else {
if (resp[1] !== 400) { if (resp[1] !== 400) {
await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber)); await this.putHeartbeatsBackInQueue(heartbeats.filter((h, i) => i === respNumber));
@@ -246,7 +239,7 @@ class WakaTimeCore {
async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> { async putHeartbeatsBackInQueue(heartbeats: Heartbeat[]): Promise<void> {
await Promise.all( await Promise.all(
heartbeats.map(async (heartbeat) => this.db?.add('heartbeatQueue', heartbeat)), heartbeats.map(async (heartbeat) => this.db?.add(config.queueName, heartbeat)),
); );
} }

View File

@@ -24,6 +24,7 @@ export const getSettings = async (): Promise<Settings> => {
blacklist: null, blacklist: null,
denyList: [], denyList: [],
hostname: config.hostname, hostname: config.hostname,
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle, loggingStyle: config.loggingStyle,
loggingType: config.loggingType, loggingType: config.loggingType,
socialMediaSites: config.socialMediaSites, socialMediaSites: config.socialMediaSites,

View File

@@ -70,10 +70,7 @@ const GitHub: HeartbeatParser = (url: string) => {
}; };
} }
const body = document.getElementsByTagName('body').item(0); const repo = document
if (!body) return;
const repo = body
.querySelector('meta[name=octolytics-dimension-repository_nwo]') .querySelector('meta[name=octolytics-dimension-repository_nwo]')
?.getAttribute('content'); ?.getAttribute('content');
if (repo?.split('/')[1] !== match[0]) return; if (repo?.split('/')[1] !== match[0]) return;

View File

@@ -40,6 +40,8 @@ chrome.runtime.onMessage.addListener(
return; return;
} }
const heartbeat = site.parser(request.url);
sendResponse({ heartbeat: site.parser(request.url) }); sendResponse({ heartbeat: site.parser(request.url) });
} }
}, },
@@ -50,6 +52,10 @@ document.body.addEventListener('click', sendHeartbeat, true);
document.body.addEventListener('keypress', sendHeartbeat, true); document.body.addEventListener('keypress', sendHeartbeat, true);
const checkIfInAMeeting = () => { const checkIfInAMeeting = () => {
if (!window.location.href.startsWith('https://meet.google.com/')) {
return;
}
const isActiveMeeting = !!document.querySelector('[data-meeting-title]'); const isActiveMeeting = !!document.querySelector('[data-meeting-title]');
if (isActiveMeeting) { if (isActiveMeeting) {
sendHeartbeat(); sendHeartbeat();
@@ -59,6 +65,4 @@ const checkIfInAMeeting = () => {
}; };
// Google Meet // Google Meet
if (window.location.href.startsWith('https://meet.google.com/')) {
checkIfInAMeeting(); checkIfInAMeeting();
}