micro-analytics / micro-analytics-cli

Public analytics as a Node.js microservice. No sysadmin experience required! 📈
MIT License
734 stars 39 forks source link

Adapter utils #63

Closed relekang closed 7 years ago

relekang commented 7 years ago

The following code is in almost all of the adapters, just some have them inline. I was wondering if we should provide these functions from micro-analytics-cli/adapter-utils or something similar?

function keyRegex(str) {
  str = str.split('*').map(s => escapeRegexp(s)).join('*');
  return new RegExp('^' + str.replace('*', '.*'));
}

function filterPaths(options) {
  return key =>
    options.ignoreWildcard
      ? key.startsWith(options.pathname)
      : key.match(keyRegex(options.pathname));
}

function filterViews(views, options) {
  return (views || []).filter(view => {
    if (options && options.before && view.time > options.before)
      return false;
    if (options && options.after && view.time < options.after)
      return false;
    return true;
  });
}
relekang commented 7 years ago

Another option might be to add it to the core of micro-analytics and then require adapters that want to implement it themselves to export config: { filtering: true }.