generated from amosproj/amos202Xss0Y-projname
-
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.
Merge pull request #255 from amosproj/feat/xd-216
feat: Configuration offline/online mode(Xd-216)
- Loading branch information
Showing
34 changed files
with
886 additions
and
525 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
2 changes: 1 addition & 1 deletion
2
libs/common/backend/insight-hub/src/lib/services/base-bearer-interaction.service.spec.ts
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
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,18 @@ | ||
{ | ||
"extends": ["../../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,3 @@ | ||
# facilities-backend-metrics | ||
|
||
This library provides a set of classes and functions to collect and report metrics from the backend services. |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'facilities-backend-metrics', | ||
preset: '../../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../../coverage/libs/facilities/backend/metrics', | ||
}; |
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 @@ | ||
{ | ||
"name": "facilities-backend-metrics", | ||
"$schema": "../../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/facilities/backend/metrics/src", | ||
"projectType": "library", | ||
"tags": ["domain:facilities", "kind:backend", "type:feature"], | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/facilities/backend/metrics/jest.config.ts" | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export * from './lib/facilities-backend-metrics.module'; |
23 changes: 23 additions & 0 deletions
23
libs/facilities/backend/metrics/src/lib/controller/metrics.controller.ts
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,23 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import { ESwaggerTag } from 'common-backend-swagger'; | ||
import { GetTimeSeriesParamsDto } from 'facilities-backend-timeseries'; | ||
|
||
import { XdMetricsService } from '../services/metrics.service'; | ||
|
||
@ApiTags(ESwaggerTag.METRICS) | ||
@Controller('metrics') | ||
export class XdMetricsController { | ||
constructor(private readonly metricsService: XdMetricsService) {} | ||
|
||
/** | ||
* Get the metrics for the asset | ||
* | ||
* @param params | ||
*/ | ||
@Get(':assetId/:propertySetName') | ||
@ApiOkResponse({ description: 'Returns metrics data for an asset' }) | ||
public getMetricsForAsset(@Param() params: GetTimeSeriesParamsDto) { | ||
return this.metricsService.getMetricsForAsset(params.assetId, params.propertySetName); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
libs/facilities/backend/metrics/src/lib/facilities-backend-metrics.module.ts
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,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { XdPrismaModule } from 'common-backend-prisma'; | ||
import { XdTimeseriesModule } from 'facilities-backend-timeseries'; | ||
|
||
import { XdMetricsController } from './controller/metrics.controller'; | ||
import { XdMetricsService } from './services/metrics.service'; | ||
|
||
@Module({ | ||
imports: [XdTimeseriesModule, XdPrismaModule], | ||
controllers: [XdMetricsController], | ||
providers: [XdMetricsService], | ||
exports: [XdMetricsService], | ||
}) | ||
export class XdMetricsModule {} |
93 changes: 93 additions & 0 deletions
93
libs/facilities/backend/metrics/src/lib/services/metrics.service.ts
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,93 @@ | ||
import { checkPumpStatus } from '@frontend/facilities/backend/utils'; | ||
import { forwardRef, Inject, Injectable } from '@nestjs/common'; | ||
import { PrismaService } from 'common-backend-prisma'; | ||
import { XdTimeseriesService } from 'facilities-backend-timeseries'; | ||
import { ITimeSeriesPumpReport } from 'facilities-shared-models'; | ||
import { from, map, switchMap } from 'rxjs'; | ||
import dayjs = require('dayjs'); | ||
import { pick } from 'lodash'; | ||
|
||
@Injectable() | ||
export class XdMetricsService { | ||
constructor( | ||
@Inject(forwardRef(() => PrismaService)) | ||
private readonly prismaService: PrismaService, | ||
|
||
private readonly timeSeriesService: XdTimeseriesService, | ||
) {} | ||
|
||
/** | ||
* Get the metrics for the asset | ||
* | ||
* @param assetId | ||
* @param propertySetName | ||
*/ | ||
public getMetricsForAsset(assetId: string, propertySetName: string) { | ||
return this.timeSeriesService | ||
.getTimeSeries({ | ||
assetId, | ||
propertySetName, | ||
from: dayjs().subtract(60, 'minute').toDate(), | ||
}) | ||
.pipe( | ||
switchMap((data) => { | ||
return this.upsertMetrics(assetId, propertySetName, data); | ||
}), | ||
); | ||
} | ||
|
||
/** | ||
* Calculates and upserts the metrics for the asset | ||
* | ||
* @param assetId | ||
* @param propertySetName | ||
* @param data | ||
* @private | ||
*/ | ||
private upsertMetrics(assetId: string, propertySetName: string, data: unknown): any { | ||
switch (propertySetName) { | ||
case 'PumpData': { | ||
const { status, indicatorMsg, metrics } = checkPumpStatus( | ||
data as unknown as ITimeSeriesPumpReport[], | ||
); | ||
return from( | ||
this.prismaService.metrics.deleteMany({ | ||
where: { | ||
assetId: assetId, | ||
}, | ||
}), | ||
).pipe( | ||
switchMap(() => { | ||
return this.prismaService.asset | ||
.update({ | ||
where: { | ||
assetId, | ||
}, | ||
data: { | ||
status, | ||
indicatorMsg, | ||
metrics: { | ||
create: metrics, | ||
}, | ||
}, | ||
}) | ||
.metrics(); | ||
}), | ||
map((metrics) => | ||
metrics.map((m) => ({ | ||
...pick(m, [ | ||
'name', | ||
'min', | ||
'max', | ||
'variance', | ||
'standardDeviation', | ||
'coefficientOfVariation', | ||
'mean', | ||
]), | ||
})), | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} |
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,22 @@ | ||
{ | ||
"extends": "../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
Oops, something went wrong.