|
| 1 | +/** |
| 2 | + * This is a tutorial script for TerminusDB which demonstrates |
| 3 | + * the creation of a database from CSV files representing information about bicycle trips |
| 4 | + * on an urban program in Washington DC |
| 5 | + */ |
| 6 | +const TerminusClient = TerminusDBClient |
| 7 | + |
| 8 | +/** |
| 9 | + * The list of CSV files that we want to import |
| 10 | + */ |
| 11 | +const csvs = [ |
| 12 | + 'https://terminusdb.com/t/data/bikeshare/2011-capitalbikeshare-tripdata.csv', |
| 13 | + 'https://terminusdb.com/t/data/bikeshare/2012Q1-capitalbikeshare-tripdata.csv', |
| 14 | + 'https://terminusdb.com/t/data/bikeshare/2010-capitalbikeshare-tripdata.csv', |
| 15 | + 'https://terminusdb.com/t/data/bikeshare/2012Q2-capitalbikeshare-tripdata.csv', |
| 16 | + 'https://terminusdb.com/t/data/bikeshare/2012Q3-capitalbikeshare-tripdata.csv', |
| 17 | + 'https://terminusdb.com/t/data/bikeshare/2012Q4-capitalbikeshare-tripdata.csv', |
| 18 | + 'https://terminusdb.com/t/data/bikeshare/2013Q1-capitalbikeshare-tripdata.csv', |
| 19 | + 'https://terminusdb.com/t/data/bikeshare/2013Q2-capitalbikeshare-tripdata.csv', |
| 20 | + 'https://terminusdb.com/t/data/bikeshare/2013Q3-capitalbikeshare-tripdata.csv', |
| 21 | + 'https://terminusdb.com/t/data/bikeshare/2013Q4-capitalbikeshare-tripdata.csv', |
| 22 | + 'https://terminusdb.com/t/data/bikeshare/2014Q1-capitalbikeshare-tripdata.csv', |
| 23 | + 'https://terminusdb.com/t/data/bikeshare/2014Q2-capitalbikeshare-tripdata.csv', |
| 24 | + 'https://terminusdb.com/t/data/bikeshare/2014Q3-capitalbikeshare-tripdata.csv', |
| 25 | + 'https://terminusdb.com/t/data/bikeshare/2014Q4-capitalbikeshare-tripdata.csv', |
| 26 | + 'https://terminusdb.com/t/data/bikeshare/2015Q1-capitalbikeshare-tripdata.csv', |
| 27 | + 'https://terminusdb.com/t/data/bikeshare/2015Q2-capitalbikeshare-tripdata.csv', |
| 28 | + 'https://terminusdb.com/t/data/bikeshare/2015Q3-capitalbikeshare-tripdata.csv', |
| 29 | + 'https://terminusdb.com/t/data/bikeshare/2015Q4-capitalbikeshare-tripdata.csv', |
| 30 | + 'https://terminusdb.com/t/data/bikeshare/2016Q1-capitalbikeshare-tripdata.csv', |
| 31 | + 'https://terminusdb.com/t/data/bikeshare/2016Q2-capitalbikeshare-tripdata.csv', |
| 32 | + 'https://terminusdb.com/t/data/bikeshare/2016Q3-capitalbikeshare-tripdata.csv', |
| 33 | + 'https://terminusdb.com/t/data/bikeshare/2016Q4-capitalbikeshare-tripdata.csv', |
| 34 | + 'https://terminusdb.com/t/data/bikeshare/2017Q1-capitalbikeshare-tripdata.csv', |
| 35 | + 'https://terminusdb.com/t/data/bikeshare/2017Q2-capitalbikeshare-tripdata.csv', |
| 36 | + 'https://terminusdb.com/t/data/bikeshare/2017Q3-capitalbikeshare-tripdata.csv', |
| 37 | + 'https://terminusdb.com/t/data/bikeshare/2017Q4-capitalbikeshare-tripdata.csv', |
| 38 | + 'https://terminusdb.com/t/data/bikeshare/201801_capitalbikeshare_tripdata.csv', |
| 39 | + 'https://terminusdb.com/t/data/bikeshare/201802-capitalbikeshare-tripdata.csv', |
| 40 | + 'https://terminusdb.com/t/data/bikeshare/201803-capitalbikeshare-tripdata.csv', |
| 41 | + 'https://terminusdb.com/t/data/bikeshare/201804-capitalbikeshare-tripdata.csv', |
| 42 | + 'https://terminusdb.com/t/data/bikeshare/201805-capitalbikeshare-tripdata.csv', |
| 43 | + 'https://terminusdb.com/t/data/bikeshare/201806-capitalbikeshare-tripdata.csv', |
| 44 | + 'https://terminusdb.com/t/data/bikeshare/201807-capitalbikeshare-tripdata.csv', |
| 45 | + 'https://terminusdb.com/t/data/bikeshare/201808-capitalbikeshare-tripdata.csv', |
| 46 | + 'https://terminusdb.com/t/data/bikeshare/201809-capitalbikeshare-tripdata.csv', |
| 47 | + 'https://terminusdb.com/t/data/bikeshare/201810-capitalbikeshare-tripdata.csv', |
| 48 | + 'https://terminusdb.com/t/data/bikeshare/201811-capitalbikeshare-tripdata.csv', |
| 49 | + 'https://terminusdb.com/t/data/bikeshare/201812-capitalbikeshare-tripdata.csv', |
| 50 | +] |
| 51 | + |
| 52 | +/** |
| 53 | + * |
| 54 | + * @param {WOQLClient} client |
| 55 | + * @param {String} id |
| 56 | + * @param {String} [title] |
| 57 | + * @param {String} [description] |
| 58 | + */ |
| 59 | +function createDatabase(client, id, title, description) { |
| 60 | + title = title || 'Bike Data' |
| 61 | + description = description || 'A Database for the Terminus Bikes Tutorial' |
| 62 | + const dbdetails = {id: id, label: title, comment: description, schema: true} |
| 63 | + return client.createDatabase(id, dbdetails) |
| 64 | +} |
| 65 | + |
| 66 | +//shorthand so we don't have to type TerminusClient every time |
| 67 | +var WOQL = TerminusClient.WOQL |
| 68 | + |
| 69 | +/** |
| 70 | + * The query which creates the schema |
| 71 | + * @param {WOQLClient} client |
| 72 | + */ |
| 73 | +function createSchema(client) { |
| 74 | + var schema = WOQL.and( |
| 75 | + WOQL.doctype('Station') |
| 76 | + .label('Bike Station') |
| 77 | + .description('A station where municipal bicycles are deposited'), |
| 78 | + WOQL.doctype('Bicycle').label('Bicycle'), |
| 79 | + WOQL.doctype('Journey') |
| 80 | + .label('Journey') |
| 81 | + .property('start_station', 'Station') |
| 82 | + .label('Start Station') |
| 83 | + .property('end_station', 'Station') |
| 84 | + .label('End Station') |
| 85 | + .property('duration', 'integer') |
| 86 | + .label('Journey Duration') |
| 87 | + .property('start_time', 'dateTime') |
| 88 | + .label('Time Started') |
| 89 | + .property('end_time', 'dateTime') |
| 90 | + .label('Time Ended') |
| 91 | + .property('journey_bicycle', 'Bicycle') |
| 92 | + .label('Bicycle Used'), |
| 93 | + ) |
| 94 | + console.log('CREATE SCHEMA GRAPH') |
| 95 | + return client.query(schema) |
| 96 | +} |
| 97 | + |
| 98 | +function appendData(text, id, addChild) { |
| 99 | + const el_id = `el_${id}` |
| 100 | + if (addChild) { |
| 101 | + var node = document.createElement('li') |
| 102 | + node.id = el_id |
| 103 | + node.className = 'list-group-item' |
| 104 | + node.append(text) |
| 105 | + document.getElementById('loadfile').prepend(node) |
| 106 | + } else { |
| 107 | + document.getElementById(el_id).innerHTML = text |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * @param {WOQLClient} client |
| 113 | + * @param {array} arr - array of URLs to load CSVs from |
| 114 | + */ |
| 115 | +function loadCSVs(client, arr) { |
| 116 | + //If you call pop() on an empty array, it returns undefined. |
| 117 | + let next = arr.pop() |
| 118 | + if (next) { |
| 119 | + const csv = getCSVVariables(next) |
| 120 | + const inputQuery = WOQL.and(csv, ...wrangles) |
| 121 | + var answer = WOQL.and(inputQuery, insertQuery) |
| 122 | + appendData(`........loading csv ${next} ${arr.length} remaining`, arr.length, true) |
| 123 | + // console.log("PRETTY_PRINTER",answer.prettyPrinter()) |
| 124 | + client |
| 125 | + .query(answer) |
| 126 | + .then(() => { |
| 127 | + appendData(`Imported ${next} `, arr.length) |
| 128 | + }) |
| 129 | + .catch(() => { |
| 130 | + appendData(`Failed to import csv ${next} `, arr.length) |
| 131 | + }) |
| 132 | + .finally(() => loadCSVs(client, arr)) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Extracting the data from a CSV and binding it to variables |
| 138 | + * @param {WOQLClient} client |
| 139 | + * @param {String} url - the URL of the CSV |
| 140 | + */ |
| 141 | +function getCSVVariables(url) { |
| 142 | + const csv = WOQL.get( |
| 143 | + WOQL.as('Start station', 'v:Start_Station') |
| 144 | + .as('End station', 'v:End_Station') |
| 145 | + .as('Start date', 'v:Start_Time') |
| 146 | + .as('End date', 'v:End_Time') |
| 147 | + .as('Duration', 'v:Duration') |
| 148 | + .as('Start station number', 'v:Start_ID') |
| 149 | + .as('End station number', 'v:End_ID') |
| 150 | + .as('Bike number', 'v:Bike') |
| 151 | + .as('Member type', 'v:Member_Type'), |
| 152 | + ).remote(url) |
| 153 | + return csv |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Wrangling the imported data to make it line up nicely |
| 158 | + */ |
| 159 | +const wrangles = [ |
| 160 | + WOQL.idgen('doc:Journey', ['v:Start_ID', 'v:Start_Time', 'v:Bike'], 'v:Journey_ID'), |
| 161 | + WOQL.idgen('doc:Station', ['v:Start_ID'], 'v:Start_Station_URL'), |
| 162 | + WOQL.cast('v:Duration', 'xsd:integer', 'v:Duration_Cast'), |
| 163 | + WOQL.cast('v:Bike', 'xsd:string', 'v:Bike_Label'), |
| 164 | + WOQL.cast('v:Start_Time', 'xsd:dateTime', 'v:Start_Time_Cast'), |
| 165 | + WOQL.cast('v:End_Time', 'xsd:dateTime', 'v:End_Time_Cast'), |
| 166 | + WOQL.cast('v:Start_Station', 'xsd:string', 'v:Start_Station_Label'), |
| 167 | + WOQL.cast('v:End_Station', 'xsd:string', 'v:End_Station_Label'), |
| 168 | + WOQL.idgen('doc:Station', ['v:End_ID'], 'v:End_Station_URL'), |
| 169 | + WOQL.idgen('doc:Bicycle', ['v:Bike_Label'], 'v:Bike_URL'), |
| 170 | + WOQL.concat('v:Start_ID to v:End_ID at v:Start_Time', 'v:Journey_Label'), |
| 171 | + WOQL.concat( |
| 172 | + 'Bike v:Bike from v:Start_Station to v:End_Station at v:Start_Time until v:End_Time', |
| 173 | + 'v:Journey_Description', |
| 174 | + ), |
| 175 | +] |
| 176 | + |
| 177 | +const insertQuery = WOQL.and( |
| 178 | + WOQL.insert('v:Journey_ID', 'Journey') |
| 179 | + .label('v:Journey_Label') |
| 180 | + .description('v:Journey_Description') |
| 181 | + .property('start_time', 'v:Start_Time_Cast') |
| 182 | + .property('end_time', 'v:End_Time_Cast') |
| 183 | + .property('duration', 'v:Duration_Cast') |
| 184 | + .property('start_station', 'v:Start_Station_URL') |
| 185 | + .property('end_station', 'v:End_Station_URL') |
| 186 | + .property('journey_bicycle', 'v:Bike_URL'), |
| 187 | + WOQL.insert('v:Start_Station_URL', 'Station').label('v:Start_Station_Label'), |
| 188 | + WOQL.insert('v:End_Station_URL', 'Station').label('v:End_Station_Label'), |
| 189 | + WOQL.insert('v:Bike_URL', 'Bicycle').label('v:Bike_Label'), |
| 190 | +) |
| 191 | + |
| 192 | +function getView(url, key, dbid) { |
| 193 | + appendData('.....loading data for visualization', 'view', true) |
| 194 | + var client = new TerminusClient.WOQLClient(url) |
| 195 | + client.connect({key: key, db: dbid}).then(() => { |
| 196 | + showView(client) |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +function showView(client) { |
| 201 | + const WOQL = TerminusClient.WOQL |
| 202 | + const View = TerminusClient.View |
| 203 | + var woql = WOQL.select('v:Start', 'v:Start_Label', 'v:End', 'v:End_Label').and( |
| 204 | + WOQL.triple('v:Journey', 'type', 'scm:Journey'), |
| 205 | + WOQL.triple('v:Journey', 'start_station', 'v:Start'), |
| 206 | + WOQL.opt().triple('v:Start', 'label', 'v:Start_Label'), |
| 207 | + WOQL.triple('v:Journey', 'end_station', 'v:End'), |
| 208 | + WOQL.opt().triple('v:End', 'label', 'v:End_Label'), |
| 209 | + WOQL.triple('v:Journey', 'journey_bicycle', 'v:Bike'), |
| 210 | + ) |
| 211 | + const woqlGraphConfig = View.graph() |
| 212 | + woqlGraphConfig.height(500).width(1200) |
| 213 | + woqlGraphConfig.node('Start_Label', 'End_Label').hidden(true) |
| 214 | + woqlGraphConfig |
| 215 | + .node('End') |
| 216 | + .icon({color: [255, 255, 255], unicode: '\uf84a'}) |
| 217 | + .text('v:End_Label') |
| 218 | + .size(25) |
| 219 | + .charge(-10) |
| 220 | + woqlGraphConfig |
| 221 | + .node('Start') |
| 222 | + .icon({color: [255, 255, 255], unicode: '\uf84a'}) |
| 223 | + .text('v:Start_Label') |
| 224 | + .size(25) |
| 225 | + .collisionRadius(10) |
| 226 | + woqlGraphConfig.edge('Start', 'End').weight(100) |
| 227 | + |
| 228 | + client |
| 229 | + .query(woql) |
| 230 | + .then(result => { |
| 231 | + const resultData = new TerminusClient.WOQLResult(result) |
| 232 | + |
| 233 | + let viewer = woqlGraphConfig.create(null) |
| 234 | + |
| 235 | + viewer.setResult(resultData) |
| 236 | + console.log(viewer.config) |
| 237 | + |
| 238 | + const graphResult = new TerminusDBComponents.GraphResultsViewer(viewer.config, viewer) |
| 239 | + graphResult.load(document.getElementById('target'), true) |
| 240 | + appendData('the Data has been loaded', 'view') |
| 241 | + }) |
| 242 | + .catch(err => { |
| 243 | + console.log(err) |
| 244 | + appendData(`ERROR in loadind data ${err.message}`, 'view') |
| 245 | + }) |
| 246 | +} |
| 247 | + |
| 248 | +/** |
| 249 | + * Runs the tutorial from start to finish |
| 250 | + * @param {String} terminus_server_url - url of the TerminusDB server |
| 251 | + * @param {String} terminus_server_key - key for access to the server |
| 252 | + * @param {String} terminus_db_id - id of the DB to be created in the tutorial |
| 253 | + */ |
| 254 | +async function runTutorial(terminus_server_url, terminus_server_key, terminus_db_id) { |
| 255 | + var client = new TerminusClient.WOQLClient(terminus_server_url) |
| 256 | + try { |
| 257 | + await client.connect({key: terminus_server_key}) |
| 258 | + appendData('......creating database', '001', true) |
| 259 | + await createDatabase(client, terminus_db_id) |
| 260 | + appendData('The database as been created!!!!', '001') |
| 261 | + appendData('......creating schema', '002', true) |
| 262 | + await createSchema(client) |
| 263 | + appendData('The schema as been created!!!!', '002') |
| 264 | + loadCSVs(client, csvs) |
| 265 | + } catch (err) { |
| 266 | + appendData(`ERROR ${err.message}`, '003', true) |
| 267 | + } |
| 268 | +} |
0 commit comments