Skip to content

Commit

Permalink
feat(annotation-server): keep last data fetch dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jannis-baum committed Jun 21, 2022
1 parent 50c1f5a commit 7dba73c
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions annotation-server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { FetchDatesModule } from './fetch-dates/fetch-dates.module';
import { GuidelinesModule } from './guidelines/guidelines.module';
import { MedicationsModule } from './medications/medications.module';
import { PhenotypesModule } from './phenotypes/phenotypes.module';
Expand Down Expand Up @@ -34,6 +35,7 @@ import { PhenotypesModule } from './phenotypes/phenotypes.module';
PhenotypesModule,
GuidelinesModule,
MedicationsModule,
FetchDatesModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
15 changes: 15 additions & 0 deletions annotation-server/src/fetch-dates/fetch-date.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, PrimaryColumn, UpdateDateColumn } from 'typeorm';

export enum FetchTarget {
MEDICATIONS = 'medications',
GUIDELINES = 'guidelines',
}

@Entity()
export class FetchDate {
@PrimaryColumn({ type: 'enum', enum: FetchTarget })
target: FetchTarget;

@UpdateDateColumn({ type: 'timestamp with time zone' })
date: Date;
}
12 changes: 12 additions & 0 deletions annotation-server/src/fetch-dates/fetch-dates.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FetchDate } from './fetch-date.entity';
import { FetchDatesService } from './fetch-dates.service';

@Module({
imports: [TypeOrmModule.forFeature([FetchDate])],
providers: [FetchDatesService],
exports: [FetchDatesService],
})
export class FetchDatesModule {}
26 changes: 26 additions & 0 deletions annotation-server/src/fetch-dates/fetch-dates.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { FetchDate, FetchTarget } from './fetch-date.entity';

@Injectable()
export class FetchDatesService {
constructor(
@InjectRepository(FetchDate)
private fetchDatesRepository: Repository<FetchDate>,
) {}

async get(target: FetchTarget): Promise<Date | null> {
const fetchDate = await this.fetchDatesRepository.findOneBy({ target });
return fetchDate?.date;
}

async set(target: FetchTarget): Promise<void> {
try {
await this.fetchDatesRepository.insert({ target });
} catch {
await this.fetchDatesRepository.update({ target }, { target });
}
}
}
6 changes: 6 additions & 0 deletions annotation-server/src/guidelines/guidelines.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export class GuidelinesController {
return this.guidelinesService.fetchGuidelines();
}

@ApiOperation({ summary: `Get the previous CPIC data update's date` })
@Get('last_update')
getLastUpdate(): Promise<Date | null> {
return this.guidelinesService.getLastUpdate();
}

@ApiOperation({ summary: 'Fetch all guidelines' })
@ApiFindGuidelinesQueries()
@Get()
Expand Down
2 changes: 2 additions & 0 deletions annotation-server/src/guidelines/guidelines.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FetchDatesModule } from '../fetch-dates/fetch-dates.module';
import { MedicationsModule } from '../medications/medications.module';
import { PhenotypesModule } from '../phenotypes/phenotypes.module';
import { GuidelineError } from './entities/guideline-error.entity';
Expand All @@ -15,6 +16,7 @@ import { GuidelinesService } from './guidelines.service';
PhenotypesModule,
MedicationsModule,
TypeOrmModule.forFeature([Guideline, GuidelineError]),
FetchDatesModule,
],
controllers: [GuidelinesController],
providers: [GuidelinesService],
Expand Down
8 changes: 8 additions & 0 deletions annotation-server/src/guidelines/guidelines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { lastValueFrom } from 'rxjs';
import { FindOptionsOrder, FindOptionsOrderValue, Repository } from 'typeorm';

import { fetchSpreadsheetCells } from '../common/utils/google-sheets';
import { FetchTarget } from '../fetch-dates/fetch-date.entity';
import { FetchDatesService } from '../fetch-dates/fetch-dates.service';
import { Medication } from '../medications/medication.entity';
import { MedicationsService } from '../medications/medications.service';
import { Phenotype } from '../phenotypes/entities/phenotype.entity';
Expand Down Expand Up @@ -46,6 +48,7 @@ export class GuidelinesService {
private guidelineErrorRepository: Repository<GuidelineError>,
private medicationsService: MedicationsService,
private phenotypesService: PhenotypesService,
private fetchDatesService: FetchDatesService,
) {
this.spreadsheetGeneResultHeader = [];
this.medicationsByNameCache = new MedicationByNameCache(
Expand Down Expand Up @@ -134,6 +137,11 @@ export class GuidelinesService {
const guidelines = await this.fetchCpicGuidelines();
await this.addGuidelineURLS(guidelines);
await this.complementAndSaveGuidelines(guidelines);
await this.fetchDatesService.set(FetchTarget.GUIDELINES);
}

async getLastUpdate(): Promise<Date | null> {
return this.fetchDatesService.get(FetchTarget.GUIDELINES);
}

private async fetchCpicGuidelines(): Promise<Map<string, Guideline[]>> {
Expand Down
6 changes: 6 additions & 0 deletions annotation-server/src/medications/medications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import { MedicationsService } from './medications.service';
export class MedicationsController {
constructor(private medicationsService: MedicationsService) {}

@ApiOperation({ summary: `Get the previous DrugBank data update's date` })
@Get('last_update')
getLastUpdate(): Promise<Date | null> {
return this.medicationsService.getLastUpdate();
}

@ApiOperation({ summary: 'Fetch all medications' })
@ApiFindMedicationsQueries()
@Get()
Expand Down
2 changes: 2 additions & 0 deletions annotation-server/src/medications/medications.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FetchDatesModule } from '../fetch-dates/fetch-dates.module';
import { Medication, MedicationSearchView } from './medication.entity';
import { MedicationsController } from './medications.controller';
import { MedicationsService } from './medications.service';
Expand All @@ -10,6 +11,7 @@ import { MedicationsService } from './medications.service';
imports: [
HttpModule,
TypeOrmModule.forFeature([Medication, MedicationSearchView]),
FetchDatesModule,
],
controllers: [MedicationsController],
providers: [MedicationsService],
Expand Down
7 changes: 7 additions & 0 deletions annotation-server/src/medications/medications.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';

import { FetchDate } from '../fetch-dates/fetch-date.entity';
import { FetchDatesService } from '../fetch-dates/fetch-dates.service';
import { Medication } from './medication.entity';
import { MedicationsService } from './medications.service';

Expand All @@ -19,6 +21,11 @@ describe('MedicationsService', () => {
provide: getRepositoryToken(Medication),
useValue: {},
},
FetchDatesService,
{
provide: getRepositoryToken(FetchDate),
useValue: {},
},
],
}).compile();

Expand Down
8 changes: 8 additions & 0 deletions annotation-server/src/medications/medications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
} from 'typeorm';

import { fetchSpreadsheetCells } from '../common/utils/google-sheets';
import { FetchTarget } from '../fetch-dates/fetch-date.entity';
import { FetchDatesService } from '../fetch-dates/fetch-dates.service';
import { DrugDto } from './dtos/drugbank.dto';
import { Medication, MedicationSearchView } from './medication.entity';

Expand All @@ -30,6 +32,7 @@ export class MedicationsService {
private configService: ConfigService,
@InjectRepository(Medication)
private medicationRepository: Repository<Medication>,
private fetchDatesService: FetchDatesService,
) {}

async findAll(
Expand Down Expand Up @@ -146,11 +149,16 @@ export class MedicationsService {
const savedMedications = await this.medicationRepository.save(
medications,
);
await this.fetchDatesService.set(FetchTarget.MEDICATIONS);
this.logger.log(
`Successfully saved ${savedMedications.length} medications!`,
);
}

async getLastUpdate(): Promise<Date | null> {
return this.fetchDatesService.get(FetchTarget.MEDICATIONS);
}

async clearAllMedicationData(): Promise<void> {
await this.medicationRepository.delete({});
}
Expand Down

0 comments on commit 7dba73c

Please sign in to comment.