Skip to content

Commit

Permalink
eventsCount caching
Browse files Browse the repository at this point in the history
  • Loading branch information
arunas-smala committed Jun 10, 2024
1 parent df7d3b6 commit d0db438
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
19 changes: 19 additions & 0 deletions database/migrations/20240610104532_cacheSubscriptionsEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.alterTable('subscriptions', (table) => {
table.jsonb('eventsCount');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.alterTable('subscriptions', (table) => {
table.dropColumn('eventsCount');
});
};
2 changes: 1 addition & 1 deletion services/datagov.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class DatagovService extends moleculer.Service {
}
} while (response?._data?.length);

this.broker.emit('tiles.events.renew');
this.broker.emit('integrations.sync.finished');
return stats;
}

Expand Down
2 changes: 1 addition & 1 deletion services/integrations.fishStockings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class IntegrationsFishStockingsService extends moleculer.Service
}
}

this.broker.emit('tiles.events.renew');
this.broker.emit('integrations.sync.finished');
return stats;
}
}
2 changes: 1 addition & 1 deletion services/integrations.lumbering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class IntegrationsLumberingService extends moleculer.Service {
}
}

this.broker.emit('tiles.events.renew');
this.broker.emit('integrations.sync.finished');
return stats;
}
}
46 changes: 37 additions & 9 deletions services/subscriptions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { FeatureCollection, parse } from 'geojsonjs';
import _ from 'lodash';
import moleculer, { Context, GenericObject } from 'moleculer';
import { Action, Method, Service } from 'moleculer-decorators';
import { Action, Event, Method, Service } from 'moleculer-decorators';
import PostgisMixin, { asGeoJsonQuery, intersectsQuery } from 'moleculer-postgis';
import { PopulateHandlerFn } from 'moleculer-postgis/src/mixin';
import DbConnection from '../mixins/database.mixin';
Expand Down Expand Up @@ -67,9 +67,15 @@ export type Subscription<
populate: 'users.resolve',
onCreate: async ({ ctx }: FieldHookCallback) => ctx.meta.user?.id,
onUpdate: async ({ ctx, entity }: FieldHookCallback) => {
if (entity.userId !== ctx.meta.user?.id) {
// Allow service updates
if (!ctx.meta?.user?.id) {
return entity.userId;
}

if (entity.userId !== ctx.meta.user.id) {
return throwNoRightsError('Unauthorized');
}

return entity.userId;
},
},
Expand Down Expand Up @@ -124,14 +130,13 @@ export type Subscription<
},

eventsCount: {
virtual: true,
populate: {
keyField: 'id',
handler: (ctx: Context, values: any[]) => {
if (!values?.length) return;
return ctx.call('subscriptions.getEventsCount', { id: values, mapping: true });
},
type: 'object',
readonly: true,
properties: {
allTime: 'number',
new: 'number',
},
get: ({ value }: FieldHookCallback) => value || { allTime: 0, new: 0 },
},

frequency: {
Expand Down Expand Up @@ -357,6 +362,29 @@ export default class SubscriptionsService extends moleculer.Service {
return countBySubscriptions;
}

@Event()
async 'integrations.sync.finished'(ctx: Context) {
const allSubscriptions: Array<Subscription<null, 'id'>> = await this.findEntities(ctx, {
fields: 'id',
});

const allIds = allSubscriptions.map((s) => s.id);
const eventsCounts = await this.actions.getEventsCount({ id: allIds, mapping: true });

for (const id in eventsCounts) {
await this.updateEntity(
ctx,
{
id,
eventsCount: eventsCounts[id],
},
{
permissive: true,
},
);
}
}

@Method
async validateApps({ ctx, value, entity }: FieldHookCallback) {
const apps: App[] = await ctx.call('apps.find', {
Expand Down
2 changes: 1 addition & 1 deletion services/tiles.events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default class TilesEventsService extends moleculer.Service {
}

@Event()
async 'tiles.events.renew'() {
async 'integrations.sync.finished'() {
this.superclustersPromises = {};
await this.renewSuperclusterIndex();
}
Expand Down

0 comments on commit d0db438

Please sign in to comment.