Skip to content

Commit

Permalink
Merge pull request #164 from openlattice/task/LATTICE-3064-migrate-sy…
Browse files Browse the repository at this point in the history
…stem-apps

migrate android system apps
  • Loading branch information
geekbeast authored Mar 9, 2022
2 parents 5f1dd21 + ee2e8ee commit 4081d18
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 57 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def PARALLEL = "$System.env.PARALLELISM"
def GC = "$System.env.GC"

if (MECHANIC_XMS == 'null' || MECHANIC_XMS == null || MECHANIC_XMS == "") {
MECHANIC_XMS = '-Xms1g'
MECHANIC_XMS = '-Xms100g'
}

if (MECHANIC_XMX == 'null' || MECHANIC_XMX == null || MECHANIC_XMX == "") {
MECHANIC_XMX = '-Xmx4g'
MECHANIC_XMX = '-Xmx200g'
}

if (MECHANIC_ARGS == 'null' || MECHANIC_ARGS == null || MECHANIC_ARGS == "") {
Expand Down
108 changes: 54 additions & 54 deletions src/main/kotlin/com/openlattice/mechanic/pods/MechanicUpgradePod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,14 @@ import com.openlattice.assembler.AssemblerConfiguration
import com.openlattice.auditing.AuditRecordEntitySetsManager
import com.openlattice.auditing.AuditingConfiguration
import com.openlattice.auditing.pods.AuditingConfigurationPod
import com.openlattice.authorization.AuthorizationManager
import com.openlattice.authorization.DbCredentialService
import com.openlattice.authorization.HazelcastAclKeyReservationService
import com.openlattice.authorization.HazelcastAuthorizationService
import com.openlattice.authorization.HazelcastPrincipalsMapManager
import com.openlattice.authorization.Principals
import com.openlattice.authorization.PrincipalsMapManager
import com.openlattice.authorization.*
import com.openlattice.authorization.mapstores.ResolvedPrincipalTreesMapLoader
import com.openlattice.conductor.rpc.ConductorConfiguration
import com.openlattice.conductor.rpc.ConductorElasticsearchApi
import com.openlattice.data.DataDeletionManager
import com.openlattice.data.EntityKeyIdService
import com.openlattice.data.ids.PostgresEntityKeyIdService
import com.openlattice.data.storage.ByteBlobDataManager
import com.openlattice.data.storage.DataDeletionService
import com.openlattice.data.storage.DataSourceResolver
import com.openlattice.data.storage.EntityDatastore
import com.openlattice.data.storage.IndexingMetadataManager
import com.openlattice.data.storage.*
import com.openlattice.data.storage.postgres.PostgresEntityDataQueryService
import com.openlattice.data.storage.postgres.PostgresEntityDatastore
import com.openlattice.datasets.DataSetService
Expand All @@ -73,6 +63,7 @@ import com.openlattice.mechanic.MechanicCli.Companion.UPGRADE
import com.openlattice.mechanic.Toolbox
import com.openlattice.mechanic.upgrades.DeleteOrgMetadataEntitySets
import com.openlattice.mechanic.upgrades.ExportOrganizationMembers
import com.openlattice.mechanic.upgrades.MigrateChronicleSystemApps
import com.openlattice.mechanic.upgrades.V3StudyMigrationUpgrade
import com.openlattice.organizations.roles.HazelcastPrincipalService
import com.openlattice.organizations.roles.SecurePrincipalsManager
Expand Down Expand Up @@ -207,26 +198,26 @@ class MechanicUpgradePod {
@Bean
fun edmManager(): EdmManager {
return EdmService(
hazelcastInstance,
aclKeyReservationService(),
authorizationService(),
postgresTypeManager(),
schemaManager(),
dataSetService()
hazelcastInstance,
aclKeyReservationService(),
authorizationService(),
postgresTypeManager(),
schemaManager(),
dataSetService()
)
}

@Bean
fun entitySetService(): EntitySetManager {
return EntitySetService(
hazelcastInstance,
eventBus,
aclKeyReservationService(),
authorizationService(),
edmManager(),
hikariDataSource,
dataSetService(),
auditingConfiguration
hazelcastInstance,
eventBus,
aclKeyReservationService(),
authorizationService(),
edmManager(),
hikariDataSource,
dataSetService(),
auditingConfiguration
)
}

Expand All @@ -238,8 +229,8 @@ class MechanicUpgradePod {
@Bean
fun dataQueryService(): PostgresEntityDataQueryService {
return PostgresEntityDataQueryService(
dataSourceResolver(),
byteBlobDataManager
dataSourceResolver(),
byteBlobDataManager
)
}

Expand All @@ -251,19 +242,19 @@ class MechanicUpgradePod {
@Bean
fun idService(): EntityKeyIdService {
return PostgresEntityKeyIdService(
dataSourceResolver(),
idGenerationService()
dataSourceResolver(),
idGenerationService()
)
}

@Bean
fun graphService(): GraphService {
return Graph(
dataSourceResolver(),
entitySetService(),
dataQueryService(),
idService(),
MetricRegistry()
dataSourceResolver(),
entitySetService(),
dataQueryService(),
idService(),
MetricRegistry()
)
}

Expand All @@ -275,25 +266,25 @@ class MechanicUpgradePod {
@Bean
fun entityDatastore(entitySetManager: EntitySetManager): EntityDatastore {
return PostgresEntityDatastore(
dataQueryService(),
edmManager(),
entitySetManager,
metricRegistry,
eventBus,
postgresLinkingFeedbackQueryService(),
lqs()
dataQueryService(),
edmManager(),
entitySetManager,
metricRegistry,
eventBus,
postgresLinkingFeedbackQueryService(),
lqs()
)
}

@Bean
fun dataDeletionService(): DataDeletionManager {
val entitySetService = entitySetService()
return DataDeletionService(
entitySetService,
authorizationService(),
entityDatastore(entitySetService),
graphService(),
jobService(),
entitySetService,
authorizationService(),
entityDatastore(entitySetService),
graphService(),
jobService(),
)
}

Expand Down Expand Up @@ -325,7 +316,7 @@ class MechanicUpgradePod {
}

@Bean
fun dbCredService(): DbCredentialService {
fun dbCredService(): DbCredentialService {
return DbCredentialService(hazelcastInstance, longIdService())
}

Expand Down Expand Up @@ -353,11 +344,11 @@ class MechanicUpgradePod {
@Bean
fun deleteOrgMetadataEntitySets(): DeleteOrgMetadataEntitySets {
return DeleteOrgMetadataEntitySets(
toolbox,
auditRecordEntitySetsManager(),
dataDeletionService(),
entitySetService(),
jobService()
toolbox,
auditRecordEntitySetsManager(),
dataDeletionService(),
entitySetService(),
jobService()
)
}

Expand All @@ -381,10 +372,19 @@ class MechanicUpgradePod {
}

@Bean
fun exportOrganizationMembers() : ExportOrganizationMembers {
fun exportOrganizationMembers(): ExportOrganizationMembers {
return ExportOrganizationMembers(toolbox, hikariDataSource, principalService(), hazelcastInstance)
}

fun migrateChronicleSystemApps(): MigrateChronicleSystemApps {
return MigrateChronicleSystemApps(
toolbox,
rhizomeConfiguration,
entitySetService(),
dataQueryService()
)
}

@PostConstruct
fun post() {
Principals.init(principalService(), hazelcastInstance)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.openlattice.mechanic.upgrades

import com.geekbeast.rhizome.configuration.RhizomeConfiguration
import com.openlattice.data.storage.postgres.PostgresEntityDataQueryService
import com.openlattice.datastore.services.EntitySetManager
import com.openlattice.datastore.services.EntitySetService
import com.openlattice.hazelcast.HazelcastMap
import com.openlattice.mechanic.Toolbox
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.apache.olingo.commons.api.edm.FullQualifiedName
import org.slf4j.LoggerFactory
import java.util.*

/**
* @author alfoncenzioka <[email protected]>
*
* This class migrates Android system apps - apps that are not considered to have been installed by the user and
* need to be filtered from app usage survey data
*/
class MigrateChronicleSystemApps(
val toolbox: Toolbox,
private val rhizomeConfiguration: RhizomeConfiguration,
private val entitySetService: EntitySetManager,
private val dataQueryService: PostgresEntityDataQueryService

) : Upgrade {


companion object {
private val logger = LoggerFactory.getLogger(MigrateChronicleSystemApps::class.java)

private const val ENTITY_SET_NAME = "chronicle_application_dictionary"

private val FULL_NAME_FQN = FullQualifiedName("general.fullname")
private val RECORD_TYPE_FQN = FullQualifiedName("ol.recordtype")

private val CREATE_SYSTEM_APPS_SQL = """
CREATE TABLE IF NOT EXISTS public.system_apps(
app_package_name text NOT NULL,
PRIMARY KEY (app_package_name)
)
""".trimIndent()

private val INSERT_SYSTEM_APPS_SQL = """
INSERT INTO public.system_apps values(?) ON CONFLICT DO NOTHING
""".trimIndent()
}

init {
val hds = getDataSource()
hds.connection.createStatement().use { statement ->
statement.execute(CREATE_SYSTEM_APPS_SQL)
}
}

private fun getDataSource(): HikariDataSource {
val (hikariConfiguration) = rhizomeConfiguration.datasourceConfigurations["chronicle"]!!
val hc = HikariConfig(hikariConfiguration)
return HikariDataSource(hc)
}

override fun upgrade(): Boolean {
val entitySets = HazelcastMap.ENTITY_SETS.getMap(toolbox.hazelcast)
val entitySetId = entitySets.find { it.value.name == ENTITY_SET_NAME }?.key ?: return false

val entitiesToWrite = getEntitiesToWrite(entitySetId)
logger.info("retrieved ${entitiesToWrite.size} system apps")

val hds = getDataSource()
val written = hds.connection.prepareStatement(INSERT_SYSTEM_APPS_SQL).use { ps ->
entitiesToWrite.forEach {
ps.setString(1, it)
ps.addBatch()
}
ps.executeBatch().sum()
}

logger.info("wrote $written entities to system_apps table")
return true
}

private fun getEntitiesToWrite(entitySetId: UUID): Set<String> {
return dataQueryService.getEntitiesWithPropertyTypeFqns(
mapOf(entitySetId to Optional.empty()),
entitySetService.getPropertyTypesOfEntitySets(setOf(entitySetId)),
mapOf(),
setOf(),
Optional.empty(),
false
).values
.filter { it[RECORD_TYPE_FQN]?.iterator()?.next().toString() == "SYSTEM" }
.map { it[FULL_NAME_FQN]?.iterator()?.next().toString() }.toSet()
}

override fun getSupportedVersion(): Long {
return Version.V2021_07_23.value
}
}
2 changes: 1 addition & 1 deletion src/main/resources/aws.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bucket: "lattice-prod-config"
bucket: "openlattice-alpha-config"
folder: "mechanic"
region: us-gov-west-1

0 comments on commit 4081d18

Please sign in to comment.