feat: fixed GitHub trending

This commit is contained in:
Nevo David 2024-03-20 17:50:52 +07:00
parent 11a581e8a7
commit 5dd29d1f5e
3 changed files with 12 additions and 42 deletions

View File

@ -3,7 +3,6 @@ import {Organization} from "@prisma/client";
import {GetOrgFromRequest} from "@gitroom/nestjs-libraries/user/org.from.request";
import {StarsService} from "@gitroom/nestjs-libraries/database/prisma/stars/stars.service";
import dayjs from "dayjs";
import {mean} from 'simple-statistics';
import {StarsListDto} from "@gitroom/nestjs-libraries/dtos/analytics/stars.list.dto";
import {ApiTags} from "@nestjs/swagger";
@ -23,15 +22,13 @@ export class AnalyticsController {
@Get('/trending')
async getTrending() {
const stars = await this._starsService.predictTrending(10);
const findFirst = stars.find(star => dayjs(star).isAfter(dayjs()));
const trendings = (await this._starsService.getTrending('')).reverse();
const dates = trendings.map(result => dayjs(result.date).toDate());
const lastTrendingDate = dates[dates.length - 1];
const todayTrending = dayjs(dayjs().format('YYYY-MM-DDT12:00:00'));
const last = todayTrending.isAfter(dayjs()) ? todayTrending.subtract(1, 'day') : todayTrending;
const nextTrending = last.add(1, 'day');
return {
last: dayjs(lastTrendingDate).format('YYYY-MM-DD HH:mm:ss'),
predictions: findFirst
last: last.format('YYYY-MM-DD HH:mm:ss'),
predictions: nextTrending.format('YYYY-MM-DD HH:mm:ss'),
}
}

View File

@ -8,7 +8,6 @@ export class StarsRepository {
private _github: PrismaRepository<'gitHub'>,
private _stars: PrismaRepository<'star'>,
private _trending: PrismaRepository<'trending'>,
private _trendingLog: PrismaRepository<'trendingLog'>
) {}
getGitHubRepositoriesByOrgId(org: string) {
return this._github.model.gitHub.findMany({
@ -41,15 +40,6 @@ export class StarsRepository {
});
}
newTrending(language: string) {
return this._trendingLog.model.trendingLog.create({
data: {
date: new Date(),
language,
},
});
}
getAllGitHubRepositories() {
return this._github.model.gitHub.findMany({
distinct: ['login'],
@ -139,18 +129,6 @@ export class StarsRepository {
});
}
getLastTrending(language: string) {
return this._trendingLog.model.trendingLog.findMany({
where: {
language,
},
orderBy: {
date: 'desc',
},
take: 100,
});
}
getStarsFilter(githubs: string[], starsFilter: StarsListDto) {
return this._stars.model.star.findMany({
orderBy: {

View File

@ -236,7 +236,6 @@ export class StarsService {
return;
}
await this.newTrending(language);
if (currentTrending) {
const list: Array<{ name: string; position: number }> = JSON.parse(
currentTrending.trendingList
@ -323,10 +322,6 @@ export class StarsService {
return this._starsRepository.replaceOrAddTrending(language, hash, arr);
}
async newTrending(language: string) {
return this._starsRepository.newTrending(language);
}
async getStars(org: string) {
const getGitHubs = await this.getGitHubRepositoriesByOrgId(org);
const list = [];
@ -369,10 +364,6 @@ export class StarsService {
return list;
}
async getTrending(language: string) {
return this._starsRepository.getLastTrending(language);
}
async getStarsFilter(orgId: string, starsFilter: StarsListDto) {
const getGitHubs = await this.getGitHubRepositoriesByOrgId(orgId);
if (getGitHubs.filter((f) => f.login).length === 0) {
@ -447,9 +438,13 @@ export class StarsService {
}
async predictTrending(max = 500) {
const trendings = (await this.getTrending('')).reverse();
const dates = await this.predictTrendingLoop(trendings, 0, max);
return dates.map((d) => dayjs(d).format('YYYY-MM-DDTHH:mm:00'));
const firstDate = dayjs().subtract(1, 'day');
return [
firstDate.format('YYYY-MM-DDT12:00:00'),
...[...new Array(max)].map((p, index) => {
return firstDate.add(index, 'day').format('YYYY-MM-DDT12:00:00');
})
];
}
async predictTrendingLoop(