-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bigquery samtalereferat metrikker (#835)
Lagre følgende metrikker i BigQuery Oppsummering av fordeling av "aktive" samtalereferat på publisert / ikke publisert i big-query tabellen SAMTALEREFERAT_FORDELING Lagre events av typene "Samtalereferat / Mote - opprettet" og "Samtalereferat / Mote - delt med bruker" i big-query tabellen SAMTALEREFERAT_EVENTS --------- Co-authored-by: johannetronstad <[email protected]>
- Loading branch information
Showing
15 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/no/nav/veilarbaktivitet/eventsLogger/BigQueryClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package no.nav.veilarbaktivitet.eventsLogger | ||
|
||
import com.google.cloud.bigquery.BigQueryOptions | ||
import com.google.cloud.bigquery.InsertAllRequest | ||
import com.google.cloud.bigquery.TableId | ||
import no.nav.veilarbaktivitet.aktivitet.domain.AktivitetData | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import java.time.ZoneOffset | ||
import java.time.ZonedDateTime | ||
|
||
enum class EventType { | ||
SAMTALEREFERAT_OPPRETTET, | ||
SAMTALEREFERAT_FIKK_INNHOLD, | ||
SAMTALEREFERAT_DELT_MED_BRUKER, | ||
} | ||
|
||
data class SamtalereferatPublisertFordeling( | ||
val antallPublisert: Int, | ||
val antallIkkePublisert: Int, | ||
) | ||
|
||
interface BigQueryClient { | ||
fun logEvent(aktivitetData: AktivitetData, eventType: EventType) | ||
fun logSamtalereferatFordeling(samtalereferatPublisertFordeling: SamtalereferatPublisertFordeling) | ||
} | ||
|
||
@Service | ||
class BigQueryClientImplementation(@Value("\${gcp.projectId}") val projectId: String): BigQueryClient { | ||
val SAMTALEREFERAT_EVENTS = "SAMTALEREFERAT_EVENTS" | ||
val SAMTALEREFERAT_FORDELING = "SAMTALEREFERAT_FORDELING" | ||
val DATASET_NAME = "aktivitet_metrikker" | ||
val moteEventsTable = TableId.of(DATASET_NAME, SAMTALEREFERAT_EVENTS) | ||
val samtalereferatFordelingTable = TableId.of(DATASET_NAME, SAMTALEREFERAT_FORDELING) | ||
|
||
fun TableId.insertRequest(row: Map<String, Any>): InsertAllRequest { | ||
return InsertAllRequest.newBuilder(this).addRow(row).build() | ||
} | ||
|
||
val bigQuery = BigQueryOptions.newBuilder().setProjectId(projectId).build().service | ||
val log = LoggerFactory.getLogger(BigQueryClient::class.java) | ||
|
||
override fun logEvent(aktivitetData: AktivitetData, eventType: EventType) { | ||
val moteRow = mapOf( | ||
"aktivitetId" to aktivitetData.id, | ||
"event" to eventType.name, | ||
"versjon" to aktivitetData.versjon, | ||
"endretDato" to ZonedDateTime.ofInstant(aktivitetData.endretDato.toInstant(), ZoneOffset.systemDefault()).toOffsetDateTime().toString(), | ||
"erPublisert" to aktivitetData.moteData.isReferatPublisert, | ||
"opprettet" to ZonedDateTime.ofInstant(aktivitetData.opprettetDato.toInstant(), ZoneOffset.systemDefault()).toOffsetDateTime().toString(), | ||
"aktivitetsType" to aktivitetData.aktivitetType.name, | ||
"referatLengde" to aktivitetData.moteData.referat.length | ||
) | ||
val moteEvent = moteEventsTable.insertRequest(moteRow) | ||
insertWhileToleratingErrors(moteEvent) | ||
} | ||
|
||
override fun logSamtalereferatFordeling(samtalereferatPublisertFordeling: SamtalereferatPublisertFordeling) { | ||
val fordelingRow = mapOf( | ||
"antallPublisert" to samtalereferatPublisertFordeling.antallPublisert, | ||
"antallIkkePublisert" to samtalereferatPublisertFordeling.antallIkkePublisert, | ||
"timestamp" to ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime().toString(), | ||
) | ||
val fordelingInsertRequest = samtalereferatFordelingTable.insertRequest(fordelingRow) | ||
insertWhileToleratingErrors(fordelingInsertRequest) | ||
} | ||
|
||
private fun insertWhileToleratingErrors(insertRequest: InsertAllRequest) { | ||
runCatching { | ||
val response = bigQuery.insertAll(insertRequest) | ||
val errors = response.insertErrors | ||
if (errors.isNotEmpty()) { | ||
log.error("Error inserting bigquery rows", errors) | ||
} | ||
}.onFailure { | ||
log.error("BigQuery error", it) | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/no/nav/veilarbaktivitet/eventsLogger/BigQueryMetrikkCron.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package no.nav.veilarbaktivitet.eventsLogger | ||
|
||
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock | ||
import org.springframework.scheduling.annotation.EnableScheduling | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
@EnableScheduling | ||
open class BigQueryMetrikkCron( | ||
val bigQueryClient: BigQueryClient, | ||
val isPublisertDAO: IsPublisertDAO | ||
) { | ||
|
||
@Scheduled(cron = "@midnight") | ||
@SchedulerLock(name = "aktiviteter_bigquery_metrikker", lockAtMostFor = "PT2M") | ||
open fun hentPublisertCron() { | ||
val fordeling = isPublisertDAO.hentIsPublisertFordeling() | ||
bigQueryClient.logSamtalereferatFordeling(fordeling) | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/no/nav/veilarbaktivitet/eventsLogger/IsPublisertDAO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package no.nav.veilarbaktivitet.eventsLogger | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
open class IsPublisertDAO( | ||
val template: JdbcTemplate | ||
) { | ||
open fun hentIsPublisertFordeling(): SamtalereferatPublisertFordeling { | ||
val sql = """ | ||
select count(*) as antall, mote.referat_publisert as erPublisert from mote | ||
join veilarbaktivitet.aktivitet a | ||
on mote.aktivitet_id = a.aktivitet_id and mote.versjon = a.versjon | ||
where mote.referat is not null | ||
and a.til_dato < NOW() | ||
and a.gjeldende = 1 -- Bare siste versjon | ||
and a.historisk_dato is null -- Ikke ta med referat i avsluttede perioder | ||
group by mote.referat_publisert | ||
""".trimIndent() | ||
return template.query(sql) { result, _ -> | ||
val erPublisert = result.getInt("erPublisert") | ||
val antall = result.getInt("antall") | ||
if (erPublisert == 1) SamtalereferatPublisertFordeling(antallPublisert = antall, antallIkkePublisert = 0) | ||
else SamtalereferatPublisertFordeling(antallPublisert = 0, antallIkkePublisert = antall) | ||
}.let { fordelinger -> | ||
SamtalereferatPublisertFordeling( | ||
antallPublisert = fordelinger.sumOf { it.antallPublisert }, | ||
antallIkkePublisert = fordelinger.sumOf { it.antallIkkePublisert }, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V8__referat_publisert_index.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
create index if not exists mote_publisert_index | ||
on mote (referat_publisert); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters