Service - Transform Units: Difference between revisions

From Izara Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
= Overview =
= Overview =


Transformations are used to convert a number of input Units into sets of output Units.
Transformations are used to convert any number of inputUnits into any number of outputUnits.


= Repository =
= Repository =
Line 7: Line 7:
https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units
https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units


= DynamoDB tables =
== Calculating values ==
 
combinationEquation and calculateByProperties fields replace out special tags first then parse through an evaluation library, eg:
* https://mathjs.org/
* https://www.npmjs.com/package/string-math
* https://www.npmjs.com/package/exact-math
* https://www.codeproject.com/Articles/12116/JavaScript-Mathematical-Expression-Evaluator


== [[Standard Config Table Per Service]] ==
Must make sure equation is validated/sanitized first, be careful of eval or new Function which can invoke arbritrary commands


=== Configuration tags ===
= Transform request =


<syntaxhighlight lang="JavaScript">
* transformConfig is primarily used as a default template, each unitTransform can adjust almost all details such as units inputted and outputted propertyValues
{
* user can use the transformConfig inputUnits calculatedProperties to recalc inputUnit property values according to the outputUnits numberOfUnits
configTag: "SupplyGraphServiceName"
* user can set to recalculate outputUnits calculatedProperties according to inputUnits property values
configKey: "SupplyGraphServiceName"
* inputUnits array can have multiple elements with the same unitTypeId, this is a guide for frontend to allow multiple units of the same unitType but eg different fabrics to be inputs in the same transformation
configValue: xxx // eg: "SupplyGraph"
* outputUnit calculatedProperties will calculate the same for all outputUnit numberOfUnits, can be adjusted manually
}
* outputUnits array can have multiple elements with the same unitTypeId for eg calcualting cost for sizes of different clothing)
</syntaxhighlight>


= transformConfig structure =
= transformConfigInputOutput =


