UNPKG

5.06 kBTypeScriptView Raw
1/** @module count */
2import { IReconfigurable } from 'pip-services3-commons-node';
3import { ConfigParams } from 'pip-services3-commons-node';
4import { ICounters } from './ICounters';
5import { CounterTiming } from './CounterTiming';
6import { ICounterTimingCallback } from './ICounterTimingCallback';
7import { CounterType } from './CounterType';
8import { Counter } from './Counter';
9/**
10 * Abstract implementation of performance counters that measures and stores counters in memory.
11 * Child classes implement saving of the counters into various destinations.
12 *
13 * ### Configuration parameters ###
14 *
15 * - options:
16 * - interval: interval in milliseconds to save current counters measurements
17 * (default: 5 mins)
18 * - reset_timeout: timeout in milliseconds to reset the counters. 0 disables the reset
19 * (default: 0)
20 */
21export declare abstract class CachedCounters implements ICounters, IReconfigurable, ICounterTimingCallback {
22 protected _interval: number;
23 protected _resetTimeout: number;
24 protected _cache: {
25 [id: string]: Counter;
26 };
27 protected _updated: boolean;
28 protected _lastDumpTime: number;
29 protected _lastResetTime: number;
30 /**
31 * Creates a new CachedCounters object.
32 */
33 constructor();
34 /**
35 * Configures component by passing configuration parameters.
36 *
37 * @param config configuration parameters to be set.
38 */
39 configure(config: ConfigParams): void;
40 /**
41 * Gets the counters dump/save interval.
42 *
43 * @returns the interval in milliseconds.
44 */
45 getInterval(): number;
46 /**
47 * Sets the counters dump/save interval.
48 *
49 * @param value a new interval in milliseconds.
50 */
51 setInterval(value: number): void;
52 /**
53 * Saves the current counters measurements.
54 *
55 * @param counters current counters measurements to be saves.
56 */
57 protected abstract save(counters: Counter[]): void;
58 /**
59 * Clears (resets) a counter specified by its name.
60 *
61 * @param name a counter name to clear.
62 */
63 clear(name: string): void;
64 /**
65 * Clears (resets) all counters.
66 */
67 clearAll(): void;
68 /**
69 * Begins measurement of execution time interval.
70 * It returns [[CounterTiming]] object which has to be called at
71 * [[CounterTiming.endTiming]] to end the measurement and update the counter.
72 *
73 * @param name a counter name of Interval type.
74 * @returns a [[CounterTiming]] callback object to end timing.
75 */
76 beginTiming(name: string): CounterTiming;
77 /**
78 * Dumps (saves) the current values of counters.
79 *
80 * @see [[save]]
81 */
82 dump(): void;
83 /**
84 * Makes counter measurements as updated
85 * and dumps them when timeout expires.
86 *
87 * @see [[dump]]
88 */
89 protected update(): void;
90 private resetIfNeeded;
91 /**
92 * Gets all captured counters.
93 *
94 * @returns a list with counters.
95 */
96 getAll(): Counter[];
97 /**
98 * Gets a counter specified by its name.
99 * It counter does not exist or its type doesn't match the specified type
100 * it creates a new one.
101 *
102 * @param name a counter name to retrieve.
103 * @param type a counter type.
104 * @returns an existing or newly created counter of the specified type.
105 */
106 get(name: string, type: CounterType): Counter;
107 private calculateStats;
108 /**
109 * Ends measurement of execution elapsed time and updates specified counter.
110 *
111 * @param name a counter name
112 * @param elapsed execution elapsed time in milliseconds to update the counter.
113 *
114 * @see [[CounterTiming.endTiming]]
115 */
116 endTiming(name: string, elapsed: number): void;
117 /**
118 * Calculates min/average/max statistics based on the current and previous values.
119 *
120 * @param name a counter name of Statistics type
121 * @param value a value to update statistics
122 */
123 stats(name: string, value: number): void;
124 /**
125 * Records the last calculated measurement value.
126 *
127 * Usually this method is used by metrics calculated
128 * externally.
129 *
130 * @param name a counter name of Last type.
131 * @param value a last value to record.
132 */
133 last(name: string, value: number): void;
134 /**
135 * Records the current time as a timestamp.
136 *
137 * @param name a counter name of Timestamp type.
138 */
139 timestampNow(name: string): void;
140 /**
141 * Records the given timestamp.
142 *
143 * @param name a counter name of Timestamp type.
144 * @param value a timestamp to record.
145 */
146 timestamp(name: string, value: Date): void;
147 /**
148 * Increments counter by 1.
149 *
150 * @param name a counter name of Increment type.
151 */
152 incrementOne(name: string): void;
153 /**
154 * Increments counter by given value.
155 *
156 * @param name a counter name of Increment type.
157 * @param value a value to add to the counter.
158 */
159 increment(name: string, value: number): void;
160}