package com.gmei

import java.io.Serializable

import org.apache.spark.sql.{SaveMode, TiContext}
import org.apache.log4j.{Level, Logger}
import scopt.OptionParser
import com.gmei.lib.AbstractParams

object WeafareStat {

  Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
  Logger.getLogger("org.apache.eclipse.jetty.server").setLevel(Level.OFF)

  case class Params(env: String = "dev"
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("WeafareStat")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    note(
      """
        |For example, the following command runs this app on a tidb dataset:
        |
        | spark-submit --class com.gmei.WeafareStat ./target/scala-2.11/feededa-assembly-0.1.jar \
      """.stripMargin +
        s"|   --env ${defaultParams.env}"
    )
  }


  def main(args: Array[String]): Unit = {
    parser.parse(args, defaultParams).map { param =>
      GmeiConfig.setup(param.env)
      val spark_env = GmeiConfig.getSparkSession()
      val sc = spark_env._2

      val ti = new TiContext(sc)
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "diary_video")
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")

      import sc.implicits._

      val video_cids = sc.sql(
        s"""
           |select distinct(cid_id) as cid_id
           |from jerry_prod.data_feed_click
           |where cid_type = 'diary'
           |and  cid_id  in (select cid from jerry_prod.diary_video where stat_date='2018-10-17')
           |and stat_date ='2018-10-17'
         """.stripMargin
      )
      video_cids.show()
      video_cids.createOrReplaceTempView("tmp1")

      val txt_cids = sc.sql(
        s"""
           |select distinct(cid_id) as cid_id
           |from jerry_prod.data_feed_click
           |where cid_type = 'diary'
           |and  cid_id  not in (select cid from jerry_prod.diary_video where stat_date='2018-10-17')
           |and stat_date ='2018-10-17'
         """.stripMargin
      )
      txt_cids.show()
      txt_cids.createOrReplaceTempView("tmp2")

      val video_count = sc.sql(
        s"""
           |select count(page_name) as vd_count
           |from online.bl_hdfs_page_view_updates pv inner join tmp1
           |on pv.referrer_id = tmp1.cid_id
           |where pv.partition_date = '20181017'
           |and pv.page_name='welfare_detail'
           |and pv.referrer='diary_detail'
         """.stripMargin
      )
      video_count.show()
      val output1 = "/home/gaoyazhe/test_vd_cids.csv"
      video_count.repartition(1).write.format("com.databricks.spark.csv").option("header","true").save(output1)

      val txt_count = sc.sql(
        s"""
           |select count(page_name) as txt_count
           |from online.bl_hdfs_page_view_updates pv inner join tmp2
           |on pv.referrer_id = tmp2.cid_id
           |where pv.partition_date = '20181017'
           |and pv.page_name='welfare_detail'
           |and pv.referrer='diary_detail'
         """.stripMargin
      )
      txt_count.show()
      val output2 = "/home/gaoyazhe/test_txt_cids.csv"
      txt_count.repartition(1).write.format("com.databricks.spark.csv").option("header","true").save(output2)


      sc.stop()

    }
  }


}
