-
-
Save eashman/99d4d49eea252349d2e461a88f6873c9 to your computer and use it in GitHub Desktop.
Revisions
-
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const async = require('async-q') //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again //NOTE: list API will only page up to 10k results. It won't list all the users //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue. const maxAge = 21 // days // Bulk delete option client.users.scroll.each({}, function(d) { -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,10 +31,14 @@ client.users.scroll.each({}, function(d) { if (deleteUsers.length) { client.users.bulk(deleteUsers).then(resp => { console.log('[deleted users]', deleteUsers.length) // NOTE: if any one operation fails it will return an error but will continue to process the other jobs // ref: https://developers.intercom.com/reference#bulk-apis }).catch(err => console.error('[bulk delet failed]', err)) } }); // --- Example of a async queue based operation --- // // const async = require('async-q') //NOTE: Rate limit is at 500 requests per minute // const delay = 120 //ms -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 32 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // License: MIT, feel free to use it! const Intercom = require('intercom-client'); const appId = 'APP_ID' @@ -21,14 +22,17 @@ client.users.scroll.each({}, function(d) { // Example of custom attributes checking let tier = user.custom_attributes.tier // Delete use if last seen is more than maxAge let deprecate = Boolean(age >= maxAge && (!tier || tier === 'starter')) if (deprecate) console.log('[deleting user]', user.email, age, tier || '') else console.log('[skipped user]', user.email, age, tier || '') return deprecate }).map(user => ({delete: user})) if (deleteUsers.length) { client.users.bulk(deleteUsers).then(resp => { console.log('[deleted users]', deleteUsers.length) }).catch(err => console.error('[bulk delet failed]', err)) } }); // const async = require('async-q') @@ -68,4 +72,27 @@ client.users.scroll.each({}, function(d) { // // console.log('[user age]', age) // } // }) // }); /** Copyright (c) 2010-2016 Thomas Zhou Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **/ -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,32 @@ const apiKey = 'APP_KEY' const client = new Intercom.Client(appId, apiKey); const async = require('async-q') //REF: https://developers.intercom.com/reference#iterating-over-all-users //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again //NOTE: list API will only page up to 10k results. It won't list all the users //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue. const maxAge = 30 // days // Bulk delete option client.users.scroll.each({}, function(d) { const users = d.body.users let deleteUsers = users.filter(user => { let diff = Date.now()/1000 - user.updated_at let age = Math.floor(diff/60/60/24) // Example of custom attributes checking let tier = user.custom_attributes.tier // Delete use if last seen is more than maxAge let deprecate = Boolean(age >= maxAge && tier != 'professional' && tier != 'print') if (deprecate) console.log('[deleting user]', user.email, age, tier || '') return deprecate }).map(user => ({delete: user})) client.users.bulk(deleteUsers).then(resp => { console.log('[deleted users]', deleteUsers.length) }).catch(err => console.error('[bulk delet failed]', err)) }); // const async = require('async-q') //NOTE: Rate limit is at 500 requests per minute // const delay = 120 //ms -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 36 additions and 38 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,43 +5,41 @@ const apiKey = 'APP_KEY' const client = new Intercom.Client(appId, apiKey); const async = require('async-q') // const async = require('async-q') //NOTE: Rate limit is at 500 requests per minute // const delay = 120 //ms // const concurrency = 1 // // const queue = async.queue((user) => { // return client.users.delete(user).then(resp => { // console.log('[deleted user]', user.email, resp.status, // ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`) // }).then(() => { // // add a delay between each request // return new Promise(resolve => setTimeout(resolve, delay)) // }).catch(err => { // // Ignore errors and continue // // NOTE: remove this catch if you want delete errors to stop the entire script // console.error(err) // }) // }, concurrency) // Using queue and deleting users one by one. // client.users.scroll.each({}, function(d) { // const users = d.body.users // // console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length); // users.forEach(user => { // let diff = Date.now()/1000 - user.updated_at // let age = Math.floor(diff/60/60/24) // // Example of custom attributes checking // let tier = user.custom_attributes.tier // // Delete use if last seen is more than maxAge // if (age >= maxAge && tier != 'professional' && tier != 'print') { // queue.push({id: user.id, updated_at: user.updated_at, email: user.email}) // } else { // if (tier) console.log('[user tier]', user.email, tier) // // console.log('[user age]', age) // } // }) // }); -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,8 @@ const async = require('async-q') //REF: https://developers.intercom.com/reference#iterating-over-all-users //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again //NOTE: list API will only page up to 10k results. It won't list all the users //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue. const maxAge = 30 // days //NOTE: Rate limit is at 500 requests per minute const delay = 120 //ms -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,12 +34,13 @@ client.users.scroll.each({}, function(d) { users.forEach(user => { let diff = Date.now()/1000 - user.updated_at let age = Math.floor(diff/60/60/24) // Example of custom attributes checking let tier = user.custom_attributes.tier // Delete use if last seen is more than maxAge if (age >= maxAge && tier != 'professional' && tier != 'print') { queue.push({id: user.id, updated_at: user.updated_at, email: user.email}) } else { // console.log('[user age]', age) } }) }); -
zthomas revised this gist
Oct 14, 2016 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,11 +33,13 @@ client.users.scroll.each({}, function(d) { console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length); users.forEach(user => { let diff = Date.now()/1000 - user.updated_at let age = Math.floor(diff/60/60/24) // Delete use if last seen is more than maxAge if (age >= maxAge) { console.log(user) queue.push({id: user.id, updated_at: user.updated_at, email: user.email}) } else { // console.log('[user age]', age) } }) }); -
zthomas created this gist
Oct 14, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ const Intercom = require('intercom-client'); const appId = 'APP_ID' const apiKey = 'APP_KEY' const client = new Intercom.Client(appId, apiKey); const async = require('async-q') //REF: https://developers.intercom.com/reference#iterating-over-all-users //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again //NOTE: scroll API will not work well for this usecase, there's no way to throttle the request const maxAge = 30 // days //NOTE: Rate limit is at 500 requests per minute const delay = 120 //ms const concurrency = 1 const queue = async.queue((user) => { return client.users.delete(user).then(resp => { console.log('[deleted user]', user.email, resp.status, ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`) }).then(() => { // add a delay between each request return new Promise(resolve => setTimeout(resolve, delay)) }).catch(err => { // Ignore errors and continue // NOTE: remove this catch if you want delete errors to stop the entire script console.error(err) }) }, concurrency) client.users.scroll.each({}, function(d) { const users = d.body.users console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length); users.forEach(user => { let diff = Date.now()/1000 - user.updated_at // Delete use if last seen is more than maxAge if (diff/60/60/24 >= maxAge) { queue.push({id: user.id, updated_at: user.updated_at, email: user.email}) } else { console.log('[user age]', Math.floor(diff/60/60/24)) } }) });