Skip to content

Commit 9f1b1a2

Browse files
fix: remove local caching of getting company data
As we have implemented the caching in the server, there's no need to have local cache for getting company data.
1 parent 5854ed8 commit 9f1b1a2

File tree

2 files changed

+4
-118
lines changed

2 files changed

+4
-118
lines changed

lib/controllers/company-insights-controller.ts

+3-40
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,26 @@
11

22
import { AnalyticsEventLabelDelimiter } from "../constants";
33
import { cache } from "../common/decorators";
4-
import * as path from "path";
54
import * as util from "util";
65

76
export class CompanyInsightsController implements ICompanyInsightsController {
8-
private static CACHE_TIMEOUT = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
9-
private get $jsonFileSettingsService(): IJsonFileSettingsService {
10-
return this.$injector.resolve<IJsonFileSettingsService>("jsonFileSettingsService", {
11-
jsonFileSettingsPath: path.join(this.$settingsService.getProfileDir(), "company-insights-data.json")
12-
});
13-
}
14-
157
constructor(private $config: IConfiguration,
168
private $httpClient: Server.IHttpClient,
17-
private $injector: IInjector,
189
private $ipService: IIPService,
19-
private $logger: ILogger,
20-
private $settingsService: ISettingsService) { }
10+
private $logger: ILogger) { }
2111

