-
Notifications
You must be signed in to change notification settings - Fork 14
optimized filtering #21
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
Open
nvdk
wants to merge
9
commits into
mu-semtech:master
Choose a base branch
from
nvdk:feature/optimized-filtering
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dbf246d
add an option to send only matches to receiving services
nvdk 19efd3c
optimize filtering
34004ff
only try sending changesets if there are any left after filtering
6285770
ensure unique indexes are set on services
9e2c2e8
only do process.env lookups once
69c219f
better logging of failed requests
fbfe704
also filter effectiveInsert and effectiveDelete
921a094
load env from correct path
d77e06d
add error handling
nvdk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const DEBUG_DELTA_FOLD = process.env["DEBUG_DELTA_FOLD"]; | ||
export const DEBUG_DELTA_MATCH = process.env["DEBUG_DELTA_MATCH"]; | ||
export const DEBUG_DELTA_NOT_SENDING_EMPTY = process.env["DEBUG_DELTA_NOT_SENDING_EMPTY"]; | ||
export const DEBUG_DELTA_SEND = process.env["DEBUG_DELTA_SEND"]; | ||
export const DEBUG_TRIPLE_MATCHES_SPEC = process.env["DEBUG_TRIPLE_MATCHES_SPEC"]; | ||
export const LOG_REQUESTS = process.env["LOG_REQUESTS"]; | ||
export const LOG_SERVER_CONFIGURATION = process.env["LOG_SERVER_CONFIGURATION"]; | ||
export const NORMALIZE_DATETIME_IN_QUAD = process.env["NORMALIZE_DATETIME_IN_QUAD"]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import dns from 'dns'; | ||
import { DEBUG_TRIPLE_MATCHES_SPEC } from './env'; | ||
/** | ||
* Filters the change sets based on a specified pattern. | ||
* | ||
* @param {Array<Object>} changeSets - An array of change set objects, | ||
* each containing `insert` and `delete` properties. | ||
* @param {Object} entry - An object containing the matching criteria. | ||
* @param {Array} entry.match - The pattern used to filter the triples | ||
* in the `insert` and `delete` arrays. | ||
* @returns {Array<Object>} A new array of change set objects with | ||
* filtered `insert` and `delete` properties. | ||
*/ | ||
export function filterChangesetsOnPattern(changeSets, entry) { | ||
const filteredChangesets = []; | ||
for (const changeSet of changeSets) { | ||
const { insert, delete: deleteSet, effectiveInsert, effectiveDelete } = changeSet; | ||
const clonedChangeSet = { | ||
...changeSet, | ||
insert: insert.filter((triple) => tripleMatchesSpec(triple, entry.match)), | ||
delete: deleteSet.filter((triple) => tripleMatchesSpec(triple, entry.match)), | ||
effectiveInsert: effectiveInsert.filter((triple) => tripleMatchesSpec(triple, entry.match)), | ||
effectiveDelete: effectiveDelete.filter((triple) => tripleMatchesSpec(triple, entry.match)), | ||
}; | ||
filteredChangesets.push(clonedChangeSet); | ||
}; | ||
return filteredChangesets; | ||
} | ||
|
||
export function tripleMatchesSpec( triple, matchSpec ) { | ||
// form of triple is {s, p, o}, same as matchSpec | ||
if(DEBUG_TRIPLE_MATCHES_SPEC) | ||
console.log(`Does ${JSON.stringify(triple)} match ${JSON.stringify(matchSpec)}?`); | ||
|
||
for( let key in matchSpec ){ | ||
// key is one of s, p, o | ||
const subMatchSpec = matchSpec[key]; | ||
const subMatchValue = triple[key]; | ||
|
||
if( subMatchSpec && !subMatchValue ) | ||
return false; | ||
|
||
for( let subKey in subMatchSpec ) | ||
// we're now matching something like {type: "url", value: "http..."} | ||
if( subMatchSpec[subKey] !== subMatchValue[subKey] ) | ||
return false; | ||
} | ||
return true; // no false matches found, let's send a response | ||
} | ||
|
||
|
||
export async function filterMatchesForOrigin( changeSets, entry ) { | ||
if( ! entry.options || !entry.options.ignoreFromSelf ) { | ||
return changeSets; | ||
} else { | ||
try { | ||
const originIpAddress = await getServiceIp( entry ); | ||
return changeSets.filter( (changeSet) => changeSet.origin != originIpAddress ); | ||
} catch(e) { | ||
console.error(`Could not filter changeset because an error was returned while looking up ip for ${entry.callback.url}`); | ||
console.error(e); | ||
return changeSets; | ||
} | ||
} | ||
} | ||
|
||
export function hostnameForEntry( entry ) { | ||
return (new URL(entry.callback.url)).hostname; | ||
} | ||
|
||
async function getServiceIp(entry) { | ||
const hostName = hostnameForEntry( entry ); | ||
return new Promise( (resolve, reject) => { | ||
dns.lookup( hostName, { family: 4 }, ( err, address) => { | ||
if( err ) | ||
reject( err ); | ||
else | ||
resolve( address ); | ||
} ); | ||
} ); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ import http from "http"; | |
import { uuid } from "mu"; | ||
|
||
const DEFAULT_RETRY_TIMEOUT = 250; | ||
const DEBUG_DELTA_SEND = process.env["DEBUG_DELTA_SEND"]; | ||
const DEBUG_DELTA_NOT_SENDING_EMPTY = process.env["DEBUG_DELTA_NOT_SENDING_EMPTY"]; | ||
|
||
function formatChangesetBody(changeSets, options) { | ||
if (options.resourceFormat == "v0.0.1") { | ||
|
@@ -115,7 +117,7 @@ export async function sendRequest( | |
// we should send contents | ||
body = formatChangesetBody(changeSets, entry.options); | ||
} | ||
if (process.env["DEBUG_DELTA_SEND"]) | ||
if (DEBUG_DELTA_SEND) | ||
console.log(`Executing send ${method} to ${url}`); | ||
try { | ||
const keepAliveAgent = new http.Agent({ | ||
|
@@ -137,10 +139,10 @@ export async function sendRequest( | |
retriesLeft | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
console.error(`Error sending delta to ${url}`, error); | ||
} | ||
} else { | ||
if (process.env["DEBUG_DELTA_SEND"] || process.env["DEBUG_DELTA_NOT_SENDING_EMPTY"]) | ||
if (DEBUG_DELTA_SEND || DEBUG_DELTA_NOT_SENDING_EMPTY) | ||
console.log(`Changeset empty. Not sending to ${entry.callback.method} ${entry.callback.url}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this, but I don't think this situation can occur. |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused code