Skip to content

Commit

Permalink
switched dates to luxon
Browse files Browse the repository at this point in the history
  • Loading branch information
neti-mateusz-mazur committed Oct 8, 2024
1 parent 17d6fce commit cdafde3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 70 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@nestjs/schedule": "^4.1.0",
"@prisma/client": "^5.19.1",
"axios": "^1.7.7",
"date-fns": "^4.1.0",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"reflect-metadata": "^0.2.2",
Expand Down
36 changes: 7 additions & 29 deletions src/service/allocator/allocator.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../db/prisma.service';
import {
getAllocatorBiggestClientDistribution,
Expand All @@ -10,7 +10,7 @@ import { groupBy } from 'lodash';
import { ProviderComplianceScoreRange } from '../../types/providerComplianceScoreRange';
import { SpsComplianceWeekResponseDto } from '../../types/spsComplianceWeekResponse.dto';
import { SpsComplianceWeekDto } from '../../types/spsComplianceWeek.dto';
import { differenceInCalendarDays } from 'date-fns';
import { DateTime } from 'luxon';

@Injectable()
export class AllocatorService {
Expand All @@ -20,39 +20,17 @@ export class AllocatorService {
) {}

async getAllocatorRetrievability() {
const allocatorWeeks = await this.prismaService.allocators_weekly.findMany({
select: {
week: true,
},
orderBy: {
week: 'desc',
},
where: {
week: {
lte: new Date(),
},
},
skip: 0,
take: 2,
});

const lastStoredFullWeek = allocatorWeeks.find(
(p) => differenceInCalendarDays(new Date(), p.week) >= 7,
);
if (!lastStoredFullWeek) {
throw new HttpException(
'No data for full week present yet',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

const averageSuccessRate =
await this.prismaService.allocators_weekly.aggregate({
_avg: {
avg_weighted_retrievability_success_rate: true,
},
where: {
week: lastStoredFullWeek.week,
week: DateTime.now()
.toUTC()
.minus({ week: 1 })
.startOf('week')
.toJSDate(),
},
});

Expand Down
32 changes: 3 additions & 29 deletions src/service/provider/provider.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../db/prisma.service';
import {
getProviderBiggestClientDistribution,
Expand All @@ -7,7 +7,7 @@ import {
} from '../../../prisma/generated/client/sql';
import { RetrievabilityWeekResponseDto } from '../../types/retrievabilityWeekResponse.dto';
import { HistogramHelper } from '../../helper/histogram.helper';
import { differenceInCalendarDays } from 'date-fns';
import { DateTime } from 'luxon';

@Injectable()
export class ProviderService {
Expand Down Expand Up @@ -51,32 +51,6 @@ export class ProviderService {
}

async getProviderRetrievability() {
const providerWeeks = await this.prismaService.providers_weekly.findMany({
select: {
week: true,
},
orderBy: {
week: 'desc',
},
where: {
week: {
lte: new Date(),
},
},
skip: 0,
take: 2,
});

const lastStoredFullWeek = providerWeeks.find(
(p) => differenceInCalendarDays(new Date(), p.week) >= 7,
);
if (!lastStoredFullWeek) {
throw new HttpException(
'No data for full week present yet',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

const providerCountAndAverageSuccessRate = await this.prismaService
.$queryRaw<
[
Expand All @@ -87,7 +61,7 @@ export class ProviderService {
]
>`select count(distinct provider)::int,
100 * avg(avg_retrievability_success_rate) as "averageSuccessRate"
from providers_weekly where week = ${lastStoredFullWeek.week};`;
from providers_weekly where week = ${DateTime.now().toUTC().minus({ week: 1 }).startOf('week').toJSDate()};`;

const weeklyHistogramResult =
await this.histogramHelper.getWeeklyHistogramResult(
Expand Down

0 comments on commit cdafde3

Please sign in to comment.