Skip to content

Commit

Permalink
Merge pull request #68 from nichdiekuh/master
Browse files Browse the repository at this point in the history
added retention policy functions, improved tests
  • Loading branch information
nichdiekuh committed Jun 27, 2015
2 parents dacb742 + 6f58f02 commit dff92a8
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 25 deletions.
27 changes: 21 additions & 6 deletions README.md
Expand Up @@ -19,8 +19,9 @@ An [InfluxDB](http://influxdb.org/) Node.js Client

## Compatibility

The latest version of node-influx is compatible with InfluxDB 0.8.x.
The development for a 0.9.x compatible version is in progress.
Version 0.3.x is compatible with InfluxDB 0.8.x
Version 0.4.x is compatible with InfluxDB 0.9.x (currently under development)


## Usage

Expand Down Expand Up @@ -115,9 +116,8 @@ getHostsDisabled( ) { }
Creates a new database - requires cluster admin privileges

```js
createDatabase(databaseName, [options,] callback) { }
createDatabase(databaseName, callback) { }
```
[Options can be an array of shard spaces and/or continuous queries](http://influxdb.com/docs/v0.8/advanced_topics/sharding_and_storage.html#configuration).

###deleteDatabase
Deletes a database - requires cluster admin privileges
Expand All @@ -133,11 +133,26 @@ Returns array of database names - requires cluster admin privileges
getDatabaseNames(function(err,arrayDatabaseNames){}) { }
```


###getMeasurements
Returns array of measurements - requires database admin privileges

```js
getMeasurements(function(err,arrayMeasurements){} ) { }
```

###getSeries
Returns array of series names from given measurement, or database if `measurementName` is omitted - requires database admin privileges

```js
getSeries([measurementName,] function(err,arraySeriesNames){} ) { }
```

###getSeriesNames
Returns array of series names from given database - requires database admin privileges
Returns array of series names from given measurement - requires database admin privileges

```js
getSeriesNames(databaseName, function(err,arraySeriesNames){} ) { }
getSeriesNames([measurementName,] function(err,arraySeriesNames){} ) { }
```

###getUsers
Expand Down
48 changes: 41 additions & 7 deletions index.js
Expand Up @@ -96,8 +96,8 @@ InfluxDB.prototype.url = function (endpoint, options, query) {
}, options || {}, query || {})

// add the global configuration if they are set and not provided by the options
if (this.options.precision && !queryObj.precision) {
queryObj.precision = this.options.precision
if (this.options.timePrecision && !queryObj.precision) {
queryObj.precision = this.options.timePrecision
}
if (this.options.database && !queryObj.db) {
queryObj.db = this.options.database
Expand Down Expand Up @@ -164,14 +164,14 @@ InfluxDB.prototype.getSeriesNames = function (measurementName, callback) {

}

InfluxDB.prototype.getSeries = function (databaseName, callback) {
InfluxDB.prototype.getSeries = function (measurementName, callback) {
var query = 'show series'

// if no measurement name is given
if (typeof databaseName === 'function') {
callback = databaseName
if (typeof measurementName === 'function') {
callback = measurementName
} else {
query = query + ' from "' + databaseName + '"'
query = query + ' from "' + measurementName + '"'
}

this.queryDB(query, function (err, results) {
Expand Down Expand Up @@ -344,7 +344,7 @@ InfluxDB.prototype.queryRaw = function (databaseName, query, callback) {
}

InfluxDB.prototype.createContinuousQuery = function (queryName, queryString, databaseName, callback) {
if (databaseName === 'function') {
if (typeof databaseName === 'function') {
callback = databaseName
databaseName = this.options.database
}
Expand All @@ -371,6 +371,40 @@ InfluxDB.prototype.dropContinuousQuery = function (queryName, databaseName, call
this.queryDB('DROP CONTINUOUS QUERY "' + queryName + '" ON "' + databaseName + '"', callback)
}


InfluxDB.prototype.createRetentionPolicy = function(rpName, databaseName, duration, replication, isDefault, callback) {
var query = 'create retention policy "' + rpName +
'" on "' + databaseName +
'" duration ' + duration +
' replication ' + replication;
if(isDefault) {
query += ' default';
}

this.queryDB(query, callback);
}

InfluxDB.prototype.getRetentionPolicies = function(databaseName, callback) {
this.queryDB('show retention policies "' + databaseName + '"', callback);
}

InfluxDB.prototype.alterRetentionPolicy = function(rpName, databaseName, duration, replication, isDefault, callback) {
var query = 'alter retention policy "' + rpName +
'" on "' + databaseName + '"';
if(duration) {
query += ' duration ' + duration;
}
if(replication) {
query += ' replication ' + replication;
}
if(isDefault) {
query += ' default';
}

this.queryDB(query, callback);
}


InfluxDB.prototype.getHostsAvailable = function () {
return this.request.getHostsAvailable()
}
Expand Down
88 changes: 76 additions & 12 deletions test.js
Expand Up @@ -34,7 +34,7 @@ describe('InfluxDB', function () {

describe('create client', function () {
it('should create an instance without error', function () {
client = influx({host: info.server.host, port: info.server.port, username: info.server.username, password: info.server.password, database: info.db.name})
client = influx({host: info.server.host, port: info.server.port, username: info.server.username, password: info.server.password, database: info.db.name, retentionPolicy : info.db.retentionPolicy})
dbClient = influx({host: info.server.host, port: info.server.port, username: info.server.username, password: info.server.password, database: info.db.name})
failClient = influx({host: info.server.host, port: 6543, username: info.server.username, password: info.server.password, database: info.db.name})
failoverClient = influx({hosts: [
Expand Down Expand Up @@ -68,6 +68,13 @@ describe('InfluxDB', function () {
var url = client.url('query', { db: info.db.name, rp: info.db.retentionPolicy, precision: info.server.timePrecision })
assert.equal(url, /*'http://'+info.server.host+':8086/' + */ 'query?u=' + info.server.username + '&p=' + info.server.password + '&db=' + info.db.name + '&rp=' + info.db.retentionPolicy + '&precision=' + info.server.timePrecision)
})

it('should build a properly formatted url', function () {
var url = client.url('query')
assert.equal(url, /*'http://'+info.server.host+':8086/' + */ 'query?u=' + info.server.username + '&p=' + info.server.password + '&precision=' + info.server.timePrecision + '&db=' + info.db.name + '&rp=' + info.db.retentionPolicy)
})


})

describe('#_createKeyValueString', function () {
Expand Down Expand Up @@ -150,27 +157,35 @@ describe('InfluxDB', function () {
})
})

describe('#createUser', function () {
it('should create a user without error', function (done) {
client.createUser(info.db.username, info.db.password, true, done)
})
it('should error when creating an existing user', function (done) {
client.createUser(info.db.username, info.db.password, function (err) {
assert(err instanceof Error)
done()
})
})
})

describe('#getUsers', function () {
it('should get an array of database users', function (done) {
client.getUsers(function (err, users) {
assert.equal(err, null)
assert(users instanceof Array)
assert.equal(users.length, 0)
assert.equal(users.length, 1)
done()
})
})
})

describe('#createUser', function () {
it('should create a user without error', function (done) {
client.createUser(info.db.username, info.db.password, false, done)
})
it('should error when creating an existing user', function (done) {
client.createUser(info.db.username, info.db.password, function (err) {
it('should error when deleting an existing user', function (done) {
failClient.getUsers( function (err) {
assert(err instanceof Error)
done()
})
})

})

describe('#setPassword', function () {
Expand Down Expand Up @@ -239,10 +254,45 @@ describe('InfluxDB', function () {
})
})


describe('#createRetentionPolicy', function () {
it('should create a rentention policy', function (done) {
dbClient.createRetentionPolicy(info.db.retentionPolicy, info.db.name, '1d', 1, true, done)
})
})

describe('#getRetentionPolicies', function () {
it('should get an array of retention policies', function (done) {
client.getRetentionPolicies(info.db.name,function (err, rps) {
assert.equal(err, null)
assert(rps instanceof Array)
assert.equal(rps.length, 1)
done()
})
})
})

describe('#alterRetentionPolicy', function () {
it('should alter a rentention policy', function (done) {
dbClient.alterRetentionPolicy(info.db.retentionPolicy, info.db.name, '1h', 1, true, done)
})
})

describe('#writePoint', function () {
it('should write a generic point into the database', function (done) {
dbClient.writePoint(info.series.name, {value: 232, value2: 123}, { foo: 'bar', foobar: 'baz'}, done)
})

it('should write a generic point into the database', function (done) {
dbClient.writePoint(info.series.name, 1, { foo: 'bar', foobar: 'baz'}, done)
})

it('should write a generic point into the database', function (done) {
dbClient.writePoint(info.series.name, {time: 1234567890, value: 232}, {}, done)

})


it('should write a point with time into the database', function (done) {
dbClient.writePoint(info.series.name, {time: new Date(), value: 232}, {}, done)
})
Expand Down Expand Up @@ -313,7 +363,6 @@ describe('InfluxDB', function () {
describe('#queryRaw', function () {
it('should read a point from the database and return raw values', function (done) {
dbClient.queryRaw('SELECT value FROM ' + info.series.name + ';', function (err, res) {
console.log(res);
assert.equal(err, null)
assert(res instanceof Array)
assert.equal(res.length, 1)
Expand All @@ -326,7 +375,7 @@ describe('InfluxDB', function () {

describe('#createContinuousQuery', function () {
it('should create a continuous query', function (done) {
dbClient.createContinuousQuery('testQuery', 'SELECT COUNT(value) INTO valuesCount_1h FROM ' + info.series.name + ' GROUP BY time(1h) ', info.db.name, function (err, res) {
dbClient.createContinuousQuery('testQuery', 'SELECT COUNT(value) INTO valuesCount_1h FROM ' + info.series.name + ' GROUP BY time(1h) ', function (err, res) {
assert.equal(err, null)
assert(res instanceof Array)
assert.equal(res.length, 1)
Expand Down Expand Up @@ -401,6 +450,21 @@ describe('InfluxDB', function () {
done()
})
})

it('should return array of series', function (done) {
client.getSeries(info.series.name, function (err, series) {
if (err) return done(err)
assert(series instanceof Array)
assert.equal(series.length, 1)
done()
})
})
it('should bubble errors through', function (done) {
client.getSeries(info.db.name, function (err) {
assert(err instanceof Error)
done()
})
})
})

describe('#getSeriesNames', function () {
Expand All @@ -421,7 +485,7 @@ describe('InfluxDB', function () {
})
})
it('should bubble errors through', function (done) {
failClient.getSeriesNames(info.db.name, function (err) {
client.getSeriesNames(info.db.name, function (err) {
assert(err instanceof Error)
done()
})
Expand Down

0 comments on commit dff92a8

Please sign in to comment.