Skip to content

Commit

Permalink
Merge pull request #226 from unitaryfund/890_data_sets
Browse files Browse the repository at this point in the history
unitaryfund/metriq-app#890: Data sets back end
  • Loading branch information
WrathfulSpatula authored Jan 23, 2024
2 parents 8ebf7d6 + a2c00c1 commit 6d776bb
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 29 deletions.
7 changes: 7 additions & 0 deletions metriq-api/api-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ router.route('/submission/:submissionId/task/:taskId')
router.route('/submission/:submissionId/platform/:platformId')
.post(submissionController.newPlatform)
.delete(submissionController.deletePlatform)
router.route('/submission/:submissionId/dataSet/:dataSetId')
.post(submissionController.newDataSet)
.delete(submissionController.deleteDataSet)
router.route('/submission/:tag/trending/:page')
.get(submissionController.tagTrending)
router.route('/submission/:tag/popular/:page')
Expand All @@ -140,12 +143,16 @@ router.route('/result/:id')
.delete(resultController.delete)
router.route('/platform/names')
.get(platformController.readNames)
router.route('/dataSet/names')
.get(platformController.readDataSetNames)
router.route('/platform/submissionCount/architecture/:id')
.get(platformController.readSubmissionCountsArchitecture)
router.route('/platform/submissionCount/provider/:id')
.get(platformController.readSubmissionCountsProvider)
router.route('/platform/submissionCount')
.get(platformController.readSubmissionCounts)
router.route('/dataSet/submissionCount')
.get(platformController.readDataSetSubmissionCounts)
router.route('/platform/:id/property')
.post(propertyController.new)
router.route('/platform/:id/subscribe')
Expand Down
5 changes: 2 additions & 3 deletions metriq-api/controller/accountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ exports.update_password = async function (req, res) {
return
}

routeWrapper(res,
loginWrapper(res,
async () => await userService.tryPasswordChange(req.auth.id, req.body),
'Successfully changed password.',
req.auth.id)
'Successfully changed password.')
}
16 changes: 14 additions & 2 deletions metriq-api/controller/platformController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,28 @@ exports.subscribe = async function (req, res) {

exports.readNames = async function (req, res) {
routeWrapper(res,
async () => await platformService.getAllNames(req.auth ? req.auth.id : 0),
async () => await platformService.getAllNames(req.auth ? req.auth.id : 0, false),
'Retrieved all platform names.', req.auth ? req.auth.id : 0)
}

exports.readDataSetNames = async function (req, res) {
routeWrapper(res,
async () => await platformService.getAllNames(req.auth ? req.auth.id : 0, true),
'Retrieved all data set names.', req.auth ? req.auth.id : 0)
}

exports.readSubmissionCounts = async function (req, res) {
routeWrapper(res,
async () => await platformService.getTopLevelNamesAndCounts(req.auth ? req.auth.id : 0),
async () => await platformService.getTopLevelNamesAndCounts(req.auth ? req.auth.id : 0, false),
'Retrieved all platform names and counts.', req.auth ? req.auth.id : 0)
}

exports.readDataSetSubmissionCounts = async function (req, res) {
routeWrapper(res,
async () => await platformService.getTopLevelNamesAndCounts(req.auth ? req.auth.id : 0, true),
'Retrieved all data set names and counts.', req.auth ? req.auth.id : 0)
}

exports.readSubmissionCountsArchitecture = async function (req, res) {
routeWrapper(res,
async () => await platformService.getTopLevelNamesAndCountsByArchitecture(req.params.id, req.auth ? req.auth.id : 0),
Expand Down
16 changes: 14 additions & 2 deletions metriq-api/controller/submissionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,28 @@ exports.deleteTask = async function (req, res) {

exports.newPlatform = async function (req, res) {
routeWrapper(res,
async () => await platformService.addOrRemoveSubmission(true, req.params.platformId, req.params.submissionId, req.auth.id),
async () => await platformService.addOrRemovePlatformSubmission(true, req.params.platformId, req.params.submissionId, req.auth.id),
'Successfully added platform to submission.', req.auth ? req.auth.id : 0)
}

exports.deletePlatform = async function (req, res) {
routeWrapper(res,
async () => await platformService.addOrRemoveSubmission(false, req.params.platformId, req.params.submissionId, req.auth.id),
async () => await platformService.addOrRemovePlatformSubmission(false, req.params.platformId, req.params.submissionId, req.auth.id),
'Successfully removed platform from submission.', req.auth ? req.auth.id : 0)
}

exports.newDataSet = async function (req, res) {
routeWrapper(res,
async () => await platformService.addOrRemoveDataSetSubmission(true, req.params.dataSetId, req.params.submissionId, req.auth.id),
'Successfully added data set to submission.', req.auth ? req.auth.id : 0)
}

exports.deleteDataSet = async function (req, res) {
routeWrapper(res,
async () => await platformService.addOrRemoveDataSetSubmission(false, req.params.dataSetId, req.params.submissionId, req.auth.id),
'Successfully removed data set from submission.', req.auth ? req.auth.id : 0)
}

exports.upvote = async function (req, res) {
routeWrapper(res,
async () => await submissionService.upvote(req.params.id, req.auth.id),
Expand Down
29 changes: 29 additions & 0 deletions metriq-api/migrations/017-890_DataSets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.addColumn('platforms', 'isDataSet',
{
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}, { transaction: t }),
queryInterface.addColumn('results', 'submissionDataSetRefId',
{
type: Sequelize.INTEGER,
allowNull: true,
references: { model: 'submissionPlatformRefs', key: 'id' }
}, { transaction: t })
])
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.removeColumn('results', 'submissionDataSetRefId', { transaction: t }),
queryInterface.removeColumn('platforms', 'isDataSet', { transaction: t })
])
})
}
}
23 changes: 23 additions & 0 deletions metriq-api/migrations/018-890_DataSetsUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.addColumn('platforms', 'url',
{
type: Sequelize.TEXT,
allowNull: false,
defaultValue: ''
}, { transaction: t })
])
})
},

