11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EmbulkのLiquidで日付計算をする

Last updated at Posted at 2018-06-11

概要

Embulkで集計期間などの日付のデフォルト値を自動でセットするようにしたくて、liquid内で日付計算する方法を調べたのでメモしておく。

本日

today文字列からdateフィルターで、本日の日付を生成する。

{% capture today %}{{ 'today' | date: '%Y-%m-%d' }}{% endcapture %}

昨日

secondsで1日の秒数を計算した値をセットする( 1日 x 60秒 x 60秒 x 24秒 = 86,400秒 )。
minusフィルダーで減らして、dateフィルターで日付の文字列に整形する。

{% assign seconds = 1 | times: 24 | times: 60 | times: 60 %}
{% capture yesterday %}{{ 'today' | date: "%s" | minus: seconds | date: "%Y-%m-%d" }}{% endcapture %}

使ってみる

本日と昨日の日付を検索してパラメータの初期値として使い、Google Analyticsのページビューの取得対象を自動指定する。

{% assign type = 'hoge' %}
{% assign seconds = 1 | times: 24 | times: 60 | times: 60 %}
{% capture yesterday %}{{ 'today' | date: "%s" | minus: seconds | date: "%Y-%m-%d" }}{% endcapture %}
{% capture today %}{{ 'today' | date: '%Y-%m-%d' }}{% endcapture %}
in:
    type: 'google_analytics'
    auth_method: 'service_account'
    json_key_content: '{{ env.GOOGLE_API_JSON }}'
    view_id: '100000000000000000000'
    time_series: 'ga:date'
    start_date: '{{ env.START_DATE | default: yesterday }}'
    end_date: '{{ env.END_DATE | default: today }}'
    incremental: true
    metrics:
      - 'ga:pageviews'

filters:
  - type: 'column'
    add_columns:
      - {name: 'type', type: 'string', default: '{{ type }}'}

out:
    type: 'td'
    apikey: '{{ env.EMBULK_TREASURE_DATA_API_KEY }}'
    endpoint: 'api.treasuredata.com'
    database: '{{ env.EMBULK_QLIFE_OUT_DB }}'
    table: 'daily_google_analytics_pageviews'
    mode: 'append'
    default_timezone: 'Asia/Tokyo'
    default_timestamp_format: '%Y-%m-%d'

参考サイト

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?