Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def merge(prev: AndroidAggregatedReportDB, obtained: AndroidReport) = {
- var deviceInfos: List[DeviceInfo] = prev.userDevice.info
- var reportsTimestams = prev.reportsTimestamps
- var reportingApp: AppInfo = null
- if (deviceInfos.head.sdkInt != obtained.userDevice.sdkInt || deviceInfos.head.language != obtained.userDevice.language || deviceInfos.head.secondsFromGMT != obtained.userDevice.secondsFromGMT) {
- deviceInfos = DeviceInfo(obtained.userDevice.sdkInt, obtained.userDevice.language, obtained.userDevice.secondsFromGMT, obtained.createdAt) :: deviceInfos
- }
- reportsTimestams = obtained.createdAt :: reportsTimestams
- reportingApp = AppInfo(obtained.reportingApp.packageName, obtained.reportingApp.versionCode, obtained.reportingApp.isSystem)
- val grouppedInstallationsList: Map[String, List[AppInstallation]] = obtained.installedAndUpdatedApps.groupBy(_.packageName)
- val grouppedUninstallationsList: Map[String, List[AppUninstallation]] = obtained.uninstalledApps.groupBy(_.packageName)
- val grouppedActionsList: Map[String, List[Any]] = (grouppedInstallationsList.toSeq ++ grouppedUninstallationsList.toSeq).groupBy(_._1).mapValues(_.map(_._2).flatten.toList)
- val mappedActionsList: List[ActionsAggregatorHelper] = grouppedActionsList.map { x =>
- val (tmpPackageName, action) = x
- var tmpVersionCode = -1L
- var tmpIsSystem = false
- val mappedActions: List[UserAction] = action.map {
- case app: AppInstallation =>
- if (app.versionCode > tmpVersionCode) {
- tmpVersionCode = app.versionCode
- tmpIsSystem = app.isSystem
- }
- if (app.lastUpdateTime > 0) {
- List(UserAction(app.installationTime, "installed"), UserAction(app.lastUpdateTime, "updated"))
- } else {
- List(UserAction(app.installationTime, "installed"))
- }
- case app: AppUninstallation =>
- List(UserAction(app.uninstallationTime, "uninstalled"))
- }.flatten
- ActionsAggregatorHelper(tmpPackageName, tmpVersionCode, tmpIsSystem, mappedActions.sortBy(_.timestamp))
- }.toList
- var apps: List[AppData] = List()
- for (action <- mappedActionsList) {
- def selector(s: AppData) = s.info.packageName == action.packageName
- val appData: Option[AppData] = prev.apps.find(selector).map {
- el =>
- val nev: List[UserAction] = action.userActions.filter(_.timestamp > el.userActions.head.timestamp)
- el.copy(userActions = nev ::: el.userActions, info = AppInfo(action.packageName, action.versionCode, action.isSystem))
- }.orElse {
- Option(AppData(AppInfo(action.packageName, action.versionCode, action.isSystem), List(), action.userActions))
- }
- apps = appData.get :: apps
- }
- for (appData <- prev.apps) {
- def selector(s: AppData) = s.info.packageName == appData.info.packageName
- if (apps.find(selector).isEmpty) {
- apps = appData :: apps
- }
- }
- var appsMerged: List[AppData] = List()
- for (appData <- apps) {
- def selector(s: AppUsage) = s.packageName == appData.info.packageName
- obtained.usedApps.find(selector).map {
- el =>
- val appMerged = appData.copy(usages = el.usages ::: appData.usages)
- appsMerged = appMerged :: appsMerged
- }.orElse {
- appsMerged = appData :: appsMerged
- Option()
- }
- }
- AndroidAggregatedReportDB(prev.adid, appsMerged, reportingApp, reportsTimestams, prev.userDevice.copy(info = deviceInfos))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement