Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TG-375: ReplayIssueCertificates job #167

Open
wants to merge 6 commits into
base: release-3.1.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions adhoc-scripts/src/main/scala/ReplayIssueCertificates.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.UUID

import com.datastax.spark.connector._
import com.datastax.spark.connector.rdd.CassandraTableScanRDD
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.ekstep.analytics.framework.{Dispatcher, FrameworkContext, OutputDispatcher}
import org.ekstep.analytics.framework.util.{CommonUtil, JSONUtils}

object ReplayIssueCertificates {

case class Event(eid: String, ets:Long, mid: String, actor: Map[String, AnyRef], context: Map[String, AnyRef], `object`: Map[String, AnyRef], edata: Map[String, AnyRef])

val batchSize = 10
def prepareEvent(batchId: String, courseId: String, usersList: List[List[String]], reIssue: Boolean): Seq[String] = {
usersList.map(users => {
val actor = Map("id" -> "Course Certificate Generator", "type" -> "System")
val context = Map("id" -> "org.sunbird.platform", "ver" -> "1.0")
val obj = Map("id" -> (batchId + "_" + courseId), "type" -> "CourseCertificateGeneration")
val edata = Map("batchId" -> batchId, "courseId" -> courseId, "userIds" -> users, "action" -> "issue-certificate", "iteration" -> 1.asInstanceOf[AnyRef], "reIssue"-> reIssue.asInstanceOf[AnyRef])
val event = Event("BE_JOB_REQUEST", System.currentTimeMillis, ("LP." + System.currentTimeMillis + "." + UUID.randomUUID), actor, context, obj, edata)
JSONUtils.serialize(event)
}).toSeq
}

def main(sc: SparkContext, batchId: String, courseId: String, env: String, kafkaBrokerList: String, reIssue: Boolean = false): Unit = {
implicit val sparkContext = sc
implicit val fc = new FrameworkContext()
val data = sc.cassandraTable("sunbird_courses", "user_enrolments").select("userid", "batchid", "courseid", "status").where("courseid = ?", courseId).where("batchid = ?", batchId).cache()
val userIds = data.collect().map(row => row.getString("userid")).toList
val usersList = userIds.grouped(batchSize).toList
val event: RDD[String] = sc.parallelize[String](prepareEvent(batchId, courseId, usersList, reIssue))
val config = Map("topic" -> (env +".coursebatch.certificate.request"), "brokerList" -> kafkaBrokerList)
OutputDispatcher.dispatch(Dispatcher("kafka", config), event);
println("Number of events pushed are: " + usersList.size)
}

}