GithubHelp home page GithubHelp logo

activitywatch / aw-client-js Goto Github PK

View Code? Open in Web Editor NEW
28.0 5.0 16.0 348 KB

Client library for ActivityWatch in JavaScript

License: MIT License

Shell 1.77% TypeScript 97.12% Makefile 1.11%
activitywatch javascript typescript nodejs

aw-client-js's Introduction

aw-client-js

Client library for ActivityWatch in TypeScript/JavaScript.

Build Status npm Known Vulnerabilities

Install

npm install aw-client

Usage

The library uses Promises for almost everything, so either use .then() or async/await syntax.

The example below is written with .then() to make it easy to run in the node REPL.

const { AWClient } = require('aw-client');
const client = new AWClient('test-client')

// Get server info
client.getInfo().then(console.log);

// List buckets
client.getBuckets().then(console.log);

// Create bucket
const bucketId = "test";
client.createBucket(bucketId, "bucket-type", "your-hostname");

// Send a heartbeat
const nowStr = (new Date()).toISOString();
const heartbeat = {timestamp: nowStr, duration: 0, data: { label: "just testing!" }};
client.heartbeat(bucketId, 5, heartbeat);

Contribute

Setup your dev environment

npm install

Build the library

npm run compile

Run the tests

npm test

aw-client-js's People

Contributors

brunoparga avatar dependabot[bot] avatar erikbjare avatar johan-bjareholt avatar mathieudutour avatar saghen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

aw-client-js's Issues

Add typescript support to aw-client.js

I'd really like to work with a typescript version of this, as it makes developing faster and less error prone (regarding to my one week experience with typescript ^_^).
As one can compile it to vanilla js, the only downside I see, is that the developers of aw-client.js should have a basic knowledge of how to use typescript. On the other hand, users of it could choose whether they want to use the typescript file, or the vanilla js one.

Example of this compiled output:

Therefore I'd like to have an aw-client.ts which gets compiled to the aw-client.js file.

Edit: I've made a quick transformation to typescript. Some params I didn't know the type of (See TODOs)
Axios also seem to support typescript (https://github.com/axios/axios/blob/master/index.d.ts)
Here is the draft: Quick draft of aw-client.ts

String response causes TypeError in getEvents

I am not entirely sure how, but it seems to be possible for the axios res.data to be a string, despite the content-type being correctly set to json, causing the snippet below to error with TypeError: n.forEach is not a function. This happens with both aw-server and aw-server-rust, and only in Firefox.

const events = await this._get("/0/buckets/" + bucketId + "/events", {
params,
});
events.forEach((event: IEvent) => {
event.timestamp = new Date(event.timestamp);
});

private async _get(endpoint: string, params: object = {}) {
return this.req
.get(endpoint, { ...params, signal: this.controller.signal })
.then((res) => (res && res.data) || res);
}

AWClient.countEvents() is querying using incorrect params

Issue

Just recently used your library for interacting with the Activity Watch client via JS I did notice that when I query the event count it does not correlate with the results I get back from fetching the events.

Context

For both queries I used the same start and end time but the count and results do not line up. I used a limit of 100 and one of my buckets came back with a count of 389 but the records returned where only 97. So I checked the request and your source code noticed that for AWClient.countEvents() you are passing the time params as "starttime" and "endtime" which are not the params stated in the docs, resulting in from what I can see it querying all the data in a bucket instead of scoping to the timestamps.

TLDR Fix

Query params for AWClient.countEvents() should be start and end, not starttime, endtime when making the request.

Check if sending new heartbeat is superfluous

As far as I know, in the server heartbeats get merged together if they have the same data and are in the time span lastEventTime<->lastEvenTime+pulsetime. Therefore sending heartbeats can be redundant.

Currently we check for this in several watchers (here for aw-watcher-web and here for aw-watcher-vscode), so we don't send unnecessary heartbeats. But I think it would be better to check for this in aw-client.js.
On the one hand, this would ensure that server traffic is kept low and, on the other hand, we wouldn't have to recode it for every watcher.

Do you agree on this being useful or have something to add?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.