From 686821eec2a096063cfae78a4ecb9a5575158dbe Mon Sep 17 00:00:00 2001
From: "E. Joshua Rigler" <erigler@usgs.gov>
Date: Wed, 10 Oct 2018 20:15:51 -0600
Subject: [PATCH] Improve --realtime option in Controller.py

The `--realtime` option for the Controller/CLI was previously only a
boolean flag that specified a fixed start/end interval depending on
the interval (minute or second). It still does this, but the user now
has the option to pass an integer argument (e.g., `--realtime 300`)
that allows the user to specify the number of seconds before NOW
to consider "realtime". For a clear explanation of how this works,
see: https://stackoverflow.com/a/34652207.
---
 geomagio/Controller.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/geomagio/Controller.py b/geomagio/Controller.py
index f56a6e1f..f4e7b5aa 100644
--- a/geomagio/Controller.py
+++ b/geomagio/Controller.py
@@ -538,11 +538,13 @@ def _main(args):
         now = UTCDateTime()
         args.endtime = UTCDateTime(now.year, now.month, now.day,
                 now.hour, now.minute)
-        if args.interval == 'minute':
-            args.starttime = args.endtime - 3600
+        if args.realtime is True:
+            if args.interval == 'minute':
+                args.starttime = args.endtime - 3600
+            else:
+                args.starttime = args.endtime - 600
         else:
-            args.starttime = args.endtime - 600
-
+            args.starttime = args.endtime - args.realtime
     if args.update:
         controller.run_as_update(args)
     else:
@@ -673,11 +675,19 @@ def parse_args(args):
             default=False,
             help='Flag to force data into miniseed blocks. Should only ' +
                     'be used when certain the data is self contained.')
+    # parser.add_argument('--realtime',
+    #         action='store_true',
+    #         default=False,
+    #         help='Flag to run the last hour if interval is minute, ' +
+    #                 'or the last 10 minutes if interval is seconds')
     parser.add_argument('--realtime',
-            action='store_true',
             default=False,
+            const=True,
+            type=int,
+            nargs='?',
             help='Flag to run the last hour if interval is minute, ' +
-                    'or the last 10 minutes if interval is seconds')
+                 'the last 10 minutes if interval is seconds, ' +
+                 'or the last N seconds if integer N is specified.')
     parser.add_argument('--input-goes-directory',
             default='.',
             help='Directory for support files for goes input of imfv283 data')
-- 
GitLab