Skip to content

Commit

Permalink
feat(logs): support resource logs
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 committed Feb 20, 2025
1 parent af400cc commit 2879675
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion esm/streams/access-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ApplicationAccessLogStream extends CleverCloudSse {
/**
* shortcut for .on('APPLICATION_LOG', (event) => ...)
* @param {Function} fn which handle logs
* @returns {any}
* @returns {ApplicationAccessLogStream}
*/
onLog (fn) {
return this.on(ACCESS_LOG_EVENT_NAME, (event) => fn(event.data));
Expand Down
2 changes: 1 addition & 1 deletion esm/streams/application-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ApplicationLogStream extends CleverCloudSse {
/**
* shortcut for .on('APPLICATION_LOG', (event) => ...)
* @param {Function} fn which handle logs
* @returns {any}
* @returns {ApplicationLogStream}
*/
onLog (fn) {
return this.on(APPLICATION_LOG_EVENT_NAME, (event) => fn(event.data));
Expand Down
89 changes: 89 additions & 0 deletions esm/streams/resource-logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

import CleverCloudSse from './clever-cloud-sse.js';

const RESOURCE_LOG_EVENT_NAME = 'RESOURCE_LOG';

export class ApplicationAddonLogStream extends cleverCloudSse {
/**
* @param {object} options
* @param {string} options.apiHost
* @param {object} options.tokens
* @param {string} options.tokens.OAUTH_CONSUMER_KEY
* @param {string} options.tokens.OAUTH_CONSUMER_SECRET
* @param {string} options.tokens.API_OAUTH_TOKEN
* @param {string} options.tokens.API_OAUTH_TOKEN_SECRET
* @param {string} options.ownerId
* @param {string} options.addonId
* @param {number} options.connectionTimeout
* @param {object} options.retryConfiguration
* @param {boolean} options.retryConfiguration.enabled
* @param {number} options.retryConfiguration.backoffFactor
* @param {number} options.retryConfiguration.initRetryTimeout
* @param {number} options.retryConfiguration.maxRetryCount
* @param {Date} options.since
* @param {Date} options.until
* @param {number} options.limit
* @param {string} options.filter
* @param {string} options.field[]
* @param {number} options.throttleElements
* @param {number} options.throttlePerInMilliseconds
*/
constructor({ apiHost, tokens, ownerId, addonId, retryConfiguration, connectionTimeout, ...options }) {
super(apiHost, tokens, retryConfiguration ?? {}, connectionTimeout);
this._ownerId = ownerId;
this._addonId = addonId;
this._options = options;
this._logsCount = 0;
this.onLog(() => { this._logsCount++ });
}

/**
* compute full URL with query params
* @returns {URL}
*/
getUrl() {
return this.buildUrl(`/v4/logs/organisations/${this._ownerId}/resources/${this._addonId}/logs`, { ...this._options,
// in case of pause() then resume():
// we don' t want N another logs, we want the initial passed number less the events count already received
limit: this._computedLimit(),
service: 'all' // TODO: debug
});
}

/**
* compute the number of events to retrieve, based on elements already received
*/
_computedLimit() {
if (this._options.limit == null) {
return null;
}

return Math.max(this._options.limit - this._logsCount, 0);
}

/**
* override default method
*/
transform(event, data) {
if (event !== RESOURCE_LOG_EVENT_NAME) {
return data;
}

const log = JSON.parse(data);

if (log.date) {
log.date = new Date(log.date);
}

return log;
}

/**
* catch Log messages from stream
* @param {Function} fn which handle logs
* @returns {ApplicationAddonLogStream}
*/
onLog(fn) {
return this.on(RESOURCE_LOG_EVENT_NAME, event => fn(event.data));
}
}

0 comments on commit 2879675

Please sign in to comment.