Skip to content

Example: Request for historical data

Thomas Spalinger edited this page Feb 13, 2021 · 1 revision

Example: Request for historical data

jq file that generates a dynamic request for historical data.

(
  now as $now |
  # apply local timezone diff to utc time
  def fixTimezone: . | (
    . -= (0|strflocaltime("%H") | tonumber) * 3600 |
    . -= (0|strflocaltime("%M") | tonumber) * 60
  );
  # returns the timespan the given amount of days as timestamp (seconds)
  def timespanDay(days): . | (
    . = (24 * 60 * 60 * days)
  );
  # returns the start of the day of the given date as timestamp
  def startOfDay: . | (
    . = if . == null then $now else . end |
    gmtime | [.[0],.[1],.[2],0,0,0,0,0] | mktime
  );
  # returns the start of the month of the given date as timestamp
  def startOfMonth: . | (
    . = if . == null then $now else . end |
    gmtime | [.[0],.[1],1,0,0,0,0,0] | mktime
  );
  # returns the last day of the month (or number of days in the month) from the given date
  def daysInMonth: . | (
    . = if . == null then $now else . end |
    (. | gmtime | .[2] = 28 | mktime | . += timespanDay(4)) as $next_month |
    $next_month - timespanDay($next_month | gmtime | .[2]) | gmtime | .[2]
  );
  # returns the start of the year from the given date as timestamp
  def startOfYear: . | (
    . = if . == null then $now else . end |
    gmtime | [.[0],0,1,0,0,0,0,0] | mktime
  );
  # returns the last day of the year (or number of days in the year) from the given date
  def daysInYear: . | (
    . = if . == null then $now else . end |
    gmtime | [.[0],11,31,0,0,0,0,0] | mktime | strftime("%j") | tonumber
  );
  . =
[
  [ # day current
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = startOfDay | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(1) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(1) | todateiso8601 )]
    ]
  ],
  [ # day before
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = (startOfDay - timespanDay(1)) | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(1) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(1) | todateiso8601 )]
    ]
  ],
  [ # month current
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = startOfMonth | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(daysInMonth) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(daysInMonth) | todateiso8601 )]
    ]
  ],
  [ # month before
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = (startOfMonth - timespanDay(1)) | startOfMonth | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay((startOfMonth - timespanDay(1)) | startOfMonth | daysInMonth) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN",     ( . = timespanDay((startOfMonth - timespanDay(1)) | startOfMonth | daysInMonth) | todateiso8601 )]
    ]
  ],
  [ # year current
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = startOfYear | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay(daysInYear) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN", ( . = timespanDay(daysInYear) | todateiso8601 )]
    ]
  ],
  [ # year before
    "DB_REQ_HISTORY_DATA_DAY",
    [
      ["DB_REQ_HISTORY_TIME_START", ( . = (startOfYear - timespanDay(1)) | startOfYear | fixTimezone | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_INTERVAL", ( . = timespanDay((startOfYear - timespanDay(1)) | startOfYear | daysInYear) | todateiso8601 )],
      ["DB_REQ_HISTORY_TIME_SPAN",     ( . = timespanDay((startOfYear - timespanDay(1)) | startOfYear | daysInYear) | todateiso8601 )]
    ]
  ]
]
)
Clone this wiki locally