@autofleet/settings 1.1.4 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -4,12 +4,14 @@ import EventEmitter from 'events';
4
4
  interface SettingsClassOptions {
5
5
  serviceUrl?: string;
6
6
  ttl?: number;
7
+ disableTestEnvCheck?: boolean;
7
8
  }
8
9
  declare type SettingValue = string | boolean | number | any[] | never;
9
10
  interface Label {
10
11
  businessModelId?: string;
11
12
  demandSourceId?: string;
12
13
  fleetId?: string;
14
+ time?: string | Date;
13
15
  }
14
16
  declare type LabelsArray = Label[];
15
17
  interface GetSettingOption {
@@ -22,9 +24,10 @@ declare class SettingsManager {
22
24
  networkEvents: EventEmitter;
23
25
  network: any;
24
26
  NEVER_DEFAULT_VALUE: string;
27
+ disableTestEnvCheck: boolean;
25
28
  static get NEVER_DEFAULT_VALUE(): string;
26
29
  static readNetworkValue(networkValue: SettingValue, defaultValue: SettingValue): SettingValue;
27
- constructor({ serviceUrl, ttl }?: SettingsClassOptions);
30
+ constructor({ serviceUrl, ttl, disableTestEnvCheck }?: SettingsClassOptions);
28
31
  get(key: SettingValue, defaultValue?: any, labels?: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<any>;
29
32
  getMultiple(settingsToGet: {
30
33
  key: SettingValue;
@@ -32,5 +35,6 @@ declare class SettingsManager {
32
35
  }[], labels: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<any>;
33
36
  setLocal(key: string, labels: LabelsArray, value: SettingValue): void;
34
37
  flush(): void;
38
+ getCacheKey(key: SettingValue, labels: LabelsArray): string;
35
39
  }
36
40
  export default SettingsManager;
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ const logger_1 = __importDefault(require("@autofleet/logger"));
21
21
  const node_cache_1 = __importDefault(require("node-cache"));
22
22
  const events_1 = __importDefault(require("events"));
23
23
  const util_1 = __importDefault(require("util"));
24
+ const moment_1 = __importDefault(require("moment"));
24
25
  dotenv_1.default.config();
25
26
  const nextTick = util_1.default.promisify(process.nextTick);
26
27
  const logger = logger_1.default();
@@ -32,10 +33,11 @@ const findUrl = (serviceUrl) => serviceUrl
32
33
  class CannotGetNetworkValueError extends Error {
33
34
  }
34
35
  class SettingsManager {
35
- constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
36
+ constructor({ serviceUrl, ttl = fiveMinutes, disableTestEnvCheck = false } = {}) {
36
37
  const localServiceUrl = findUrl(serviceUrl);
37
38
  this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
38
39
  this.ttl = ttl;
40
+ this.disableTestEnvCheck = disableTestEnvCheck;
39
41
  this.settingsCache = new node_cache_1.default();
40
42
  this.networkEvents = new events_1.default();
41
43
  this.network = new network_1.default({
@@ -57,7 +59,7 @@ class SettingsManager {
57
59
  if (typeof defaultValue === 'undefined') {
58
60
  throw new Error('Can\'t get a key without defaultValue');
59
61
  }
60
- const cacheKey = `${key}_${JSON.stringify(labels)}`;
62
+ const cacheKey = this.getCacheKey(key, labels);
61
63
  const cacheValue = this.settingsCache.get(cacheKey);
62
64
  if (cacheValue !== undefined) {
63
65
  if (waitingToNetwork === cacheValue) {
@@ -73,7 +75,7 @@ class SettingsManager {
73
75
  }
74
76
  return cacheValue;
75
77
  }
76
- if (process.env.NODE_ENV === 'test') {
78
+ if (!this.disableTestEnvCheck && process.env.NODE_ENV === 'test') {
77
79
  return defaultValue;
78
80
  }
79
81
  let networkValue;
@@ -113,7 +115,7 @@ class SettingsManager {
113
115
  const settingsToReturn = new Map();
114
116
  const settingsToFetch = [];
115
117
  settingsToGet.map((obj) => {
116
- const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
118
+ const cacheKey = this.getCacheKey(obj.key, labels);
117
119
  const cacheValue = this.settingsCache.get(cacheKey);
118
120
  if (cacheValue) {
119
121
  settingsToReturn.set(obj.key, cacheValue);
@@ -149,7 +151,7 @@ class SettingsManager {
149
151
  }
150
152
  }
151
153
  return settingsToGet.map((setting) => {
152
- const cacheKey = `${setting.key}_${JSON.stringify(labels)}`;
154
+ const cacheKey = this.getCacheKey(setting.key, labels);
153
155
  let returnValue;
154
156
  try {
155
157
  returnValue = SettingsManager.readNetworkValue(settingsToReturn.get(setting.key), setting.defaultValue);
@@ -164,11 +166,22 @@ class SettingsManager {
164
166
  });
165
167
  }
166
168
  setLocal(key, labels, value) {
167
- const cacheKey = `${key}_${JSON.stringify(labels)}`;
169
+ const cacheKey = this.getCacheKey(key, labels);
168
170
  this.settingsCache.set(cacheKey, value, this.ttl);
169
171
  }
170
172
  flush() {
171
173
  return this.settingsCache.flushAll();
172
174
  }
175
+ // eslint-disable-next-line class-methods-use-this
176
+ getCacheKey(key, labels) {
177
+ const newLabels = labels.map((l) => {
178
+ if (l.time) {
179
+ return Object.assign(Object.assign({}, l), { time: moment_1.default(l.time).startOf('hour').format(), day: moment_1.default(l.time).day() });
180
+ }
181
+ return l;
182
+ });
183
+ const cacheKey = `${key}_${JSON.stringify(newLabels)}`;
184
+ return cacheKey;
185
+ }
173
186
  }
174
187
  exports.default = SettingsManager;
@@ -105,6 +105,18 @@ describe('Settings', () => {
105
105
  expect(value).toEqual('value1');
106
106
  expect(m.isDone()).toBeTruthy();
107
107
  }));
108
+ it('Finds with time label', () => __awaiter(void 0, void 0, void 0, function* () {
109
+ const time = new Date();
110
+ const labels = [{ fleetId: 'uuid', time }];
111
+ const m = mockSetting('key1', 'value1', labels);
112
+ const settings = new index_1.default({
113
+ serviceUrl: `http://${serviceUrl}/`,
114
+ });
115
+ const settingLabel = [{ fleetId: 'uuid', time }];
116
+ const value = yield settings.get('key1', 'dv', settingLabel);
117
+ expect(value).toEqual('value1');
118
+ expect(m.isDone()).toBeTruthy();
119
+ }));
108
120
  it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
109
121
  const settings = new index_1.default({
110
122
  serviceUrl: `http://${serviceUrl}/`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/settings",
3
- "version": "1.1.4",
3
+ "version": "1.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  "bluebird": "^3.7.2",
21
21
  "dotenv": "^8.2.0",
22
22
  "jest": "^22.4.4",
23
+ "moment": "^2.29.1",
23
24
  "nock": "^10.0.2",
24
25
  "node-cache": "^5.1.2"
25
26
  },
package/src/index.test.ts CHANGED
@@ -117,6 +117,20 @@ describe('Settings', () => {
117
117
  expect(m.isDone()).toBeTruthy();
118
118
  });
119
119
 
120
+ it('Finds with time label', async () => {
121
+ const time = new Date();
122
+ const labels = [{ fleetId: 'uuid', time }];
123
+ const m = mockSetting('key1', 'value1', labels);
124
+ const settings = new Settings({
125
+ serviceUrl: `http://${serviceUrl}/`,
126
+ });
127
+
128
+ const settingLabel = [{ fleetId: 'uuid', time }];
129
+ const value = await settings.get('key1', 'dv', settingLabel);
130
+ expect(value).toEqual('value1');
131
+ expect(m.isDone()).toBeTruthy();
132
+ });
133
+
120
134
  it('Returns default value when network error', async () => {
121
135
  const settings = new Settings({
122
136
  serviceUrl: `http://${serviceUrl}/`,
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ import Logger from '@autofleet/logger';
7
7
  import NodeCache from 'node-cache';
8
8
  import EventEmitter from 'events';
9
9
  import util from 'util';
10
+ import moment from 'moment';
10
11
 
11
12
  dotenv.config();
12
13
 
@@ -23,6 +24,7 @@ const findUrl = (serviceUrl?: string) => serviceUrl
23
24
  interface SettingsClassOptions {
24
25
  serviceUrl?: string;
25
26
  ttl?: number;
27
+ disableTestEnvCheck?: boolean;
26
28
  }
27
29
 
28
30
  type SettingValue = string | boolean | number | any[] | never;
@@ -31,6 +33,7 @@ interface Label {
31
33
  businessModelId?: string;
32
34
  demandSourceId?: string;
33
35
  fleetId?: string;
36
+ time?: string | Date;
34
37
  }
35
38
 
36
39
  type LabelsArray = Label[];
@@ -54,6 +57,8 @@ class SettingsManager {
54
57
 
55
58
  NEVER_DEFAULT_VALUE: string;
56
59
 
60
+ disableTestEnvCheck: boolean;
61
+
57
62
  static get NEVER_DEFAULT_VALUE() {
58
63
  return 'NEVER_DEFAULT_VALUE';
59
64
  }
@@ -68,11 +73,12 @@ class SettingsManager {
68
73
  return returnValue;
69
74
  }
70
75
 
71
- constructor({ serviceUrl, ttl = fiveMinutes }:SettingsClassOptions = {}) {
76
+ constructor({ serviceUrl, ttl = fiveMinutes, disableTestEnvCheck = false }:SettingsClassOptions = {}) {
72
77
  const localServiceUrl = findUrl(serviceUrl);
73
78
 
74
79
  this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
75
80
  this.ttl = ttl;
81
+ this.disableTestEnvCheck = disableTestEnvCheck;
76
82
  this.settingsCache = new NodeCache();
77
83
  this.networkEvents = new EventEmitter();
78
84
  this.network = new Network({
@@ -90,7 +96,7 @@ class SettingsManager {
90
96
  throw new Error('Can\'t get a key without defaultValue');
91
97
  }
92
98
 
93
- const cacheKey = `${key}_${JSON.stringify(labels)}`;
99
+ const cacheKey = this.getCacheKey(key, labels);
94
100
  const cacheValue = this.settingsCache.get(cacheKey);
95
101
  if (cacheValue !== undefined) {
96
102
  if (waitingToNetwork === cacheValue) {
@@ -107,7 +113,7 @@ class SettingsManager {
107
113
  return cacheValue;
108
114
  }
109
115
 
110
- if (process.env.NODE_ENV === 'test') {
116
+ if (!this.disableTestEnvCheck && process.env.NODE_ENV === 'test') {
111
117
  return defaultValue;
112
118
  }
113
119
 
@@ -154,7 +160,7 @@ class SettingsManager {
154
160
  const settingsToFetch: any[] = [];
155
161
 
156
162
  settingsToGet.map((obj) => {
157
- const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
163
+ const cacheKey = this.getCacheKey(obj.key, labels);
158
164
  const cacheValue = this.settingsCache.get(cacheKey);
159
165
  if (cacheValue) {
160
166
  settingsToReturn.set(obj.key, cacheValue);
@@ -192,7 +198,7 @@ class SettingsManager {
192
198
  }
193
199
 
194
200
  return settingsToGet.map((setting: any) => {
195
- const cacheKey = `${setting.key}_${JSON.stringify(labels)}`;
201
+ const cacheKey = this.getCacheKey(setting.key, labels);
196
202
  let returnValue;
197
203
  try {
198
204
  returnValue = SettingsManager.readNetworkValue(settingsToReturn.get(setting.key), setting.defaultValue);
@@ -206,13 +212,30 @@ class SettingsManager {
206
212
  }
207
213
 
208
214
  setLocal(key: string, labels: LabelsArray, value: SettingValue) {
209
- const cacheKey = `${key}_${JSON.stringify(labels)}`;
215
+ const cacheKey = this.getCacheKey(key, labels);
210
216
  this.settingsCache.set(cacheKey, value, this.ttl);
211
217
  }
212
218
 
213
219
  flush() {
214
220
  return this.settingsCache.flushAll();
215
221
  }
222
+
223
+ // eslint-disable-next-line class-methods-use-this
224
+ getCacheKey(key: SettingValue, labels: LabelsArray) {
225
+ const newLabels = labels.map((l) => {
226
+ if (l.time) {
227
+ return {
228
+ ...l,
229
+ time: moment(l.time).startOf('hour').format(),
230
+ day: moment(l.time).day(),
231
+ };
232
+ }
233
+ return l;
234
+ });
235
+
236
+ const cacheKey = `${key}_${JSON.stringify(newLabels)}`;
237
+ return cacheKey;
238
+ }
216
239
  }
217
240
 
218
241
  export default SettingsManager;