-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstracted GCS to generic file cache module
- Loading branch information
1 parent
d4bcf40
commit 71c451e
Showing
20 changed files
with
256 additions
and
100 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
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,8 @@ | ||
/* | ||
https://docs.nestjs.com/controllers#controllers | ||
*/ | ||
|
||
import { Controller } from '@nestjs/common'; | ||
|
||
@Controller() | ||
export class BuildingsController { } |
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,21 @@ | ||
import { BuildingsService } from './buildings.service'; | ||
import { BuildingsController } from './buildings.controller'; | ||
import { BigQueryService } from '../bigquery/bigquery.service'; | ||
import { GcsService } from '../gcs/gcs.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
/* | ||
https://docs.nestjs.com/modules | ||
*/ | ||
|
||
import { Module } from '@nestjs/common'; | ||
import { CloudstorageCacheModule } from '../cloudstorage-cache/cloudstorage-cache.module'; | ||
|
||
@Module({ | ||
imports: [ConfigModule, CloudstorageCacheModule], | ||
controllers: [ | ||
BuildingsController], | ||
providers: [ | ||
BuildingsService, BigQueryService, GcsService], | ||
exports: [BuildingsService] | ||
}) | ||
export class BuildingsModule { } |
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,41 @@ | ||
/* | ||
https://docs.nestjs.com/providers#services | ||
*/ | ||
|
||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { BigQueryService } from '../bigquery/bigquery.service'; | ||
import { GcsService } from '../gcs/gcs.service'; | ||
import { IsAuthenticatedGuard } from '../guards/is-authenticated.guard'; | ||
import { AuthedUser, User } from '../decorators/authed-user.decorator'; | ||
import { ValidateLatLngUser } from '../decorators/validate-lat-lng-user.decorator'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { CloudStorageCacheService } from '../cloudstorage-cache/cloudstorage-cache.service'; | ||
import { GetBuildingsQuery } from './dto/get-buildings-query.dto'; | ||
import { Building } from './interfaces/building.interface'; | ||
|
||
@Injectable() | ||
export class BuildingsService { | ||
|
||
logger = new Logger('PlacesService'); | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly bigQueryService: BigQueryService, | ||
private readonly cloudStorageCache: CloudStorageCacheService, | ||
) {} | ||
|
||
async getBuildings(query: GetBuildingsQuery): Promise<Building[]> { | ||
const { lat, lng, radius } = query; | ||
|
||
// Check if cached results exist in GCS | ||
const cacheKey = `get-places-brands-${JSON.stringify(query)}`; | ||
const cachedResult = await this.cloudStorageCache.getJSON(cacheKey); | ||
if (cachedResult) { | ||
return cachedResult; | ||
} | ||
|
||
const results = await this.bigQueryService.getBuildingsNearby( lat, lng, radius,1); | ||
await this.cloudStorageCache.storeJSON (results,cacheKey); | ||
return results;// | ||
} | ||
} |
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,35 @@ | ||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { IsNumber, IsOptional, IsString, MaxLength, Min, MinLength, ValidateIf } from 'class-validator'; | ||
|
||
export class GetBuildingsQuery { | ||
|
||
@ApiPropertyOptional({ | ||
description: 'Latitude coordinate. Required if country code is not provided.', | ||
example: 40.7128, | ||
}) | ||
@ValidateIf(o => !o.country) | ||
@IsNumber() | ||
lat?: number; | ||
|
||
@ApiPropertyOptional({ | ||
description: 'Longitude coordinate. Required if country code is not provided.', | ||
example: -74.0060, | ||
}) | ||
@ValidateIf(o => !o.country) | ||
@IsNumber() | ||
lng?: number; | ||
|
||
@ApiPropertyOptional({ | ||
description: 'Search radius in meters, defaulting to 1000 meters if not provided.', | ||
example: 1000, | ||
minimum: 1, | ||
default: 1000, | ||
}) | ||
@ValidateIf(o => !o.country) | ||
@IsOptional() | ||
@IsNumber() | ||
@Min(1) | ||
radius?: number = 1000; // Default radius is 1000 meters if not provided | ||
|
||
} |
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,5 @@ | ||
|
||
|
||
export interface Building { | ||
|
||
} |
Empty file.
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 @@ | ||
/* | ||
https://docs.nestjs.com/modules | ||
*/ | ||
|
||
import { Module } from '@nestjs/common'; | ||
import { CloudStorageCacheService } from './cloudstorage-cache.service'; | ||
import { GcsService } from '../gcs/gcs.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
@Module({ | ||
imports: [ConfigModule], | ||
controllers: [], | ||
providers: [CloudStorageCacheService, GcsService], | ||
exports: [CloudStorageCacheService] | ||
}) | ||
export class CloudstorageCacheModule {} |
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,43 @@ | ||
|
||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { BigQuery } from '@google-cloud/bigquery'; | ||
import { Place } from '../places/interfaces/place.interface'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { GcsService } from '../gcs/gcs.service'; | ||
@Injectable() | ||
export class CloudStorageCacheService { | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly gcsService: GcsService) { | ||
|
||
} | ||
|
||
|
||
logger = new Logger('CloudStorageCacheService'); | ||
|
||
async getJSON (cacheKey: string): Promise<any[]|null> { | ||
const gcs = new Storage(); | ||
const bucket = gcs.bucket(process.env.GCS_BUCKET); | ||
const file = bucket.file(cacheKey); | ||
try { | ||
const data = await file.download(); | ||
return JSON.parse(data.toString()); | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
async storeJSON(data: any[], cacheKey: string): Promise<boolean> { | ||
const gcs = new Storage(); | ||
const bucket = gcs.bucket(process.env.GCS_BUCKET); | ||
const file = bucket.file(cacheKey); | ||
try { | ||
await file.save(JSON.stringify(data)); | ||
return true | ||
} catch (error) { | ||
this.logger.error(`Error storing cache: ${error}`); | ||
return false | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// src/places/interfaces/place.interface.ts | ||
|
||
export interface Place { | ||
id: string; | ||
geometry: Geometry; | ||
|
Empty file.
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
Oops, something went wrong.