package com.redoop.mlsql
import java.net.URLEncoder
import java.nio.charset.Charset
import net.sf.json.JSONObject
import org.apache.http.client.fluent.{Form, Request}
import tech.mlsql.utils.PathFun
import com.google.gson.{JsonObject, JsonParser}
object RunScriptCommand {
var consoleUrl = s"http://192.168.72.128:9002"
var auth_secret = s"mlsql"
var owner = s"test@redoop.com"

def main(args: Array[String]): Unit = {
var scriptfile = args(0)
var start = args(1)
var end = args(2)

var id = getScriptId(scriptfile)

var scriptFileGetUrl = PathFun(consoleUrl).add(s"/api_v1/script_file/get?id=${id}").toPath
val script = Request.Get(scriptFileGetUrl)
.connectTimeout(60 * 1000)
.socketTimeout(10 * 60 * 1000).addHeader("access-token", auth_secret)
.execute().returnContent().asString()

val obj = JSONObject.fromObject(script)
val scriptContent = obj.getString("content")
//val owner = obj.getString("owner")
// 赋值
var newScriptContent = scriptContent.replace("&start",start).replace("&end",end)

var runScriptUrl = PathFun(consoleUrl).add(s"/api_v1/run/script").toPath
val res = Request.Post(runScriptUrl).
connectTimeout(60 * 1000).socketTimeout(12 * 60 * 60 * 1000).
addHeader("access-token", auth_secret).
bodyForm(Form.form().add("sql", newScriptContent).
add("owner", owner).build(), Charset.forName("utf8"))
.execute().returnContent().asString()
println(res)
val address = res
val str = address.replaceAll("\\["," ").replaceAll("\\]","")

println(str)
println(gson(str,"end"))

}

def getScriptId(path: String) = {
def encode(str: String) = {
URLEncoder.encode(str, "utf-8")
}

var url = PathFun(consoleUrl).add(s"/api_v1/script_file/path/id?path=${encode(path)}&owner=${encode(owner)}").toPath
println(url)
val script = Request.Get(url)
.connectTimeout(60 * 1000)
.socketTimeout(10 * 60 * 1000).addHeader("access-token", auth_secret)
.execute().returnContent().asString()
script.toInt
}
//
def gson(str: String,name:String) ={
val json = new JsonParser()
val obj = json.parse(str).asInstanceOf[JsonObject]
obj.get(name)

}



}



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>allwefantasy</groupId>
<artifactId>mlsql-api-console</artifactId>
<version>1.5.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.11.8</scala.version>
<scala.binary.version>2.11</scala.binary.version>
<scala.jline.version>2.11.0-M3</scala.jline.version>
<serviceframework.version>2.0.1</serviceframework.version>
</properties>

<profiles>
<profile>
<id>shade</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala.version}</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.11.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.11.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.11.5</version>
</dependency>

<dependency>
<groupId>tech.mlsql</groupId>
<artifactId>streamingpro-api</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>

<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-json_2.11</artifactId>
<version>3.3.0</version>
</dependency>

<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-json-ext_2.11</artifactId>
<version>3.3.0</version>
</dependency>

<dependency>
<groupId>net.csdn</groupId>
<artifactId>serviceframework-jetty-9-server_${scala.binary.version}</artifactId>
<version>${serviceframework.version}</version>
</dependency>
<dependency>
<groupId>net.csdn</groupId>
<artifactId>serviceframework-orm_${scala.binary.version}</artifactId>
<version>${serviceframework.version}</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.json4s</groupId>
<artifactId>json4s-jackson_2.11</artifactId>
<version>3.6.1</version>
</dependency>

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.3.1</version>
</dependency>

<dependency>
<groupId>net.csdn</groupId>
<artifactId>serviceframework-web_2.11</artifactId>
<version>${serviceframework.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>

<!--thrift相关-->
<exclusion>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
</exclusion>
<exclusion>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.scala-lang</groupId>
<artifactId>jline</artifactId>
</exclusion>

<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>repository.com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.11.0</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<includes>
<include>**/*.java</include>
<include>**/*.scala</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-g</compilerArgument>
<verbose>true</verbose>
<encoding>UTF-8</encoding>
</configuration>

</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<configuration>
<args>
<arg>
-g:vars
</arg>
</args>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 要将源码放上去,需要加入这个插件 -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>