-
Notifications
You must be signed in to change notification settings - Fork 68
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: support usage based pricing #1787
base: main
Are you sure you want to change the base?
Conversation
8b23ff1
to
dc37609
Compare
de2df86
to
f38a546
Compare
2c527ea
to
e85d325
Compare
import "time" | ||
|
||
const ( | ||
DefaultMeterResolution = time.Minute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be removed as soon as we have better granularity.
postSplitAtLine := l.CloneForCreate(UpdateInput{ | ||
ParentLine: &parentLine, | ||
Status: billingentity.InvoiceLineStatusValid, | ||
PeriodStart: splitAt, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just double checking: is this is also splitAt and and previous ends at splitAt one should be exclusive when we query usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code assumes that (added the comment):
// Period represents a time period, in billing the time period is always interpreted as
// [start, end) (i.e. start is inclusive, end is exclusive).
This is aligned with the current logic where clickhouse's tumbleStart
is inclusive and tumbleEnd
is exclusive.
|
||
switch meter.Aggregation { | ||
case models.MeterAggregationSum, models.MeterAggregationCount, | ||
models.MeterAggregationMax, models.MeterAggregationUniqueCount: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure Max works here. Max of period chunk is different then max of whole period
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry, I was in a bit of rush for the meters, I have updated the docs and the algorithm.
Aggregation: in.Meter.Aggregation, | ||
FilterSubject: in.Subjects, | ||
From: &in.Line.Period.Start, | ||
To: &in.Line.Period.End, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feature has group by filters that should be applied here too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- refactor diff calculation - use jsonb for usage attribution - fix README.md typos - document period
Overview
Add line-splitting capability to support mid-period invoicing. See README.md for more detailed information.
Algorithm
To achieve the behavior described above, we are using line splitting. By default we would have one line per billing period that would eventually be part of an invoice:
When the usage-based line can be billed mid-period, we
split
the line into two:As visible:
valid
tosplit
: it will be ignored in any calculation, it becomes a grouping line between invoicesperiod.start
andasof
(time of invoicing): it will be addedd to the freshly created invoiceasof
andperiod.end
: it will be pushed to the gathering invoiceWhen creating a new invoice between
asof
andperiod.end
the same logic continues, but without marking SplitLine2split
, instead the new line is added to the original line's parent line:This flattening approach allows us to not to have to recusively traverse lines in the database.