|
| 1 | +package io.prediction.e2.eventdistributionchecker |
| 2 | + |
| 3 | +import io.prediction.controller.PDataSource |
| 4 | +import io.prediction.controller.EmptyEvaluationInfo |
| 5 | +import io.prediction.controller.EmptyActualResult |
| 6 | +import io.prediction.controller.Params |
| 7 | +import io.prediction.data.storage.Event |
| 8 | +import io.prediction.data.storage.Storage |
| 9 | +import io.prediction.workflow.StopAfterReadInterruption |
| 10 | + |
| 11 | +import org.apache.spark.SparkContext |
| 12 | +import org.apache.spark.SparkContext._ |
| 13 | +import org.apache.spark.rdd.RDD |
| 14 | +import org.apache.spark.FutureAction |
| 15 | +import org.apache.spark.rdd.AsyncRDDActions |
| 16 | +import org.apache.spark.storage.StorageLevel.MEMORY_ONLY_SER |
| 17 | +import org.apache.spark.storage.StorageLevel.MEMORY_ONLY |
| 18 | +import org.apache.spark.Accumulator |
| 19 | +import org.apache.spark.Accumulable |
| 20 | + |
| 21 | +import grizzled.slf4j.Logger |
| 22 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 23 | +import scala.concurrent.Await |
| 24 | +import scala.concurrent.duration._ |
| 25 | + |
| 26 | +import com.github.nscala_time.time.Imports._ |
| 27 | +import org.apache.spark.mllib.linalg.Vector |
| 28 | +import org.apache.spark.mllib.stat.{MultivariateStatisticalSummary, Statistics} |
| 29 | +import org.apache.spark.mllib.linalg.Vectors |
| 30 | +import org.joda.time.Days |
| 31 | +import org.joda.time.Hours |
| 32 | + |
| 33 | + |
| 34 | +case class DataSourceParams( |
| 35 | + appId: Int, |
| 36 | + sample: Option[Int] |
| 37 | +) extends Params |
| 38 | + |
| 39 | + |
| 40 | +abstract class AbstractChecker extends Serializable { |
| 41 | + def check(): Unit |
| 42 | +} |
| 43 | + |
| 44 | +class BasicChecker(ds: DataSource, eventsRDD: RDD[Event], sample: Option[Int]) |
| 45 | + extends AbstractChecker { |
| 46 | + @transient lazy val logger = Logger[this.type] |
| 47 | + |
| 48 | + import io.prediction.e2.eventdistributionchecker.AggEvent.rddToEventRDD |
| 49 | + import io.prediction.e2.eventdistributionchecker.AggEvent.rddToKeyedEventRDD |
| 50 | + |
| 51 | + val _sample = sample.getOrElse(0) |
| 52 | + |
| 53 | + val fTypeDist: FutureAction[Seq[((String, Option[String]), AggEvent.Result)]] = |
| 54 | + eventsRDD |
| 55 | + .map(e => ((e.entityType, e.targetEntityType), e)) |
| 56 | + .countAndSample(_sample) |
| 57 | + .collectAsync() |
| 58 | + |
| 59 | + val fEventTimeDist = eventsRDD |
| 60 | + .map{ e => (e.eventTime.toString("yyyy-MM"), e) } |
| 61 | + .countAndSample(_sample) |
| 62 | + .collectAsync() |
| 63 | + |
| 64 | + val fEventTimeNameDist = eventsRDD |
| 65 | + .map{ e => ((e.eventTime.toString("yyyy-MM"), e.event), e) } |
| 66 | + .countAndSample(_sample) |
| 67 | + .collectAsync() |
| 68 | + |
| 69 | + val fEventTimeNameTypeDist = eventsRDD |
| 70 | + .map{ e => |
| 71 | + ((e.eventTime.toString("yyyy-MM"), e.event, e.entityType, e.targetEntityType), e) |
| 72 | + } |
| 73 | + .countAndSample(_sample) |
| 74 | + .collectAsync() |
| 75 | + |
| 76 | + val fTypeDistinctId = EventUtils.distinctEntityId( |
| 77 | + eventsRDD, _.entityType).collectAsync() |
| 78 | + |
| 79 | + def check(): Unit = { |
| 80 | + logger.info("Entity Type Distribution") |
| 81 | + AggEvent.print(fTypeDist.get(), true) |
| 82 | + |
| 83 | + |
| 84 | + logger.info("Event Time Distribution") |
| 85 | + AggEvent.print(fEventTimeDist.get(), true) |
| 86 | + |
| 87 | + logger.info("Event (Time, Name, EntityType, TargetEntityType) Distribution") |
| 88 | + AggEvent.print(fEventTimeNameTypeDist.get(), true) |
| 89 | + |
| 90 | + logger.info("Event (EntityType) Distinct Id") |
| 91 | + val typeDistinctIdStats = EventUtils.distStats(fTypeDistinctId.get(), true) |
| 92 | + logger.info(typeDistinctIdStats) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +class DataSource(val dsp: DataSourceParams) |
| 98 | + extends PDataSource[TrainingData, |
| 99 | + EmptyEvaluationInfo, Query, EmptyActualResult] { |
| 100 | + |
| 101 | + @transient lazy val logger = Logger[this.type] |
| 102 | + @transient lazy val eventsDb = Storage.getPEvents() |
| 103 | + |
| 104 | + override |
| 105 | + def readTraining(sc: SparkContext): TrainingData = { |
| 106 | + val eventsRDD: RDD[Event] = eventsDb.find(appId = dsp.appId)(sc) |
| 107 | + .setName("EventsRDD") |
| 108 | + .cache |
| 109 | + |
| 110 | + logger.info(s"EventsCount: ${eventsRDD.count}") |
| 111 | + |
| 112 | + val checkers = Seq[AbstractChecker]( |
| 113 | + new BasicChecker(this, eventsRDD, sample=dsp.sample) |
| 114 | + ) |
| 115 | + |
| 116 | + checkers.foreach(_.check) |
| 117 | + |
| 118 | + throw new StopAfterReadInterruption() |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +class TrainingData( |
| 123 | + val events: RDD[Event] |
| 124 | +) extends Serializable { |
| 125 | + override def toString = { |
| 126 | + s"events: [${events.count()}] (${events.take(2).toList}...)" |
| 127 | + } |
| 128 | +} |
0 commit comments