Skip to content

Commit

Permalink
Merge pull request #154 from WorldBank-Transport/develop
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
olafveerman authored Jun 26, 2017
2 parents 137c66b + 9d89d2f commit 8f97bfd
Show file tree
Hide file tree
Showing 35 changed files with 3,362 additions and 626 deletions.
223 changes: 199 additions & 24 deletions app/db/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ export function dropProjects () {
return db.schema.dropTableIfExists('projects');
}

export function dropScenarios () {
DEBUG && console.log('Dropping table: scenarios');
return db.schema.dropTableIfExists('scenarios');
}

export function dropProjectsFiles () {
DEBUG && console.log('Dropping table: projects_files');
return db.schema.dropTableIfExists('projects_files');
}

export function dropProjectsAA () {
DEBUG && console.log('Dropping table: projects_aa');
return db.schema.dropTableIfExists('projects_aa');
}

export function dropScenarios () {
DEBUG && console.log('Dropping table: scenarios');
return db.schema.dropTableIfExists('scenarios');
}

export function dropScenariosFiles () {
DEBUG && console.log('Dropping table: scenarios_files');
return db.schema.dropTableIfExists('scenarios_files');
}

export function dropScenariosSettings () {
DEBUG && console.log('Dropping table: scenarios_settings');
return db.schema.dropTableIfExists('scenarios_settings');
}

