-
Notifications
You must be signed in to change notification settings - Fork 10
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 Code Examples #18
Open
shaun-scale
wants to merge
2
commits into
master
Choose a base branch
from
add-code-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const scaleapi = require('scaleapi'); // Change to "../lib/scaleapi.js" if you intend to run in this repo | ||
const fs = require('fs'); | ||
|
||
// HOW IT WORKS: | ||
// Given a .csv file or array of task ids you'd like cancelled, | ||
// go through and cancel Tasks for each row. | ||
|
||
// INPUT PARAMETERS: | ||
const API_KEY = 'live_xxxxx'; | ||
const DO_DRY_RUN = true; | ||
const fileName = 'list/of/task/ids_to_cancel.csv' | ||
|
||
const client = scaleapi.ScaleClient(API_KEY); | ||
|
||
(async function () { | ||
// ==================== | ||
// === READ IN ROWS === | ||
// ==================== | ||
|
||
// Read in Task Details from CSV | ||
let rows = readCsv(fileName); | ||
|
||
// Alternatively, create just an array of rows to cancel Tasks from | ||
// let rows = [ | ||
// '5d4121900591c138750xxxxx' | ||
// ] | ||
|
||
// ==================== | ||
// === PROCESS ROWS === | ||
// ==================== | ||
|
||
// Process each row as needed | ||
rows = rows.map(row => row[0]).filter(id => id.length === 24); | ||
|
||
console.log(`Number of Rows Found: ${rows.length}`); | ||
|
||
// ==================== | ||
// === CANCEL TASKS === | ||
// ==================== | ||
|
||
await Promise.map( | ||
rows, | ||
async row => { | ||
if (DO_DRY_RUN) { | ||
console.log('Would be cancelling Task Id: ' + row); | ||
} else { | ||
await new Promise((resolve, reject) => { | ||
client.cancelTask(row, (err, task) => { | ||
// do something with task | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
} else { | ||
console.log(`Task Cancelled: ${task.task_id}`); | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
}, | ||
{ concurrency: 10 }, | ||
); | ||
|
||
console.log('Finished Running Script'); | ||
}()); | ||
|
||
function readCsv(fileName, hasHeader = false) { | ||
const rows = | ||
fs.readFileSync(fileName, { encoding: 'utf8' }) | ||
.split('\n') | ||
.map(r => r.split(",").map(s => s.trim())) || []; | ||
|
||
return hasHeader ? rows.splice(1) : rows; | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const scaleapi = require('scaleapi'); // Change to "../lib/scaleapi.js" if you intend to run in this repo | ||
const fs = require('fs'); | ||
|
||
// HOW IT WORKS: | ||
// Given a .csv file or array of information to make tasks from (often attachment urls), | ||
// go through and submit Tasks to Scale for each row. | ||
|
||
// You will want to specify which task creation method you'd like to use, as well as | ||
// any input parameters relating to how you'd like your task completed | ||
|
||
// INPUT PARAMETERS: | ||
const API_KEY = 'live_xxxxx'; | ||
const DRY_RUN = true; | ||
const fileName = 'list/of/attachment_urls_and_other_data.csv' | ||
|
||
const client = scaleapi.ScaleClient(API_KEY); | ||
|
||
(async function () { | ||
// ==================== | ||
// === READ IN ROWS === | ||
// ==================== | ||
|
||
// Read in Task Details from CSV | ||
let rows = readCsv(fileName); | ||
|
||
// Alternatively, create just an array of rows to create Tasks from | ||
// let rows = [ | ||
// 'https://www.scale.com/img/is/awesome.jpg' | ||
// ] | ||
|
||
// ==================== | ||
// === PROCESS ROWS === | ||
// ==================== | ||
|
||
// Process each row as needed, in this case, get first column value | ||
rows = rows.map(row => row[0]); | ||
|
||
console.log(`Number of Rows Found: ${rows.length}`); | ||
|
||
// ==================== | ||
// === CREATE TASKS === | ||
// ==================== | ||
|
||
if (rows.length > 0) { | ||
await Promise.map( | ||
rows, | ||
async row => { | ||
if (DO_DRY_RUN) { | ||
console.log('Creating Task for ' + row); | ||
} else { | ||
await new Promise((resolve, reject) => { | ||
client.createAnnotationTask( | ||
{ | ||
callback_url: 'http://www.example.com/callback', | ||
project: 'coolest_project_name', | ||
objects_to_annotate: ['person', 'land vehicle'], | ||
with_labels: true, | ||
attachment: row, | ||
attachment_type: 'image', | ||
}, | ||
(err, task) => { | ||
// do something with task | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
} else { | ||
console.log(`Task Created: ${task.task_id}`); | ||
resolve(); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
}, | ||
{ concurrency: 5 }, | ||
); | ||
|
||
console.log('Finished Running Script'); | ||
} | ||
}()); | ||
|
||
function readCsv(fileName, hasHeader = false) { | ||
const rows = | ||
fs.readFileSync(fileName, { encoding: 'utf8' }) | ||
.split('\n') | ||
.map(r => r.split(",").map(s => s.trim())) || []; | ||
|
||
return hasHeader ? rows.splice(1) : rows; | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const scaleapi = require('scaleapi'); // Change to "../lib/scaleapi.js" if you intend to run in this repo | ||
const fs = require('fs'); | ||
|
||
const maxTasksReturnedPerCall = 100; | ||
|
||
// HOW IT WORKS: | ||
// Given a list of search filters ("PARAMS"), it will page through tasks and write | ||
// the output to a JSON file. | ||
|
||
// INPUT PARAMETERS: | ||
const API_KEY = 'live_xxx'; | ||
|
||
const MAX_TASKS_TO_RETURN = 100000; // Get up to the n most recently created tasks | ||
|
||
const OUTPUT_FILE = '/the/place/to/put/it.json'; | ||
shaun-scale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const PARAMS = { // All params optional | ||
type: 'annotation', | ||
status: 'completed', | ||
project: 'cool_project_name', | ||
completed_after: '2019-03-01T00:00:00.000Z' | ||
}; | ||
|
||
(async function () { | ||
|
||
// =============================== | ||
// == MAIN FUNCTION WE CAN CALL == | ||
// =============================== | ||
|
||
// Get list of task objects | ||
let getTasks = async function(client, maxTasksToReturn = 1000000, params = {}) { | ||
// Initialize everything | ||
let lastPageCount = maxTasksReturnedPerCall; | ||
let output = []; | ||
|
||
// Go through page by page | ||
while (lastPageCount === maxTasksReturnedPerCall && output.length < maxTasksToReturn) { | ||
try { | ||
// fetch some tasks | ||
let tasks = await new Promise((resolve, reject) => { | ||
client.tasks({ | ||
...params, | ||
offset: output.length, | ||
limit: maxTasksToReturn - output.length < maxTasksReturnedPerCall ? maxTasksToReturn - output.length : maxTasksReturnedPerCall | ||
}, (err, tasklist) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
} else { | ||
resolve(tasklist); | ||
} | ||
}); | ||
}); | ||
|
||
// concat with output | ||
output = output.concat(tasks.docs); | ||
lastPageCount = tasks.docs.length; | ||
|
||
console.log(`Fetched ${output.length} tasks`); | ||
} catch(err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
console.log(`Finished fetching ${output.length} tasks`); | ||
|
||
return output; | ||
} | ||
|
||
// ============================ | ||
// == CALL OUR MAIN FUNCTION == | ||
// ============================ | ||
|
||
const client = new scaleapi.ScaleClient(API_KEY); | ||
let tasks = await getTasks(client, MAX_TASKS_TO_RETURN, PARAMS); | ||
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(tasks)); | ||
}()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an external library called Bluebird that our environment automatically monkey patches in. There is no Promise.map in node.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah!! This makes SO much more sense. I thought my google-fu was just off and I was missing something but couldn't explain how this worked otherwise. Will update.