Skip to content

Commit

Permalink
refactor(server): refactor billing (#1648)
Browse files Browse the repository at this point in the history
* refactor(server): refactor billing

* fix
  • Loading branch information
0fatal committed Nov 7, 2023
1 parent 1ffa9ba commit 4bf3428
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 92 deletions.
2 changes: 0 additions & 2 deletions build/charts/laf-server/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ spec:
value: {{ .Values.default_region.fixed_namespace | quote}}
- name: DATABASE_URL
value: {{ .Values.databaseUrl | quote}}
- name: METERING_DATABASE_URL
value: {{ .Values.meteringDatabaseUrl | quote}}
- name: JWT_SECRET
value: {{ .Values.jwt.secret | quote}}
- name: API_SERVER_URL
Expand Down
1 change: 0 additions & 1 deletion build/charts/laf-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

apiServerHost: ""
databaseUrl: ""
meteringDatabaseUrl: ""
apiServerUrl: ""
siteName: "laf"
# init default region conf
Expand Down
8 changes: 1 addition & 7 deletions build/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ set -e
set -x

DATABASE_URL="mongodb://${DB_USERNAME:-admin}:${PASSWD_OR_SECRET}@mongodb-0.mongo.${NAMESPACE}.svc.cluster.local:27017/sys_db?authSource=admin&replicaSet=rs0&w=majority"
METERING_DATABASE_URL="mongodb://${DB_USERNAME:-admin}:${PASSWD_OR_SECRET}@mongodb-0.mongo.${NAMESPACE}.svc.cluster.local:27017/sealos-resources?authSource=admin&replicaSet=rs0&w=majority"
helm install mongodb -n ${NAMESPACE} \
--set db.username=${DB_USERNAME:-admin} \
--set db.password=${PASSWD_OR_SECRET} \
Expand Down Expand Up @@ -79,7 +78,6 @@ LOG_SERVER_SECRET=$PASSWD_OR_SECRET
RUNTIME_EXPORTER_SECRET=$PASSWD_OR_SECRET
helm install server -n ${NAMESPACE} \
--set databaseUrl=${DATABASE_URL} \
--set meteringDatabaseUrl=${METERING_DATABASE_URL} \
--set jwt.secret=${SERVER_JWT_SECRET} \
--set apiServerHost=api.${DOMAIN} \
--set apiServerUrl=${EXTERNAL_HTTP_SCHEMA}://api.${DOMAIN} \
Expand All @@ -101,11 +99,7 @@ helm install server -n ${NAMESPACE} \
$([ "$ENABLE_MONITOR" = "true" ] && echo "--set default_region.prometheus_url=${PROMETHEUS_URL}") \
./charts/laf-server

## 6. install metering service
sealos run docker.io/labring/sealos-cloud-resources-controller:latest --env MONGO_URI=${METERING_DATABASE_URL} --env DEFAULT_NAMESPACE=resources-system
sealos run docker.io/labring/sealos-cloud-resources-metering-controller:latest --env MONGO_URI=${METERING_DATABASE_URL} --env DEFAULT_NAMESPACE=resources-system

## 7. install laf-web
## 6. install laf-web
helm install web -n ${NAMESPACE} \
--set domain=www.${DOMAIN} \
./charts/laf-web
2 changes: 0 additions & 2 deletions deploy/install-on-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ sealos pull labring/openebs:v1.9.0
sealos pull labring/cert-manager:v1.8.0
sealos pull labring/metrics-server:v0.6.2
sealos pull lafyun/laf:latest
sealos pull docker.io/labring/sealos-cloud-resources-metering-controller:latest
sealos pull docker.io/labring/sealos-cloud-resources-controller:latest
sealos pull docker.io/labring/ingress-nginx:v1.8.1

# install k8s cluster
Expand Down
33 changes: 7 additions & 26 deletions server/src/billing/billing-creation-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ApplicationBilling,
ApplicationBillingState,
} from './entities/application-billing'
import { MeteringDatabase } from './metering-database'
import { CalculatePriceDto } from './dto/calculate-price.dto'
import { BillingService } from './billing.service'
import { ApplicationBundle } from 'src/application/entities/application-bundle'
Expand Down Expand Up @@ -110,17 +109,19 @@ export class BillingCreationTaskService {

// determine latest billing time & next metering time
const latestBillingTime = await this.getLatestBillingTime(appid)
const nextMeteringTime = await this.determineNextMeteringTime(
appid,
latestBillingTime,
const nextMeteringTime = new Date(
latestBillingTime.getTime() + 1000 * 60 * 60,
)

if (!nextMeteringTime) {
if (nextMeteringTime > new Date()) {
this.logger.warn(`No next metering time for application: ${appid}`)
return
}

const meteringData = await this.billing.getMeteringData(app)
const meteringData = await this.billing.getMeteringData(
app,
nextMeteringTime,
)

// get application bundle
const bundle = await this.bundleService.findOne(appid)
Expand Down Expand Up @@ -200,26 +201,6 @@ export class BillingCreationTaskService {
return dto
}

private async determineNextMeteringTime(
appid: string,
latestBillingTime: Date,
) {
const db = MeteringDatabase.db
const nextMeteringData = await db
.collection('metering')
.findOne(
{ category: appid, time: { $gt: latestBillingTime } },
{ sort: { time: 1 } },
)

if (!nextMeteringData) {
this.logger.debug(`No next metering data for application: ${appid}`)
return null
}

return nextMeteringData.time as Date
}

private async getLatestBillingTime(appid: string) {
const db = SystemDatabase.db

Expand Down
4 changes: 3 additions & 1 deletion server/src/billing/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class BillingService {
}
}

async getMeteringData(app: Application) {
async getMeteringData(app: Application, time: Date) {
const region = await this.region.findOne(app.regionId)

const prom = new PrometheusDriver({
Expand All @@ -230,13 +230,15 @@ export class BillingService {
const cpuTask = prom
.instantQuery(
`sum(max_over_time(laf_runtime_cpu_limit{container!="",appid="${app.appid}"}[1h])) by (appid)`,
time,
)
.then((res) => res.result[0])
.then((res) => Number(res.value.value))

const memoryTask = prom
.instantQuery(
`sum(max_over_time(laf_runtime_memory_limit{container!="",appid="${app.appid}"}[1h])) by (appid)`,
time,
)
.then((res) => res.result[0])
.then((res) => Number(res.value.value))
Expand Down
46 changes: 0 additions & 46 deletions server/src/billing/metering-database.ts

This file was deleted.

7 changes: 0 additions & 7 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ export class ServerConfig {
return process.env.DATABASE_URL
}

static get METERING_DATABASE_URL() {
if (!process.env.METERING_DATABASE_URL) {
throw new Error('METERING_DATABASE_URL is not defined')
}
return process.env.METERING_DATABASE_URL
}

static get JWT_SECRET() {
if (!process.env.JWT_SECRET) {
throw new Error('JWT_SECRET is not defined')
Expand Down

0 comments on commit 4bf3428

Please sign in to comment.