export function dropOperations () {
DEBUG && console.log('Dropping table: operations');
return db.schema.dropTableIfExists('operations');
Expand All @@ -34,6 +44,36 @@ export function dropOperationsLogs () {
return db.schema.dropTableIfExists('operations_logs');
}

export function dropResults () {
DEBUG && console.log('Dropping table: results');
return db.schema.dropTableIfExists('results');
}

export function dropResultsPoi () {
DEBUG && console.log('Dropping table: results_poi');
return db.schema.dropTableIfExists('results_poi');
}

export function dropProjectsOrigins () {
DEBUG && console.log('Dropping table: projects_origins');
return db.schema.dropTableIfExists('projects_origins');
}

export function dropProjectsOriginsIndicators () {
DEBUG && console.log('Dropping table: projects_origins_indicators');
return db.schema.dropTableIfExists('projects_origins_indicators');
}

export function dropProjectsSourceData () {
DEBUG && console.log('Dropping table: projects_source_data');
return db.schema.dropTableIfExists('projects_source_data');
}

export function dropScenariosSourceData () {
DEBUG && console.log('Dropping table: scenarios_source_data');
return db.schema.dropTableIfExists('scenarios_source_data');
}

export function createProjectsTable () {
DEBUG && console.log('Creating table: projects');
return db.schema.createTable('projects', table => {
Expand All @@ -48,41 +88,52 @@ export function createProjectsTable () {
});
}

export function createScenariosTable () {
DEBUG && console.log('Creating table: scenarios');
return db.schema.createTable('scenarios', table => {
export function createProjectsFilesTable () {
DEBUG && console.log('Creating table: projects_files');
return db.schema.createTable('projects_files', table => {
table.increments('id').primary();
table.string('name');
table.text('description');
table.string('status');
table.boolean('master').defaultTo(false);
table.string('type');
table.string('path');
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.json('admin_areas');
// Arbitrary additional json data.
table.json('data');
table.timestamps();

table.unique(['project_id', 'name']);
});
}

export function createProjectsFilesTable () {
DEBUG && console.log('Creating table: projects_files');
return db.schema.createTable('projects_files', table => {
export function createProjectsAATable () {
DEBUG && console.log('Creating table: projects_aa');
return db.schema.createTable('projects_aa', table => {
table.increments('id').primary();
table.string('name');
table.string('type');
table.string('path');
table.json('geometry');
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
// Arbitrary additional json data.
table.json('data');
});
}

export function createScenariosTable () {
DEBUG && console.log('Creating table: scenarios');
return db.schema.createTable('scenarios', table => {
table.increments('id').primary();
table.string('name');
table.text('description');
table.string('status');
table.boolean('master').defaultTo(false);
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.json('admin_areas');
table.timestamps();

table.unique(['project_id', 'name']);
});
}

Expand All @@ -92,6 +143,7 @@ export function createScenariosFilesTable () {
table.increments('id').primary();
table.string('name');
table.string('type');
table.string('subtype');
table.string('path');
table.integer('project_id').unsigned();
table.foreign('project_id')
Expand All @@ -101,12 +153,24 @@ export function createScenariosFilesTable () {
table.foreign('scenario_id')
.references('scenarios.id')
.onDelete('CASCADE');
// Arbitrary additional json data.
table.json('data');
table.timestamps();
});
}

export function createScenariosSettingsTable () {
DEBUG && console.log('Creating table: scenarios_settings');
return db.schema.createTable('scenarios_settings', table => {
table.string('key');
table.text('value');
table.integer('scenario_id').unsigned();
table.foreign('scenario_id')
.references('scenarios.id')
.onDelete('CASCADE');
table.timestamps();
table.primary(['scenario_id', 'key']);
});
}

export function createOperationsTable () {
DEBUG && console.log('Creating table: operations');
return db.schema.createTable('operations', table => {
Expand Down Expand Up @@ -139,17 +203,128 @@ export function createOperationsLogsTable () {
});
}

export function createResultsTable () {
DEBUG && console.log('Creating table: results');
return db.schema.createTable('results', table => {
table.increments('id').primary();
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.integer('scenario_id').unsigned();
table.foreign('scenario_id')
.references('scenarios.id')
.onDelete('CASCADE');
table.integer('origin_id').unsigned();
table.foreign('origin_id')
.references('projects_origins.id')
.onDelete('CASCADE');
table.integer('project_aa_id').unsigned();
table.foreign('project_aa_id')
.references('projects_aa.id')
.onDelete('CASCADE');
});
}

export function createResultsPoiTable () {
DEBUG && console.log('Creating table: results_poi');
return db.schema.createTable('results_poi', table => {
table.increments('id').primary();
table.integer('result_id').unsigned();
table.foreign('result_id')
.references('results.id')
.onDelete('CASCADE');
table.string('type');
table.integer('time');
});
}

export function createProjectsOriginsTable () {
DEBUG && console.log('Creating table: projects_origins');
return db.schema.createTable('projects_origins', table => {
table.increments('id').primary();
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.string('name');
table.json('coordinates');
});
}

export function createProjectsOriginsIndicatorsTable () {
DEBUG && console.log('Creating table: projects_origins_indicators');
return db.schema.createTable('projects_origins_indicators', table => {
table.increments('id').primary();
table.integer('origin_id').unsigned();
table.foreign('origin_id')
.references('projects_origins.id')
.onDelete('CASCADE');
table.string('key');
table.string('label');
table.integer('value');
});
}

export function createProjectsSourceData () {
DEBUG && console.log('Creating table: projects_source_data');
return db.schema.createTable('projects_source_data', table => {
table.increments('id').primary();
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.string('name');
table.string('type');
table.json('data');
});
}

export function createScenariosSourceData () {
DEBUG && console.log('Creating table: scenarios_source_data');
return db.schema.createTable('scenarios_source_data', table => {
table.increments('id').primary();
table.integer('project_id').unsigned();
table.foreign('project_id')
.references('projects.id')
.onDelete('CASCADE');
table.integer('scenario_id').unsigned();
table.foreign('scenario_id')
.references('scenarios.id')
.onDelete('CASCADE');
table.string('name');
table.string('type');
table.json('data');
});
}

export function setupStructure () {
return dropScenariosFiles()
.then(() => dropProjectsFiles())
.then(() => dropResultsPoi())
.then(() => dropResults())
.then(() => dropProjectsAA())
.then(() => dropOperationsLogs())
.then(() => dropOperations())
.then(() => dropScenariosSettings())
.then(() => dropScenariosSourceData())
.then(() => dropScenarios())
.then(() => dropProjectsSourceData())
.then(() => dropProjectsOriginsIndicators())
.then(() => dropProjectsOrigins())
.then(() => dropProjects())
.then(() => createProjectsTable())
.then(() => createProjectsAATable())
.then(() => createScenariosTable())
.then(() => createScenariosSettingsTable())
.then(() => createOperationsTable())
.then(() => createOperationsLogsTable())
.then(() => createProjectsFilesTable())
.then(() => createScenariosFilesTable());
.then(() => createScenariosFilesTable())
.then(() => createProjectsOriginsTable())
.then(() => createProjectsOriginsIndicatorsTable())
.then(() => createResultsTable())
.then(() => createResultsPoiTable())
.then(() => createScenariosSourceData())
.then(() => createProjectsSourceData());
}
18 changes: 4 additions & 14 deletions app/plugins/rra-osm-p2p-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,10 @@ const rraOsmRoute = {

if (path.match(/changeset\/[0-9]+\/upload/)) {
// Update the database with the road generation time.
db.transaction(function (trx) {
return trx('scenarios')
.select('*')
.where('id', scId)
.first()
.then(scenario => {
let data = scenario.data;
data.rn_updated_at = (new Date());
return trx('scenarios')
.update({ data })
.where('id', scId);
})
.then(() => trx.commit());
})
db('scenarios_settings')
.update({value: (new Date())})
.where('scenario_id', scId)
.where('key', 'rn_updated_at')
.then(() => handleIt());
} else {
handleIt();
Expand Down
40 changes: 28 additions & 12 deletions app/routes/projects--create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,48 @@ module.exports = [
}
},
handler: (request, reply) => {
const now = new Date();
const data = request.payload;
const base = {
status: 'pending',
created_at: (new Date()),
updated_at: (new Date())
created_at: now,
updated_at: now
};

db('projects')
.returning('*')
.insert(Object.assign({}, data, base))
.then(res => {
const projectData = res[0];

.then(projectData => {
projectData = projectData[0];
// Create first scenario. This is needed to store the related files.
db('scenarios')
return db('scenarios')
.returning('*')
.insert({
name: 'Main scenario',
project_id: projectData.id,
status: 'pending',
master: true,
created_at: (new Date()),
updated_at: (new Date()),
data: {
res_gen_at: 0,
rn_updated_at: 0
}
created_at: now,
updated_at: now
})
.then(scenarioData => {
scenarioData = scenarioData[0];
return db.batchInsert('scenarios_settings', [
{
scenario_id: scenarioData.id,
key: 'res_gen_at',
value: 0,
created_at: now,
updated_at: now
},
{
scenario_id: scenarioData.id,
key: 'rn_updated_at',
value: 0,
created_at: now,
updated_at: now
}
]);
})
.then(() => reply(projectData))
.catch(err => reply(Boom.badImplementation(err)));
Expand Down
Loading

0 comments on commit 8f97bfd

Please sign in to comment.