Skip to content

Commit

Permalink
costig multiplier added
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlebl committed Jul 29, 2024
1 parent a224ec7 commit 912b309
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 31 deletions.
23 changes: 14 additions & 9 deletions nomad-front-end/src/components/AccountsComponents/AccountsForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ const AccountsForm = props => {
})
}

const fetchCosts = () => {
if (dateRange) {
values.dateRange = dateRange.map(date => date.format('YYYY-MM-DD'))
}

if (type !== 'Grants') {
props.getCosts(token, values)
} else {
props.getGrantsCosts(token, { dateRange: values.dateRange })
}
}

if (!dateRange) {
return Modal.confirm({
title: 'Date range not defined',
Expand All @@ -47,18 +59,11 @@ const AccountsForm = props => {
</div>
),
onOk() {
if (dateRange) {
values.dateRange = dateRange.map(date => date.format('YYYY-MM-DD'))
}

if (type !== 'Grants') {
props.getCosts(token, values)
} else {
props.getGrantsCosts(token, { dateRange: values.dateRange })
}
fetchCosts()
}
})
}
fetchCosts()
}

return (
Expand Down
12 changes: 10 additions & 2 deletions nomad-front-end/src/components/AccountsComponents/GrantForm.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Form, Button, Input, Space, Modal, message } from 'antd'
import { Form, Button, Input, Space, Modal, message, InputNumber } from 'antd'
import { QuestionCircleOutlined, CloseOutlined } from '@ant-design/icons'

import SelectGrpUsr from '../Forms/SelectGrpUsr/SelectGrpUsr'
Expand Down Expand Up @@ -80,7 +80,6 @@ const GrantForm = props => {
}
})