2212
public async getCompanyData(): Promise<ICompanyData> {
2313
let companyData: ICompanyData = null;
24-
const { currentPublicIP, cacheKey } = await this.getIPInfo();
25-
26-
companyData = await this.getCompanyDataFromCache(cacheKey);
27-
28-
if (!companyData && currentPublicIP) {
29-
companyData = await this.getCompanyDataFromPlaygroundInsightsEndpoint(currentPublicIP);
30-
if (companyData && currentPublicIP) {
31-
await this.$jsonFileSettingsService.saveSetting<ICompanyData>(cacheKey, companyData, { useCaching: true });
32-
}
33-
}
34-
35-
return companyData;
36-
}
37-
38-
private async getIPInfo(): Promise<{ currentPublicIP: string, cacheKey: string }> {
3914
let currentPublicIP: string = null;
40-
let keyInJsonFile: string = null;
4115

4216
try {
4317
currentPublicIP = await this.$ipService.getCurrentIPv4Address();
44-
keyInJsonFile = `companyInformation_${currentPublicIP}`;
4518
} catch (err) {
4619
this.$logger.trace(`Unable to get current public ip address. Error is: `, err);
4720
}
4821

49-
return { currentPublicIP, cacheKey: keyInJsonFile };
50-
}
51-
52-
private async getCompanyDataFromCache(keyInJsonFile: string): Promise<ICompanyData> {
53-
let companyData: ICompanyData = null;
54-
55-
try {
56-
if (keyInJsonFile) {
57-
companyData = await this.$jsonFileSettingsService.getSettingValue<ICompanyData>(keyInJsonFile, { cacheTimeout: CompanyInsightsController.CACHE_TIMEOUT });
58-
}
59-
} catch (err) {
60-
this.$logger.trace(`Unable to get data from file, error is:`, err);
22+
if (currentPublicIP) {
23+
companyData = await this.getCompanyDataFromPlaygroundInsightsEndpoint(currentPublicIP);
6124
}
6225

6326
return companyData;

test/controllers/company-insights-controller.ts

+1-78
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { CompanyInsightsController } from "../../lib/controllers/company-insight
66
describe("companyInsightsController", () => {
77
const insightsUrlEndpoint = "/api/insights?ipAddress=%s";
88
const currentIp = "8.8.8.8";
9-
const profileDir = "profileDir";
10-
const cacheTimeout = 30 * 24 * 60 * 60 * 1000; // 2 days in milliseconds
119
const defaultCompanyData = {
1210
company: {
1311
name: "Progress",
@@ -29,26 +27,6 @@ describe("companyInsightsController", () => {
2927
employeeCount: "500"
3028
};
3129

32-
const defaultExpectedDataPassedToGetSetting: any[] = [{ settingName: `companyInformation_${currentIp}`, cacheOpts: { cacheTimeout } }];
33-
const defaultExpectedDataPassedToSaveSetting: any[] = [
34-
{
35-
cacheOpts: {
36-
useCaching: true
37-
},
38-
key: "companyInformation_8.8.8.8",
39-
value: {
40-
country: "Bulgaria",
41-
employeeCount: "500",
42-
industries: "Software__Software 2",
43-
name: "Progress",
44-
revenue: "123131"
45-
}
46-
}
47-
];
48-
49-
let dataPassedToGetSettingValue: { settingName: string, cacheOpts?: ICacheTimeoutOpts }[] = [];
50-
let dataPassedToSaveSettingValue: { key: string, value: any, cacheOpts?: IUseCacheOpts }[] = [];
51-
let getSettingValueResult: IDictionary<any> = null;
5230
let httpRequestCounter = 0;
5331
let httpRequestResult: any = null;
5432
let testInjector: IInjector = null;
@@ -68,43 +46,24 @@ describe("companyInsightsController", () => {
6846
});
6947

7048
injector.register("logger", LoggerStub);
71-
injector.register("injector", injector);
7249
injector.register("ipService", {
7350
getCurrentIPv4Address: async (): Promise<string> => currentIp
7451
});
7552

76-
injector.register("settingsService", {
77-
getProfileDir: (): string => profileDir
78-
});
79-
80-
injector.register("jsonFileSettingsService", {
81-
getSettingValue: async (settingName: string, cacheOpts?: ICacheTimeoutOpts): Promise<any> => {
82-
dataPassedToGetSettingValue.push({ settingName, cacheOpts });
83-
return getSettingValueResult;
84-
},
85-
86-
saveSetting: async (key: string, value: any, cacheOpts?: IUseCacheOpts): Promise<void> => {
87-
dataPassedToSaveSettingValue.push({ key, value, cacheOpts });
88-
}
89-
});
90-
9153
injector.register("companyInsightsController", CompanyInsightsController);
9254

9355
return injector;
9456
};
9557

9658
beforeEach(() => {
97-
dataPassedToGetSettingValue = [];
98-
dataPassedToSaveSettingValue = [];
99-
getSettingValueResult = null;
10059
httpRequestCounter = 0;
10160
httpRequestResult = defaultCompanyData;
10261
testInjector = createTestInjector();
10362
companyInsightsController = testInjector.resolve<ICompanyInsightsController>("companyInsightsController");
10463
});
10564

10665
describe("getCompanyData", () => {
107-
describe("returns null when data does not exist in the cache and", () => {
66+
describe("returns null when", () => {
10867
it("the http client fails to get data", async () => {
10968
const httpClient = testInjector.resolve<Server.IHttpClient>("httpClient");
11069
const errMsg = "custom error";
@@ -146,31 +105,13 @@ describe("companyInsightsController", () => {
146105
const companyData = await companyInsightsController.getCompanyData();
147106
assert.deepEqual(companyData, null);
148107
assert.equal(httpRequestCounter, 0, "We should not have any http request");
149-
assert.deepEqual(dataPassedToGetSettingValue, [], "When we are unable to get IP, we should not try to get value from the cache.");
150-
assert.deepEqual(dataPassedToSaveSettingValue, [], "When we are unable to get IP, we should not persist anything.");
151108
});
152109
});
153110

154111
describe("returns correct data when", () => {
155-
it("data for current ip exist in the cache", async () => {
156-
httpRequestResult = null;
157-
158-
getSettingValueResult = defaultExpectedCompanyData; // data in the file should be in the already parsed format
159-
const companyData = await companyInsightsController.getCompanyData();
160-
assert.deepEqual(companyData, defaultExpectedCompanyData);
161-
162-
assert.equal(httpRequestCounter, 0, "In case we have data for the company in our cache, we should not make any http requests");
163-
assert.deepEqual(dataPassedToGetSettingValue, defaultExpectedDataPassedToGetSetting);
164-
assert.deepEqual(dataPassedToSaveSettingValue, []);
165-
});
166-
167-
describe("data for current ip does not exist in the cache and", () => {
168-
169112
it("response contains company property", async () => {
170113
const companyData = await companyInsightsController.getCompanyData();
171114
assert.deepEqual(companyData, defaultExpectedCompanyData);
172-
assert.deepEqual(dataPassedToGetSettingValue, defaultExpectedDataPassedToGetSetting);
173-
assert.deepEqual(dataPassedToSaveSettingValue, defaultExpectedDataPassedToSaveSetting);
174115
});
175116

176117
it("response contains company property and industries in it are not populated", async () => {
@@ -191,26 +132,8 @@ describe("companyInsightsController", () => {
191132
industries: null,
192133
employeeCount: "500"
193134
});
194-
195-
assert.deepEqual(dataPassedToGetSettingValue, defaultExpectedDataPassedToGetSetting);
196-
assert.deepEqual(dataPassedToSaveSettingValue, [
197-
{
198-
cacheOpts: {
199-
useCaching: true
200-
},
201-
key: "companyInformation_8.8.8.8",
202-
value: {
203-
country: "Bulgaria",
204-
employeeCount: "500",
205-
industries: null,
206-
name: "Progress",
207-
revenue: "123131"
208-
}
209-
}
210-
]);
211135
});
212136

213-
});
214137
});
215138

216139
it("is called only once per process", async () => {

0 commit comments

Comments
 (0)