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: added get_event API to fetch a single event by ID #36

Merged
merged 6 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions .github/workflows/nodejs.yml
Expand Up @@ -12,7 +12,6 @@ on:
jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]
Expand All @@ -31,8 +30,6 @@ jobs:
key: ${{ runner.os }}-activitywatch-${{ matrix.aw-version }}
- name: Build
run: make build
- name: lint
run: make lint
- name: Download aw-server
run: wget -nc --no-verbose https://github.com/ActivityWatch/activitywatch/releases/download/v${{ matrix.aw-version }}/activitywatch-v${{ matrix.aw-version }}-linux-x86_64.zip
- name: Unzip aw-server
Expand All @@ -41,3 +38,20 @@ jobs:
run: |
export PATH=./activitywatch/aw-server:$PATH
make test

lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Build
run: make build
- name: Lint
run: make lint
11 changes: 10 additions & 1 deletion src/aw-client.ts
Expand Up @@ -59,7 +59,9 @@ export class AWClient {
this.testing = options.testing || false;
if (typeof options.baseURL === "undefined") {
const port = !options.testing ? 5600 : 5666;
this.baseURL = `http://localhost:${port}`;
// Note: had to switch to 127.0.0.1 over localhost as otherwise there's
// a possibility it tries to connect to IPv6's `::1`, which will be refused.
this.baseURL = `http://127.0.0.1:${port}`;
} else {
this.baseURL = options.baseURL;
}
Expand Down Expand Up @@ -122,6 +124,13 @@ export class AWClient {
return bucket;
}

public async getEvent(bucketId: string, eventId: number): Promise<IEvent> {
// Get a single event by ID
const event = (await this.req.get("/0/buckets/" + bucketId + "/events/" + eventId)).data;
event.timestamp = new Date(event.timestamp);
return event;
}

public async getEvents(bucketId: string, params: { [k: string]: any }): Promise<IEvent[]> {
const events = (await this.req.get("/0/buckets/" + bucketId + "/events", { params })).data;
events.forEach((event: IEvent) => {
Expand Down
35 changes: 25 additions & 10 deletions src/test/test.ts
Expand Up @@ -20,12 +20,12 @@ const testevent: IEvent = {
},
};

describe("All", () => {
describe("aw-client interface", () => {
before("Delete test bucket", () => {
// Delete bucket if it exists
return awc.deleteBucket(bucketId)
.catch((err) => {
if (err && err.response.status === 404) {
if (err && err.response && err.response.status === 404) {
return "ok";
}
throw err;
Expand All @@ -44,15 +44,23 @@ describe("All", () => {
});
});

// NOTE: This test will fail in CI until v0.12 is released (with support for 'get event by ID')
it("Post event, get event and assert", () => {
return awc.insertEvent(bucketId, testevent).then((resp) => {
console.log("insertEvent", resp);
return awc.getEvents(bucketId, { limit: 1 });
})
.then((resp) => {
console.log("getEvents", resp);
assert.equal(testevent.timestamp.toISOString(), resp[0].timestamp.toISOString());
assert.equal(testevent.data.label, resp[0].data.label);
console.log("result from getEvents", resp);
assert.equal(resp.length, 1);
const event: IEvent = resp[0];
console.log("getEvent", event);
return awc.getEvent(bucketId, event.id!);
})
.then((resp) => {
console.log("result from getEvent", resp);
assert.equal(testevent.timestamp.toISOString(), resp.timestamp.toISOString());
assert.equal(testevent.data.label, resp.data.label);
});
});

Expand Down Expand Up @@ -100,19 +108,26 @@ describe("All", () => {
});

it("Query", async () => {
await awc.heartbeat(bucketId, 5, testevent);
const e1 = {...testevent, timestamp: new Date("2022-01-01")};
const e2 = {...testevent, timestamp: new Date("2022-01-02")};
await awc.heartbeat(bucketId, 5, e1);
await awc.heartbeat(bucketId, 5, e2);

// Both these are valid timeperiod specs
const timeperiods = [
{start: testevent.timestamp, end: testevent.timestamp},
`${testevent.timestamp.toISOString()}/${testevent.timestamp.toISOString()}`,
{start: e1.timestamp, end: e2.timestamp},
`${e1.timestamp.toISOString()}/${e2.timestamp.toISOString()}`,
];
const query = [
`bucket="${bucketId}";`,
"RETURN=query_bucket(bucket);",
];
console.log(timeperiods);
const resp = await awc.query(timeperiods, query);
console.log("query", resp);
assert.equal(testevent.timestamp.toISOString(), new Date(resp[0][0].timestamp).toISOString());
assert.equal(testevent.data.label, resp[0][0].data.label);
assert.equal(e1.timestamp.toISOString(), new Date(resp[0][1].timestamp).toISOString());
assert.equal(e1.data.label, resp[0][1].data.label);
assert.equal(e2.timestamp.toISOString(), new Date(resp[0][0].timestamp).toISOString());
assert.equal(e2.data.label, resp[0][0].data.label);
});
});