Skip to content

Commit

Permalink
OK-711: use kouta-external Elastic client in getToteutus
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalliok committed Jan 16, 2025
1 parent 56aada1 commit 14159a4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kouta-external.elasticsearch.url={{kouta_external_elasticsearch7_url}}
kouta-external.elasticsearch.auth-enabled={{kouta_external_elasticsearch_auth_enabled | default('false')}}
kouta-external.elasticsearch.username={{kouta_external_elasticsearch_username}}
kouta-external.elasticsearch.password={{kouta_external_elasticsearch_password}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ import org.json4s.jackson.JsonMethods.{parse, render, compact}
import org.asynchttpclient.Dsl._
import org.asynchttpclient._

import com.sksamuel.elastic4s.{ElasticClient => ScalaElasticClient}
import co.elastic.clients.elasticsearch._types.query_dsl.{Query, QueryBuilders, MatchQuery}
import co.elastic.clients.elasticsearch.{ElasticsearchClient => JavaElasticClient}

import fi.oph.kouta.logging.Logging
import fi.oph.kouta.external.domain.indexed.ToteutusIndexed
import fi.oph.kouta.external.elasticsearch.ElasticsearchClient
import fi.oph.kouta.external.util.KoutaJsonFormats

import java.util.concurrent.CompletableFuture
import scala.compat.java8.FutureConverters._
import scala.concurrent.Future

import scala.collection.immutable.Stream.concat
import scala.collection.JavaConverters._

object ElasticQueries {
import org.json4s.JsonDSL._

def toteutusByOid(oid: String): Query =
QueryBuilders.bool.must(
MatchQuery.of(q => q.field("oid.keyword").query(oid))._toQuery()
).build()._toQuery()

def toteutusSearch(after: Option[String]) : JValue = {
val query = (("query" -> ("match" -> ("tila" -> "julkaistu"))) ~
("size" -> 1000) ~
Expand All @@ -26,8 +40,18 @@ object ElasticQueries {
}
}

object ElasticClient extends Logging {
class ToteutusOps(
val client: ScalaElasticClient,
val clientJava: JavaElasticClient
) extends ElasticsearchClient with KoutaJsonFormats {
val index: String = "toteutus-kouta"
def getToteutus(oid: String) =
searchItems[ToteutusIndexed](List(ElasticQueries.toteutusByOid(oid)).asJava)
}

object ToteutusOps extends ToteutusOps(ElasticsearchClient.client, ElasticsearchClient.clientJava)

object ElasticClient extends Logging {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
implicit val formats = DefaultFormats

Expand Down Expand Up @@ -63,8 +87,7 @@ object ElasticClient extends Logging {
}
}

def getToteutus(oid: String): Future[JValue] =
getJson(s"toteutus-kouta/_doc/$oid").map{_ \ "_source"}
def getToteutus(oid: String): List[ToteutusIndexed] = ToteutusOps.getToteutus(oid)

def listPublished(after: Option[String]): Stream[JValue] = {
logger.info(s"listPublished: querying page after $after")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fi.oph.kouta.europass

import scala.xml._
import org.json4s._
import fi.oph.kouta.external.domain.indexed.ToteutusIndexed

object EuropassConversion {
implicit val formats = DefaultFormats
Expand Down Expand Up @@ -50,4 +51,20 @@ object EuropassConversion {
</loq:learningOpportunity>
}

def toteutusAsElmXmlNew(toteutus: ToteutusIndexed): Elem = {
val oid: String = toteutus.oid.map(_.toString).getOrElse("")
val langs = List("en", "fi", "sv")
<loq:learningOpportunity id={toteutusUrl(oid)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
{langs.map(konfoUrl(_, oid))}
<loq:learningAchievementSpecification
idref={koulutusUrl((toteutus.koulutusOid.map(_.toString).getOrElse("")))}/>
{toteutus.tarjoajat.map{t =>
<loq:providedBy idref={organisaatioUrl(t.oid.toString)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/"/>
}}
{toteutus.nimi.keys.map(lang => nimiAsElmXml(lang.name, toteutus.nimi(lang)))}
</loq:learningOpportunity>
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import java.io.{File, BufferedWriter, FileWriter}
object Publisher extends Logging {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global

def toteutusToFile(oid: String, dest: BufferedWriter) =
ElasticClient.getToteutus(oid)
.map(EuropassConversion.toteutusAsElmXml)
.map{toteutusXml => {
dest.write(
<loq:Courses xsdVersion="3.1.0"
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<loq:learningOpportunityReferences>
{toteutusXml}
</loq:learningOpportunityReferences>
</loq:Courses>.toString
)
dest.close()
}}
def toteutusToFile(oid: String, dest: BufferedWriter) = {
val toteutusXml = EuropassConversion.toteutusAsElmXmlNew(
ElasticClient.getToteutus(oid)(0)
)
dest.write(
<loq:Courses xsdVersion="3.1.0"
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<loq:learningOpportunityReferences>
{toteutusXml}
</loq:learningOpportunityReferences>
</loq:Courses>.toString
)
dest.close()
}

def publishedToteutuksetToFile(dest: BufferedWriter) = {
var writtenToteutusCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import scala.concurrent.duration._
import scala.concurrent.Await

import fi.oph.kouta.europass.ElasticClient
import fi.oph.kouta.external.domain.indexed.ToteutusIndexed

class ElasticClientSpec extends ScalatraFlatSpec with ElasticFixture {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
Expand All @@ -24,12 +25,10 @@ class ElasticClientSpec extends ScalatraFlatSpec with ElasticFixture {
}

"example toteutus" should "be loadable" in {
Await.result(ElasticClient.getToteutus("1.2.246.562.17.00000000000000000002")
.map{result: JValue => assert((result \ "tila").extract[String]
== "julkaistu")}, 60.second)
Await.result(ElasticClient.getToteutus("1.2.246.562.17.00000000000000000002")
.map{result: JValue => assert((result \ "koulutusOid").extract[String]
== "1.2.246.562.13.00000000000000000001")}, 60.second)
val tot = ElasticClient.getToteutus("1.2.246.562.17.00000000000000000002")(0)
assert(tot.tila.name == "julkaistu")
assert(tot.koulutusOid.map(_.toString).getOrElse("")
== "1.2.246.562.13.00000000000000000001")
}

"published toteutukset" should "have both toteutukset" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class PublisherSpec extends ScalatraFlatSpec with ElasticFixture {

"toteutusToFile" should "create correct toteutusXml from ElasticSearch" in {
val writer = new StringWriter()
Await.result(
Publisher.toteutusToFile(
"1.2.246.562.17.00000000000000000001", new BufferedWriter(writer)),
60.second)
Publisher.toteutusToFile(
"1.2.246.562.17.00000000000000000001", new BufferedWriter(writer)
)
assert(writer.toString.contains("<loq:contentUrl>https://opintopolku.fi/konfo/sv/toteutus/1.2.246.562.17.00000000000000000001</loq:contentUrl>"))
assert(writer.toString.contains("<loq:providedBy idref=\"https://rdf.oph.fi/organisaatio/1.2.246.562.10.594252633210\"/>"))

Expand Down

0 comments on commit 14159a4

Please sign in to comment.