1
1
mirror of https://github.com/tstack/lnav.git synced 2024-09-19 08:48:02 +03:00

[pt] allow relative times for papertrail time bounds

This commit is contained in:
Timothy Stack 2015-09-14 23:32:05 -07:00
parent ec473edc85
commit 4f32bddd93
5 changed files with 30 additions and 4 deletions

View File

@ -546,11 +546,13 @@ COMMANDS
clear-partition
Clear the partition the top line is a part of.
pt-min-time [<date>]
pt-min-time [<date>|<relative-time>]
Set/get the minimum time range for any papertrail queries.
Absolute or relative time values can be specified.
pt-max-time [<date>]
pt-max-time [<date>|<relative-time>]
Set/get the maximum time range for any papertrail queries.
Absolute or relative time values can be specified.
SQL QUERIES (experimental)

View File

@ -1567,7 +1567,10 @@ static string com_pt_time(string cmdline, vector<string> &args)
}
}
else if (args.size() >= 2) {
string all_args = cmdline.substr(cmdline.find(args[1], args[0].size()));
struct timeval new_time = { 0, 0 };
relative_time rt;
struct relative_time::parse_error pe;
date_time_scanner dts;
struct exttm tm;
time_t now;
@ -1575,7 +1578,15 @@ static string com_pt_time(string cmdline, vector<string> &args)
time(&now);
dts.dts_keep_base_tz = true;
dts.set_base_time(now);
if (dts.scan(args[1].c_str(), args[1].size(), NULL, &tm, new_time) != NULL) {
if (rt.parse(all_args, pe)) {
tm.et_tm = *gmtime(&now);
rt.add(tm);
new_time.tv_sec = timegm(&tm.et_tm);
}
else {
dts.scan(args[1].c_str(), args[1].size(), NULL, &tm, new_time);
}
if (new_time.tv_sec != 0) {
if (args[0] == "pt-min-time") {
lnav_data.ld_pt_min_time = new_time.tv_sec;
retval = refresh_pt_search();

View File

@ -48,6 +48,7 @@ static struct {
{ "pm", pcrepp("\\Apm|p\\.m\\.\\b") },
{ "a", pcrepp("\\Aa\\b") },
{ "an", pcrepp("\\Aan\\b") },
{ "at", pcrepp("\\Aat\\b") },
{ "time", pcrepp("\\A(\\d{1,2}):(\\d{2})(?::(\\d{2}))?") },
{ "num", pcrepp("\\A((?:-|\\+)?\\d+)") },
{ "us", pcrepp("\\Amicros(?:econds?)?|us(?![a-zA-Z])") },
@ -149,6 +150,8 @@ bool relative_time::parse(const char *str, size_t len, struct parse_error &pe_ou
number = 1;
number_set = true;
break;
case RTT_AT:
break;
case RTT_TIME: {
string hstr = pi.get_substr(pc[0]);
string mstr = pi.get_substr(pc[1]);
@ -239,6 +242,10 @@ bool relative_time::parse(const char *str, size_t len, struct parse_error &pe_ou
case RTT_NOON:
this->rt_field[RTF_HOURS] = 12;
this->rt_is_absolute[RTF_HOURS] = true;
for (int lpc = RTF_MICROSECONDS; lpc < RTF_HOURS; lpc++) {
this->rt_field[lpc] = 0;
this->rt_is_absolute[lpc] = true;
}
break;
case RTT__MAX:

View File

@ -48,6 +48,7 @@ public:
RTT_PM,
RTT_A,
RTT_AN,
RTT_AT,
RTT_TIME,
RTT_NUMBER,
RTT_MICROS,

View File

@ -37,6 +37,8 @@ struct {
const char *reltime;
const char *expected;
} TEST_DATA[] = {
{ "today at 4am", "0y0m0d4H0M0S0U" },
{ "yesterday at noon", "0y0m-1d12H0M0S0U" },
{ "a minute ago", "0y0m0d0h-1m0s0u" },
{ "1m ago", "0y0m0d0h-1m0s0u" },
{ "a min ago", "0y0m0d0h-1m0s0u" },
@ -75,9 +77,12 @@ int main(int argc, char *argv[])
relative_time rt;
for (int lpc = 0; TEST_DATA[lpc].reltime; lpc++) {
bool rc;
rt.clear();
rt.parse(TEST_DATA[lpc].reltime, pe);
rc = rt.parse(TEST_DATA[lpc].reltime, pe);
printf("%s %s %s\n", TEST_DATA[lpc].reltime, TEST_DATA[lpc].expected, rt.to_string().c_str());
assert(rc);
assert(std::string(TEST_DATA[lpc].expected) == rt.to_string());
}