Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conversion rate in reaction scheme #2159

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/api/entities/reaction_material_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ class ReactionMaterialEntity < ApplicationEntity
expose! :waste
expose! :gas_type
expose! :gas_phase_data
expose! :conversion_rate
end
end
2 changes: 2 additions & 0 deletions app/api/helpers/report_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ def build_sql_reaction_sample(columns, c_id, ids, checkedAll = false)
# reactions_sample:
equivalent: ['r_s.equivalent', '"r eq"', 10],
reference: ['r_s.reference', '"r ref"', 10],
conversion_rate: ['r_s.conversion_rate', '"r conversion rate"', 10],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/ModuleLength: Module has too many lines. [611/100]

},
analysis: {
name: ['anac."name"', '"name"', 10],
Expand Down Expand Up @@ -723,6 +724,7 @@ def force_molfile_selection
equivalent
reference
type
conversion_rate
],
sample: %i[
sample_svg_file
Expand Down
2 changes: 2 additions & 0 deletions app/models/reaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
# variations :jsonb
# plain_text_description :text
# plain_text_observation :text
# gaseous :boolean default(FALSE)
# vessel_size :jsonb
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_product_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_purification_solvent_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_reactant_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_solvent_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
25 changes: 14 additions & 11 deletions app/models/reactions_starting_material_sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
#
# Table name: reactions_samples
#
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# id :integer not null, primary key
# reaction_id :integer
# sample_id :integer
# reference :boolean
# equivalent :float
# position :integer
# type :string
# deleted_at :datetime
# waste :boolean default(FALSE)
# coefficient :float default(1.0)
# show_label :boolean default(FALSE), not null
# gas_type :integer default("off")
# gas_phase_data :jsonb
# conversion_rate :float
#
# Indexes
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default class NumeralInputWithUnitsCompo extends Component {

togglePrefix(currentUnit) {
const units = ['TON/h', 'TON/m', 'TON/s', '°C', '°F', 'K', 'h', 'm', 's'];
const excludedUnits = ['ppm', 'TON'];
const excludedUnits = ['ppm', 'TON', '%'];
const { onMetricsChange, unit } = this.props;
if (units.includes(currentUnit)) {
// eslint-disable-next-line no-unused-expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Material extends Component {
this.gasFieldsUnitsChanged = this.gasFieldsUnitsChanged.bind(this);
this.handleCoefficientChange = this.handleCoefficientChange.bind(this);
this.debounceHandleAmountUnitChange = debounce(this.handleAmountUnitChange, 500);
this.yieldOrConversionRate = this.yieldOrConversionRate.bind(this);
}

handleMaterialClick(sample) {
Expand Down Expand Up @@ -242,34 +243,66 @@ class Material extends Component {
return result > 1 ? '100%' : `${(result * 100).toFixed(0)}%`;
}

equivalentOrYield(material) {
const { reaction, materialGroup } = this.props;
if (materialGroup === 'products') {
const refMaterial = reaction.getReferenceMaterial();
let calculateYield = material.equivalent;
if (material.gas_type === 'gas') {
calculateYield = this.recalculateYieldForGasProduct(material, reaction);
} else if (reaction.hasPolymers()) {
calculateYield = `${((material.equivalent || 0) * 100).toFixed(0)}%`;
} else if (refMaterial && (refMaterial.decoupled || material.decoupled)) {
calculateYield = 'n.a.';
} else if (material.purity < 1 && material.equivalent > 1) {
calculateYield = `${((material.purity / 100 * (material.amount_g * 1000)) * 100).toFixed(1)}%`;
} else {
calculateYield = `${((material.equivalent <= 1 ? material.equivalent || 0 : 1) * 100).toFixed(0)}%`;
}
calculateYield(material, reaction) {
const refMaterial = reaction.getReferenceMaterial();
let calculateYield = material.equivalent;
if (material.gas_type === 'gas') {
calculateYield = this.recalculateYieldForGasProduct(material, reaction);
} else if (reaction.hasPolymers()) {
calculateYield = `${((material.equivalent || 0) * 100).toFixed(0)}%`;
} else if (refMaterial && (refMaterial.decoupled || material.decoupled)) {
calculateYield = 'n.a.';
} else if (material.purity < 1 && material.equivalent > 1) {
calculateYield = `${((material.purity / 100 * (material.amount_g * 1000)) * 100).toFixed(1)}%`;
} else {
calculateYield = `${((material.equivalent <= 1 ? material.equivalent || 0 : 1) * 100).toFixed(0)}%`;
}
return calculateYield;
}

conversionRateField(material) {
const { reaction } = this.props;
const condition = material.conversion_rate / 100 > 1;
const allowedConversionRateValue = material.conversion_rate && condition
? 100 : material.conversion_rate;
return (
<div>
<NumeralInputWithUnitsCompo
precision={4}
value={allowedConversionRateValue || 'n.d.'}
unit="%"
disabled={!permitOn(reaction)}
onChange={(e) => this.handleConversionRateChange(e)}
size="sm"
/>
</div>
);
}

yieldOrConversionRate(material) {
const { reaction, displayYieldField } = this.props;
if (displayYieldField === true || displayYieldField === null) {
return (
<div>
<Form.Control
name="yield"
type="text"
bsClass="bs-form--compact form-control"
size="sm"
value={calculateYield || 'n.d.'}
value={this.calculateYield(material, reaction) || 'n.d.'}
disabled
/>
</div>
);
}
return this.conversionRateField(material);
}

equivalentOrYield(material) {
const { materialGroup } = this.props;
if (materialGroup === 'products') {
return this.yieldOrConversionRate(material);
}
return (
<NumeralInputWithUnitsCompo
size="sm"
Expand Down Expand Up @@ -522,6 +555,20 @@ class Material extends Component {
}
}

handleConversionRateChange(e) {
const { onChange, materialGroup } = this.props;
const conversionRate = e.value;
if (onChange && e) {
const event = {
type: 'conversionRateChanged',
materialGroup,
sampleID: this.materialId(),
conversionRate
};
onChange(event);
}
}

handleGasFieldsChange(field, e, currentValue) {
const { materialGroup, onChange } = this.props;
if (onChange && e.value !== undefined && e.unit !== undefined && e.value !== currentValue) {
Expand Down Expand Up @@ -1075,4 +1122,5 @@ Material.propTypes = {
canDrop: PropTypes.bool,
isOver: PropTypes.bool,
lockEquivColumn: PropTypes.bool.isRequired,
displayYieldField: PropTypes.bool,
};
Loading
Loading