#!/bin/bash

#------------------------------------------------------------------------------------------------------
__init (){
#  pwd
#  cd ${git_path}
#  pwd
  cd /home/jhjung/git/dsk-iac/scripts/steampipe_iac/

  git pull

  datetime=`date "+%Y.%m.%d %H:%M:%S"`
  echo -e "*기준 시간 : ${datetime}\n" > ${file}
  cat ${origin} >> ${file}
}
#------------------------------------------------------------------------------------------------------
__git_push (){
  git add ${file}
  git commit -m 'steampipe schedule'
  git push
}
#------------------------------------------------------------------------------------------------------
__append (){
  line_count=`cat ${exec_log} | grep -v -- -- | egrep -v '(name|ri_count)' | wc -l`
  echo -e "\n${title}  [${line_count}]\n" >> ${file}
  cat ${exec_log} >> ${file}
}
#------------------------------------------------------------------------------------------------------
__query_exec (){
  steampipe query "${1}" > ${exec_log}
  __log_sed
}
#------------------------------------------------------------------------------------------------------
__log_sed (){
  sed -i 's/+/|/g' ${exec_log}
  sed -i "s/node-role.kubernetes.io\///g" ${exec_log}
  sed -i '1d;$d' ${exec_log}
}
#------------------------------------------------------------------------------------------------------
node_query="""
SELECT
    name,
    annotations ->> 'projectcalico.org/IPv4Address' AS IP,
    COALESCE(taints -> 0 ->> 'key', '-') AS Taints_key,
    COALESCE(tags ->> 'kops.k8s.io/instancegroup', '-') AS Instance_group,
    capacity ->> 'cpu' AS CPU,
    CEIL((CAST(regexp_replace(capacity ->> 'memory', 'Ki', '') AS FLOAT) / 1024 / 1024)) AS Memory,
    tags ->> 'topology.kubernetes.io/zone' AS Zone,
    tags ->> 'beta.kubernetes.io/instance-type' AS Instance_type,
    node_info ->> 'osImage' AS OS,
    node_info ->> 'kubeletVersion' AS K8S_ver,
    node_info ->> 'containerRuntimeVersion' AS Runtime_ver
FROM
    kubernetes_node
ORDER BY
    Taints_key
"""

resources_query="""
(SELECT
    'sts' as kind,
    name,
    available_replicas as count,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'cpu' AS request_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'memory' AS request_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'cpu' AS limit_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'memory' AS limit_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') ->> 'name' AS c_name,
    namespace

FROM
    kubernetes_stateful_set
WHERE
    name not like 'rel-%')
union
(SELECT
    'deploy' as kind,
    name,
    available_replicas as count,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'cpu' AS request_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'memory' AS request_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'cpu' AS limit_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'memory' AS limit_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') ->> 'name' AS c_name,
    namespace
FROM
    kubernetes_deployment
WHERE
    name not like 'rel-%')
union
(SELECT
    'ds' as kind,
    name,
    number_available as count,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'cpu' AS request_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'requests' ->> 'memory' AS request_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'cpu' AS limit_cpu,
    jsonb_array_elements(template -> 'spec' -> 'containers') -> 'resources' -> 'limits' ->> 'memory' AS limit_mem,
    jsonb_array_elements(template -> 'spec' -> 'containers') ->> 'name' AS c_name,
    namespace
FROM
    kubernetes_daemonset
WHERE
    name not like 'rel-%')
order by kind
"""

service_query="""
SELECT
  name,
  namespace,
  type,
  lower(p ->> 'nodePort') as Node_Port,
  age(current_timestamp, creation_timestamp)
FROM
  kubernetes_service,
  jsonb_array_elements(ports) as p
WHERE 
  type='NodePort'
ORDER BY
  Node_Port
"""
  #name not like '%rel-%'

aws_ri_query="""
SELECT
    COALESCE(a.availability_zone, b.availability_zone, '-') AS availability_zone,
    COALESCE(a.instance_type, b.instance_type, c.instance_type, '-') AS instance_type,
    COALESCE(c.cpu, 0) AS cpu,
    COALESCE(c.memory, 0) AS memory,
    COALESCE(a.ri_count, 0) AS ri_count,
    COALESCE(b.ec2_count, 0) AS ec2_count,
    COALESCE(b.ec2_count, 0) - COALESCE(a.ri_count, 0) AS result
FROM
    (SELECT
        availability_zone,
        instance_type,
        SUM(instance_count) AS ri_count
     FROM
        aws_ec2_reserved_instance
     WHERE
        instance_state='active'
     GROUP BY
        availability_zone,
        instance_type
    ) a
FULL OUTER JOIN
    (SELECT
        placement_availability_zone AS availability_zone,
        instance_type,
        COUNT(*) AS ec2_count
     FROM
        aws_ec2_instance
     WHERE
        instance_state='running' AND
        instance_lifecycle!='spot'
     GROUP BY
        availability_zone,
        instance_type
    ) b
ON
    a.availability_zone = b.availability_zone AND
    a.instance_type = b.instance_type
INNER JOIN
    (SELECT
        instance_type,
        (CAST(memory_info ->> 'SizeInMiB' AS FLOAT) / 1024) AS memory,
        (CAST(v_cpu_info ->> 'DefaultCores' AS FLOAT) * 2) AS cpu
     FROM
        aws_ec2_instance_type
     WHERE
        instance_type in (SELECT instance_type FROM aws_ec2_instance WHERE instance_state='running')
     GROUP BY
        instance_type, memory, cpu
    ) c
ON
    COALESCE(a.instance_type, b.instance_type, '-') = c.instance_type
ORDER BY availability_zone
"""
        #instance_type in (SELECT instance_type FROM aws_ec2_instance WHERE instance_state='running' AND instance_lifecycle!='spot')
#------------------------------------------------------------------------------------------------------
#git_path="/home/jhjung/git/dsk-iac"
origin="org_README.md"
exec_log="query.log"
file="../../README.md"
#------------------------------------------------------------------------------------------------------
__init

title="## 노드 목록"
__query_exec "${node_query}"
__append

title="## 리소스 목록"
__query_exec "${resources_query}"
__append

title="## 서비스 목록 (NodePort)"
__query_exec "${service_query}"
__append

title="## 예약 인스턴스 사용 내역"
__query_exec "${aws_ri_query}"
__append
#------------------------------------------------------------------------------------------------------
rm ${exec_log}

__git_push
