#!/bin/bash

#temp=$(kubectl get svc -n dsk-middle -o wide | grep elasticsearch | grep NodePort | awk {'print $5'})
#export ES_NODEPORT=${temp:5:(-4)}
ES_NODEPORT=`kubectl get svc -n dsk-middle |grep elasticsearch | grep NodePort | awk -F '9200:' {'print $2'} | awk -F '/' '{print $1}'`
echo $ES_NODEPORT

export MASTER_IP=$(kubectl get nodes -o wide | grep control-plane | awk {'print $6'} | head -1)
echo $MASTER_IP

export NUM_SHARDS=2
export NUM_REPLICAS=1

SECURE=true
SECURE=false

if [ $SECURE = true ]
then
PARAM="-u elastic:elastic --insecure"
PROTO="https"
else
PARAM=""
PROTO="http"
fi

echo Secure=$SECURE
echo Param=$PARAM
echo Proto=$PROTO

echo 'cat indices'
echo "curl ${PARAM} -X GET ${PROTO}://${MASTER_IP}:${ES_NODEPORT}/_cat/indices"
curl ${PARAM} -X GET ${PROTO}://${MASTER_IP}:${ES_NODEPORT}/_cat/indices


######### Elasticsearch template 정의 #########
echo 'ilm policy trace_span'
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/trace_span' -H 'Content-Type: application/json' -d '{
    "policy": {
        "phases": {
            "delete": {
                "min_age": "3d",
                "actions": {
                    "delete": {}
                }
            }
        }
    }
}'
echo '\n'

echo 'template trace_span'
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/trace_span' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "trace_span-*"
    ],
    "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "1",
            "refresh_interval": "1s",
            "lifecycle": {
                "name": "trace_span"
            }
        }
    },
    "mappings": {
        "properties": {
            "tenantId": {
                "type": "keyword"
            },
            "userId": {
                "type": "keyword"
            },
            "hostKey": {
                "type": "keyword"
            },
            "agentKey": {
                "type": "keyword"
            },
            "agentKind": {
                "type": "keyword"
            },
            "agentVersion": {
                "type": "keyword"
            },
            "traceStreamID": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "traceID": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "spanID": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "operationName": {
                "type": "keyword"
            },
            "processID": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "processStreamID": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "references": {
                "properties": {
                    "trace_id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    },
                    "span_id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    }
                }
            },
            "startTime": {
                "type": "long"
            },
            "duration": {
                "type": "long"
            },
            "tags": {
                "properties": {
                    "type": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    },
                    "value": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    },
                    "key": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "ignore_above": 256,
                                "type": "keyword"
                            }
                        }
                    },
                    "v_type": {
                        "type": "long"
                    }
                }
            },
            "logs": {
                "properties": {
                    "fields": {
                        "properties": {
                            "v_str": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "ignore_above": 256,
                                        "type": "keyword"
                                    }
                                }
                            },
                            "key": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "ignore_above": 256,
                                        "type": "keyword"
                                    }
                                }
                            }
                        }
                    },
                    "timestamp": {
                        "type": "date"
                    }
                }
            }
        }
    }
}'
echo '\n'

## log-message
echo 'ilm policy log-message'
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/log-message' -H 'Content-Type: application/json' -d '{
    "policy": {
        "phases": {
            "delete": {
                "min_age": "3d",
                "actions": {
                    "delete": {}
                }
            }
        }
    }
}'
echo '\n'

echo 'template log-message'
curl $PARAM -X PUT $PROTO'://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/log-message' -H 'Content-Type: application/json' -d '{
    "order": 0,
    "index_patterns": [
        "log-message-*"
    ],
    "settings": {
        "index": {
            "lifecycle": {
                "name": "log-message"
            },
            "refresh_interval": "1s",
            "number_of_shards": "2",
            "number_of_replicas": "1"
        }
    },
    "mappings": {
        "properties": {
            "usr_app_kind": {
                "type": "keyword"
            },
            "dsk_usr_id": {
                "type": "keyword"
            },
            "dsk_agent_name": {
                "type": "keyword"
            },
            "dsk_agent_key": {
                "type": "keyword"
            },
            "usr_tag": {
                "type": "keyword"
            },
            "dsk_host_key": {
                "type": "keyword"
            },
            "usr_app_name": {
                "type": "keyword"
            },
            "dsk_cluster_id": {
                "type": "keyword"
            },
            "message": {
                "type": "text"
            },
            "dsk_usr_tenant": {
                "type": "keyword"
            },
            "dsk_log_path": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "ignore_above": 256,
                        "type": "keyword"
                    }
                }
            },
            "dsk_timestamp": {
                "type": "long"
            }
        }
    }
}'
echo '\n'
