Skip to content

Commit

Permalink
augment: Activity Pipe: Don't create variable of cache
Browse files Browse the repository at this point in the history
Helps with Memory Use
  • Loading branch information
jarofgreen committed Mar 10, 2020
1 parent 15fa6ac commit 057d4b5
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/lib/pipes/activity-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ActivityPipe extends Pipe {
run(){
return new Promise(async resolve => {

let activityList = cache.activities;
let augmentedActivities = [];

let pipe = this;
Expand All @@ -20,7 +19,7 @@ class ActivityPipe extends Pipe {
eventActivities.forEach(function(activity){
let activityId = pipe.normaliseActivityId(activity);

if (activityList[activityId] !== undefined){
if (cache.activities[activityId] !== undefined){
augmentedActivities = augmentedActivities.concat(pipe.getActivityLabels(activityId));
// Get labels of broader activities too
augmentedActivities = augmentedActivities.concat(pipe.getBroaderActivities(activityId));
Expand Down Expand Up @@ -50,38 +49,35 @@ class ActivityPipe extends Pipe {
}

/**
Most activities in the ActivityList only have a prefLabel but some have
Most activities in the cache.activities only have a prefLabel but some have
altLabel so we should get this too.
**/
getActivityLabels(activityKey){
let labels = [];
let activityList = cache.activities;
// Get the labels from the cached ActivityList
labels.push(activityList[activityKey]['prefLabel']);
if (activityList[activityKey]['altLabel'] !== undefined){
activityList[activityKey]['altLabel'].forEach(function(altLabel){
// Get the labels from the cached cache.activities
labels.push(cache.activities[activityKey]['prefLabel']);
if (cache.activities[activityKey]['altLabel'] !== undefined){
cache.activities[activityKey]['altLabel'].forEach(function(altLabel){
labels.push(altLabel);
});
}
return labels;
}

/**
Recursively checks activities in the ActivityList for broader field
Recursively checks activities in the cache.activities for broader field
and returns all labels of broader concepts. The broader field is an
array of ids that can also be found in the ActivityList.
array of ids that can also be found in the cache.activities.
**/
getBroaderActivities(activityKeys, activitiesSoFar = []){
let activityList = cache.activities;

if (!Array.isArray(activityKeys)){
activityKeys = [activityKeys];
}

let pipe = this;
activityKeys.forEach(function (activityKey){

let broaderKeys = activityList[activityKey]['broader'];
let broaderKeys = cache.activities[activityKey]['broader'];
if (broaderKeys !== undefined){
let broaderLabels = [];
broaderKeys.forEach(function(broaderKey){
Expand All @@ -99,21 +95,20 @@ class ActivityPipe extends Pipe {
}

/**
Check for labels from the ActivityList in the name and description
Check for labels from the cache.activities in the name and description
fields of the event, and apply these activities if found.
TODO: we could do a fuzzy match between the event description
and the activity definition and look at score to decide whether
to tag the event with that activity.
**/
extractActivities(normalisedEvent){
let activityList = cache.activities;
let labels = [];
let searchFields = ['name', 'description'];

let pipe = this;
// TODO: this is not efficient, maybe reindex by label in the cache?
for (const id of Object.keys(activityList)) {
let activity = activityList[id];
for (const id of Object.keys(cache.activities)) {
let activity = cache.activities[id];
searchFields.forEach(function(field){
if (normalisedEvent.body[field] !== undefined){
if(pipe.searchTextForActivity(normalisedEvent.body[field].toLowerCase(), activity.prefLabel.toLowerCase())){
Expand Down Expand Up @@ -151,7 +146,7 @@ class ActivityPipe extends Pipe {
}

/**
In the ActivityList ids take the form https://openactive.io/activity-list#{id}
In the cache.activities ids take the form https://openactive.io/activity-list#{id}
but some data uses http, www, and/or a forward slash before the #{id}. So let's
strip these out.
**/
Expand Down

0 comments on commit 057d4b5

Please sign in to comment.