Skip to content

Commit

Permalink
Improve run billing estimation (#3201) - GUI, consider instance start…
Browse files Browse the repository at this point in the history
… date for run price estimation, backported to stage/0.16/uat
  • Loading branch information
rodichenko committed Apr 20, 2023
1 parent 32aeaca commit ba9b0cf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
3 changes: 3 additions & 0 deletions client/src/components/runs/logs/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,9 @@ class Logs extends localization.LocalizedReactComponent {
let price;
if (pricePerHour) {
const adjustPrice = (value) => {
if (value === 0) {
return 0;
}
let cents = Math.ceil(value * 100);
if (cents < 1) {
cents = 1;
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/search/preview/PipelineRunPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ export default class PipelineRunPreview extends React.Component {
lineHeight: '20px'
};
const adjustPrice = (value) => {
if (value === 0) {
return 0;
}
let cents = Math.ceil(value * 100);
if (cents < 1) {
cents = 1;
Expand Down
8 changes: 4 additions & 4 deletions client/src/utils/evaluate-run-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export default function evaluateRunPrice (
};
}
const {
totalDuration,
totalNonPausedDuration
totalBillableDuration,
totalBillableRunningDuration
} = getRunDurationInfo(
run,
analyseSchedulingPhase,
Expand All @@ -59,8 +59,8 @@ export default function evaluateRunPrice (
workersPrice = 0
} = run;
const format = (value) => Math.ceil(value * 100.0) / 100.0;
const master = computePricePerHour * (totalNonPausedDuration / SECONDS_IN_HOUR) +
diskPricePerHour * (totalDuration / SECONDS_IN_HOUR);
const master = computePricePerHour * (totalBillableRunningDuration / SECONDS_IN_HOUR) +
diskPricePerHour * (totalBillableDuration / SECONDS_IN_HOUR);
return {
master: format(master),
workers: format(workersPrice),
Expand Down
48 changes: 47 additions & 1 deletion client/src/utils/run-duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export {RunHistoryPhase};
* @property {boolean} wasPaused - if run was ever paused
* @property {number} totalDuration - total duration (from job submission) in seconds
* @property {number} totalRunningDuration - total running duration in seconds
* @property {number} totalBillableDuration - total billable duration in seconds
* @property {number} totalBillableRunningDuration - total billable and running duration in seconds
* (duration after initialization, including paused intervals)
* @property {number} totalNonPausedDuration - total non-paused duration in seconds
* (scheduling stages, running stages)
Expand Down Expand Up @@ -119,6 +121,35 @@ function getIntervalsTotalDuration (intervals = []) {
.reduce((duration, interval) => duration + getIntervalDuration(interval), 0);
}

/**
* @param {string} fromDate
* @param {RunInterval[]} intervals
* @param {RunHistoryPhase} phase
* @returns {number}
*/
function getRunningDuration (fromDate, intervals, ...phase) {
if (!fromDate) {
return 0;
}
const date = moment.utc(fromDate);
const filtered = (intervals || [])
.filter((interval) => phase.includes(interval.phase))
.filter((interval) => interval.start >= date || (!interval.end || interval.end > date))
.map((interval) => {
const {
start
} = interval;
if (start >= date) {
return interval;
}
return {
...interval,
start: date
};
});
return getIntervalsTotalDuration(filtered);
}

/**
* Gets run duration info (running, paused, paused intervals etc.)
* @param {RunInfo} run
Expand Down Expand Up @@ -163,6 +194,19 @@ export default function getRunDurationInfo (
.filter((interval) => interval.phase === RunHistoryPhase.paused);
const scheduledIntervals = filteredIntervals
.filter((interval) => interval.phase === RunHistoryPhase.scheduled);
const totalBillableDuration = getRunningDuration(
run.instanceStartDate,
filteredIntervals,
RunHistoryPhase.running,
RunHistoryPhase.paused,
RunHistoryPhase.scheduled
);
const totalBillableRunningDuration = getRunningDuration(
run.instanceStartDate,
filteredIntervals,
RunHistoryPhase.running,
RunHistoryPhase.scheduled
);
const activeDuration = getIntervalsTotalDuration(runningIntervals);
const pausedDuration = getIntervalsTotalDuration(pausedIntervals);
const schedulingDuration = getIntervalsTotalDuration(scheduledIntervals);
Expand All @@ -184,6 +228,8 @@ export default function getRunDurationInfo (
runningIntervals,
scheduledIntervals,
runningDate,
scheduledDate
scheduledDate,
totalBillableDuration,
totalBillableRunningDuration
};
}
1 change: 1 addition & 0 deletions client/src/utils/run-duration/run-statuses-timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import moment from 'moment-timezone';
* @typedef {object} RunInfo
* @property {string} [startDate]
* @property {string} [endDate]
* @property {string} [instanceStartDate]
* @property {string} [status]
* @property {RunStatusInfo[]} [runStatuses]
*/
Expand Down

0 comments on commit ba9b0cf

Please sign in to comment.