-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieve-metrics.js
More file actions
90 lines (73 loc) · 3.53 KB
/
retrieve-metrics.js
File metadata and controls
90 lines (73 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pkg from 'windwardrestapi';
import fs from "fs";
const {WindwardClient, Template, Xml_10DataSource, OutputFormatEnum} = pkg;
const config = JSON.parse(fs.readFileSync('./config.json'));
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
async function main()
{
/**
* Step 1: INITIALIZE THE RESTFUL CLIENT AND SET UP THE TEMPLATE AND DATA SOURCE
* --------------------------------------------------------------------------------------------
*/
console.log("Initializing Fluent RESTful Client...");
// Create a new instance of the client using the RESTful Engine URL provided in the config file
// and pass the license to the client.
//
// The license should be specified in the config file
// If you don't have a license key, you can leave it blank and it will produce output with a watermark.
let client = new WindwardClient.WindwardClient(config['restful-engine-url'], config['license']);
// Display the version info for the restful engine
let version = await client.getVersionInfo();
console.log("Fluent RESTFul Engine Version: ", version);
// The datasource document I am sending to the engine to be processed
const dataSourcePath = './files/InvestmentFactSheet.xml';
// Create the object and pass in the datasource name, and the data file path.
let dataSource = new Xml_10DataSource('InvestmentFactSheet', undefined, dataSourcePath, undefined);
//The template file I wish to process.
const templatePath = './files/InvestmentFactSheet.docx';
//Create a new template object (to see all the input params check the documentation)
let template = new Template(OutputFormatEnum.PDF, [dataSource], undefined, templatePath,
undefined, undefined, undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined, undefined, undefined);
/**
* Step 2: REQUEST THE TEMPLATE METRICS
* --------------------------------------------------------------------------------------------
*/
console.log("Requesting Template Metrics...");
// Request metrics from the engine for our template
let metrics = await client.postMetrics(template);
//check postMetrics status and wait if not ready.
while(true) {
await sleep(1000);
let status = await client.getMetricsStatus(metrics.Guid);
if (status == 302) {
// The metrics request is complete, we can now proceed to retrieve the template metrics
break;
}
else if (status == 201 || status == 202 || status == 404) {
// The metrics request is still in progress, continue waiting
continue;
}
else {
// Potentially have an error, proceed to retrieve the metrics to get error details
console.error("Error retrieving metrics. Status code: ", status);
break;
}
}
/**
* Step 3: RETRIEVE THE METRICS
* --------------------------------------------------------------------------------------------
*/
console.log("Retrieving Metrics...");
// Retrieve the template metrics
let templateMetrics = await client.getMetrics(metrics.Guid);
// Write the template metrics to a file
fs.writeFile("./files/metrics.json", JSON.stringify(templateMetrics, null, 2), function(err){});
console.log("Template metrics saved to /files/metrics.json");
// Delete the metrics from the engine
await client.deleteMetrics(templateMetrics.Guid);
}
// Execute the main() method
main();