Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nlu-server): send data to usage API (#171)
* feat(nlu-server): send data to usage API * rename usage of word billing for usage * always display error message * pr review comments
- Loading branch information
1 parent
889aa7c
commit ca6e230
Showing
6 changed files
with
132 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import axios from 'axios' | ||
import _ from 'lodash' | ||
import { UsagePayload, UsageData, UsageSender, UsageType } from './typings' | ||
|
||
export class UsageClient { | ||
constructor(private usageURL: string) {} | ||
|
||
public async sendUsage<S extends UsageSender, T extends UsageType>(sender: S, type: T, records: UsageData<S, T>[]) { | ||
const timestamp = new Date().toISOString() | ||
const usage: UsagePayload<S, T> = { | ||
meta: { | ||
timestamp, | ||
schema_version: '1.0.0', | ||
sender, | ||
type | ||
}, | ||
schema_version: '1.0.0', | ||
records | ||
} | ||
|
||
try { | ||
await axios.post(this.usageURL, usage) | ||
} catch (err) { | ||
if (axios.isAxiosError(err) && err.response?.data) { | ||
const { data } = err.response | ||
const message = this._serialize(data) | ||
err.message += `: ${message}` | ||
} | ||
throw err | ||
} | ||
} | ||
|
||
private _serialize = (data: any): string => { | ||
if (_.isString(data)) { | ||
return data | ||
} | ||
try { | ||
const str = JSON.stringify(data) | ||
return str | ||
} catch (err) { | ||
return `${data}` | ||
} | ||
} | ||
} |
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 @@ | ||
export * from './typings' | ||
export * from './client' |
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,30 @@ | ||
type Is<X, Y> = X extends Y ? true : false | ||
type And<X extends boolean, Y extends boolean> = X extends false ? false : Y extends false ? false : true | ||
|
||
export type UsageSender = 'nlu' // other services might also send usage | ||
export type UsageType = 'training_time' // other services might also send other usage types | ||
|
||
export type UsageMetadata<S extends UsageSender, T extends UsageType> = { | ||
timestamp: string | ||
sender: S | ||
type: T | ||
schema_version: string | ||
} | ||
|
||
export type UsageData<S extends UsageSender, T extends UsageType> = And< | ||
Is<S, 'nlu'>, | ||
Is<T, 'training_time'> | ||
> extends true | ||
? { | ||
app_id: string | ||
model_id: string | ||
training_time: number | ||
timestamp: string | ||
} | ||
: never // other combination of sender + type might have other payload | ||
|
||
export type UsagePayload<S extends UsageSender, T extends UsageType> = { | ||
meta: UsageMetadata<S, T> | ||
schema_version: string | ||
records: UsageData<S, T>[] | ||
} |
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