<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
{
{
inputUnitTypes: {
inputUnits: [
{unitTypeId}: {
{
stageId: "xx", // optional stage that Units get updated to
unitTypeId: "zz",
quantityProperty: "xx", // optional, name of property that is used as quantity to spread across output Units, if none set then quantity is 1
stageId: "xx", // optional, stage that applies stageAdjustments to input unit
splitIntegerOnly: false, // default false, if true splits can only be integers, will not process transformation if split per outputUnit is not integer
numberOfUnits: 1, // default number of inputUnits used to perform this transform
}, //
calculatedProperties:{ // calculates changes to inputUnits unitTrackedProperties
},
unitTypeTrackedPropertyId: {
outputUnitTypes: {
integerOnly: false, // default false, if true then calculated splits must be integers, will not process transformation if split per outputUnit are not integers
{unitTypeId}: {
quantityReductionSetting: "spread"|"clearPerUnit", // if not all quantityUnitTypeTrackedPropertyId are used up in transformation, do we reduce all numberOfUnits quantity equally, or clear quantity out one unit at a time
stageId: "xx", // required initial stage Units are set to
valueUsePerOutputUnit: 1,
calculatedProperties: { // calculates a property added to the outputUnits
},
{propertyName}: {
// ...
},
},  
//..
],
outputUnits: [
{
unitTypeId: "zz",
stageId: "xx", // optional, stage that applies stageAdjustments to output unit
numberOfUnits: 1, // default number of outputUnits to create, calculations are based on this and inputUnit numberOfUnits
calculatedProperties: { // calculates value for unitTypeTrackedProperties added to outputUnits
unitTypeTrackedPropertyId: {
calculationType: "calculateValue", // can add other types later
calculationType: "calculateValue", // can add other types later
combinationEquation: "[propertyA]+[propertyB]", // optional, method of combining multiple calculateByProperties together, defaults to adding/sum
combinationEquation: "[calcPropTagA]+[calcPropTagB]", // optional, method of combining multiple calculateByProperties together, defaults to adding/sum
calculateByProperties: [ // can set many inputType properties
calculateByProperties: {
{
calcPropTag: { // any string, used in combinationEquation
inputUnitTypeId: "xxx", // different unitTypeIds can have different properties/calculations
calculation: "[valueUsePerOutputUnit:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]*[unitTypeTrackedProperty:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]",
propertyName: "xxx", // property that gets inserted into calculation
calculation: "[property]*[quantity]",
or
defaultValue: "30", // not calculated, user can overwrite defaultValue
round: "round", //optional: "round"|"ceil"|"floor"|..
},
},
// ..
// ..
]
}
},  
},  
//..
//..
Line 52: Line 73:
},  
},  
//..
//..
},
],
 
}
}
</syntaxhighlight>
</syntaxhighlight>


== Calculating values ==
= objectSchemas =  
 
combinationEquation and calculateByProperties.calculation fields replace out special tags first like [propertyA]/[propertyB]/[property]/[quantity], the parse through an evaluation library, eg:
 
* https://mathjs.org/
* https://www.npmjs.com/package/string-math
* https://www.npmjs.com/package/exact-math
* https://www.codeproject.com/Articles/12116/JavaScript-Mathematical-Expression-Evaluator
 
Must make sure equation is validated/sanitized first, be careful of eval or new Function which can invoke arbritrary commands
 
= Transform request =
 
* Input Units might have multiple Unit records of the same UnitType, and each record has it's quantityProperty which might be >1
* All quantityProperty will be used completely in one transformation, no partial quantities handled
* Do not differentiate which inputUnits are used on which outputUnits, if want to do that need to split into multiple transformations
* When we calculate calculatedProperties we sum all inputUnits calculations together to work out a value per quantity, then apply the ratio set for each outputUnit to calculate each outputUnits property
* outputUnits are split by itemType, but each itemType is split by sets so different calculatedProperties can be done (eg calcualting cost for sizes of different clothing)
* each itemType sets the quantity (number of Units) that are created
* each itemType set defaults to even ratio usage of inputUnit total quantity
* each itemType set can set to override default ratio with a set value (total or per Unit) for each input ItemType, as long as all input quantities are used in full (either by having >0 sets use remaining ratio, or override accurately to use all quantity)
* if multiple
 
= Graph database =
 
== [[Service - Supply Graph]]==
 
=== Nodes ===


<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
{
{
nodeLabel: "{transformUnitsLib.TRANSFORM_UNITS_GRAPH_NODE_LABEL}", // transformUnits
objectType: "transformConfig",
schema: {
addOnDataStructure: [
identifier: true,
{
restrictProperties: true,
type:"versionedData",
restrictRelationships: true,
versionedDataLabel: "transformConfigInputOutput",
properties: {
storageResourceTag : "xx",
transformConfigId: {
fieldNames: [
identifier: true, //(random uuid)
{  
fieldName: "inputUnits",
},
{
fieldName: "outputUnits",
},
]
},
],
storageResources:{
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
fieldNames: {
transformConfigId: {
type: "string",
            randomOnCreate: true
},
{
fieldName: "inputUnits",
type: "arrayMixed",
requiredOnCreate: true
validation: {
  itemValidation:{} // for validate item in array
},
},
},
},
}
{  
}
fieldName: "outputUnits",
</syntaxhighlight>
type: "arrayMixed",
* One transform units task
requiredOnCreate: true
 
validation: {
<syntaxhighlight lang="JavaScript">
  itemValidation:{} // for validate item in array
{
nodeLabel: "{transformUnitsLib.TRANSFORM_UNITS_CONFIG_GRAPH_NODE_LABEL}", // transformUnitsConfig
schema: {
identifier: true,
restrictProperties: true,
restrictRelationships: true,
properties: {
transformConfigId: {
identifier: true, //(random uuid)
},
},
},
},
}
},
}
identifiers: [
{
fieldName: "transformConfigId"
},
]
},
</syntaxhighlight>
</syntaxhighlight>
* Main Transform Confirm object
=== Versioned data ===
==== transformUnitsConfigSettings ====
* At the moment a stage Adjustment can only be edited by the user who created it (use standard createdBy relationship), in future could make RBAC per stageAdjustment


<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
{
{
properties: {
objectType: "unitTransform",
inputUnitTypes: [],
storageResources:{
outputUnitTypes: [],
"xxx":{
storageType: "graph",
graphServerTag: "xx",
}
},
fieldNames: {
unitTransformId: {
type: "string",
            randomOnCreate: true
},
},
},
}
identifiers: [
{
fieldName: "unitTransformId"
},
]
},
</syntaxhighlight>
</syntaxhighlight>


=== Relationships ===
= relationshipSchemas =  


<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
{
[
relationshipType: "{transformUnitsLib.TO_TRANSFORM_TRANSFORM_UNITS_FROM_GRAPH_REL_TYPE}", // "toTransform_TransformUnits"
  {
schema: {
    "oldTransformUnitTrackedProperty": {
elementCanBeRemoved: false,
      storageResources:{
allPropertiesImmutable: true,
"xxx":{
restrictProperties: true,
storageType: "graph",
properties: {
graphServerTag: "xx",
originTimestamp: //timestamp the request to create/change this relationship was sent
}
},
      },
}
      links: [
}
        {
          storageResourceTags: ["xxx"],
          from: {
            objType: {
              serviceTag: "transformUnits",
              objectType: "unitTransform"
            },
            linkType: "one",
          },
          to: {
            objType: {
              serviceTag: "unitType",
              objectType: "unitTrackedProperty"
            },
            linkType: "many",
          }
        }
      ]
    }
  }
]
</syntaxhighlight>
</syntaxhighlight>
* connects inputUnits to transformUnits node
<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
{
[
relationshipType: "{transformUnitsLib.FROM_TRANSFORM_TRANSFORM_UNITS_FROM_GRAPH_REL_TYPE}", // "fromTransform_TransformUnits"
  {
schema: {
    "newTransformUnitTrackedProperty": {
elementCanBeRemoved: false,
      storageResources:{
allPropertiesImmutable: true,
"xxx":{
restrictProperties: true,
storageType: "graph",
properties: {
graphServerTag: "xx",
originTimestamp: //timestamp the request to create/change this relationship was sent
}
},
      },
}
fieldNames: {
}
adjustmentValue: {
type: "string", // so can adjust any type of property, will need cast type in logic
requiredOnCreate: true,
},
},       
      links: [
        {
          storageResourceTags: ["xxx"],
          from: {
            objType: {
              serviceTag: "transformUnits",
              objectType: "unitTransform"
            },
            linkType: "one",
          },
          to: {
            objType: {
              serviceTag: "unitType",
              objectType: "unitTrackedProperty"
            },
            linkType: "many",
          }
        }
      ]
    }
  }
]
</syntaxhighlight>
</syntaxhighlight>
* connects outputUnits to transformUnits node
 
* links unitTransform event to all before and after unitTrackedProperties that are adjusted by this transformation


= Working documents =
= Working documents =

Revision as of 12:18, 16 February 2025

Overview

Transformations are used to convert any number of inputUnits into any number of outputUnits.

Repository

https://bitbucket.org/izara-supply-units/izara-supply-units-transform-units

Calculating values

combinationEquation and calculateByProperties fields replace out special tags first then parse through an evaluation library, eg:

Must make sure equation is validated/sanitized first, be careful of eval or new Function which can invoke arbritrary commands

Transform request

  • transformConfig is primarily used as a default template, each unitTransform can adjust almost all details such as units inputted and outputted propertyValues
  • user can use the transformConfig inputUnits calculatedProperties to recalc inputUnit property values according to the outputUnits numberOfUnits
  • user can set to recalculate outputUnits calculatedProperties according to inputUnits property values
  • inputUnits array can have multiple elements with the same unitTypeId, this is a guide for frontend to allow multiple units of the same unitType but eg different fabrics to be inputs in the same transformation
  • outputUnit calculatedProperties will calculate the same for all outputUnit numberOfUnits, can be adjusted manually
  • outputUnits array can have multiple elements with the same unitTypeId for eg calcualting cost for sizes of different clothing)

transformConfigInputOutput

{
	inputUnits: [
		{
			unitTypeId: "zz",
			stageId: "xx", // optional, stage that applies stageAdjustments to input unit
			numberOfUnits: 1, // default number of inputUnits used to perform this transform
			calculatedProperties:{ // calculates changes to inputUnits unitTrackedProperties
				unitTypeTrackedPropertyId: {
					integerOnly: false, // default false, if true then calculated splits must be integers, will not process transformation if split per outputUnit are not integers
					quantityReductionSetting: "spread"|"clearPerUnit", // if not all quantityUnitTypeTrackedPropertyId are used up in transformation, do we reduce all numberOfUnits quantity equally, or clear quantity out one unit at a time
					valueUsePerOutputUnit: 1,
				},
				// ...
			},
		}, 
		//..
	],
	outputUnits: [
		{
			unitTypeId: "zz",
			stageId: "xx", // optional, stage that applies stageAdjustments to output unit
			numberOfUnits: 1, // default number of outputUnits to create, calculations are based on this and inputUnit numberOfUnits
			calculatedProperties: { // calculates value for unitTypeTrackedProperties added to outputUnits
				unitTypeTrackedPropertyId: {
					calculationType: "calculateValue", // can add other types later
					combinationEquation: "[calcPropTagA]+[calcPropTagB]", // optional, method of combining multiple calculateByProperties together, defaults to adding/sum
					calculateByProperties: {
						calcPropTag: { // any string, used in combinationEquation
							calculation: "[valueUsePerOutputUnit:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]*[unitTypeTrackedProperty:{inputUnitTypeId}:{unitTypeTrackedPropertyId}]",
							
							or
							
							defaultValue: "30", // not calculated, user can overwrite defaultValue
							
							round: "round", //optional: "round"|"ceil"|"floor"|..
							
						},
						// ..
					}
				}, 
				//..
			}
		}, 
		//..
	],

}

objectSchemas

{
	objectType: "transformConfig",
		addOnDataStructure: [
			{
				type:"versionedData",
				versionedDataLabel: "transformConfigInputOutput",
				storageResourceTag : "xx",
				fieldNames: [
					{ 
						fieldName: "inputUnits",
					},
					{ 
						fieldName: "outputUnits",
					},
				]
			},
		],
		storageResources:{
		"xxx":{
			storageType: "graph",
			graphServerTag: "xx",
		}
	},
	fieldNames: {
		transformConfigId: {
			type: "string",
            randomOnCreate: true
		},
		{ 
			fieldName: "inputUnits",
			type: "arrayMixed",
			requiredOnCreate: true
			validation: {
			  itemValidation:{} // for validate item in array
			},
		},
		{ 
			fieldName: "outputUnits",
			type: "arrayMixed",
			requiredOnCreate: true
			validation: {
			  itemValidation:{} // for validate item in array
			},
		},
	},
	identifiers: [
		{
			fieldName: "transformConfigId"
		},
	]
},
{
	objectType: "unitTransform",
	storageResources:{
		"xxx":{
			storageType: "graph",
			graphServerTag: "xx",
		}
	},
	fieldNames: {
		unitTransformId: {
			type: "string",
            randomOnCreate: true
		},
	},
	identifiers: [
		{
			fieldName: "unitTransformId"
		},
	]
},

relationshipSchemas

[
  {
    "oldTransformUnitTrackedProperty": {
      storageResources:{
		"xxx":{
			storageType: "graph",
			graphServerTag: "xx",
		}
      },
      links: [
        {
          storageResourceTags: ["xxx"],
          from: {
            objType: {
              serviceTag: "transformUnits",
              objectType: "unitTransform"
            },
            linkType: "one",
          },
          to: {
            objType: {
              serviceTag: "unitType",
              objectType: "unitTrackedProperty"
            },
            linkType: "many",
          }
        }
      ]
    }
  }
]
[
  {
    "newTransformUnitTrackedProperty": {
      storageResources:{
		"xxx":{
			storageType: "graph",
			graphServerTag: "xx",
		}
      },
		fieldNames: {
			adjustmentValue: {
				type: "string", // so can adjust any type of property, will need cast type in logic
				requiredOnCreate: true,
			},
		},         
      links: [
        {
          storageResourceTags: ["xxx"],
          from: {
            objType: {
              serviceTag: "transformUnits",
              objectType: "unitTransform"
            },
            linkType: "one",
          },
          to: {
            objType: {
              serviceTag: "unitType",
              objectType: "unitTrackedProperty"
            },
            linkType: "many",
          }
        }
      ]
    }
  }
]
  • links unitTransform event to all before and after unitTrackedProperties that are adjusted by this transformation

Working documents

Transform Units