Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
dsvoronin committed Jan 13, 2021
1 parent c5ba922 commit f14c67c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.net.URL
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter.ISO_DATE

/**
* @param endpoints list of elastic endpoints to send logs
Expand All @@ -32,9 +27,9 @@ internal class HttpElasticClient(

private val elasticApi = elasticServiceFactory.createApiService(endpoints)

private val timezone = ZoneId.of("Europe/Moscow")
private val timestampFormatter = timeProvider.formatter("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSZ")

private val timestampFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSZ")
private val isoDate = timeProvider.formatter("yyyy-MM-dd")

override fun sendMessage(
level: String,
Expand All @@ -43,25 +38,25 @@ internal class HttpElasticClient(
throwable: Throwable?
) {
try {
val nowWithTimeZone = ZonedDateTime.ofInstant(timeProvider.now().toInstant(), timezone)
val nowWithoutTimeZone = LocalDateTime.ofInstant(timeProvider.now().toInstant(), timezone)
val nowWithTimeZone = timeProvider.now()

val params = mutableMapOf(
"@timestamp" to nowWithTimeZone.format(timestampFormatter),
"@timestamp" to timestampFormatter.format(nowWithTimeZone),
"level" to level,
"build_id" to buildId,
"message" to message
)

if (!throwable?.message.isNullOrBlank()) {
params["error_message"] = throwable?.message
val errorMessage = throwable?.message
if (!errorMessage.isNullOrBlank()) {
params["error_message"] = errorMessage
}

params.putAll(metadata)

elasticApi.log(
indexPattern = indexPattern,
date = nowWithoutTimeZone.format(ISO_DATE),
date = isoDate.format(nowWithTimeZone),
params = params
).enqueue(
object : Callback<ResponseBody> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.avito.time

import java.text.DateFormat
import java.util.Date
import java.util.TimeZone

class StubTimeProvider : TimeProvider {

private val timeProvider = DefaultTimeProvider()

override val timeZone: TimeZone = timeProvider.timeZone

lateinit var now: Date

override fun isSameDay(date1: Date, date2: Date): Boolean = timeProvider.isSameDay(date1, date2)
Expand All @@ -17,4 +21,6 @@ class StubTimeProvider : TimeProvider {
override fun now(): Date = now

override fun toDate(seconds: Long): Date = timeProvider.toDate(seconds)

override fun formatter(pattern: String): DateFormat = timeProvider.formatter(pattern)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.avito.time

import java.text.DateFormat
import java.util.Date
import java.util.TimeZone
import java.util.concurrent.TimeUnit

/**
Expand All @@ -12,6 +14,8 @@ class TimeMachineProvider : TimeProvider {

private val defaultTimeProvider = DefaultTimeProvider()

override val timeZone: TimeZone = defaultTimeProvider.timeZone

fun moveForwardOn(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS) {
timeShift += unit.toMillis(time)
}
Expand All @@ -29,4 +33,6 @@ class TimeMachineProvider : TimeProvider {
override fun now() = defaultTimeProvider.toDate(nowInSeconds())

override fun toDate(seconds: Long) = defaultTimeProvider.toDate(seconds)

override fun formatter(pattern: String): DateFormat = defaultTimeProvider.formatter(pattern)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.avito.time

import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import java.util.concurrent.TimeUnit

/**
* Used in android runtime < 26, so no java.time API yet
* consider using ThreeTenABP
*/
class DefaultTimeProvider : TimeProvider {

override val timeZone: TimeZone = TimeZone.getTimeZone("Europe/Moscow")

override fun isSameDay(date1: Date, date2: Date): Boolean {
return isSameDay(
Calendar.getInstance().apply { time = date1 },
Expand All @@ -29,4 +39,10 @@ class DefaultTimeProvider : TimeProvider {
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
}

override fun formatter(pattern: String): DateFormat {
return SimpleDateFormat(pattern, Locale.getDefault()).apply {
timeZone = this@DefaultTimeProvider.timeZone
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.avito.time

import java.text.DateFormat
import java.util.Date
import java.util.TimeZone

interface TimeProvider {

val timeZone: TimeZone

fun isSameDay(date1: Date, date2: Date): Boolean

fun nowInMillis(): Long
Expand All @@ -13,4 +17,6 @@ interface TimeProvider {
fun now(): Date

fun toDate(seconds: Long): Date

fun formatter(pattern: String): DateFormat
}
2 changes: 1 addition & 1 deletion subprojects/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.caching=true
android.useAndroidX=true
# The next version of all artifacts
# Scheme: year.<version>
projectVersion=2021.1.6
projectVersion=2021.1.7
# TODO: MBS-9285 - https://github.com/gradle/gradle/issues/12660
systemProp.kotlinVersion=1.4.21
systemProp.androidGradlePluginVersion=4.1.1
Expand Down

0 comments on commit f14c67c

Please sign in to comment.