Skip to content

[usage] Rename ReconcileUsageWithLedger to ReconcileUsage #12917

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

Merged
merged 1 commit into from
Sep 13, 2022
Merged
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
353 changes: 175 additions & 178 deletions components/usage-api/go/v1/usage.pb.go

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions components/usage-api/go/v1/usage_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 33 additions & 33 deletions components/usage-api/typescript/src/usage/v1/usage.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Timestamp } from "../../google/protobuf/timestamp.pb";

export const protobufPackage = "usage.v1";

export interface ReconcileUsageWithLedgerRequest {
export interface ReconcileUsageRequest {
/** from specifies the starting time range for this request. */
from:
| Date
Expand All @@ -21,7 +21,7 @@ export interface ReconcileUsageWithLedgerRequest {
to: Date | undefined;
}

export interface ReconcileUsageWithLedgerResponse {
export interface ReconcileUsageResponse {
}

export interface PaginatedRequest {
Expand Down Expand Up @@ -233,12 +233,12 @@ export function costCenter_BillingStrategyToNumber(object: CostCenter_BillingStr
}
}

function createBaseReconcileUsageWithLedgerRequest(): ReconcileUsageWithLedgerRequest {
function createBaseReconcileUsageRequest(): ReconcileUsageRequest {
return { from: undefined, to: undefined };
}

export const ReconcileUsageWithLedgerRequest = {
encode(message: ReconcileUsageWithLedgerRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
export const ReconcileUsageRequest = {
encode(message: ReconcileUsageRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.from !== undefined) {
Timestamp.encode(toTimestamp(message.from), writer.uint32(10).fork()).ldelim();
}
Expand All @@ -248,10 +248,10 @@ export const ReconcileUsageWithLedgerRequest = {
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): ReconcileUsageWithLedgerRequest {
decode(input: _m0.Reader | Uint8Array, length?: number): ReconcileUsageRequest {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseReconcileUsageWithLedgerRequest();
const message = createBaseReconcileUsageRequest();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
Expand All @@ -269,41 +269,41 @@ export const ReconcileUsageWithLedgerRequest = {
return message;
},

fromJSON(object: any): ReconcileUsageWithLedgerRequest {
fromJSON(object: any): ReconcileUsageRequest {
return {
from: isSet(object.from) ? fromJsonTimestamp(object.from) : undefined,
to: isSet(object.to) ? fromJsonTimestamp(object.to) : undefined,
};
},

toJSON(message: ReconcileUsageWithLedgerRequest): unknown {
toJSON(message: ReconcileUsageRequest): unknown {
const obj: any = {};
message.from !== undefined && (obj.from = message.from.toISOString());
message.to !== undefined && (obj.to = message.to.toISOString());
return obj;
},

fromPartial(object: DeepPartial<ReconcileUsageWithLedgerRequest>): ReconcileUsageWithLedgerRequest {
const message = createBaseReconcileUsageWithLedgerRequest();
fromPartial(object: DeepPartial<ReconcileUsageRequest>): ReconcileUsageRequest {
const message = createBaseReconcileUsageRequest();
message.from = object.from ?? undefined;
message.to = object.to ?? undefined;
return message;
},
};

function createBaseReconcileUsageWithLedgerResponse(): ReconcileUsageWithLedgerResponse {
function createBaseReconcileUsageResponse(): ReconcileUsageResponse {
return {};
}

export const ReconcileUsageWithLedgerResponse = {
encode(_: ReconcileUsageWithLedgerResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
export const ReconcileUsageResponse = {
encode(_: ReconcileUsageResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): ReconcileUsageWithLedgerResponse {
decode(input: _m0.Reader | Uint8Array, length?: number): ReconcileUsageResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseReconcileUsageWithLedgerResponse();
const message = createBaseReconcileUsageResponse();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
Expand All @@ -315,17 +315,17 @@ export const ReconcileUsageWithLedgerResponse = {
return message;
},

fromJSON(_: any): ReconcileUsageWithLedgerResponse {
fromJSON(_: any): ReconcileUsageResponse {
return {};
},

toJSON(_: ReconcileUsageWithLedgerResponse): unknown {
toJSON(_: ReconcileUsageResponse): unknown {
const obj: any = {};
return obj;
},

fromPartial(_: DeepPartial<ReconcileUsageWithLedgerResponse>): ReconcileUsageWithLedgerResponse {
const message = createBaseReconcileUsageWithLedgerResponse();
fromPartial(_: DeepPartial<ReconcileUsageResponse>): ReconcileUsageResponse {
const message = createBaseReconcileUsageResponse();
return message;
},
};
Expand Down Expand Up @@ -1053,12 +1053,12 @@ export const UsageServiceDefinition = {
responseStream: false,
options: {},
},
/** Triggers reconciliation of usage with ledger implementation. */
reconcileUsageWithLedger: {
name: "ReconcileUsageWithLedger",
requestType: ReconcileUsageWithLedgerRequest,
/** Triggers reconciliation of usage. */
reconcileUsage: {
name: "ReconcileUsage",
requestType: ReconcileUsageRequest,
requestStream: false,
responseType: ReconcileUsageWithLedgerResponse,
responseType: ReconcileUsageResponse,
responseStream: false,
options: {},
},
Expand All @@ -1085,11 +1085,11 @@ export interface UsageServiceServiceImplementation<CallContextExt = {}> {
request: SetCostCenterRequest,
context: CallContext & CallContextExt,
): Promise<DeepPartial<SetCostCenterResponse>>;
/** Triggers reconciliation of usage with ledger implementation. */
reconcileUsageWithLedger(
request: ReconcileUsageWithLedgerRequest,
/** Triggers reconciliation of usage. */
reconcileUsage(
request: ReconcileUsageRequest,
context: CallContext & CallContextExt,
): Promise<DeepPartial<ReconcileUsageWithLedgerResponse>>;
): Promise<DeepPartial<ReconcileUsageResponse>>;
/** ListUsage retrieves all usage for the specified attributionId and theb given time range */
listUsage(request: ListUsageRequest, context: CallContext & CallContextExt): Promise<DeepPartial<ListUsageResponse>>;
}
Expand All @@ -1105,11 +1105,11 @@ export interface UsageServiceClient<CallOptionsExt = {}> {
request: DeepPartial<SetCostCenterRequest>,
options?: CallOptions & CallOptionsExt,
): Promise<SetCostCenterResponse>;
/** Triggers reconciliation of usage with ledger implementation. */
reconcileUsageWithLedger(
request: DeepPartial<ReconcileUsageWithLedgerRequest>,
/** Triggers reconciliation of usage. */
reconcileUsage(
request: DeepPartial<ReconcileUsageRequest>,
options?: CallOptions & CallOptionsExt,
): Promise<ReconcileUsageWithLedgerResponse>;
): Promise<ReconcileUsageResponse>;
/** ListUsage retrieves all usage for the specified attributionId and theb given time range */
listUsage(request: DeepPartial<ListUsageRequest>, options?: CallOptions & CallOptionsExt): Promise<ListUsageResponse>;
}
Expand Down
8 changes: 4 additions & 4 deletions components/usage-api/usage/v1/usage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ service UsageService {
// SetCostCenter stores the given cost center
rpc SetCostCenter(SetCostCenterRequest) returns (SetCostCenterResponse) {}

// Triggers reconciliation of usage with ledger implementation.
rpc ReconcileUsageWithLedger(ReconcileUsageWithLedgerRequest) returns (ReconcileUsageWithLedgerResponse) {}
// Triggers reconciliation of usage.
rpc ReconcileUsage(ReconcileUsageRequest) returns (ReconcileUsageResponse) {}

// ListUsage retrieves all usage for the specified attributionId and theb given time range
rpc ListUsage(ListUsageRequest) returns (ListUsageResponse) {}
}

message ReconcileUsageWithLedgerRequest {
message ReconcileUsageRequest {
// from specifies the starting time range for this request.
google.protobuf.Timestamp from = 1;

// to specifies the end time range for this request.
google.protobuf.Timestamp to = 2;
}

message ReconcileUsageWithLedgerResponse {}
message ReconcileUsageResponse {}

message PaginatedRequest {
int64 per_page = 1;
Expand Down
8 changes: 4 additions & 4 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (s *UsageService) SetCostCenter(ctx context.Context, in *v1.SetCostCenterRe
return &v1.SetCostCenterResponse{}, nil
}

func (s *UsageService) ReconcileUsageWithLedger(ctx context.Context, req *v1.ReconcileUsageWithLedgerRequest) (*v1.ReconcileUsageWithLedgerResponse, error) {
func (s *UsageService) ReconcileUsage(ctx context.Context, req *v1.ReconcileUsageRequest) (*v1.ReconcileUsageResponse, error) {
from := req.GetFrom().AsTime()
to := req.GetTo().AsTime()

Expand Down Expand Up @@ -259,7 +259,7 @@ func (s *UsageService) ReconcileUsageWithLedger(ctx context.Context, req *v1.Rec

// now has to be computed after we've collected all data, to ensure that it's always greater than any of the records we fetch
now := s.nowFunc()
inserts, updates, err := reconcileUsageWithLedger(instances, usageDrafts, s.pricer, now)
inserts, updates, err := reconcileUsage(instances, usageDrafts, s.pricer, now)
if err != nil {
logger.WithError(err).Errorf("Failed to reconcile usage with ledger.")
return nil, status.Errorf(codes.Internal, "Failed to reconcile usage with ledger.")
Expand All @@ -284,10 +284,10 @@ func (s *UsageService) ReconcileUsageWithLedger(ctx context.Context, req *v1.Rec
logger.Infof("Updated %d Usage records in the database.", len(updates))
}

return &v1.ReconcileUsageWithLedgerResponse{}, nil
return &v1.ReconcileUsageResponse{}, nil
}

func reconcileUsageWithLedger(instances []db.WorkspaceInstanceForUsage, drafts []db.Usage, pricer *WorkspacePricer, now time.Time) (inserts []db.Usage, updates []db.Usage, err error) {
func reconcileUsage(instances []db.WorkspaceInstanceForUsage, drafts []db.Usage, pricer *WorkspacePricer, now time.Time) (inserts []db.Usage, updates []db.Usage, err error) {

instancesByID := dedupeWorkspaceInstancesForUsage(instances)

Expand Down
14 changes: 7 additions & 7 deletions components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"gorm.io/gorm"
)

func TestUsageService_ReconcileUsageWithLedger(t *testing.T) {
func TestUsageService_ReconcileUsage(t *testing.T) {
dbconn := dbtest.ConnectForTests(t)
from := time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)
to := time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestUsageService_ReconcileUsageWithLedger(t *testing.T) {

client := newUsageService(t, dbconn)

_, err := client.ReconcileUsageWithLedger(context.Background(), &v1.ReconcileUsageWithLedgerRequest{
_, err := client.ReconcileUsage(context.Background(), &v1.ReconcileUsageRequest{
From: timestamppb.New(from),
To: timestamppb.New(to),
})
Expand Down Expand Up @@ -89,7 +89,7 @@ func newUsageService(t *testing.T, dbconn *gorm.DB) v1.UsageServiceClient {
return client
}

func TestReconcileWithLedger(t *testing.T) {
func TestReconcile(t *testing.T) {
now := time.Date(2022, 9, 1, 10, 0, 0, 0, time.UTC)
pricer, err := NewWorkspacePricer(map[string]float64{
"default": 0.1666666667,
Expand All @@ -102,15 +102,15 @@ func TestReconcileWithLedger(t *testing.T) {
require.NoError(t, err)

t.Run("no action with no instances and no drafts", func(t *testing.T) {
inserts, updates, err := reconcileUsageWithLedger(nil, nil, pricer, now)
inserts, updates, err := reconcileUsage(nil, nil, pricer, now)
require.NoError(t, err)
require.Len(t, inserts, 0)
require.Len(t, updates, 0)
})

t.Run("no action with no instances but existing drafts", func(t *testing.T) {
drafts := []db.Usage{dbtest.NewUsage(t, db.Usage{})}
inserts, updates, err := reconcileUsageWithLedger(nil, drafts, pricer, now)
inserts, updates, err := reconcileUsage(nil, drafts, pricer, now)
require.NoError(t, err)
require.Len(t, inserts, 0)
require.Len(t, updates, 0)
Expand All @@ -131,7 +131,7 @@ func TestReconcileWithLedger(t *testing.T) {
StartedTime: db.NewVarcharTime(now.Add(1 * time.Minute)),
}

inserts, updates, err := reconcileUsageWithLedger([]db.WorkspaceInstanceForUsage{instance, instance}, nil, pricer, now)
inserts, updates, err := reconcileUsage([]db.WorkspaceInstanceForUsage{instance, instance}, nil, pricer, now)
require.NoError(t, err)
require.Len(t, inserts, 1)
require.Len(t, updates, 0)
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestReconcileWithLedger(t *testing.T) {
Metadata: nil,
})

inserts, updates, err := reconcileUsageWithLedger([]db.WorkspaceInstanceForUsage{instance}, []db.Usage{draft}, pricer, now)
inserts, updates, err := reconcileUsage([]db.WorkspaceInstanceForUsage{instance}, []db.Usage{draft}, pricer, now)
require.NoError(t, err)
require.Len(t, inserts, 0)
require.Len(t, updates, 1)
Expand Down
2 changes: 1 addition & 1 deletion components/usage/pkg/scheduler/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r *LedgerJob) Run() (err error) {
WithField("to", now)

logger.Info("Running ledger job. Reconciling usage records.")
_, err = r.usageClient.ReconcileUsageWithLedger(ctx, &v1.ReconcileUsageWithLedgerRequest{
_, err = r.usageClient.ReconcileUsage(ctx, &v1.ReconcileUsageRequest{
From: timestamppb.New(hourAgo),
To: timestamppb.New(now),
})
Expand Down
2 changes: 1 addition & 1 deletion components/usage/pkg/scheduler/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *fakeUsageClient) SetCostCenter(ctx context.Context, in *v1.SetCostCente
}

// Triggers reconciliation of usage with ledger implementation.
func (c *fakeUsageClient) ReconcileUsageWithLedger(ctx context.Context, in *v1.ReconcileUsageWithLedgerRequest, opts ...grpc.CallOption) (*v1.ReconcileUsageWithLedgerResponse, error) {
func (c *fakeUsageClient) ReconcileUsage(ctx context.Context, in *v1.ReconcileUsageRequest, opts ...grpc.CallOption) (*v1.ReconcileUsageResponse, error) {
atomic.AddInt32(&c.ReconcileUsageWithLedgerCallCount, 1)
time.Sleep(50 * time.Millisecond)

Expand Down