Skip to content

Commit

Permalink
paged single metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Jan 17, 2025
1 parent db3c27c commit eadc956
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 91 deletions.
31 changes: 22 additions & 9 deletions proto/autogenerated/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [LndSeed](#LndSeed)

- GetSingleUsageMetrics
- auth type: __Metrics__
- input: [SingleUsageMetricReq](#SingleUsageMetricReq)
- output: [UsageMetricTlv](#UsageMetricTlv)

- GetUsageMetrics
- auth type: __Metrics__
- input: [UsageMetricReq](#UsageMetricReq)
- input: [LatestUsageMetricReq](#LatestUsageMetricReq)
- output: [UsageMetrics](#UsageMetrics)

- GetUserInfo
Expand Down Expand Up @@ -581,11 +586,18 @@ The nostr server will send back a message response, and inside the body there wi
- This methods has an __empty__ __request__ body
- output: [LndSeed](#LndSeed)

- GetSingleUsageMetrics
- auth type: __Metrics__
- http method: __post__
- http route: __/api/reports/usage/single__
- input: [SingleUsageMetricReq](#SingleUsageMetricReq)
- output: [UsageMetricTlv](#UsageMetricTlv)

- GetUsageMetrics
- auth type: __Metrics__
- http method: __post__
- http route: __/api/reports/usage__
- input: [UsageMetricReq](#UsageMetricReq)
- input: [LatestUsageMetricReq](#LatestUsageMetricReq)
- output: [UsageMetrics](#UsageMetrics)

- GetUserInfo
Expand Down Expand Up @@ -1070,6 +1082,9 @@ The nostr server will send back a message response, and inside the body there wi
- __token__: _string_
- __url__: _string_

### LatestUsageMetricReq
- __limit__: _number_ *this field is optional

### LinkNPubThroughTokenRequest
- __token__: _string_

Expand Down Expand Up @@ -1141,9 +1156,6 @@ The nostr server will send back a message response, and inside the body there wi
- __tag__: _string_

### MetricsFile
- __app_id__: _string_
- __metrics_name__: _string_
- __page__: _number_

### MigrationUpdate
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional
Expand Down Expand Up @@ -1302,6 +1314,11 @@ The nostr server will send back a message response, and inside the body there wi
- __amount__: _number_
- __invoice__: _string_

### SingleUsageMetricReq
- __app_id__: _string_
- __metrics_name__: _string_
- __page__: _number_

### UpdateChannelPolicyRequest
- __policy__: _[ChannelPolicy](#ChannelPolicy)_
- __update__: _[UpdateChannelPolicyRequest_update](#UpdateChannelPolicyRequest_update)_
Expand All @@ -1319,10 +1336,6 @@ The nostr server will send back a message response, and inside the body there wi
- __success__: _boolean_
- __validate_in_nano__: _number_

### UsageMetricReq
- __limit__: _number_ *this field is optional
- __metrics_file__: _[MetricsFile](#MetricsFile)_ *this field is optional

### UsageMetricTlv
- __available_chunks__: ARRAY of: _number_
- __base_64_tlvs__: ARRAY of: _string_
Expand Down
34 changes: 32 additions & 2 deletions proto/autogenerated/go/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ type Client struct {
GetNPubLinkingState func(req GetNPubLinking) (*NPubLinking, error)
GetPaymentState func(req GetPaymentStateRequest) (*PaymentState, error)
GetSeed func() (*LndSeed, error)
GetUsageMetrics func(req UsageMetricReq) (*UsageMetrics, error)
GetSingleUsageMetrics func(req SingleUsageMetricReq) (*UsageMetricTlv, error)
GetUsageMetrics func(req LatestUsageMetricReq) (*UsageMetrics, error)
GetUserInfo func() (*UserInfo, error)
GetUserOffer func(req OfferId) (*OfferConfig, error)
GetUserOfferInvoices func(req GetUserOfferInvoicesReq) (*OfferInvoices, error)
Expand Down Expand Up @@ -1057,7 +1058,36 @@ func NewClient(params ClientParams) *Client {
}
return &res, nil
},
GetUsageMetrics: func(req UsageMetricReq) (*UsageMetrics, error) {
GetSingleUsageMetrics: func(req SingleUsageMetricReq) (*UsageMetricTlv, error) {
auth, err := params.RetrieveMetricsAuth()
if err != nil {
return nil, err
}
finalRoute := "/api/reports/usage/single"
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
resBody, err := doPostRequest(params.BaseURL+finalRoute, body, auth)
if err != nil {
return nil, err
}
result := ResultError{}
err = json.Unmarshal(resBody, &result)
if err != nil {
return nil, err
}
if result.Status == "ERROR" {
return nil, fmt.Errorf(result.Reason)
}
res := UsageMetricTlv{}
err = json.Unmarshal(resBody, &res)
if err != nil {
return nil, err
}
return &res, nil
},
GetUsageMetrics: func(req LatestUsageMetricReq) (*UsageMetrics, error) {
auth, err := params.RetrieveMetricsAuth()
if err != nil {
return nil, err
Expand Down
15 changes: 8 additions & 7 deletions proto/autogenerated/go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ type HttpCreds struct {
Token string `json:"token"`
Url string `json:"url"`
}
type LatestUsageMetricReq struct {
Limit int64 `json:"limit"`
}
type LinkNPubThroughTokenRequest struct {
Token string `json:"token"`
}
Expand Down Expand Up @@ -393,9 +396,6 @@ type LnurlWithdrawInfoResponse struct {
Tag string `json:"tag"`
}
type MetricsFile struct {
App_id string `json:"app_id"`
Metrics_name string `json:"metrics_name"`
Page int64 `json:"page"`
}
type MigrationUpdate struct {
Closure *ClosureMigration `json:"closure"`
Expand Down Expand Up @@ -554,6 +554,11 @@ type SetMockInvoiceAsPaidRequest struct {
Amount int64 `json:"amount"`
Invoice string `json:"invoice"`
}
type SingleUsageMetricReq struct {
App_id string `json:"app_id"`
Metrics_name string `json:"metrics_name"`
Page int64 `json:"page"`
}
type UpdateChannelPolicyRequest struct {
Policy *ChannelPolicy `json:"policy"`
Update *UpdateChannelPolicyRequest_update `json:"update"`
Expand All @@ -571,10 +576,6 @@ type UsageMetric struct {
Success bool `json:"success"`
Validate_in_nano int64 `json:"validate_in_nano"`
}
type UsageMetricReq struct {
Limit int64 `json:"limit"`
Metrics_file *MetricsFile `json:"metrics_file"`
}
type UsageMetricTlv struct {
Available_chunks []int64 `json:"available_chunks"`
Base_64_tlvs []string `json:"base_64_tlvs"`
Expand Down
24 changes: 23 additions & 1 deletion proto/autogenerated/ts/express_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,28 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetSingleUsageMetrics) throw new Error('method: GetSingleUsageMetrics is not implemented')
app.post('/api/reports/usage/single', async (req, res) => {
const info: Types.RequestInfo = { rpcName: 'GetSingleUsageMetrics', batch: false, nostr: false, batchSize: 0}
const stats: Types.RequestStats = { startMs:req.startTimeMs || 0, start:req.startTime || 0n, parse: process.hrtime.bigint(), guard: 0n, validate: 0n, handle: 0n }
let authCtx: Types.AuthContext = {}
try {
if (!methods.GetSingleUsageMetrics) throw new Error('method: GetSingleUsageMetrics is not implemented')
const authContext = await opts.MetricsAuthGuard(req.headers['authorization'])
authCtx = authContext
stats.guard = process.hrtime.bigint()
const request = req.body
const error = Types.SingleUsageMetricReqValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)
const query = req.query
const params = req.params
const response = await methods.GetSingleUsageMetrics({rpcName:'GetSingleUsageMetrics', ctx:authContext , req: request})
stats.handle = process.hrtime.bigint()
res.json({status: 'OK', ...response})
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
} catch (ex) { const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
})
if (!opts.allowNotImplementedMethods && !methods.GetUsageMetrics) throw new Error('method: GetUsageMetrics is not implemented')
app.post('/api/reports/usage', async (req, res) => {
const info: Types.RequestInfo = { rpcName: 'GetUsageMetrics', batch: false, nostr: false, batchSize: 0}
Expand All @@ -1117,7 +1139,7 @@ export default (methods: Types.ServerMethods, opts: ServerOptions) => {
authCtx = authContext
stats.guard = process.hrtime.bigint()
const request = req.body
const error = Types.UsageMetricReqValidate(request)
const error = Types.LatestUsageMetricReqValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authContext }, opts.metricsCallback)
const query = req.query
Expand Down
16 changes: 15 additions & 1 deletion proto/autogenerated/ts/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,21 @@ export default (params: ClientParams) => ({
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetUsageMetrics: async (request: Types.UsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => {
GetSingleUsageMetrics: async (request: Types.SingleUsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetricTlv)> => {
const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/reports/usage/single'
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.UsageMetricTlvValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetUsageMetrics: async (request: Types.LatestUsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => {
const auth = await params.retrieveMetricsAuth()
if (auth === null) throw new Error('retrieveMetricsAuth() returned null')
let finalRoute = '/api/reports/usage'
Expand Down
17 changes: 16 additions & 1 deletion proto/autogenerated/ts/nostr_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,22 @@ export default (params: NostrClientParams, send: (to:string, message: NostrRequ
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetUsageMetrics: async (request: Types.UsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => {
GetSingleUsageMetrics: async (request: Types.SingleUsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetricTlv)> => {
const auth = await params.retrieveNostrMetricsAuth()
if (auth === null) throw new Error('retrieveNostrMetricsAuth() returned null')
const nostrRequest: NostrRequest = {}
nostrRequest.body = request
const data = await send(params.pubDestination, {rpcName:'GetSingleUsageMetrics',authIdentifier:auth, ...nostrRequest })
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
if (data.status === 'OK') {
const result = data
if(!params.checkResult) return { status: 'OK', ...result }
const error = Types.UsageMetricTlvValidate(result)
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
}
return { status: 'ERROR', reason: 'invalid response' }
},
GetUsageMetrics: async (request: Types.LatestUsageMetricReq): Promise<ResultError | ({ status: 'OK' }& Types.UsageMetrics)> => {
const auth = await params.retrieveNostrMetricsAuth()
if (auth === null) throw new Error('retrieveNostrMetricsAuth() returned null')
const nostrRequest: NostrRequest = {}
Expand Down
18 changes: 17 additions & 1 deletion proto/autogenerated/ts/nostr_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,14 +799,30 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetSingleUsageMetrics':
try {
if (!methods.GetSingleUsageMetrics) throw new Error('method: GetSingleUsageMetrics is not implemented')
const authContext = await opts.NostrMetricsAuthGuard(req.appId, req.authIdentifier)
stats.guard = process.hrtime.bigint()
authCtx = authContext
const request = req.body
const error = Types.SingleUsageMetricReqValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.GetSingleUsageMetrics({rpcName:'GetSingleUsageMetrics', ctx:authContext , req: request})
stats.handle = process.hrtime.bigint()
res({status: 'OK', ...response})
opts.metricsCallback([{ ...info, ...stats, ...authContext }])
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback); if (opts.throwErrors) throw e }
break
case 'GetUsageMetrics':
try {
if (!methods.GetUsageMetrics) throw new Error('method: GetUsageMetrics is not implemented')
const authContext = await opts.NostrMetricsAuthGuard(req.appId, req.authIdentifier)
stats.guard = process.hrtime.bigint()
authCtx = authContext
const request = req.body
const error = Types.UsageMetricReqValidate(request)
const error = Types.LatestUsageMetricReqValidate(request)
stats.validate = process.hrtime.bigint()
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger, { ...info, ...stats, ...authCtx }, opts.metricsCallback)
const response = await methods.GetUsageMetrics({rpcName:'GetUsageMetrics', ctx:authContext , req: request})
Expand Down
Loading

0 comments on commit eadc956

Please sign in to comment.