This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
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.
feat: initial sqs report calculation support
- Loading branch information
1 parent
419eb10
commit 66cf07c
Showing
13 changed files
with
202 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src", | ||
"compilerOptions": {} | ||
"compilerOptions": { | ||
"assets": [ | ||
{ "include": "./config/*.yaml", "outDir": "./dist", "watchAssets": true } | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 @@ | ||
COUCH_SQS_CLIENT_CONFIG: | ||
BASE_URL: http://localhost:4984 |
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,27 @@ | ||
import { readFileSync } from 'fs'; | ||
import * as yaml from 'js-yaml'; | ||
import { join } from 'path'; | ||
|
||
const CONFIG_FILENAME = 'app.yaml'; | ||
|
||
export function AppConfiguration() { | ||
return flatten( | ||
yaml.load(readFileSync(join(__dirname, CONFIG_FILENAME), 'utf8')) as Record< | ||
string, | ||
string | ||
>, | ||
); | ||
} | ||
|
||
function flatten(obj: any, prefix = '', delimiter = '_') { | ||
return Object.keys(obj).reduce((acc: any, k: string) => { | ||
const pre = prefix.length ? prefix + delimiter : ''; | ||
|
||
if (typeof obj[k] === 'object') | ||
Object.assign(acc, flatten(obj[k], pre + k)); | ||
else { | ||
acc[pre + k] = obj[k]; | ||
} | ||
return acc; | ||
}, {}); | ||
} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CouchSqsClient } from './couch-sqs.client'; | ||
|
||
describe('CouchSqsClientService', () => { | ||
let service: CouchSqsClient; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CouchSqsClient], | ||
}).compile(); | ||
|
||
service = module.get<CouchSqsClient>(CouchSqsClient); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,36 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { catchError, map, Observable } from 'rxjs'; | ||
|
||
export class CouchSqsClientConfig { | ||
BASE_URL = ''; | ||
BASIC_AUTH_USER = ''; | ||
BASIC_AUTH_PASSWORD = ''; | ||
} | ||
|
||
export interface QueryRequest { | ||
query: string; | ||
args?: string[]; | ||
} | ||
|
||
export class CouchSqsClient { | ||
private readonly logger: Logger = new Logger(CouchSqsClient.name); | ||
|
||
constructor(private httpService: HttpService) {} | ||
|
||
executeQuery(path: string, query: QueryRequest): Observable<string> { | ||
return this.httpService.post(path, query).pipe( | ||
map((response) => response.data), | ||
catchError((err) => { | ||
this.logger.error(err); | ||
this.logger.debug( | ||
'[CouchSqsClient] Could not execute Query: ', | ||
this.httpService.axiosRef.defaults.url, | ||
path, | ||
query, | ||
); | ||
throw err; | ||
}), | ||
); | ||
} | ||
} |
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,23 +1,57 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { ReportCalculator } from './report-calculator'; | ||
import { ReportData } from '../../domain/report-data'; | ||
import { delay, Observable, of } from 'rxjs'; | ||
import { map, mergeAll, Observable, switchMap } from 'rxjs'; | ||
import { ReportCalculation } from '../../domain/report-calculation'; | ||
import { Reference } from '../../domain/reference'; | ||
import { DefaultReportStorage } from '../storage/report-storage.service'; | ||
import { CouchSqsClient } from '../../couchdb/couch-sqs.client'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { Reference } from '../../domain/reference'; | ||
|
||
@Injectable() | ||
export class SqsReportCalculator implements ReportCalculator { | ||
constructor( | ||
private sqsClient: CouchSqsClient, | ||
private reportStorage: DefaultReportStorage, | ||
) {} | ||
|
||
calculate(reportCalculation: ReportCalculation): Observable<ReportData> { | ||
return of( | ||
new ReportData( | ||
`ReportData:${uuidv4()}`, | ||
reportCalculation.report, | ||
new Reference(reportCalculation.id), | ||
).setData({ | ||
foo: 'bar', | ||
dummyReportData: 'foo', | ||
return this.reportStorage.fetchReport(reportCalculation.report).pipe( | ||
switchMap((report) => { | ||
if (!report) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
if (report.mode !== 'sql') { | ||
throw new BadRequestException(); | ||
} | ||
|
||
if (report.queries.length === 0) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
return report.queries.flatMap((query) => { | ||
return this.sqsClient | ||
.executeQuery('/app/_design/sqlite:config', { | ||
query: query, | ||
args: [], // TODO pass args here | ||
}) | ||
.pipe( | ||
map((rawResponse) => { | ||
return new ReportData( | ||
`ReportData:${uuidv4()}`, | ||
reportCalculation.report, | ||
new Reference(reportCalculation.id), | ||
).setData(rawResponse); | ||
}), | ||
); | ||
}); | ||
}), | ||
).pipe(delay(5000)); | ||
mergeAll(), | ||
); | ||
} | ||
} |
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,28 @@ | ||
import { | ||
CouchSqsClient, | ||
CouchSqsClientConfig, | ||
} from '../../couchdb/couch-sqs.client'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
export const CouchSqsClientFactory = ( | ||
httpService: HttpService, | ||
configService: ConfigService, | ||
): CouchSqsClient => { | ||
const CONFIG_BASE = 'COUCH_SQS_CLIENT_CONFIG_'; | ||
|
||
const config: CouchSqsClientConfig = { | ||
BASE_URL: configService.getOrThrow(CONFIG_BASE + 'BASE_URL'), | ||
BASIC_AUTH_USER: configService.getOrThrow(CONFIG_BASE + 'BASIC_AUTH_USER'), | ||
BASIC_AUTH_PASSWORD: configService.getOrThrow( | ||
CONFIG_BASE + 'BASIC_AUTH_PASSWORD', | ||
), | ||
}; | ||
|
||
httpService.axiosRef.defaults.baseURL = config.BASE_URL; | ||
httpService.axiosRef.defaults.headers['Authorization'] = `Basic ${Buffer.from( | ||
`${config.BASIC_AUTH_USER}:${config.BASIC_AUTH_PASSWORD}`, | ||
).toString('base64')}`; | ||
|
||
return new CouchSqsClient(httpService); | ||
}; |
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