Skip to content

Commit

Permalink
transform progress in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Feb 22, 2012
1 parent aa64b26 commit 44ba81d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
7 changes: 4 additions & 3 deletions attachments/script/costco.js
Expand Up @@ -74,12 +74,13 @@ var costco = function() {
var _id = data.uuids[0]
transformDoc["_id"] = _id
app.io.emit('save', transformDoc)
util.notify("Transforming documents. This could take a while... (you can close and come back later)", {persist: true, loader: true})
app.io.on(_id, function (err, doc) {
util.notify("Transforming documents...", {persist: true, loader: true})
app.io.on(_id, function (err, data) {
if (err && callback) callback(err)
if (data.progress) return util.notify("Transforming documents... " + data.progress + "%", {persist: true, loader: true})
util.notify("Documents updated successfully!")
recline.initializeTable(app.offset)
if (callback) callback(false, doc)
if (callback) callback(false, data)
})
})

Expand Down
40 changes: 28 additions & 12 deletions service/transformer.js
Expand Up @@ -14,24 +14,40 @@ module.exports = function (t) {

follow({db: db, include_docs: true, filter: "datacouch/by_value", query_params: {k: "type", v: "pendingTransformation"}}, function(error, change) {
if (error) return console.error(error)
txn({"uri": db + '/' + change.id}, transform, function(error, newData) {
if (error) return console.error('transformation error on ' + change.id, err)
txn({"uri": db + '/' + change.id, "timeout": 120000}, transform, function(err, newData) {
if (err) return console.error('transformation error on ' + change.id, err)
t.sockets.emit(newData._id, false, newData)
return console.log('transformation success on ' + newData.dataset)
})
})

function getDBInfo(db, callback) {
request({url: t.couchurl + db}, callback)
}

function transform(doc, txncb) {
var down = request({url: t.couchurl + "/" + doc.dataset + '/_all_docs?include_docs=true'}),
up = request({url: t.couchurl + '/' + doc.dataset + '/_bulk_docs', method: "POST"}),
tr = transfuse(['rows', /./, 'doc'], doc.transform, JSONStream.stringify("{\"docs\":[\n", "\n,\n", "\n]}\n"));
down.pipe(tr)
tr.pipe(up)
up.on('error', txncb)
up.on('end', function() {
doc.type = "transformation"
doc.finishedAt = new Date()
txncb()
getDBInfo(doc.dataset, function(err, resp, dbInfo) {
if (err) return txncb(err)
var down = request({url: t.couchurl + doc.dataset + '/_all_docs?include_docs=true'}),
up = request({url: t.couchurl + doc.dataset + '/_bulk_docs', method: "POST"}),
tr = transfuse(['rows', /./, 'doc'], doc.transform, JSONStream.stringify("{\"docs\":[\n", "\n,\n", "\n]}\n"));
down.pipe(tr)
tr.pipe(up)
var count = 0
tr.on('data', function(chunk) {
count++
var progress = (count/dbInfo.doc_count)*100
if ((count % 500) === 0) {
console.log(progress)
t.sockets.emit(doc._id, false, {progress: progress})
}
})
up.on('error', txncb)
up.on('end', function() {
doc.type = "transformation"
doc.finishedAt = new Date()
txncb()
})
})
}

Expand Down

2 comments on commit 44ba81d

@rufuspollock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks interesting Max. I've had the transform code in Recline working again since January and it would be great to be able to integrate the client side transform stuff there with backend support when that's available -- as it appears to be in datacouch (it will be a lot faster I imagine ...)

@max-mapper
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word. im waiting on @colegillespie to finish his initial hookup of your recline fork with this branch of datacouch then we'll be in business. aa64b26 added the ability to create the same sandboxed js environment in the browser and on the client to control what modules are available in both places. this means when you do a preview/transform in the browser it will be the same javascript environment as when you do it using the new server side transformation stuff. the API for server side transformations right now is to submit a doc that looks like this: { "type": "pendingTransformation", "transform": "function(doc, emit) { }", "dataset": "abcde12345", "user": "maxogden"} then when it finishes it gets updated with a finishedAt timestamp and the type gets switched to transformation

Please sign in to comment.