"""
Jobs lib application file.
"""
import datetime

from sqlobject.sqlbuilder import OR

from lib import database as db
from lib.config import AppConf


conf = AppConf()


def orCondition():
    """
    Create time filter for jobs to be run.

    A job record be should run at least once per day and may run multiple
    times on a day, but with a minimum interval of N hours between runs.
    A cron manager could even run a job session (of job records) hourly,
    yet skip over job records where the data we have is recent enough.

    This process is useful for getting data say 4 times a day at 6-hour
    intervals. Also, if a job session crashes and is restarted, it only
    continues with the jobs not completed in that job session.

    A job record should be run if ANY of the following are true:
        - it has never run, or
        - it has not run today (since we need values for today and we might
            not again the job session again today), or
        - it was run today but more than N hours ago (the time since
            the last success for today is old enough that it is useful
            to get more records for today, as we might want to compare
            changes over the day).
    """
    now = datetime.datetime.now()

    # Use current time to get midnight for today, but as datetime object.
    dateCuttoff = datetime.datetime(now.year, now.month, now.day)

    # Use minimum number of hours between job sessions to get the cuttoff time
    # for considering a job run recently.
    interval = conf.getint('TrendCron', 'interval')
    hoursCuttoff = now - datetime.timedelta(hours=interval)

    # From the last two conditions, we check whether the last completed time
    # is either less than the start of today or the recent cuttoff time,
    # therefore we can simplify by comparing against the higher of the two.
    recencyCuttoff = max(dateCuttoff, hoursCuttoff)

    # The ORM needs `==` rather than `is` to build the query correctly.
    return OR(db.PlaceJob.q.lastCompleted == None,  # noqa: E711
              db.PlaceJob.q.lastCompleted < recencyCuttoff)

@@ -313,18 +313,18 @@
 iple
-%0A   
  times
+%0A   
  on 
@@ -386,19 +386,19 @@
 uns.
-%0A   
  A cron
+%0A   
  man
@@ -455,20 +455,16 @@
  hourly,
-%0A   
  yet ski
@@ -469,16 +469,20 @@
 kip over
+%0A   
  job rec
@@ -800,24 +800,16 @@
 re true:
-%0A       
  - it ha
@@ -819,24 +819,16 @@
 ever
- run, or
 %0A       
  - i
@@ -819,24 +819,32 @@
 ever%0A       
+ run, or
  - it has no
@@ -893,22 +893,16 @@
 y and we
- might
 %0A       
@@ -902,19 +902,21 @@
         
-   
+might
  not aga
@@ -950,24 +950,16 @@
 day), or
-%0A       
  - it wa
@@ -973,16 +973,24 @@
 oday but
+%0A       
  more th
@@ -1019,28 +1019,16 @@
 me since
-%0A           
  the las
@@ -1053,16 +1053,24 @@
 y is old
+%0A       
  enough 
@@ -1086,28 +1086,16 @@
 s useful
-%0A           
  to get 
@@ -1129,16 +1129,24 @@
 we might
+%0A       
  want to
@@ -1153,28 +1153,16 @@
  compare
-%0A           
  changes

Update wrapping