evalinkdocs

Example queries

Ready-made queries to copy and adapt. Replace the values in angle brackets, such as <site id>, with your own. For the rules behind them, see Write SQL queries.

Alarms of the last 30 days with site and event name

SELECT time.ts(a.time),
       d.evalink_device_id,
       a.alarm_code,
       ed.name_translated
FROM analytics.alarm a
  JOIN analytics.device d USING (device_id)
  JOIN analytics.event_definition ed USING (event_code)
WHERE a.time BETWEEN time.ago('30 days') AND time.now();

Alarms per type and day of the last month

SELECT time.format(time.truncate(a.time, 'day'), 'YYYY-MM-DD') AS day,
       a.alarm_type,
       COUNT(*)
FROM analytics.alarm a
WHERE time.last(a.time, '30 days')
  AND a.alarm_value = '1'
  AND a.test_message = '0'
GROUP BY 1, 2
ORDER BY 1, 2;

Steps and results of one workflow

SELECT w.id,
       w.workflow_template_name,
       w.step,
       w.step_title,
       w.step_type,
       w.step_description,
       w.step_attributes,
       w.step_attributes."contactId" AS contact_id,
       w.step_reference,
       w.step_status,
       w.step_completed_at,
       w.step_result_json,
       w.step_result_json.calls[0]."contactId" AS call_0_contact
FROM analytics.workflow_analyzer_by_id w
WHERE w.id = '<workflow id>'
ORDER BY w.step;

Recent workflows of a site with their main and attached alarms

SELECT w.id,
       time.format(w.time),
       w.workflow_template_name,
       p_alarm.alarm_code AS primary_alarm_code,
       a_alarm.alarm_code AS attached_alarm_code
FROM analytics.workflow_by_device w
  JOIN analytics.attached_alarm aa USING (workflow_id, time)
  JOIN analytics.alarm_by_id p_alarm ON p_alarm.id = w.alarm_id
  JOIN analytics.alarm_by_id a_alarm ON a_alarm.id = aa.alarm_id
WHERE time.last(w.time, '30 days')
  AND w.device_id = '<site id>';

Event log of a site with workflow, user and alarm creation time

WITH period AS (
  SELECT time.ago('30 days') AS from_ms,
         time.now() AS to_ms
)
SELECT el.id,
       time.format(el.time),
       el.type,
       el.full_title,
       a.alarm_code,
       w.workflow_template_name,
       u.first_name || ' ' || u.last_name AS user_name,
       time.format(ah.time) AS alarm_created_time
FROM analytics.event_log_by_device el
  JOIN period ON TRUE
  LEFT JOIN analytics.alarm_by_id a ON a.id = el.alarm_id
  LEFT JOIN analytics.workflow_by_id w ON w.id = el.workflow_id
  LEFT JOIN analytics.user u ON u.id = w.editing_user_id
  LEFT JOIN analytics.alarm_history_item ah
    ON ah.alarm_id = a.id AND ah.time = a.time AND ah.type = 'ALARM_CREATION'
WHERE el.time BETWEEN period.from_ms AND period.to_ms
  AND ah.time BETWEEN period.from_ms AND period.to_ms -- makes the history lookup fast
  AND el.device_id = '<site id>';

Latest workflow of every site this year

WITH latest_workflow AS (
  SELECT *
  FROM (
    SELECT w.*,
           ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY time DESC) AS rn
    FROM analytics.workflow_by_device w
    WHERE w.time BETWEEN time.start_of('year') AND time.now()
  ) sub
  WHERE rn = 1
)
SELECT w.id,
       time.format(w.time),
       d.evalink_device_id,
       a.alarm_type,
       a.partition_name,
       a.alarm_code,
       ed.name_translated
FROM latest_workflow w
  JOIN analytics.alarm_by_id a USING (alarm_id)
  JOIN analytics.event_definition ed USING (event_code)
  JOIN analytics.device d ON d.id = w.device_id;