Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#169): cache expiry for remote places #183

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"liquidjs": "^10.9.2",
"lodash": "^4.17.21",
"luxon": "^3.4.4",
"node-cache": "^5.1.2",
"pino-pretty": "^10.2.3",
"typescript": "^5.2.2",
"uuid": "^9.0.1"
Expand Down
27 changes: 21 additions & 6 deletions src/lib/cht-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ChtSession from './cht-session';
import { Config, ContactType } from '../config';
import { UserPayload } from '../services/user-payload';
import { AxiosInstance } from 'axios';
import NodeCache from "node-cache";

export type PlacePayload = {
name: string;
Expand Down Expand Up @@ -30,8 +31,12 @@ export type RemotePlace = {
};

export class ChtApi {
public readonly chtSession: ChtSession;
private axiosInstance: AxiosInstance;
public chtSession: ChtSession;
axiosInstance: AxiosInstance;
// 60 min cache
private static cache = new NodeCache({
stdTTL: 60 * 60
});

constructor(session: ChtSession) {
this.chtSession = session;
Expand Down Expand Up @@ -67,6 +72,7 @@ export class ChtApi {
const url = `api/v1/places`;
console.log('axios.post', url);
const resp = await this.axiosInstance.post(url, payload);
ChtApi.cache.del(payload.contact_type);
return resp.data.id;
};

Expand Down Expand Up @@ -99,6 +105,7 @@ export class ChtApi {
if (!resp.data.ok) {
throw Error('response from chtApi.updatePlace was not OK');
}
ChtApi.cache.del(payload.contact_type);

return doc;
};
Expand Down Expand Up @@ -182,20 +189,28 @@ export class ChtApi {
include_docs: true,
};
console.log('axios.get', url, params);
const resp = await this.axiosInstance.get(url, { params });
let places: any[] | undefined = ChtApi.cache.get(placeType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously cache was keyed by [domain][contact-type] but now only by [contact-type]. Isn't the domain dimension required? This allowed independant caches and timers for each county eCHIS instance... just because the cache has expired for Nairobi county instance, should it also expire for Migori county instance?

if (places === undefined) {
const resp = await this.axiosInstance.get(url, { params });
places = resp.data.rows;
ChtApi.cache.set(placeType, places);
}

return resp.data.rows
.map((row: any): RemotePlace => {
return places?.map((row: any): RemotePlace => {
inromualdo marked this conversation as resolved.
Show resolved Hide resolved
const nameData = row.key[1];
return {
id: row.id,
name: nameData.substring('name:'.length),
lineage: extractLineage(row.doc),
type: 'remote',
};
});
}) || [];
};

clearCacheOfPlaceType = (placeType: string) => {
ChtApi.cache.del(placeType);
}

getDoc = async (id: string): Promise<any> => {
const url = `medic/${id}`;
console.log('axios.get', url);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cht-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class ChtSession {
public readonly axiosInstance: AxiosInstance;
public readonly sessionToken: string;

private constructor(authInfo: AuthenticationInfo, sessionToken: string, username: string, facilityId: string) {
constructor(authInfo: AuthenticationInfo, sessionToken: string, username: string, facilityId: string) {
this.authInfo = authInfo;
this.username = username;
this.facilityId = facilityId;
Expand Down
53 changes: 0 additions & 53 deletions src/lib/remote-place-cache.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/lib/remote-place-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import SessionCache from '../services/session-cache';
import { RemotePlace, ChtApi } from './cht-api';
import { Config, ContactType, HierarchyConstraint } from '../config';
import { Validation } from './validation';
import RemotePlaceCache from './remote-place-cache';

type RemotePlaceMap = { [key: string]: RemotePlace };

Expand Down Expand Up @@ -123,7 +122,7 @@ async function findRemotePlacesInHierarchy(
hierarchyLevel: HierarchyConstraint,
chtApi: ChtApi
) : Promise<RemotePlace[]> {
let searchPool = await RemotePlaceCache.getPlacesWithType(chtApi, hierarchyLevel.contact_type);
let searchPool = await chtApi.getPlacesWithType(hierarchyLevel.contact_type);
searchPool = searchPool.filter(remotePlace => chtApi.chtSession.isPlaceAuthorized(remotePlace));

const topDownHierarchy = Config.getHierarchyWithReplacement(place.type, 'desc');
Expand Down
5 changes: 2 additions & 3 deletions src/lib/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from 'lodash';
import SessionCache from '../services/session-cache';
import { ChtApi, RemotePlace } from './cht-api';
import RemotePlaceCache from './remote-place-cache';
import RemotePlaceResolver from './remote-place-resolver';
import { Config, ContactType, HierarchyConstraint } from '../config';
import Place from '../services/place';
Expand Down Expand Up @@ -56,7 +55,7 @@ async function getRemoteResults(
chtApi: ChtApi,
dataPrefix: string
) : Promise<RemotePlace[]> {
let remoteResults = (await RemotePlaceCache.getPlacesWithType(chtApi, hierarchyLevel.contact_type))
let remoteResults = (await chtApi.getPlacesWithType(hierarchyLevel.contact_type))
.filter(remotePlace => chtApi.chtSession.isPlaceAuthorized(remotePlace))
.filter(place => place.name.includes(searchString));

Expand All @@ -71,7 +70,7 @@ async function getRemoteResults(
continue;
}

const placesAtLevel = await RemotePlaceCache.getPlacesWithType(chtApi, constrainingHierarchy.contact_type);
const placesAtLevel = await chtApi.getPlacesWithType(constrainingHierarchy.contact_type);
const relevantPlaceIds = placesAtLevel
.filter(remotePlace => remotePlace.name.includes(searchStringAtLevel))
.map(remotePlace => remotePlace.id);
Expand Down
3 changes: 0 additions & 3 deletions src/liquid/app/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@

<div class="navbar-dropdown">
{% if op == 'table' %}
<a class="navbar-item" hx-post="/app/refresh-all" hx-swap="none">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not support manual refresh? Do you need to wait an hour?

<span class="material-symbols-outlined">refresh</span> Refresh
</a>
<a class="navbar-item" hx-post="/app/remove-all" hx-swap="none">
<span class="material-symbols-outlined">delete</span> Clear
</a>
Expand Down
2 changes: 0 additions & 2 deletions src/routes/add-place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import PlaceFactory from '../services/place-factory';
import SessionCache from '../services/session-cache';
import RemotePlaceResolver from '../lib/remote-place-resolver';
import { UploadManager } from '../services/upload-manager';
import RemotePlaceCache from '../lib/remote-place-cache';

export default async function addPlace(fastify: FastifyInstance) {
fastify.get('/add-place', async (req, resp) => {
Expand Down Expand Up @@ -131,7 +130,6 @@ export default async function addPlace(fastify: FastifyInstance) {
}

const chtApi = new ChtApi(req.chtSession);
RemotePlaceCache.clear(chtApi, place.type.name);
await RemotePlaceResolver.resolveOne(place, sessionCache, chtApi, { fuzz: true });
place.validate();

Expand Down
23 changes: 11 additions & 12 deletions src/routes/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Auth from '../lib/authentication';
import { ChtApi } from '../lib/cht-api';
import { Config } from '../config';
import DirectiveModel from '../services/directive-model';
import RemotePlaceCache from '../lib/remote-place-cache';
import RemotePlaceResolver from '../lib/remote-place-resolver';
import SessionCache from '../services/session-cache';
import { UploadManager } from '../services/upload-manager';
Expand Down Expand Up @@ -69,17 +68,17 @@ export default async function sessionCache(fastify: FastifyInstance) {
resp.header('HX-Redirect', '/');
});

fastify.post('/app/refresh-all', async (req, resp) => {
const sessionCache: SessionCache = req.sessionCache;
const chtApi = new ChtApi(req.chtSession);

RemotePlaceCache.clear(chtApi);

const places = sessionCache.getPlaces({ created: false });
await RemotePlaceResolver.resolve(places, sessionCache, chtApi, { fuzz: true });
places.forEach(p => p.validate());
resp.header('HX-Redirect', '/');
});
// fastify.post('/app/refresh-all', async (req, resp) => {
// const sessionCache: SessionCache = req.sessionCache;
// const chtApi = new ChtApi(req.chtSession);
//
// //RemotePlaceCache.clear(chtApi);
//
// const places = sessionCache.getPlaces({ created: false });
// await RemotePlaceResolver.resolve(places, sessionCache, chtApi, { fuzz: true });
// places.forEach(p => p.validate());
// resp.header('HX-Redirect', '/');
// });

// initiates place creation via the job manager
fastify.post('/app/apply-changes', async (req, resp) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default async function authentication(fastify: FastifyInstance) {
signed: false,
httpOnly: true,
expires,
secure: true
secure: !process.env.CHT_DEV_HTTP
});

resp.header('HX-Redirect', `/`);
Expand Down
2 changes: 0 additions & 2 deletions src/services/upload-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as RetryLogic from '../lib/retry-logic';
import { ChtApi, PlacePayload } from '../lib/cht-api';
import { Config } from '../config';
import Place, { PlaceUploadState } from './place';
import RemotePlaceCache from '../lib/remote-place-cache';
import { UploadNewPlace } from './upload.new';
import { UploadReplacementWithDeletion } from './upload.replacement';
import { UploadReplacementWithDeactivation } from './upload.deactivate';
Expand Down Expand Up @@ -69,7 +68,6 @@ export class UploadManager extends EventEmitter {
place.creationDetails.password = password;
}

await RemotePlaceCache.add(place, chtApi);
delete place.uploadError;

console.log(`successfully created ${JSON.stringify(place.creationDetails)}`);
Expand Down
46 changes: 0 additions & 46 deletions test/lib/remote-place-cache.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/lib/search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { expect } from 'chai';

import { RemotePlace } from '../../src/lib/cht-api';
import RemotePlaceCache from '../../src/lib/remote-place-cache';
import SearchLib from '../../src/lib/search';
import { mockChtApi, mockChtSession, mockValidContactType } from '../mocks';
import SessionCache from '../../src/services/session-cache';
import { Config } from '../../src/config';
import RemotePlaceResolver from '../../src/lib/remote-place-resolver';

describe('lib/remote-place-cache.ts', () => {
beforeEach(() => {
RemotePlaceCache.clear({});
});

const parentPlace: RemotePlace = {
id: 'parent-id',
name: 'parent',
Expand Down
Loading
Loading