Skip to content

Commit

Permalink
add counter
Browse files Browse the repository at this point in the history
  • Loading branch information
weibaohui committed Oct 5, 2023
1 parent 32da7fa commit c9538ec
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/backend/k8s/Counter/Counter.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ResType } from '@backend/k8s/watch/watch.model'
import { WatchService } from '@backend/k8s/watch/watch.service'
import { Injectable, Logger } from '@nestjs/common'
import { ClientService } from '@backend/k8s/client/client.service'
import { Cron, CronExpression } from '@nestjs/schedule'

@Injectable()
export class CounterService {
private readonly logger = new Logger(CounterService.name)
private counter: Map<string, number> = new Map<string, number>()
private counterLine: Map<string, Array<number>> = new Map<string, Array<number>>()
constructor(
public clientService: ClientService,
public watchService: WatchService,
) {
this.startCounter()
}

private startCounter() {
for (const key in ResType) {
const k = ResType[key]
this.watchService.watch(k, (d) => {
const v = this.counter.get(k) ? this.counter.get(k) : 0
switch (d.type) {
case 'ADDED':
this.counter.set(k, v + 1)
break
case 'DELETED':
this.counter.set(k, v - 1)
break
}
})
}
}

@Cron(CronExpression.EVERY_SECOND)
printCounter() {
this.counterLine.forEach((v, k) => {
this.logger.debug(`${k}=${v}`)
})
}

@Cron(CronExpression.EVERY_SECOND)
captureCounter() {
this.counter.forEach((v, k) => {
const array = this.counterLine.get(k) ? this.counterLine.get(k) : []
array.push(v)
if (array.length > 10)
array.shift()

this.counterLine.set(k, array)
})
}
}
2 changes: 2 additions & 0 deletions src/backend/k8s/k8s.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClusterRoleBindingController } from '@backend/k8s/ClusterRoleBinding/Cl
import { ClusterRoleBindingService } from '@backend/k8s/ClusterRoleBinding/ClusterRoleBinding.service'
import { ConfigmapController } from '@backend/k8s/configmap/configmap.controller'
import { ConfigMapService } from '@backend/k8s/configmap/configmap.service'
import { CounterService } from '@backend/k8s/Counter/Counter.service'
import { CronJobController } from '@backend/k8s/cronjob/cronjob.controller'
import { CronJobService } from '@backend/k8s/cronjob/cronjob.service'
import { DaemonSetController } from '@backend/k8s/daemonset/daemonset.controller'
Expand Down Expand Up @@ -104,6 +105,7 @@ import { ReplicaSetController } from '@backend/k8s/replicaset/replicaset.control
PersistentVolumeService, PersistentVolumeClaimService, ClusterRoleService,
ClusterRoleBindingService, RoleService, RoleBindingService,
ServiceAccountService, MutatingWebhookService, ValidatingWebhookService,
CounterService,
],
exports: [K8sService],
})
Expand Down
2 changes: 2 additions & 0 deletions src/backend/k8s/k8s.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClusterRoleService } from '@backend/k8s/ClusterRole/ClusterRole.service'
import { ClusterRoleBindingService } from '@backend/k8s/ClusterRoleBinding/ClusterRoleBinding.service'
import { ConfigMapService } from '@backend/k8s/configmap/configmap.service'
import { CounterService } from '@backend/k8s/Counter/Counter.service'
import { CronJobService } from '@backend/k8s/cronjob/cronjob.service'
import { DaemonSetService } from '@backend/k8s/daemonset/daemonset.service'
import { DeploymentService } from '@backend/k8s/deployment/deployment.service'
Expand Down Expand Up @@ -75,5 +76,6 @@ export class K8sService {
public serviceAccountService: ServiceAccountService,
public mutatingWebhookService: MutatingWebhookService,
public validatingWebhookService: ValidatingWebhookService,
public counterService: CounterService,
) {}
}

0 comments on commit c9538ec

Please sign in to comment.