Create an export of live jobs
How to create an export of live jobs
You can create an export task of live jobs using an AQS query.
For more information, see:
Example JavaScript
This is an example of an export task. In summary:
-
Specify an AQS query that fetches items of the Street Lighting Heads interface and returns their Unit Number attribute.
-
Initiate an export in CSV file format (including Geometry data).
-
Repeatedly check if the export task has completed.
-
After the task has completed, construct the download URL and output it to console.
// load a http request library
const axios = require('axios');
// enter your api key here!
const apiKey = '6582de5a-1c3d-4873-a07c-ce17e363823e';
// specify the AQS query that will fetch our item data
const aqs = {
type: 'Query',
properties: {
// search within the Street Lighting Heads interface
dodiCode: 'designInterfaces_streetLightingHeads',
// return a subset of attributes, alternatively use "All" to get everything
attributes: ['attributes_streetLightingUnitsUnitNumber'],
},
};
// run the export
axios({
method: 'POST',
url: 'https://api.uk.alloyapp.io/api/export',
headers: { Authorization: `Bearer ${apiKey}` },
// specify the export model
data: {
aqs,
// optional filename
filename: 'myexport.csv',
// optionally export geometry or not
exportGeometry: true,
},
})
.then((response) => {
console.log('export started with task id: ' + response.data.alloyTaskId);
// start checking for task completion because it is asynchronous
checkForTaskCompletion(response.data.alloyTaskId);
})
.catch((error) => {
// output any error data to the console
console.log(error.response.data);
});
// define a function to check for task completion
function checkForTaskCompletion(taskId) {
setTimeout(async () => {
try {
// make the service call to get the task
const task = await getTask(taskId);
// switch based on the status of the task
switch (task.status) {
case 'Queued':
console.log('task queued, waiting for task status to change');
break;
case 'Running':
console.log('task running, waiting for task status to change');
break;
case 'Complete':
console.log('task completed! fetching file...');
// now get the item id that was made by the export
const fileItemId = await getExportFileItemId(taskId);
// generate the url to the file
const applyContentDispositionHeader = true; // if true forces browser to download, if false you get a file stream
const fileUrl = `https://api.uk.alloyapp.io/api/file/${fileItemId}?token=${apiKey}&applyContentDispositionHeader=${
applyContentDispositionHeader ? 'true' : 'false'
}`;
// output the file url to the console
// if running in a browser the url will force the browser to download the file
// you can also access the file stream to save it to disk or do further processing
console.log('file url: ' + fileUrl);
// the task succeeded so finish processing here
return;
case 'Failed':
console.log('task failed: ' + task.error.message);
// the task failed so finish processing here
return;
default:
console.log('unknown task status, waiting for task status to change...');
}
// if we reach here, we want to queue another task check
checkForTaskCompletion(taskId);
} catch (e) {
// log error, there was a problem getting the task
console.log(e);
}
}, 2000);
}
// define a function to get a task by id
async function getTask(taskId) {
return new Promise((resolve, reject) => {
// fetch the task by id
axios({
method: 'GET',
url: `https://api.uk.alloyapp.io/api/task/${taskId}`,
headers: { Authorization: `Bearer ${apiKey}` },
})
.then((response) => {
// resolve the promise with our task
resolve(response.data.task);
})
.catch((error) => {
// reject the promise with the error data
reject(error.response.data);
});
});
}
// define a function to get an exported file item by task id
async function getExportFileItemId(taskId) {
return new Promise((resolve, reject) => {
// fetch the exported file item for the stated task id
axios({
method: 'GET',
url: `https://api.uk.alloyapp.io/api/export/${taskId}/file`,
headers: { Authorization: `Bearer ${apiKey}` },
})
.then((response) => {
// resolve the promise with our file item id
resolve(response.data.fileItemId);
})
.catch((error) => {
// reject the promise with the error data
reject(error.response.data);
});
});
}