-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加pv,uv数据展示
- Loading branch information
Showing
6 changed files
with
148 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,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' | ||
}; | ||
} | ||
} |
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,11 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class UpdatePvDto { | ||
@IsString() | ||
readonly from: string; | ||
} | ||
|
||
export class UpdateUvDto { | ||
@IsString() | ||
readonly from: string; | ||
} |
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,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; | ||
} |
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,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 {} |
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,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); | ||
} | ||
} |