mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
teach API to slice modal times of days
Expand glob patterns to regexps, optimally, the fast prefix kind. For @jasoncalabrese, @stavior, @kenstack and friends. :-)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
var consts = require('../../constants');
|
||||
var es = require('event-stream');
|
||||
var sgvdata = require('sgvdata');
|
||||
var expand = require('expand-braces');
|
||||
|
||||
var ID_PATTERN = /^[a-f\d]{24}$/;
|
||||
function isId (value) {
|
||||
@@ -245,6 +246,41 @@ function configure (app, wares, ctx) {
|
||||
});
|
||||
api.get('/echo/:echo/:model?/:spec?', echo_query);
|
||||
|
||||
function prep_patterns (req, res, next) {
|
||||
var pattern = [ ];
|
||||
if (req.params.prefix) {
|
||||
pattern.push('^' + req.params.prefix);
|
||||
}
|
||||
if (req.params.regex) {
|
||||
pattern.push('.*' + req.params.regex);
|
||||
}
|
||||
pattern = expand(pattern.join(''));
|
||||
req.pattern = pattern;
|
||||
var matches = pattern.map(function iter (pat) {
|
||||
return new RegExp(pat);
|
||||
});
|
||||
var query = {
|
||||
dateString: {
|
||||
$in: matches
|
||||
}
|
||||
};
|
||||
|
||||
if (req.query.find) {
|
||||
if (req.query.find.dateString) {
|
||||
req.query.find.dateString.$in = query.dateString.$in;
|
||||
} else {
|
||||
req.query.find.dateString = query.dateString;
|
||||
}
|
||||
} else {
|
||||
req.query.find = query;
|
||||
}
|
||||
next( );
|
||||
}
|
||||
api.get('/times/echo/:prefix?/:regex?', prep_patterns, function (req, res, next) {
|
||||
res.json({ req: { params: req.params, query: req.query}, pattern: req.pattern});
|
||||
});
|
||||
api.get('/times/:prefix?/:regex?', prep_patterns, query_models, format_entries);
|
||||
|
||||
// Allow previewing your post content, just echos everything you
|
||||
// posted back out.
|
||||
api.post('/entries/preview', function (req, res, next) {
|
||||
|
||||
Reference in New Issue
Block a user