Selecting Records from A Certain Time PeriodΒΆ

Let’s take the Apache access log from the built-in examples (see here for extracting examples). The /user/examples/views/my_webserver_access_logs.sx view embeds a pattern and a location for turning /user/examples/data/apache_access_log.sx.gz into selectable tuple stream. The record timestamp is exposed under the name timestamp and response code as response:

//select records from an arbitrary period of apache access log
//note that T is alias for function STR_TO_TIME

@[/user/examples/views/my_webserver_access_logs.sx]
 .filter(timestamp > STR_TO_TIME('2016-03-14 18:00:00 EET') AND timestamp < T('2016-03-15 02:00:00 EET'))

//same using SQL syntax:

SELECT * FROM @[/user/examples/views/my_webserver_access_logs.sx]
WHERE timestamp > STR_TO_TIME('2016-03-14 18:00:00 EET') AND timestamp < T('2016-03-15 02:00:00 EET')

Selecting the latest records is easy using NOW() and timestamp operators. For example retrieving records from the last five hours:

init(
  '_query.now':T('2016-03-16 00:00:00.000 +0200')   // set the script to execute in the past
);

@[/user/examples/views/my_webserver_access_logs.sx]
 .filter(timestamp >= now()[-5 hour] AND timestamp <= now());