Skip to content

Commit

Permalink
reverting some sourceName/destinationName to
Browse files Browse the repository at this point in the history
sourceId/destinationId
  • Loading branch information
dzheng13341 committed Aug 7, 2023
1 parent fd20ba2 commit 89563b2
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 106 deletions.
10 changes: 5 additions & 5 deletions src/server/models/Conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const sqlFile = database.sqlFile;

class Conversion {
/**
* @param {*} sourceName The unit id of the source.
* @param {*} destinationName The unit id of the destination.
* @param {*} sourceId The unit id of the source.
* @param {*} destinationId The unit id of the destination.
* @param {*} bidirectional Is this conversion bidirectional?
* @param {*} slope The slope of the conversion.
* @param {*} intercept The intercept of the conversion.
* @param {*} note Comments by the admin or OED inserted.
*/
constructor(sourceName, destinationName, bidirectional, slope, intercept, note) {
this.sourceName = sourceName;
this.destinationName = destinationName;
constructor(sourceId, destinationId, bidirectional, slope, intercept, note) {
this.sourceId = sourceId;
this.destinationId = destinationId;
this.bidirectional = bidirectional;
this.slope = slope;
this.intercept = intercept;
Expand Down
30 changes: 15 additions & 15 deletions src/server/routes/conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const router = express.Router();

function formatConversionForResponse(item) {
return {
sourceName: item.sourceName, destinationName: item.destinationName, bidirectional: item.bidirectional, slope: item.slope, intercept: item.intercept, note: item.note
sourceId: item.sourceId, destinationId: item.destinationId, bidirectional: item.bidirectional, slope: item.slope, intercept: item.intercept, note: item.note
};
}

Expand All @@ -36,14 +36,14 @@ router.get('/', async (req, res) => {
router.post('/edit', async (req, res) => {
const validConversion = {
type: 'object',
required: ['sourceName', 'destinationName', 'bidirectional', 'slope', 'intercept'],
required: ['sourceId', 'destinationId', 'bidirectional', 'slope', 'intercept'],
properties: {
sourceName: {
sourceId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
},
destinationName: {
destinationId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
Expand Down Expand Up @@ -73,7 +73,7 @@ router.post('/edit', async (req, res) => {
} else {
const conn = getConnection();
try {
const updatedConversion = new Conversion(req.body.sourceName, req.body.destinationName, req.body.bidirectional,
const updatedConversion = new Conversion(req.body.sourceId, req.body.destinationId, req.body.bidirectional,
req.body.slope, req.body.intercept, req.body.note);
await updatedConversion.update(conn);
} catch (err) {
Expand All @@ -90,14 +90,14 @@ router.post('/edit', async (req, res) => {
router.post('/addConversion', async (req, res) => {
const validConversion = {
type: 'object',
required: ['sourceName', 'destinationName', 'bidirectional', 'slope', 'intercept'],
required: ['sourceId', 'destinationId', 'bidirectional', 'slope', 'intercept'],
properties: {
sourceName: {
sourceId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
},
destinationName: {
destinationId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
Expand Down Expand Up @@ -128,8 +128,8 @@ router.post('/addConversion', async (req, res) => {
try {
await conn.tx(async t => {
const newConversion = new Conversion(
req.body.sourceName,
req.body.destinationName,
req.body.sourceId,
req.body.destinationId,
req.body.bidirectional,
req.body.slope,
req.body.intercept,
Expand All @@ -152,14 +152,14 @@ router.post('/delete', async (req, res) => {
// Only require a source and destination id
const validConversion = {
type: 'object',
required: ['sourceName', 'destinationName'],
required: ['sourceId', 'destinationId'],
properties: {
sourceName: {
sourceId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
},
destinationName: {
destinationId: {
type: 'number',
// Do not allow negatives for now
minimum: 0
Expand All @@ -177,12 +177,12 @@ router.post('/delete', async (req, res) => {
try {
// Don't worry about checking if the conversion already exists
// Just try to delete it to save the extra database call, since the database will return an error anyway if the row does not exist
await Conversion.delete(req.body.sourceName, req.body.destinationName, conn);
await Conversion.delete(req.body.sourceId, req.body.destinationId, conn);
} catch (err) {
log.error(`Error while deleting conversion with error(s): ${err}`);
failure(res, 500, `Error while deleting conversion with errors(s): ${err}`);
}
success(res, `Successfully deleted conversion ${req.body.sourceName} -> ${req.body.destinationName}`);
success(res, `Successfully deleted conversion ${req.body.sourceId} -> ${req.body.destinationId}`);
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/server/services/graph/createConversionArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ async function createCikArray(graph, conn) {
await assignIndex(destinations, conn);
for (const source of sources) {
for (const destination of destinations) {
const sourceName = source.id;
const destinationName = destination.id;
const sourceId = source.id;
const destinationId = destination.id;
// The shortest path from source to destination.
const path = getPath(graph, sourceName, destinationName);
const path = getPath(graph, sourceId, destinationId);
// Check if the path exists.
// If not, we will do nothing since the array has been initialized with [Nan, Nan, ''].
if (path !== null) {
Expand Down
20 changes: 10 additions & 10 deletions src/server/services/graph/createConversionGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async function createConversionGraph(conn) {

const conversions = await Conversion.getAll(conn);
for (let i = 0; i < conversions.length; ++i) {
graph.addLink(conversions[i].sourceName, conversions[i].destinationName);
graph.addLink(conversions[i].sourceId, conversions[i].destinationId);
if (conversions[i].bidirectional) {
graph.addLink(conversions[i].destinationName, conversions[i].sourceName);
graph.addLink(conversions[i].destinationId, conversions[i].sourceId);
}
}

Expand All @@ -33,16 +33,16 @@ async function createConversionGraph(conn) {
/**
* Returns the list of units on the shortest path from source to destination.
* @param {*} graph The conversion graph.
* @param {*} sourceName The source unit's id.
* @param {*} destinationName The destination unit's id.
* @param {*} sourceId The source unit's id.
* @param {*} destinationId The destination unit's id.
* @returns
*/
function getPath(graph, sourceName, destinationName) {
function getPath(graph, sourceId, destinationId) {
const pathFinder = path.aStar(graph, {
oriented: true
});
// The path is returned in reversed order so we need to reverse it.
const p = pathFinder.find(sourceName, destinationName).reverse();
const p = pathFinder.find(sourceId, destinationId).reverse();
if (p.length <= 1) {
// Returns null if the path doesn't exist.
return null;
Expand All @@ -52,17 +52,17 @@ function getPath(graph, sourceName, destinationName) {

/**
* Returns all shortest paths from a unit to others.
* @param {*} sourceName The source unit's id.
* @param {*} sourceId The source unit's id.
* @param {*} graph The conversion graph.
* @returns
*/
function getAllPaths(graph, sourceName) {
function getAllPaths(graph, sourceId) {
let paths = [];
// Loops through all the unit's ids in the graph.
graph.forEachNode(destination => {
const destinationName = destination.id;
const destinationId = destination.id;
// The shortest path from source to destination.
const currentPath = getPath(graph, sourceName, destinationName);
const currentPath = getPath(graph, sourceId, destinationId);
// Checks if the path exists.
if (currentPath !== null) {
paths.push(currentPath);
Expand Down
46 changes: 23 additions & 23 deletions src/server/services/graph/handleSuffixUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const { getAllPaths } = require('./createConversionGraph');

/**
* Adds the new unit and conversions to the database and the conversion graph.
* @param {*} sourceName The source unit's id.
* @param {*} destinationName The destination unit's id.
* @param {*} sourceId The source unit's id.
* @param {*} destinationId The destination unit's id.
* @param {*} slope The conversion's slope from source to destination.
* @param {*} intercept The conversion's intercept from source to destination.
* @param {*} unitName The new unit's name.
* @param {*} unitIdentifier The new unit's identifier.
* @param {*} conn The connection to use.
*/
async function addNewUnitAndConversion(sourceName, destinationName, slope, intercept, unitName, unitIdentifier, graph, conn) {
const sourceUnit = await Unit.getById(sourceName, conn);
const destinationUnit = await Unit.getById(destinationName, conn);
async function addNewUnitAndConversion(sourceId, destinationId, slope, intercept, unitName, unitIdentifier, graph, conn) {
const sourceUnit = await Unit.getById(sourceId, conn);
const destinationUnit = await Unit.getById(destinationId, conn);
// Add a new units where: name is unitName, identifier is unitIdentifier, type_of_unit is Unit.type.suffix,
// displayable and preferredDisplay is the same as destination.
// Note a type_of_unit of suffix is different than a unit with a suffix string.
Expand All @@ -30,13 +30,13 @@ async function addNewUnitAndConversion(sourceName, destinationName, slope, inter
await newUnit.insert(conn);

// Create the conversion from the prefix unit to this new unit.
const newConversion = new Conversion(sourceName, newUnit.id, false, slope, intercept,
const newConversion = new Conversion(sourceId, newUnit.id, false, slope, intercept,
`${sourceUnit.name}${newUnit.name} (created by OED for unit with suffix)`);
await newConversion.insert(conn);

// Add the new node and conversion to the graph.
graph.addNode(newUnit.id, newUnit.name);
graph.addLink(sourceName, newUnit.id);
graph.addLink(sourceId, newUnit.id);
}

/**
Expand All @@ -49,17 +49,17 @@ async function addNewUnitAndConversion(sourceName, destinationName, slope, inter
* @param {*} conn The connection to use.
*/
async function verifyConversion(expectedSlope, expectedIntercept, source, destination, graph, conn) {
const sourceName = source.id;
const destinationName = destination.id;
const currentConversion = await Conversion.getBySourceDestination(sourceName, destinationName, conn);
const sourceId = source.id;
const destinationId = destination.id;
const currentConversion = await Conversion.getBySourceDestination(sourceId, destinationId, conn);
if (!currentConversion) {
// The destination suffix unit exists but the conversion doesn't.
// Create a new conversion with desired values.
const newConversion = new Conversion(sourceName, destinationName, false, expectedSlope, expectedIntercept,
const newConversion = new Conversion(sourceId, destinationId, false, expectedSlope, expectedIntercept,
`${source.name}${destination.name} (created by OED for unit with suffix)`);
// Insert the new conversion to database and graph.
await newConversion.insert(conn);
graph.addLink(sourceName, destinationName);
graph.addLink(sourceId, destinationId);
} else if (currentConversion.slope !== expectedSlope || currentConversion.intercept !== expectedIntercept) {
// While unlikely, the conversion changed so update
currentConversion.slope = expectedSlope;
Expand Down Expand Up @@ -99,11 +99,11 @@ async function hideSuffixUnit(unit, paths, graph, conn) {
/**
* Check if the suffix unit's displayable is the same as the destination unit.
* @param {*} suffixUnit The suffix unit to check.
* @param {*} destinationName The destination unit's id
* @param {*} destinationId The destination unit's id
* @param {*} conn The connection to use.
*/
async function verifyUnit(suffixUnit, destinationName, conn) {
const destinationUnit = await Unit.getById(destinationName, conn);
async function verifyUnit(suffixUnit, destinationId, conn) {
const destinationUnit = await Unit.getById(destinationId, conn);
if (suffixUnit.displayable !== destinationUnit.displayable) {
suffixUnit.displayable = destinationUnit.displayable;
await suffixUnit.update(conn);
Expand All @@ -124,10 +124,10 @@ async function handleSuffixUnits(graph, conn) {
const paths = getAllPaths(graph, unit.id);
// Analyze each path
for (const p of paths) {
const sourceName = p[0].id;
const destinationName = p[p.length - 1].id;
const sourceId = p[0].id;
const destinationId = p[p.length - 1].id;
// The destination unit.
const destinationUnit = await Unit.getById(destinationName, conn);
const destinationUnit = await Unit.getById(destinationId, conn);
// We don't need to create any new units/conversions if the destination unit has the type of suffix or it's not displayed.
if (destinationUnit.typeOfUnit === Unit.unitType.SUFFIX || destinationUnit.displayable === Unit.displayableType.NONE) {
continue;
Expand All @@ -141,10 +141,10 @@ async function handleSuffixUnits(graph, conn) {
// See if this unit already exists. Would if this was done before where this path existed.
if (neededSuffixUnit === null) {
// If not then add the new unit and conversion.
await addNewUnitAndConversion(sourceName, destinationName, slope, intercept, unitName, unitIdentifier, graph, conn);
await addNewUnitAndConversion(sourceId, destinationId, slope, intercept, unitName, unitIdentifier, graph, conn);
} else {
// If it already exists then check if the unit and conversion are correct.
await verifyUnit(neededSuffixUnit, destinationName, conn);
await verifyUnit(neededSuffixUnit, destinationId, conn);
await verifyConversion(slope, intercept, unit, neededSuffixUnit, graph, conn);
}
}
Expand All @@ -164,13 +164,13 @@ async function handleSuffixUnits(graph, conn) {
*/
async function removeAdditionalConversionsAndUnits(suffixUnit, conn) {
// Get all conversions from this suffix unit.
const conversions = (await Conversion.getAll(conn)).filter((conversion) => conversion.sourceName === suffixUnit.id);
const conversions = (await Conversion.getAll(conn)).filter((conversion) => conversion.sourceId === suffixUnit.id);
conversions.forEach(async (conversion) => {
const destinationUnit = await Unit.getById(conversion.destinationName, conn);
const destinationUnit = await Unit.getById(conversion.destinationId, conn);
// The units that OED adds are suffix unit.
if (destinationUnit.typeOfUnit === Unit.unitType.SUFFIX) {
// Delete the conversion.
await Conversion.delete(conversion.sourceName, conversion.destinationName, conn);
await Conversion.delete(conversion.sourceId, conversion.destinationId, conn);
// Hide the destination unit.
destinationUnit.displayable = Unit.displayableType.NONE;
await destinationUnit.update(conn);
Expand Down
6 changes: 3 additions & 3 deletions src/server/services/graph/pathConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ async function pathConversion(path, conn) {
let intercept = 0;
let suffix = '';
for (let i = 0; i < path.length - 1; ++i) {
const sourceName = path[i].id;
const destinationName = path[i + 1].id;
const [newSlope, newIntercept, newSuffix] = await conversionValues(sourceName, destinationName, conn);
const sourceId = path[i].id;
const destinationId = path[i + 1].id;
const [newSlope, newIntercept, newSuffix] = await conversionValues(sourceId, destinationId, conn);
// Updates the path conversion for this new edge.
[slope, intercept] = updatedConversion(slope, intercept, newSlope, newIntercept);
// Updates the suffix for this new edge.
Expand Down
2 changes: 1 addition & 1 deletion src/server/sql/conversion/insert_new_conversion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

INSERT INTO conversions(source_id, destination_id, bidirectional, slope, intercept, note)
VALUES (${sourceName}, ${destinationName}, ${bidirectional}, ${slope}, ${intercept}, ${note});
VALUES (${sourceId}, ${destinationId}, ${bidirectional}, ${slope}, ${intercept}, ${note});
2 changes: 1 addition & 1 deletion src/server/sql/conversion/update_conversion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ UPDATE conversions
slope = ${slope},
intercept = ${intercept},
note = ${note}
WHERE source_id = ${sourceName} AND destination_id = ${destinationName};
WHERE source_id = ${sourceId} AND destination_id = ${destinationId};
6 changes: 3 additions & 3 deletions src/server/test/db/conversionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const Unit = require('../../models/Unit');
* @param {*} actual The actual conversion.
*/
function expectConversionToBeEquivalent(expected, actual) {
expect(actual).to.have.property('sourceName', expected.sourceName);
expect(actual).to.have.property('destinationName', expected.destinationName);
expect(actual).to.have.property('sourceId', expected.sourceId);
expect(actual).to.have.property('destinationId', expected.destinationId);
expect(actual).to.have.property('bidirectional', expected.bidirectional);
expect(actual).to.have.property('slope', expected.slope);
expect(actual).to.have.property('intercept', expected.intercept);
Expand Down Expand Up @@ -49,7 +49,7 @@ mocha.describe('Conversions', () => {
const conversionPreInsert = new Conversion(unitAId, unitBId, true, 1.23, 4.56, 'Note');
await conversionPreInsert.insert(conn);

// Updates the conversion. Note that the sourceName and destinationName can't be changed.
// Updates the conversion. Note that the sourceId and destinationId can't be changed.
conversionPreInsert.bidirectional = false;
conversionPreInsert.intercept = 3.14;
conversionPreInsert.note = 'New note';
Expand Down
Loading

0 comments on commit 89563b2

Please sign in to comment.