Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.52 KB | None | 0 0
  1.   def merge(prev: AndroidAggregatedReportDB, obtained: AndroidReport) = {
  2.     var deviceInfos: List[DeviceInfo] = prev.userDevice.info
  3.     var reportsTimestams = prev.reportsTimestamps
  4.     var reportingApp: AppInfo = null
  5.  
  6.     if (deviceInfos.head.sdkInt != obtained.userDevice.sdkInt || deviceInfos.head.language != obtained.userDevice.language || deviceInfos.head.secondsFromGMT != obtained.userDevice.secondsFromGMT) {
  7.       deviceInfos = DeviceInfo(obtained.userDevice.sdkInt, obtained.userDevice.language, obtained.userDevice.secondsFromGMT, obtained.createdAt) :: deviceInfos
  8.     }
  9.     reportsTimestams = obtained.createdAt :: reportsTimestams
  10.     reportingApp = AppInfo(obtained.reportingApp.packageName, obtained.reportingApp.versionCode, obtained.reportingApp.isSystem)
  11.  
  12.  
  13.     val grouppedInstallationsList: Map[String, List[AppInstallation]] = obtained.installedAndUpdatedApps.groupBy(_.packageName)
  14.     val grouppedUninstallationsList: Map[String, List[AppUninstallation]] = obtained.uninstalledApps.groupBy(_.packageName)
  15.     val grouppedActionsList: Map[String, List[Any]] = (grouppedInstallationsList.toSeq ++ grouppedUninstallationsList.toSeq).groupBy(_._1).mapValues(_.map(_._2).flatten.toList)
  16.  
  17.     val mappedActionsList: List[ActionsAggregatorHelper] = grouppedActionsList.map { x =>
  18.       val (tmpPackageName, action) = x
  19.       var tmpVersionCode = -1L
  20.       var tmpIsSystem = false
  21.       val mappedActions: List[UserAction] = action.map {
  22.         case app: AppInstallation =>
  23.           if (app.versionCode > tmpVersionCode) {
  24.             tmpVersionCode = app.versionCode
  25.             tmpIsSystem = app.isSystem
  26.           }
  27.           if (app.lastUpdateTime > 0) {
  28.             List(UserAction(app.installationTime, "installed"), UserAction(app.lastUpdateTime, "updated"))
  29.           } else {
  30.             List(UserAction(app.installationTime, "installed"))
  31.           }
  32.         case app: AppUninstallation =>
  33.           List(UserAction(app.uninstallationTime, "uninstalled"))
  34.       }.flatten
  35.       ActionsAggregatorHelper(tmpPackageName, tmpVersionCode, tmpIsSystem, mappedActions.sortBy(_.timestamp))
  36.     }.toList
  37.  
  38.     var apps: List[AppData] = List()
  39.     for (action <- mappedActionsList) {
  40.       def selector(s: AppData) = s.info.packageName == action.packageName
  41.       val appData: Option[AppData] = prev.apps.find(selector).map {
  42.         el =>
  43.           val nev: List[UserAction] = action.userActions.filter(_.timestamp > el.userActions.head.timestamp)
  44.           el.copy(userActions = nev ::: el.userActions, info = AppInfo(action.packageName, action.versionCode, action.isSystem))
  45.       }.orElse {
  46.         Option(AppData(AppInfo(action.packageName, action.versionCode, action.isSystem), List(), action.userActions))
  47.       }
  48.       apps = appData.get :: apps
  49.     }
  50.  
  51.     for (appData <- prev.apps) {
  52.       def selector(s: AppData) = s.info.packageName == appData.info.packageName
  53.       if (apps.find(selector).isEmpty) {
  54.         apps = appData :: apps
  55.       }
  56.     }
  57.  
  58.     var appsMerged: List[AppData] = List()
  59.     for (appData <- apps) {
  60.       def selector(s: AppUsage) = s.packageName == appData.info.packageName
  61.       obtained.usedApps.find(selector).map {
  62.         el =>
  63.           val appMerged = appData.copy(usages = el.usages ::: appData.usages)
  64.           appsMerged = appMerged :: appsMerged
  65.       }.orElse {
  66.         appsMerged = appData :: appsMerged
  67.         Option()
  68.       }
  69.     }
  70.  
  71.     AndroidAggregatedReportDB(prev.adid, appsMerged, reportingApp, reportsTimestams, prev.userDevice.copy(info = deviceInfos))
  72.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement