Skip to content

Commit

Permalink
Get to parity with retention policy queries
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Oct 15, 2016
1 parent 98bde98 commit a4abeb1
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 1 deletion.
121 changes: 121 additions & 0 deletions src/index.ts
Expand Up @@ -173,6 +173,20 @@ export interface QueryOptions {
database?: string;
}

/**
* RetentionOptions are passed into passed into the {@link
* InfluxDB#createRetentionPolicy} and {@link InfluxDB#alterRetentionPolicy}.
* See the [Downsampling and Retention page](https://docs.influxdata.com/
* influxdb/v1.0/guides/downsampling_and_retention/) on the Influx docs for
* more information.
*/
export interface RetentionOptions {
database?: string;
duration: string;
replication: number;
default?: boolean;
}

/**
* Parses the URL out into into a ClusterConfig object
*/
Expand Down Expand Up @@ -691,6 +705,113 @@ export class InfluxDB {
}, "POST")).then(assertNoErrors);
}

/**
* Creates a new retention policy on a database. You can read more about
* [Downsampling and Retention](https://docs.influxdata.com/influxdb/v1.0/
* guides/downsampling_and_retention/) on the InfluxDB website.
*
* @param {String} name The retention policy name
* @param {Object} options
* @param {String} [options.database] Database to create the policy on,
* uses the default database if not provided.
* @param {String} options.duration How long data in the retention policy
* should be stored for, should be in a format like `7d`. See details
* [here](https://docs.influxdata.com/influxdb/v1.0/query_language/spec/#durations)
* @param {Number} options.replication How many servers data in the series
* should be replicated to.
* @param {Boolean} [options.default] Whether the retention policy should
* be the default policy on the database.
* @returns {Promise<void>}
* @example
* influx.createRetentionPolicy('7d', {
* duration: '7d',
* replication: 1
* })
*/
public createRetentionPolicy(name: string, options: RetentionOptions): Promise<void> {
const q = `create retention policy ${grammar.escape.quoted(name)} on `
+ grammar.escape.quoted(options.database || this.defaultDB())
+ ` duration ${options.duration} replication ${options.replication}`
+ (options.default ? " default" : "");

return this.pool.json(this.getQueryOpts({ q }, "POST")).then(assertNoErrors);
}

/**
* Alters an existing retention policy on a database.
*
* @param {String} name The retention policy name
* @param {Object} options
* @param {String} [options.database] Database to create the policy on,
* uses the default database if not provided.
* @param {String} options.duration How long data in the retention policy
* should be stored for, should be in a format like `7d`. See details
* [here](https://docs.influxdata.com/influxdb/v1.0/query_language/spec/#durations)
* @param {Number} options.replication How many servers data in the series
* should be replicated to.
* @param {Boolean} [options.default] Whether the retention policy should
* be the default policy on the database.
* @returns {Promise<void>}
* @example
* influx.alterRetentionPolicy('7d', {
* duration: '7d',
* replication: 1,
* default: true
* })
*/
public alterRetentionPolicy(name: string, options: RetentionOptions): Promise<void> {
const q = `alter retention policy ${grammar.escape.quoted(name)} on `
+ grammar.escape.quoted(options.database || this.defaultDB())
+ ` duration ${options.duration} replication ${options.replication}`
+ (options.default ? " default" : "");

return this.pool.json(this.getQueryOpts({ q }, "POST")).then(assertNoErrors);
}

/**
* Shows retention policies on the database
*
* @param {String} [database] The database to list policies on, uses the
* default database if not provided.
* @returns {Promise<Array<{
* name: String,
* duration: String,
* shardGroupDuration: String,
* replicaN: Number,
* default: Boolean
* }>>}
* @example
* influx.showRetentionPolicies().then(policies => {
* expect(policies.slice()).to.deep.equal([
* {
* name: 'autogen',
* duration: '0s',
* shardGroupDuration: '168h0m0s',
* replicaN: 1,
* default: true,
* },
* {
* name: '7d',
* duration: '168h0m0s',
* shardGroupDuration: '24h0m0s',
* replicaN: 1,
* default: false,
* },
* ])
* })
*/
public showRetentionPolicies(database: string = this.defaultDB()): Promise<{
default: boolean,
duration: string,
name: string,
replicaN: number,
shardGroupDuration: string,
}[]> {
return this.pool.json(this.getQueryOpts({
q: `show retention policies on ${grammar.escape.quoted(database)}`,
}, "GET")).then(parseSingle);
}

