Skip to content

Commit

Permalink
Merge pull request #10 from dougestey/postgresql
Browse files Browse the repository at this point in the history
MongoDB -> PostgreSQL
  • Loading branch information
dougestey committed Jul 28, 2018
2 parents d92e709 + 3a96d8c commit 8988cf0
Show file tree
Hide file tree
Showing 27 changed files with 779 additions and 277 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_URL=http://localhost
BOOTSTRAP_DB=false
TRACK_ENABLED=true
TRACK_NPC=false
TRACK_FLEETS=true
FLEET_EXPIRY_IN_MINUTES=30
Expand Down
16 changes: 16 additions & 0 deletions api/controllers/FleetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ module.exports = {
}

return res.status(200).json(fleet);
},

async active (req, res) {
let fleets = await Fleet.find({ isActive: true }).limit(150)
.populate('characters')
.populate('history')
.populate('kills');

for (let fleet of fleets) {
let system = await System.findOne(fleet.system)
.populate('region');

fleet.system = system;
}

return res.status(200).json(fleets);
}

};
2 changes: 1 addition & 1 deletion api/controllers/SystemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {

systemId = parseInt(systemId);

let system = await Swagger.system(systemId);
let system = await System.findOne(systemId);

if (!system)
return res.notFound();
Expand Down
2 changes: 1 addition & 1 deletion api/models/Alliance.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {

attributes: {

allianceId: { type: 'number', unique: true },
id: { type: 'number', autoIncrement: false, required: true },

name: 'string',

Expand Down
4 changes: 3 additions & 1 deletion api/models/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {

attributes: {

characterId: { type: 'number', unique: true },
id: { type: 'number', autoIncrement: false, required: true },

name: 'string',

Expand All @@ -23,6 +23,8 @@ module.exports = {

fleet: { model: 'fleet' },

history: { collection: 'fleet', via: 'history' },

// Meta

lastEsiUpdate: 'string',
Expand Down
32 changes: 32 additions & 0 deletions api/models/Constellation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Constellation.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

let datastore = 'sdeDev';

if (process.env.NODE_ENV === 'production') {
datastore = 'sde';
}

module.exports = {

datastore,

tableName: 'mapConstellations',

attributes: {

createdAt: false,

updatedAt: false,

id: { columnName: 'constellationID', type: 'number', autoIncrement: false, required: true },

name: { columnName: 'constellationName', type: 'string' },

}

};
2 changes: 1 addition & 1 deletion api/models/Corporation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {

attributes: {

corporationId: { type: 'number', unique: true },
id: { type: 'number', autoIncrement: false, required: true },

name: 'string',

Expand Down
2 changes: 2 additions & 0 deletions api/models/Fleet.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module.exports = {

characters: { collection: 'character', via: 'fleet' },

history: { collection: 'character', via: 'history' },

kills: { collection: 'kill', via: 'fleet' },

// Meta
Expand Down
32 changes: 32 additions & 0 deletions api/models/Region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Region.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

let datastore = 'sdeDev';

if (process.env.NODE_ENV === 'production') {
datastore = 'sde';
}

module.exports = {

datastore,

tableName: 'mapRegions',

attributes: {

createdAt: false,

updatedAt: false,

id: { columnName: 'regionID', type: 'number', autoIncrement: false, required: true },

name: { columnName: 'regionName', type: 'string' },

}

};
6 changes: 5 additions & 1 deletion api/models/Stargate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/**
* Stargate.js
*
* @description :: A model definition. Represents a database table/collection/etc.
* @description :: How to get this information from the SDE is something I've
* :: researched for some time to no avail, likely due to the
* :: i18n mechanisms CCP employs. So we hit ESI for it. For now.
*
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

// TODO: Determine where this lies in the SDE
module.exports = {

attributes: {
Expand Down
29 changes: 24 additions & 5 deletions api/models/System.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

let datastore = 'sdeDev';

if (process.env.NODE_ENV === 'production') {
datastore = 'sde';
}

module.exports = {

datastore,

tableName: 'mapSolarSystems',

attributes: {

systemId: { type: 'number', unique: true },
createdAt: false,

updatedAt: false,

id: {
columnName: 'solarSystemID',
type: 'number',
autoIncrement: false,
required: true
},

name: 'string',
name: { columnName: 'solarSystemName', type: 'string' },

securityStatus: 'number',
securityStatus: { columnName: 'security', type: 'number' },

// Relationships

fleets: { collection: 'fleet', via: 'system' },
constellation: { columnName: 'constellationID', model: 'constellation' },

kills: { collection: 'kill', via: 'system' }
region: { columnName: 'regionID', model: 'region' },

}

Expand Down
26 changes: 23 additions & 3 deletions api/models/Type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

let datastore = 'sdeDev';

if (process.env.NODE_ENV === 'production') {
datastore = 'sde';
}

module.exports = {

datastore,

tableName: 'invTypes',

attributes: {

typeId: { type: 'number', unique: true },
createdAt: false,

name: 'string',
updatedAt: false,

}
id: {
columnName: 'typeID',
type: 'number',
autoIncrement: false,
required: true
},

name: { columnName: 'typeName', type: 'string' },

description: 'string',

}
};
15 changes: 13 additions & 2 deletions api/services/Identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

// Takes a set of characterIds, resolves them to local records,
// and returns an array of local ids.
//
// Now that Sentinel uses PostgreSQL with real character IDs as unique
// identifiers, this function should return the same thing passed to it.
let _resolveCharactersToIds = async(ids) => {
let resolved = [];

Expand Down Expand Up @@ -78,6 +81,7 @@ let _createFleet = async(killmail, kill, system) => {
.fetch();

await Fleet.addToCollection(fleet.id, 'characters').members(characters);
await Fleet.addToCollection(fleet.id, 'history').members(characters);
await Fleet.addToCollection(fleet.id, 'kills').members([kill.id]);

return FleetSerializer.one(fleet.id);
Expand All @@ -93,9 +97,12 @@ let _updateFleet = async(fleet, kill, system) => {

// Then we fetch the three latest kills in order to determine who's actively in the fleet.
let latestKills = await Kill.find({ fleet: fleet.id }).sort('time DESC').limit(3);
let existingCharacters = await Character.find({ fleet: fleet.id });
let activeCharacters = _determineActiveCharacters(latestKills);
let composition = _determineActiveComposition(latestKills);

existingCharacters = existingCharacters.map((character) => character.id);

// Convert characterIds to local ids. This also creates character records for those
// that Sentinel's DB isn't already aware of.
activeCharacters = await _resolveCharactersToIds(activeCharacters);
Expand All @@ -109,6 +116,10 @@ let _updateFleet = async(fleet, kill, system) => {
fleet.system = system;
}

// Ensure we're preserving character fleet history.
await Fleet.addToCollection(fleet.id, 'history').members(existingCharacters);
await Fleet.addToCollection(fleet.id, 'history').members(activeCharacters);

// Update the fleet's collection of characters. This will drop expired ones.
await Fleet.replaceCollection(fleet.id, 'characters').members(activeCharacters);

Expand Down Expand Up @@ -142,7 +153,7 @@ let Identifier = {
let fleetIds = [];

for (let characterId of attackersWithIds) {
let record = await Character.findOne({ characterId });
let record = await Character.findOne(characterId);

if (record && record.fleet)
fleetIds.push(record.fleet);
Expand Down Expand Up @@ -176,7 +187,7 @@ let Identifier = {
let candidates = fleets.map((candidate) => {
let { id, characters } = candidate;

characters = characters.map((c) => c.characterId);
characters = characters.map((c) => c.id);

let mostPilots, leastPilots;

Expand Down
6 changes: 3 additions & 3 deletions api/services/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ let Resolver = {
// Unfortunately Fuzzworks doesn't give us an itemName for stargates.
// It does seem to handle belts and stations fine. More testing req'd.
if (!itemName) {
let type = await Swagger.type(typeId);
let type = await Type.findOne(typeId);

if (type && type.name) {
if (type.name.indexOf('Stargate') !== -1) {
let { name } = await Swagger.stargate(itemId);
let { name: systemName } = await Swagger.system(systemId);
let { name: systemName } = await System.findOne(systemId);

itemName = `${systemName} - ${name}`;
} else {
Expand All @@ -62,7 +62,7 @@ let Resolver = {
let ships = await Swagger.names(_.uniq(shipTypeIds));

_.forEach(ids, (shipTypeId, characterId) => {
let charIndex = _.findIndex(characters, 'characterId', parseInt(characterId)),
let charIndex = _.findIndex(characters, 'id', parseInt(characterId)),
shipIndex = _.findIndex(ships, 'id', shipTypeId);

if (charIndex !== -1)
Expand Down
Loading

0 comments on commit 8988cf0

Please sign in to comment.