1
+
2
+ import CleverCloudSse from './clever-cloud-sse.js' ;
3
+
4
+ const RESOURCE_LOG_EVENT_NAME = 'RESOURCE_LOG' ;
5
+
6
+ export class ApplicationAddonLogStream extends cleverCloudSse {
7
+ /**
8
+ * @param {object } options
9
+ * @param {string } options.apiHost
10
+ * @param {object } options.tokens
11
+ * @param {string } options.tokens.OAUTH_CONSUMER_KEY
12
+ * @param {string } options.tokens.OAUTH_CONSUMER_SECRET
13
+ * @param {string } options.tokens.API_OAUTH_TOKEN
14
+ * @param {string } options.tokens.API_OAUTH_TOKEN_SECRET
15
+ * @param {string } options.ownerId
16
+ * @param {string } options.addonId
17
+ * @param {number } options.connectionTimeout
18
+ * @param {object } options.retryConfiguration
19
+ * @param {boolean } options.retryConfiguration.enabled
20
+ * @param {number } options.retryConfiguration.backoffFactor
21
+ * @param {number } options.retryConfiguration.initRetryTimeout
22
+ * @param {number } options.retryConfiguration.maxRetryCount
23
+ * @param {Date } options.since
24
+ * @param {Date } options.until
25
+ * @param {number } options.limit
26
+ * @param {string } options.filter
27
+ * @param {string } options.field[]
28
+ * @param {number } options.throttleElements
29
+ * @param {number } options.throttlePerInMilliseconds
30
+ */
31
+ constructor ( { apiHost, tokens, ownerId, addonId, retryConfiguration, connectionTimeout, ...options } ) {
32
+ super ( apiHost , tokens , retryConfiguration ?? { } , connectionTimeout ) ;
33
+ this . _ownerId = ownerId ;
34
+ this . _addonId = addonId ;
35
+ this . _options = options ;
36
+ this . _logsCount = 0 ;
37
+ this . onLog ( ( ) => { this . _logsCount ++ } ) ;
38
+ }
39
+
40
+ /**
41
+ * compute full URL with query params
42
+ * @returns {URL }
43
+ */
44
+ getUrl ( ) {
45
+ return this . buildUrl ( `/v4/logs/organisations/${ this . _ownerId } /resources/${ this . _addonId } /logs` , { ...this . _options ,
46
+ // in case of pause() then resume():
47
+ // we don' t want N another logs, we want the initial passed number less the events count already received
48
+ limit : this . _computedLimit ( ) ,
49
+ service : 'all' // TODO: debug
50
+ } ) ;
51
+ }
52
+
53
+ /**
54
+ * compute the number of events to retrieve, based on elements already received
55
+ */
56
+ _computedLimit ( ) {
57
+ if ( this . _options . limit == null ) {
58
+ return null ;
59
+ }
60
+
61
+ return Math . max ( this . _options . limit - this . _logsCount , 0 ) ;
62
+ }
63
+
64
+ /**
65
+ * override default method
66
+ */
67
+ transform ( event , data ) {
68
+ if ( event !== RESOURCE_LOG_EVENT_NAME ) {
69
+ return data ;
70
+ }
71
+
72
+ const log = JSON . parse ( data ) ;
73
+
74
+ if ( log . date ) {
75
+ log . date = new Date ( log . date ) ;
76
+ }
77
+
78
+ return log ;
79
+ }
80
+
81
+ /**
82
+ * catch Log messages from stream
83
+ * @param {Function } fn which handle logs
84
+ * @returns {ApplicationAddonLogStream }
85
+ */
86
+ onLog ( fn ) {
87
+ return this . on ( RESOURCE_LOG_EVENT_NAME , event => fn ( event . data ) ) ;
88
+ }
89
+ }
0 commit comments