console.log(usrGrpIdArray)
const reqObject = {
...values,
include: props.tagsState.map(i => ({ isGroup: i.type === 'group', id: i.id, name: i.name }))
Expand Down Expand Up @@ -114,6 +113,15 @@ const GrantForm = props => {
<Form.Item label='Description' name='description'>
<Input style={{ width: 470 }} />
</Form.Item>
<Form.Item
label='Costing multiplier'
name='multiplier'
initialValue={1}
rules={[{ required: true, message: 'Please input multiplier. Default value is 1' }]}
tooltip='Costing multiplier can be used to adjust costing set in instrument costing table'
>
<InputNumber min={0} />
</Form.Item>
<SelectGrpUsr
groupList={props.groupList}
userList={props.userList}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const SetGrantsTable = props => {
title: 'Description',
dataIndex: 'description'
},
{
title: 'Costing multiplier',
dataIndex: 'multiplier',
width: 150,
align: 'center'
},
{
title: 'Include',
align: 'center',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const infoModalConfig = {
<strong>Grants</strong>
</div>
<p>
This calculation will work only if grants have been preset. In "Set Grants" table you can
assign either individual users or whole group to a grant. If you assign individual user and
his own group to two different grants, the experiments that were originated by the user will
be charged to the grant corresponding to the user and not to the one corresponding to the
whole group.
This calculation will work only if grants have been preset and it is based on instrument
costing and costing multiplier in the time of archiving of the experiments. In "Set Grants"
table you can assign either individual users or whole group to a grant. If you assign
individual user and his own group to two different grants, the experiments that were
originated by the user will be charged to the grant corresponding to the user and not to the
one corresponding to the whole group.
</p>
<Alert
message='If there are experiments originated by a user/s who was not assigned to a grant in the time of archiving the calculation issues a warning with corresponding username/s which allows to amend the results by using users type calculation'
Expand Down
14 changes: 9 additions & 5 deletions nomad-rest-api/controllers/admin/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export async function getCosts(req, res) {
searchParamsClaims.$and = [...searchParamsClaims.$and, { group: groupId }]
}


const expArray = await Experiment.find(searchParams, 'instrument totalExpTime user')
const claimsArray = await Claim.find(searchParamsClaims, 'instrument expTime user')

Expand Down Expand Up @@ -218,7 +217,7 @@ export async function putInstrumentsCosting(req, res) {

export async function postGrant(req, res) {
const errors = validationResult(req)
const { grantCode, description, include } = req.body
const { grantCode, description, include, multiplier } = req.body

try {
if (!errors.isEmpty()) {
Expand All @@ -235,7 +234,8 @@ export async function postGrant(req, res) {
const newGrantObj = {
grantCode: grantCode.toUpperCase(),
description,
include
include,
multiplier
}

const grant = new Grant(newGrantObj)
Expand Down Expand Up @@ -269,7 +269,7 @@ export async function deleteGrant(req, res) {
}

export async function putGrant(req, res) {
const { description, include, _id } = req.body
const { description, multiplier, include, _id } = req.body
try {
const grants = await Grant.find({})
if (checkDuplicate(include, grants, _id)) {
Expand All @@ -278,7 +278,11 @@ export async function putGrant(req, res) {
})
}

const updatedGrant = await Grant.findByIdAndUpdate(req.body._id, { description, include })
const updatedGrant = await Grant.findByIdAndUpdate(req.body._id, {
description,
include,
multiplier
})

if (!updatedGrant) {
return res.sendStatus(404)
Expand Down
6 changes: 3 additions & 3 deletions nomad-rest-api/controllers/claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Instrument from '../models/instrument.js'
import ManualExperiment from '../models/manualExperiment.js'
import Claim from '../models/claim.js'
import sendUploadCmd from './tracker/sendUploadCmd.js'
import { getGrantId } from '../utils/accountsUtils.js'
import { getGrantInfo } from '../utils/accountsUtils.js'

export const getFolders = async (req, res) => {
const { instrumentId } = req.params
Expand Down Expand Up @@ -144,12 +144,12 @@ export const approveClaims = async (req, res) => {
req.body.map(async id => {
const claim = await Claim.findById(id)
const instrument = await Instrument.findById(claim.instrument, 'cost')
const grantId = await getGrantId(claim.user, claim.group)
const { grantId, multiplier } = await getGrantInfo(claim.user, claim.group)

claim.status = 'Approved'
claim.grantCosting = {
grantId,
cost: +claim.expTime * instrument.cost
cost: +claim.expTime * instrument.cost * multiplier
}

await claim.save()
Expand Down
6 changes: 3 additions & 3 deletions nomad-rest-api/controllers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import User from '../models/user.js'
import Instrument from '../models/instrument.js'
import { getIO } from '../socket.js'
import { getNMRiumDataObj } from '../utils/nmriumUtils.js'
import { getGrantId } from '../utils/accountsUtils.js'
import { getGrantInfo } from '../utils/accountsUtils.js'

export const postData = async (req, res) => {
const { datasetName, expNo, dataPath } = req.body
Expand Down Expand Up @@ -61,11 +61,11 @@ export const postData = async (req, res) => {

//Calculation of costs for grants

const grantId = await getGrantId(experiment.user.id, experiment.group.id)
const { grantId, multiplier } = await getGrantInfo(experiment.user.id, experiment.group.id)

experiment.grantCosting = {
grantId,
cost: moment.duration(experiment.totalExpTime).asHours() * instrument.cost
cost: moment.duration(experiment.totalExpTime).asHours() * instrument.cost * multiplier
}

await experiment.save()
Expand Down
7 changes: 6 additions & 1 deletion nomad-rest-api/models/grant.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const grantSchema = new Schema({
required: true
}
}
]
],
multiplier: {
type: Number,
required: true,
default: 1
}
})

export default model('Grant', grantSchema)
6 changes: 3 additions & 3 deletions nomad-rest-api/utils/accountsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getSearchParamsClaims = dateRange => {
return searchParamsClaims
}

const getGrantId = async (userId, groupId) => {
const getGrantInfo = async (userId, groupId) => {
let grant = await Grant.findOne({
include: {
$elemMatch: { id: userId }
Expand All @@ -67,10 +67,10 @@ const getGrantId = async (userId, groupId) => {
}

if (grant) {
return Promise.resolve(grant._id)
return Promise.resolve({ grantId: grant._id, multiplier: grant.multiplier })
} else {
return Promise.resolve(undefined)
}
}

export { checkDuplicate, getSearchParams, getSearchParamsClaims, getGrantId }
export { checkDuplicate, getSearchParams, getSearchParamsClaims, getGrantInfo }

0 comments on commit 912b309

Please sign in to comment.