Skip to content

Commit f1fb225

Browse files
authored
Implement index:stats (#613)
What does this PR do? This PR implements the recently added index:stats (@ Kuzzle Backend v2.10.0) So it can be used directly, instead of having to use query to access it. Todo: Implementation Unit Testing Doc + Code Snippet How should this be manually tested? Launch a Kuzzle stack. Directly use sdk.index.stats() Verify that you get stats that make sense. Closes #608 ℹ️ It seems that Codecov is not taking the latest commit? 🤦
1 parent 778b699 commit f1fb225

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
code: true
3+
type: page
4+
title: stats
5+
description: Gets detailed storage statistics
6+
---
7+
8+
# stats
9+
10+
<SinceBadge version="Kuzzle 2.10.0"/>
11+
<SinceBadge version="auto-version"/>
12+
13+
Gets detailed storage usage statistics.
14+
15+
<br/>
16+
17+
```js
18+
stats([options]);
19+
```
20+
21+
<br/>
22+
23+
| Arguments | Type | Description |
24+
| --------- | ----------------- | ------------- |
25+
| `options` | <pre>object</pre> | Query options |
26+
27+
### options
28+
29+
Additional query options
30+
31+
| Property | Type<br/>(default) | Description |
32+
| ---------- | ------------------------------- | ---------------------------------------------------------------------------- |
33+
| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |
34+
35+
## Resolves
36+
37+
Returns detailed storage usage statistics: overall index/collection sizes and the number of documents per collection.
38+
39+
## Usage
40+
41+
<<< ./snippets/stats.js
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const stats = await kuzzle.index.stats();
2+
3+
console.log(JSON.stringify(stats));
4+
/*
5+
{
6+
"indexes":[
7+
{
8+
"name":"nyc-open-data",
9+
"size":42,
10+
"collections":[
11+
{
12+
"name":"yellow-taxi",
13+
"documentCount":42,
14+
"size":42
15+
}
16+
]
17+
}
18+
],
19+
"size":42
20+
}
21+
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: index#stats
3+
description: Gets detailed storage statistics
4+
hooks:
5+
before: |
6+
curl -X POST kuzzle:7512/nyc-open-data/_create
7+
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
8+
after: curl -X DELETE kuzzle:7512/nyc-open-data
9+
template: catch
10+
expected: ["documentCount":0]

src/controllers/Index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseController } from './Base';
2+
import { JSONObject } from '../types';
23

34
export class IndexController extends BaseController {
45

@@ -99,4 +100,19 @@ export class IndexController extends BaseController {
99100
return this.query(request, options)
100101
.then(response => response.result.deleted);
101102
}
103+
104+
/**
105+
* Returns detailed storage usage statistics.
106+
*
107+
* @see https://docs.kuzzle.io/sdk/js/7/controllers/index/stats/
108+
*
109+
* @param options Additional options
110+
* - `queuable` If true, queues the request during downtime, until connected to Kuzzle again
111+
*/
112+
stats (options: { queuable?: boolean } = {}): Promise<JSONObject> {
113+
return this.query({
114+
action: 'stats'
115+
}, options)
116+
.then(response => response.result);
117+
}
102118
}

test/controllers/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,37 @@ describe('Index Controller', () => {
120120
});
121121
});
122122
});
123+
124+
describe('stats', () => {
125+
it('should call index/stats query and return a Promise which resolves to an object containing detailed storage statistics', () => {
126+
const result = {
127+
indexes: [
128+
{
129+
name: 'nyc-open-data',
130+
size: 42,
131+
collections: [
132+
{
133+
name: 'yellow-taxi',
134+
documentCount: 42,
135+
size: 42,
136+
}
137+
]
138+
}
139+
]
140+
};
141+
kuzzle.query.resolves({result});
142+
143+
return kuzzle.index.stats(options)
144+
.then(res => {
145+
should(kuzzle.query)
146+
.be.calledOnce()
147+
.be.calledWith({
148+
controller: 'index',
149+
action: 'stats'
150+
}, options);
151+
152+
should(res).be.equal(result);
153+
});
154+
});
155+
});
123156
});

0 commit comments

Comments
 (0)