Skip to content
Merged
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
58 changes: 26 additions & 32 deletions src/aw-client.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import axios, { AxiosInstance, AxiosError } from 'axios';
import axios from 'axios';
import { AxiosInstance, AxiosError } from 'axios';

// Default interfaces for events and heartbeats
export interface Heartbeat {
// Default interface for events
export interface Event {
id?: number;
timestamp: Date;
duration?: number; // duration in seconds
data: { [k: string]: any };
}
export interface Event extends Heartbeat {
duration: number;
}

// Interfaces for coding activity
export interface AppEditorActivityHeartbeat extends Heartbeat {
export interface AppEditorEvent extends Event {
data: {
project: string; // Path to the current project / workDir
file: string; // Path to the current file
language: string; // Coding Language identifier (e.g. javascript, python, ...)
[k: string]: any; // Additional (custom) data
}
}
export interface AppEditorActivityEvent extends AppEditorActivityHeartbeat {
duration: number;
}

export interface Bucket {
id: string,
Expand All @@ -35,10 +30,10 @@ export interface Bucket {
}

interface HeartbeatQueueItem {
onSuccess: (heartbeat: Heartbeat) => void;
onSuccess: (heartbeat: Event) => void;
onError: (err: AxiosError) => void;
pulsetime: number;
heartbeat: Heartbeat;
heartbeat: Event;
}

export class AWClient {
Expand All @@ -64,7 +59,6 @@ export class AWClient {
this.baseURL = options.baseURL;
}


this.req = axios.create({
baseURL: this.baseURL + '/api',
timeout: 10000,
Expand All @@ -80,18 +74,18 @@ export class AWClient {
}

ensureBucket(bucketId: string, type: string, hostname: string): Promise<{ alreadyExist: boolean }> {
return this.req.post('/0/buckets/' + bucketId, {
client: this.clientname,
type,
hostname,
}).then(() => ({alreadyExist: false})).catch(err => {
// Will return 304 if bucket already exists
if (err && err.response && err.response.status == 304) {
return {alreadyExist: true};
}
throw err
});
}
return this.req.post('/0/buckets/' + bucketId, {
client: this.clientname,
type,
hostname,
}).then(() => ({alreadyExist: false})).catch(err => {
// Will return 304 if bucket already exists
if (err && err.response && err.response.status == 304) {
return {alreadyExist: true};
}
throw err
});
}

createBucket(bucketId: string, type: string, hostname: string) {
return this.req.post('/0/buckets/' + bucketId, {
Expand Down Expand Up @@ -170,7 +164,7 @@ export class AWClient {
* @param pulsetime The maximum amount of time in seconds since the last heartbeat to be merged with the previous heartbeat in aw-server
* @param heartbeat The actual heartbeat event
*/
heartbeat(bucketId: string, pulsetime: number, heartbeat: Heartbeat): Promise<Heartbeat> {
heartbeat(bucketId: string, pulsetime: number, heartbeat: Event): Promise<Event> {
// Create heartbeat queue for bucket if not already existing
if (!this.heartbeatQueues.hasOwnProperty(bucketId)) {
this.heartbeatQueues[bucketId] = {
Expand All @@ -192,13 +186,13 @@ export class AWClient {
});
}

private send_heartbeat(bucketId: string, pulsetime: number, data: Heartbeat): Promise<Heartbeat> {
private send_heartbeat(bucketId: string, pulsetime: number, data: Event): Promise<Event> {
return this.req.post('/0/buckets/' + bucketId + "/heartbeat?pulsetime=" + pulsetime, data)
.then(res => res.data)
.then(heartbeat => {
heartbeat.timestamp = new Date(heartbeat.timestamp)
return heartbeat
});
.then(res => res.data)
.then(heartbeat => {
heartbeat.timestamp = new Date(heartbeat.timestamp)
return heartbeat
});
}

// Start heartbeat queue processing if not currently processing
Expand Down