down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.removeColumn('platforms', 'url', { transaction: t })
])
})
}
}

8 changes: 8 additions & 0 deletions metriq-api/models/platformModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ module.exports = function (sequelize, DataTypes) {
architectureId: {
type: DataTypes.INTEGER,
allowNull: false
},
isDataSet: {
type: DataTypes.BOOLEAN,
allowNull: false
},
url: {
type: DataTypes.TEXT,
allowNull: false
}
}, {})
Model.associate = function (db) {
Expand Down
55 changes: 46 additions & 9 deletions metriq-api/service/platformService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const SubmissionSqlService = require('./submissionSqlService')
const submissionSqlService = new SubmissionSqlService()
const SubmissionPlatformRefService = require('./submissionPlatformRefService')
const submissionPlatformRefService = new SubmissionPlatformRefService()
const SubmissionDataSetRefService = require('./submissionDataSetRefService')
const submissionDataSetRefService = new SubmissionDataSetRefService()
const ResultService = require('./resultService')
const resultService = new ResultService()
const PlatformSubscriptionService = require('./platformSubscriptionService')
Expand Down Expand Up @@ -90,8 +92,8 @@ class PlatformService extends ModelService {
return { success: true, body: filtered }
}

async getTopLevelNamesAndCounts (userId) {
const result = await this.getTopLevelNames()
async getTopLevelNamesAndCounts (userId, isDataSet) {
const result = await this.getTopLevelNames(isDataSet)
return await this.populate(result, userId)
}

Expand All @@ -105,21 +107,21 @@ class PlatformService extends ModelService {
return await this.populate(result, userId)
}

async getTopLevelNames () {
async getTopLevelNames (isDataSet) {
return (await sequelize.query(
'SELECT id, name, description FROM platforms WHERE platforms."platformId" is NULL '
'SELECT id, name, description, url FROM platforms WHERE platforms."platformId" is NULL AND platforms."isDataSet" = ' + (isDataSet ? 'TRUE' : ' FALSE')
))[0]
}

async getTopLevelNamesByArchitecture (architectureId) {
return (await sequelize.query(
'SELECT id, name, description FROM platforms WHERE platforms."platformId" is NULL AND platforms."architectureId" = ' + architectureId
'SELECT id, name, description, url FROM platforms WHERE platforms."platformId" is NULL AND platforms."isDataSet" = FALSE AND platforms."architectureId" = ' + architectureId
))[0]
}

async getTopLevelNamesByProvider (providerId) {
return (await sequelize.query(
'SELECT id, name, description FROM platforms WHERE platforms."platformId" is NULL AND platforms."providerId" = ' + providerId
'SELECT id, name, description, url FROM platforms WHERE platforms."platformId" is NULL AND platforms."isDataSet" = FALSE AND platforms."providerId" = ' + providerId
))[0]
}

Expand Down Expand Up @@ -175,8 +177,8 @@ class PlatformService extends ModelService {
return await this.SequelizeServiceInstance.findOne({ name: name })
}

async getAllNames (userId) {
const result = await this.SequelizeServiceInstance.projectAll(['id', 'name'])
async getAllNames (userId, isDataSet) {
const result = await this.SequelizeServiceInstance.findAndProject({ isDataSet }, ['id', 'name', 'url'])
if (userId) {
for (let i = 0; i < result.length; i++) {
result[i].dataValues.isSubscribed = !!(await platformSubscriptionService.getByFks(userId, result[i].dataValues.id))
Expand All @@ -199,6 +201,8 @@ class PlatformService extends ModelService {
platform.platformId = reqBody.parentPlatform ? reqBody.parentPlatform : null
platform.architectureId = reqBody.architecture
platform.providerId = reqBody.provider
platform.isDataSet = reqBody.isDataSet !== null ? reqBody.isDataSet : false
platform.url = reqBody.url !== null ? reqBody.url : ''

if (reqBody.parentPlatform) {
const parentPlatform = await this.getByPk(platform.platformId)
Expand Down Expand Up @@ -317,13 +321,16 @@ class PlatformService extends ModelService {
if (reqBody.architecture !== undefined) {
platform.architectureId = reqBody.architecture ? parseInt(reqBody.architecture) : null
}
if (reqBody.url !== undefined) {
platform.url = reqBody.url
}

await platform.save()

return await this.getSanitized(platform.id, userId)
}

async addOrRemoveSubmission (isAdd, platformId, submissionId, userId) {
async addOrRemovePlatformSubmission (isAdd, platformId, submissionId, userId) {
const platform = await this.getByPk(platformId)
if (!platform) {
return { success: false, error: 'Platform not found.' }
Expand Down Expand Up @@ -352,6 +359,36 @@ class PlatformService extends ModelService {

return { success: true, body: submission }
}

async addOrRemoveDataSetSubmission (isAdd, dataSetId, submissionId, userId) {
const dataSet = await this.getByPk(dataSetId)
if (!dataSet) {
return { success: false, error: 'Data set not found.' }
}

let submission = await submissionSqlService.getByPk(submissionId)
if (!submission) {
return { success: false, error: 'Submission not found.' }
}

if (isAdd) {
await submissionDataSetRefService.createOrFetch(submission.id, userId, dataSet.id)
} else {
const ref = await submissionDataSetRefService.getByFks(submission.id, dataSet.id)
if (ref) {
const results = (await resultService.getByDataSetIdSubmissionId(dataSet.id, submission.id)).body
if (results && results.length) {
return { success: false, error: 'Cannot delete submission data set reference with result. Change or delete results in the submission that use this platform, first.' }
}
await submissionDataSetRefService.deleteByPk(ref.id)
}
}

submission = await submissionSqlService.getEagerByPk(submissionId)
submission = await submissionSqlService.populate(submission, userId)

return { success: true, body: submission }
}
}

module.exports = PlatformService
Loading

0 comments on commit 6d776bb

Please sign in to comment.