|
| 1 | +/*jslint browser: true, devel: true*/ |
| 2 | +/*global $, jQuery, tableau, ts_url */ |
| 3 | + |
| 4 | +(function() { |
| 5 | + // Create the connector object. |
| 6 | + var myConnector = tableau.makeConnector(); |
| 7 | + |
| 8 | + // TODO: make the server report types. |
| 9 | + // Map LNT types to Tableau datatypes. |
| 10 | + var col_type_mapper = { |
| 11 | + "compile_status": tableau.dataTypeEnum.int, |
| 12 | + "execution_status": tableau.dataTypeEnum.int, |
| 13 | + "compile_time": tableau.dataTypeEnum.float, |
| 14 | + "execution_time": tableau.dataTypeEnum.float, |
| 15 | + "score": tableau.dataTypeEnum.int, |
| 16 | + "mem_bytes": tableau.dataTypeEnum.int, |
| 17 | + "hash_status": tableau.dataTypeEnum.int, |
| 18 | + "hash": tableau.dataTypeEnum.string, |
| 19 | + "code_size": tableau.dataTypeEnum.int}; |
| 20 | + |
| 21 | + function getValue(payload_url) { |
| 22 | + var value = $.ajax({ |
| 23 | + url: payload_url, |
| 24 | + async: false |
| 25 | + }).responseText; |
| 26 | + return JSON.parse(value); |
| 27 | + } |
| 28 | + |
| 29 | + function get_matching_machines(regexp) { |
| 30 | + const name_regexp = new RegExp(regexp); |
| 31 | + var resp = getValue(ts_url + "/machines/"); |
| 32 | + var machines = resp.machines; |
| 33 | + return machines.filter(function (name_ids) { |
| 34 | + console.log(name_ids.name + ", " + name_regexp.test(name_ids.name)); |
| 35 | + return name_regexp.test(name_ids.name); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Define the schema. |
| 40 | + myConnector.getSchema = function (schemaCallback) { |
| 41 | + var search_info = JSON.parse(tableau.connectionData); |
| 42 | + tableau.reportProgress("Getting Schema from LNT."); |
| 43 | + |
| 44 | + // Lookup machines of interest, and gather run fields. |
| 45 | + var machine_names = get_matching_machines(search_info.machine_regexp); |
| 46 | + if (machine_names.length === 0) { |
| 47 | + tableau.abortWithError("Did not match any machine names matching: " + |
| 48 | + search_info.machine_regexp); |
| 49 | + } |
| 50 | + |
| 51 | + $.getJSON(ts_url + "/fields/", function (resp) { |
| 52 | + var fields = resp.fields; |
| 53 | + var cols = []; |
| 54 | + cols.push({ |
| 55 | + id: "machine_name", |
| 56 | + alias: "Machine Name", |
| 57 | + dataType: tableau.dataTypeEnum.string |
| 58 | + }); |
| 59 | + cols.push({ |
| 60 | + id: "run_id", |
| 61 | + alias: "Run ID", |
| 62 | + dataType: tableau.dataTypeEnum.int |
| 63 | + }); |
| 64 | + cols.push({ |
| 65 | + id: "run_order", |
| 66 | + alias: "Run Order", |
| 67 | + dataType: tableau.dataTypeEnum.string |
| 68 | + }); |
| 69 | + cols.push({ |
| 70 | + id: "run_date", |
| 71 | + alias: "Run DateTime", |
| 72 | + dataType: tableau.dataTypeEnum.datetime |
| 73 | + }); |
| 74 | + cols.push({ |
| 75 | + id: "test_name", |
| 76 | + alias: "Test", |
| 77 | + dataType: tableau.dataTypeEnum.string |
| 78 | + }); |
| 79 | + |
| 80 | + fields.forEach(function(field) { |
| 81 | + cols.push({ |
| 82 | + id: field.column_name, |
| 83 | + alias: field.column_name, |
| 84 | + dataType: col_type_mapper[field.column_name] |
| 85 | + }); |
| 86 | + }); |
| 87 | + var tableSchema = { |
| 88 | + id: "lnt_machine_feed", |
| 89 | + alias: "Performance Data from " + resp.generated_by, |
| 90 | + columns: cols, |
| 91 | + incrementColumnId: "run_id" |
| 92 | + }; |
| 93 | + schemaCallback([tableSchema]); |
| 94 | + }); |
| 95 | + }; |
| 96 | + |
| 97 | + // Download the data. |
| 98 | + myConnector.getData = function (table, doneCallback) { |
| 99 | + var last_run_id = parseInt(table.incrementValue || 0); |
| 100 | + |
| 101 | + // Get latest machines. |
| 102 | + var search_info = JSON.parse(tableau.connectionData); |
| 103 | + var machine_names = get_matching_machines(search_info.machine_regexp); |
| 104 | + if (machine_names.length === 0) { |
| 105 | + tableau.abortWithError("Did not match any machine names matching: " + |
| 106 | + search_info.machine_regexp); |
| 107 | + } else { |
| 108 | + tableau.reportProgress("Found " + machine_names.length + |
| 109 | + " machines to fetch."); |
| 110 | + } |
| 111 | + |
| 112 | + machine_names.forEach(function (machine) { |
| 113 | + var url = ts_url + "/machines/" + machine.id; |
| 114 | + var machine_info = getValue(url); |
| 115 | + var machine_name = machine_info.machine.name; |
| 116 | + var tableData = []; |
| 117 | + |
| 118 | + machine_info.runs.forEach(function(run, index) { |
| 119 | + var run_data; |
| 120 | + var runs_total = machine_info.runs.length; |
| 121 | + // Run based incremental refresh. If we have already seen data, skip it. |
| 122 | + if (run.id <= last_run_id) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + var status_msg = "Getting Machine: " + machine_name |
| 127 | + + " Run: " + run.id |
| 128 | + + " (" + (index + 1) + "/" + runs_total + ")"; |
| 129 | + |
| 130 | + tableau.reportProgress(status_msg); |
| 131 | + run_data = getValue(ts_url + "/runs/" + run.id); |
| 132 | + |
| 133 | + var date_str = run_data.run.end_time; |
| 134 | + var run_date = new Date(date_str); |
| 135 | + var derived_run_data = { |
| 136 | + "machine_name": machine_name, |
| 137 | + "run_id": run.id, |
| 138 | + "run_order": run[run.order_by], |
| 139 | + "run_date": run_date |
| 140 | + }; |
| 141 | + run_data.tests.forEach(function (element) { |
| 142 | + element.test_name = element.name; |
| 143 | + delete element.name; |
| 144 | + var data = Object.assign({}, derived_run_data, element); |
| 145 | + tableData.push(data); |
| 146 | + |
| 147 | + }); |
| 148 | + run_data = null; |
| 149 | + }); |
| 150 | + |
| 151 | + table.appendRows(tableData); |
| 152 | + |
| 153 | + }); |
| 154 | + doneCallback(); |
| 155 | + }; |
| 156 | + |
| 157 | + tableau.registerConnector(myConnector); |
| 158 | + |
| 159 | + // Create event listeners for when the user submits the form. |
| 160 | + $(document) |
| 161 | + .ready(function () { |
| 162 | + $("#submitButton") |
| 163 | + .click(function () { |
| 164 | + var requested_machines = { |
| 165 | + machine_regexp: $("#machine-name") |
| 166 | + .val() |
| 167 | + .trim() |
| 168 | + }; |
| 169 | + // This will be the data source name in Tableau. |
| 170 | + tableau.connectionName = requested_machines.machine_regexp + " (LNT)"; |
| 171 | + tableau.connectionData = JSON.stringify(requested_machines); |
| 172 | + tableau.submit(); // This sends the connector object to Tableau |
| 173 | + }); |
| 174 | + }); |
| 175 | +})(); |
| 176 | + |
| 177 | + |
0 commit comments