Skip to content

[usage] Store credits as integer values in the usage table #11429

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

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class DBWorkspaceInstanceUsage {
@Index("ind_stoppedAt")
stoppedAt: string;

@Column("double")
@Column("bigint")
creditsUsed: number;

@Column()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";

export class New1658123371893 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX \`ind_stoppedAt\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP INDEX \`ind_startedAt\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP INDEX \`ind_attributionId\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP TABLE \`d_b_workspace_instance_usage\``);

await queryRunner.query(
`CREATE TABLE \`d_b_workspace_instance_usage\` (\`instanceId\` char(36) NOT NULL, \`attributionId\` varchar(255) NOT NULL, \`startedAt\` timestamp(6) NOT NULL, \`stoppedAt\` timestamp(6) NULL, \`creditsUsed\` bigint NOT NULL, \`generationId\` int NOT NULL, \`deleted\` tinyint NOT NULL, INDEX \`ind_attributionId\` (\`attributionId\`), INDEX \`ind_startedAt\` (\`startedAt\`), INDEX \`ind_stoppedAt\` (\`stoppedAt\`), PRIMARY KEY (\`instanceId\`)) ENGINE=InnoDB`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX \`ind_stoppedAt\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP INDEX \`ind_startedAt\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP INDEX \`ind_attributionId\` ON \`d_b_workspace_instance_usage\``);
await queryRunner.query(`DROP TABLE \`d_b_workspace_instance_usage\``);
}
}
2 changes: 1 addition & 1 deletion components/usage/pkg/controller/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func usageReportToUsageRecords(report UsageReport, pricer *WorkspacePricer, now
AttributionID: attributionId,
StartedAt: instance.CreationTime.Time(),
StoppedAt: stoppedAt,
CreditsUsed: float64(pricer.CreditsUsedByInstance(&instance, now)),
CreditsUsed: pricer.CreditsUsedByInstance(&instance, now),
GenerationId: 0,
Deleted: false,
})
Expand Down
2 changes: 1 addition & 1 deletion components/usage/pkg/db/workspace_instance_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type WorkspaceInstanceUsage struct {
AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"`
StartedAt time.Time `gorm:"column:startedAt;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"startedAt"`
StoppedAt sql.NullTime `gorm:"column:stoppedAt;type:timestamp;" json:"stoppedAt"`
CreditsUsed float64 `gorm:"column:creditsUsed;type:double;" json:"creditsUsed"`
CreditsUsed int64 `gorm:"column:creditsUsed;type:bigint;" json:"creditsUsed"`
GenerationId int `gorm:"column:generationId;type:int;" json:"generationId"`
Deleted bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
}
Expand Down