/**
* writePoints sends a list of points together in a batch to InfluxDB. In
* each point you must specify the measurement name to write into as well
Expand Down
3 changes: 3 additions & 0 deletions test/create-fixtures.js
Expand Up @@ -42,6 +42,9 @@ const queries = [
fixture('selectFromGroup', 'select top(my_value, 1) from series_0 group by my_tag order by time desc', { db }),
fixture('error', 'this is not a valid query!'),

update(`create retention policy "7d" on "${db}" duration 7d replication 1`),
fixture('showRetentionPolicies', `show retention policies on "${db}"`),

update(`drop user john`),
update(`drop user steve`),
update(`drop database "${db}"`),
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/v1.0.0/error.json
@@ -1,3 +1,3 @@
{
"error": "error parsing query: found this, expected SELECT, DELETE, SHOW, CREATE, DROP, GRANT, REVOKE, ALTER, SET, KILL at line 1, char 1"
}
}
33 changes: 33 additions & 0 deletions test/fixture/v1.0.0/showRetentionPolicies.json
@@ -0,0 +1,33 @@
{
"results": [
{
"series": [
{
"columns": [
"name",
"duration",
"shardGroupDuration",
"replicaN",
"default"
],
"values": [
[
"autogen",
"0s",
"168h0m0s",
1,
true
],
[
"7d",
"168h0m0s",
"24h0m0s",
1,
false
]
]
}
]
}
]
}
79 changes: 79 additions & 0 deletions test/unit/influx.test.ts
Expand Up @@ -627,5 +627,84 @@ describe('influxdb', () => {
return influx.query(['select * from series_0', 'select * from series_1']);
});
});

describe('.createRetentionPolicy', () => {
beforeEach(() => setDefaultDB('my_db'));

it('creates non-default policies', () => {
expectQuery('json', 'create retention policy "7d\\"" on "test" ' +
'duration 7d replication 1');

return influx.createRetentionPolicy('7d"', {
database: 'test',
duration: '7d',
replication: 1
});
});

it('creates default policies', () => {
expectQuery('json', 'create retention policy "7d\\"" on "my_db" ' +
'duration 7d replication 1 default');

return influx.createRetentionPolicy('7d"', {
duration: '7d',
replication: 1,
default: true,
});
});
});

describe('.alterRetentionPolicy', () => {
beforeEach(() => setDefaultDB('my_db'));

it('creates non-default policies', () => {
expectQuery('json', 'alter retention policy "7d\\"" on "test" ' +
'duration 7d replication 1');

return influx.alterRetentionPolicy('7d"', {
database: 'test',
duration: '7d',
replication: 1
});
});

it('creates default policies', () => {
expectQuery('json', 'alter retention policy "7d\\"" on "my_db" ' +
'duration 7d replication 1 default');

return influx.alterRetentionPolicy('7d"', {
duration: '7d',
replication: 1,
default: true,
});
});
});

it('shows retention policies', () => {
const data = dbFixture('showRetentionPolicies');
expectQuery('json', 'show retention policies on "my\\"db"', 'GET', data);
influx.showRetentionPolicies('my"db');
setDefaultDB('my_db')
expectQuery('json', 'show retention policies on "my_db"', 'GET', data);

return influx.showRetentionPolicies().then(res => {
expect(res.slice()).to.deep.equal([
{
name: 'autogen',
duration: '0s',
shardGroupDuration: '168h0m0s',
replicaN: 1,
default: true,
},
{
name: '7d',
duration: '168h0m0s',
shardGroupDuration: '24h0m0s',
replicaN: 1,
default: false,
},
])
})
});
});
});

0 comments on commit a4abeb1

Please sign in to comment.