---
apiVersion: batch/v1
kind: Job
metadata:
  name: trews-capture
spec:
  template: # create pods using pod definition in this template
    metadata:
      name: trews-capture
      namespace: default
    spec:
      restartPolicy: Never
      nodeSelector:
        opsdx_nodegroup: spot-nodes
      containers:
      - name: universe
        image: 359300513585.dkr.ecr.us-east-1.amazonaws.com/utilities:1.0.211
        imagePullPolicy: IfNotPresent
        command:
          - sh
          - -c
          - "aws s3 cp s3://opsdx-deployment/scripts/trews-capture.sh /tmp && chmod 755 /tmp/trews-capture.sh && /tmp/trews-capture.sh"
        env:
        - name: capture_query # Must return a single column of pat_ids
          value: "select distinct E.pat_id from get_alert_stats_by_enc(now() - interval '6 hours', now()) E inner join cdm_s on cdm_s.enc_id = E.enc_id and cdm_s.fid = 'age' inner join cdm_t on cdm_t.enc_id = E.enc_id and cdm_t.fid = 'care_unit' group by E.pat_id, E.enc_id having count(*) filter (where cdm_s.value::numeric < 18) = 0 and count(*) filter(where cdm_t.fid in ('HCGH LABOR & DELIVERY', 'HCGH EMERGENCY-PEDS', 'HCGH 2C NICU', 'HCGH 1CX PEDIATRICS', 'HCGH 2N MCU')) = 0"
        - name: query_id
          value: "dev.capture.test"
        - name: output_bucket
          value: "opsdx-clarity-etl-stage"
        - name: output_dir
          value: "trews-capture"
        - name: trews_url
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: trews-url
        - name: db_host
          value: "dev.db.jh.opsdx.io"
        - name: db_port
          value: "5432"
        - name: db_name
          value: "opsdx_dev"
        - name: db_user
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: db-username
        - name: db_password
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: db-password
        - name: AWS_DEFAULT_REGION
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: aws-region
        - name: AWS_ACCESS_KEY_ID
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: aws-access-key-id
        - name: AWS_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: trews-capture-secrets
              key: aws-secret-access-key
        securityContext:
          privileged: true
