Skip to content

Commit

Permalink
feat: add pv uv
Browse files Browse the repository at this point in the history
添加pv,uv数据展示
  • Loading branch information
B1anker committed Oct 12, 2019
1 parent b16f9be commit b1da2f1
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LoggerMiddleware from '@/middlewares/logger';
import { AnalyzeModule } from '@/modules/analyze/analyze.module';
import { ArchivesModule } from '@/modules/archives/archives.module';
import { AuthModule } from '@/modules/auth/auth.module';
import { CategoryModule } from '@/modules/category/category.module';
Expand All @@ -23,6 +24,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
entities: ['src/**/**.entity{.ts,.js}'],
synchronize: true
}),
AnalyzeModule,
UsersModule,
AuthModule,
PostModule,
Expand Down
44 changes: 44 additions & 0 deletions src/modules/analyze/analyze.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Body, Controller, Get, Put, ValidationPipe } from '@nestjs/common';
import { UpdatePvDto, UpdateUvDto } from './analyze.dto';
import { AnalyzeService } from './analyze.service';

@Controller('analyze')
export class AnalyzeController {
constructor(private readonly analyzeService: AnalyzeService) {}

@Get('pv')
public async pv() {
return {
message: 'ok',
data: {
pv: Number(await this.analyzeService.pv())
}
};
}

@Put('pv')
public async updatePv(@Body(new ValidationPipe()) updatePvDto: UpdatePvDto) {
await this.analyzeService.updatePv(updatePvDto.from);
return {
message: 'ok'
};
}

@Get('uv')
public async uv() {
return {
message: 'ok',
data: {
uv: Number(await this.analyzeService.uv())
}
};
}

@Put('uv')
public async updateUv(@Body(new ValidationPipe()) updateUvDto: UpdateUvDto) {
await this.analyzeService.updateUv(updateUvDto.from);
return {
message: 'ok'
};
}
}
11 changes: 11 additions & 0 deletions src/modules/analyze/analyze.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsString } from 'class-validator';

export class UpdatePvDto {
@IsString()
readonly from: string;
}

export class UpdateUvDto {
@IsString()
readonly from: string;
}
16 changes: 16 additions & 0 deletions src/modules/analyze/analyze.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Visit {
@PrimaryGeneratedColumn()
id: number;

@Column({ length: 32 })
type: string;

@Column('bigint')
count: number;

@Column('text')
from: string;
}
13 changes: 13 additions & 0 deletions src/modules/analyze/analyze.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Visit } from '@/modules/analyze/analyze.entity';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AnalyzeController } from './analyze.controller';
import { AnalyzeService } from './analyze.service';

@Module({
imports: [TypeOrmModule.forFeature([Visit])],
controllers: [AnalyzeController],
providers: [AnalyzeService],
exports: [AnalyzeService]
})
export class AnalyzeModule {}
62 changes: 62 additions & 0 deletions src/modules/analyze/analyze.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Repository } from 'typeorm';

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Visit } from './analyze.entity';

export interface Archive {
title: string;
created: number;
id: string;
}

@Injectable()
export class AnalyzeService {
constructor(
@InjectRepository(Visit) private readonly repository: Repository<Visit>
) {}

public async pv (): Promise<number> {
return this.repository.query(`SELECT sum(count) as count FROM visit where type = 'pv'`).then((res) => {
return res && res[0] && res[0].count || 0;
});
}

public async updatePv(from: string) {
let pv = await this.repository.findOne({
type: 'pv',
from
});
if (!pv) {
pv = new Visit();
pv.type = 'pv';
pv.count = 1;
pv.from = from;
} else {
pv.count++;
}
this.repository.save(pv);
}

public async uv (): Promise<number> {
return this.repository.query(`SELECT sum(count) as count FROM visit where type = 'uv'`).then((res) => {
return res && res[0] && res[0].count || 0;
});
}

public async updateUv(from: string) {
let uv = await this.repository.findOne({
type: 'uv',
from
});
if (!uv) {
uv = new Visit();
uv.type = 'uv';
uv.count = 1;
uv.from = from;
} else {
uv.count++;
}
this.repository.save(uv);
}
}

0 comments on commit b1da2f1

Please sign in to comment.