This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MetricsController } from './metrics.controller'; | ||
import { MetricsService } from './metrics.service'; | ||
|
||
describe('MetricsController', () => { | ||
let controller: MetricsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [MetricsController], | ||
providers: [MetricsService], | ||
}).compile(); | ||
|
||
controller = module.get<MetricsController>(MetricsController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { MetricsService } from './metrics.service'; | ||
|
||
@Controller('metrics') | ||
export class MetricsController { | ||
constructor(private readonly metricsService: MetricsService) {} | ||
|
||
@Get() | ||
getMetrics(): Promise<any> { | ||
return this.metricsService.getMetrics(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MetricsService } from './metrics.service'; | ||
import { MetricsController } from './metrics.controller'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { VodsRepository } from 'src/vods/vods.repository'; | ||
import { QueuesRepository } from 'src/queues/queues.repository'; | ||
import { ChannelsRepository } from 'src/channels/channels.repository'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([ | ||
VodsRepository, | ||
QueuesRepository, | ||
ChannelsRepository, | ||
]), | ||
], | ||
controllers: [MetricsController], | ||
providers: [MetricsService], | ||
}) | ||
export class MetricsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MetricsService } from './metrics.service'; | ||
|
||
describe('MetricsService', () => { | ||
let service: MetricsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [MetricsService], | ||
}).compile(); | ||
|
||
service = module.get<MetricsService>(MetricsService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
Injectable, | ||
InternalServerErrorException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { ChannelsRepository } from 'src/channels/channels.repository'; | ||
import { QueuesRepository } from 'src/queues/queues.repository'; | ||
import { VodsRepository } from 'src/vods/vods.repository'; | ||
|
||
@Injectable() | ||
export class MetricsService { | ||
private logger = new Logger('MetricsService'); | ||
|
||
constructor( | ||
@InjectRepository(ChannelsRepository) | ||
private readonly channelsRepository: ChannelsRepository, | ||
@InjectRepository(QueuesRepository) | ||
private readonly queuesRepository: QueuesRepository, | ||
@InjectRepository(VodsRepository) | ||
private readonly vodsRepository: VodsRepository, | ||
) {} | ||
async getMetrics() { | ||
try { | ||
const vodCount = await this.vodsRepository | ||
.createQueryBuilder('vods') | ||
.getCount(); | ||
const channelCount = await this.channelsRepository | ||
.createQueryBuilder('channels') | ||
.getCount(); | ||
const channels = await this.channelsRepository | ||
.createQueryBuilder('channel') | ||
.loadRelationCountAndMap('channel.vodCount', 'channel.vods', 'vod') | ||
.getMany(); | ||
|
||
let channelsString = ''; | ||
|
||
for await (const channel of channels) { | ||
channelsString += `channel_vod_count{id="${channel.displayName}"} ${channel['vodCount']}\n`; | ||
} | ||
|
||
const metrics = | ||
`vod_count{id="ceres"} ${vodCount}\nchannel_count{id="ceres"} ${channelCount}\n${channelsString}`.toString(); | ||
return metrics; | ||
} catch (error) { | ||
this.logger.error(error); | ||
throw new InternalServerErrorException('Error fetching metrics'); | ||
} | ||
} | ||
} |