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

Add hubot brain storage module #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The Hubot markov model can optionally be configured by setting environment varia
* `memory`, the default, which stores transitions entirely in-process (lost on restart);
* `redis`, which stores data in a Redis cache; or
* `postgres`, which stores data in a PostgreSQL database.
* `hubot_brain`, store transitions in the hubot brain, useful when using other plugins like hubot-redis-brain

* `HUBOT_MARKOV_STORAGE_URL` supplies additional configuration required by the `redis` and `postgres` storage backends. The formats are `redis://${USER}:${PASSWORD}@${HOSTNAME}:${PORT}/${DBNUM}` and `postgres://${USER}:${PASSWORD}@${HOSTNAME}:${PORT}/${DATABASE}` with defaults omitted.

Expand Down
18 changes: 18 additions & 0 deletions src/storage/hubot-brain.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
MemoryStorage = require('./memory')

# Markov storage implementation that uses built-in hubot brain
class HubotBrainStorage extends MemoryStorage
constructor: (connStr, modelName, robot) ->
super connStr, modelName
@brain = robot.brain
@brainKey = 'markov-transitions-'+modelName
@brain.on 'loaded', =>
brainData = @brain.get @brainKey
@transitions = brainData unless !brainData?

incrementTransitions: (transitions, callback) ->
super transitions, () =>
@brain.set @brainKey, @transitions
process.nextTick callback

module.exports = HubotBrainStorage
1 change: 1 addition & 0 deletions src/storage/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ module.exports =
redis: require('./redis')
postgres: require('./postgres')
memory: require('./memory')
hubot_brain: require('./hubot-brain')