Move all file in fr.dcproject.
This commit is contained in:
22
src/main/kotlin/fr/dcproject/component/views/KoinModule.kt
Normal file
22
src/main/kotlin/fr/dcproject/component/views/KoinModule.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package fr.dcproject.component.views
|
||||
|
||||
import fr.dcproject.application.Configuration
|
||||
import fr.dcproject.component.article.ArticleForView
|
||||
import fr.dcproject.component.article.ArticleViewManager
|
||||
import org.apache.http.HttpHost
|
||||
import org.elasticsearch.client.RestClient
|
||||
import org.koin.dsl.module
|
||||
|
||||
val viewKoinModule = module {
|
||||
|
||||
single {
|
||||
val config: Configuration = get()
|
||||
// Elasticsearch Client
|
||||
val esClient = RestClient.builder(
|
||||
HttpHost.create(config.elasticsearch)
|
||||
).build().apply {
|
||||
createEsIndexForViews()
|
||||
}
|
||||
ArticleViewManager<ArticleForView>(esClient)
|
||||
}
|
||||
}
|
||||
18
src/main/kotlin/fr/dcproject/component/views/ViewManager.kt
Normal file
18
src/main/kotlin/fr/dcproject/component/views/ViewManager.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
package fr.dcproject.component.views
|
||||
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.views.entity.ViewAggregation
|
||||
import org.elasticsearch.client.Response
|
||||
import org.joda.time.DateTime
|
||||
|
||||
interface ViewManager <T> {
|
||||
/**
|
||||
* Add view to one entity
|
||||
*/
|
||||
fun addView(ip: String, entity: T, citizen: CitizenI? = null, dateTime: DateTime = DateTime.now()): Response?
|
||||
|
||||
/**
|
||||
* Get Views aggregations
|
||||
*/
|
||||
fun getViewsCount(entity: T): ViewAggregation
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package fr.dcproject.component.views
|
||||
|
||||
import fr.dcproject.common.utils.waitElasticsearchIsUp
|
||||
import org.elasticsearch.client.Request
|
||||
import org.elasticsearch.client.RestClient
|
||||
|
||||
fun RestClient.createEsIndexForViews() {
|
||||
waitElasticsearchIsUp()
|
||||
|
||||
/* Create index if not exist */
|
||||
if (performRequest(Request("HEAD", "/views?include_type_name=false")).statusLine.statusCode == 404) {
|
||||
Request(
|
||||
"PUT",
|
||||
"/views?include_type_name=false"
|
||||
).apply {
|
||||
//language=JSON
|
||||
setJsonEntity(
|
||||
"""
|
||||
{
|
||||
"settings": {
|
||||
"number_of_shards": 5
|
||||
},
|
||||
"mappings": {
|
||||
"properties": {
|
||||
"logged": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"type": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"user_ref": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"id": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"version_id": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"ip": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"citizen_id": {
|
||||
"type": "keyword"
|
||||
},
|
||||
"view_at": {
|
||||
"type": "date"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
}.let {
|
||||
performRequest(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package fr.dcproject.component.views.dto
|
||||
|
||||
import fr.dcproject.component.views.entity.ViewAggregation
|
||||
|
||||
class ViewAggregation(
|
||||
val total: Int,
|
||||
val unique: Int
|
||||
) {
|
||||
constructor(views: ViewAggregation) : this(views.total, views.unique)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package fr.dcproject.component.views.dto
|
||||
|
||||
interface Viewable {
|
||||
var views: ViewAggregation
|
||||
|
||||
class Imp(views: fr.dcproject.component.views.entity.ViewAggregation) : Viewable {
|
||||
override var views: ViewAggregation = ViewAggregation(views.total, views.unique)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.dcproject.component.views.entity
|
||||
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.EntityUpdatedAt
|
||||
import fr.postgresjson.entity.EntityUpdatedAtImp
|
||||
|
||||
class ViewAggregation(
|
||||
val total: Int,
|
||||
val unique: Int
|
||||
) : EntityI,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp() {
|
||||
constructor() : this(0, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user