Skip to content

Commit

Permalink
suppress out of order data processing (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan authored Jul 11, 2024
1 parent 386e189 commit cc5d610
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 140 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout the code to specific branch
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '17'
- name: Setup Android SDK
uses: android-actions/setup-android@v2
uses: android-actions/setup-android@v3
- name: Cache Gradle packages
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Cache SonarQube packages
uses: actions/cache@v3
uses: actions/cache@v4
if: github.ref_name == 'main' && github.event_name == 'push'
with:
path: ~/.sonar/cache
Expand All @@ -42,7 +42,7 @@ jobs:
run: ./gradlew build jacocoTestReport sonar
- name: Upload build artifacts
if: github.ref_name == 'main' && github.event_name == 'push'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: build
path: app/build/outputs
Expand Down
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

23 changes: 11 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ android {

defaultConfig {
applicationId "org.blitzortung.android.app"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 34
versionCode 318
versionName '2.2.3'
versionCode 319
versionName '2.2.4'
multiDexEnabled false
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -26,8 +26,8 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
// See bug https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/183
testCoverageEnabled false
enableUnitTestCoverage true
enableAndroidTestCoverage false
}
}
compileOptions {
Expand All @@ -47,7 +47,6 @@ android {
}
}

buildToolsVersion = '33.0.2'
useLibrary 'android.test.base'
lint {
abortOnError false
Expand All @@ -59,12 +58,12 @@ android {
namespace 'org.blitzortung.android.app'
}

def dagger_version = '2.51'
def dagger_version = '2.51.1'

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.media:media:1.7.0'
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'androidx.work:work-runtime-ktx:2.9.0'
implementation 'org.osmdroid:osmdroid-android:6.1.18'
Expand All @@ -80,9 +79,9 @@ dependencies {
testImplementation 'org.assertj:assertj-core:3.25.3'
testImplementation 'io.mockk:mockk:1.13.10'
testImplementation 'org.robolectric:robolectric:4.11.1'
testImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
testImplementation 'androidx.test:core:1.6.1'
androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}

kapt.includeCompileClasspath = false
Expand Down
73 changes: 42 additions & 31 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import org.blitzortung.android.util.isAtLeast
import org.osmdroid.config.Configuration
import org.osmdroid.tileprovider.util.StorageUtils
import org.osmdroid.util.GeoPoint
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject
import kotlin.math.roundToInt

Expand All @@ -94,6 +95,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
private lateinit var strikeListOverlay: StrikeListOverlay
private lateinit var ownLocationOverlay: OwnLocationOverlay
private lateinit var fadeOverlay: FadeOverlay
private var currentSequenceNumber = AtomicLong()

private var clearData: Boolean = false

Expand Down Expand Up @@ -137,48 +139,57 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
is ResultEvent -> {

statusComponent.indicateError(event.failed)
if (!event.failed) {
currentResult = event

Log.d(LOG_TAG, "Main.onDataUpdate() $event")
if (!event.failed && event.sequenceNumber != null) {
val updatedSequenceNumber =
currentSequenceNumber.updateAndGet { previousSequenceNumber ->
if (previousSequenceNumber < event.sequenceNumber) event.sequenceNumber else previousSequenceNumber
}
if (updatedSequenceNumber == event.sequenceNumber) {
currentResult = event

val resultParameters = event.parameters
Log.d(LOG_TAG, "Main.onDataUpdate() $event")

clearDataIfRequested()
val resultParameters = event.parameters

val initializeOverlay = strikeListOverlay.parameters != resultParameters
with(strikeListOverlay) {
parameters = resultParameters
rasterParameters = event.rasterParameters
referenceTime = event.referenceTime
}
clearDataIfRequested()

if (event.updated >= 0 && !initializeOverlay) {
strikeListOverlay.expireStrikes()
} else {
strikeListOverlay.clear()
}
val initializeOverlay = strikeListOverlay.parameters != resultParameters
with(strikeListOverlay) {
parameters = resultParameters
rasterParameters = event.rasterParameters
referenceTime = event.referenceTime
}

if (event.strikes != null) {
val strikes = if (event.updated > 0 && !initializeOverlay) {
val size = event.strikes.size
event.strikes.subList(size - event.updated, size)
if (event.updated >= 0 && !initializeOverlay) {
strikeListOverlay.expireStrikes()
} else {
event.strikes
strikeListOverlay.clear()
}

if (event.strikes != null) {
val strikes = if (event.updated > 0 && !initializeOverlay) {
val size = event.strikes.size
event.strikes.subList(size - event.updated, size)
} else {
event.strikes
}
strikeListOverlay.addStrikes(strikes)
}
strikeListOverlay.addStrikes(strikes)
}

binding.alertView.setColorHandler(strikeColorHandler, strikeListOverlay.parameters.intervalDuration)
binding.alertView.setColorHandler(
strikeColorHandler,
strikeListOverlay.parameters.intervalDuration
)

strikeListOverlay.refresh()
mapFragment.mapView.invalidate()
strikeListOverlay.refresh()
mapFragment.mapView.invalidate()

binding.legendView.requestLayout()
binding.timeSlider.update(event.parameters, event.history!!)
binding.legendView.requestLayout()
binding.timeSlider.update(event.parameters, event.history!!)

if (event.flags.mode == Mode.ANIMATION || !event.containsRealtimeData()) {
setHistoricStatusString()
if (event.flags.mode == Mode.ANIMATION || !event.containsRealtimeData()) {
setHistoricStatusString()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import org.osmdroid.events.MapListener
import org.osmdroid.events.ScrollEvent
import org.osmdroid.events.ZoomEvent
import java.util.Locale
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.properties.Delegates
Expand Down Expand Up @@ -76,6 +77,7 @@ class MainDataHandler @Inject constructor(
private set

private var period: Int = 0
private var sequenceNumber = AtomicLong()

private var dataProvider: DataProvider? = null

Expand Down Expand Up @@ -155,12 +157,13 @@ class MainDataHandler @Inject constructor(

private fun updateUsingCache() {
var flags = Flags(mode = mode)
val sequenceNumber = sequenceNumber.incrementAndGet()

val parameters = activeParameters
val cachedResult = cache.get(parameters)
if (cachedResult != null) {
Log.d(LOG_TAG, "MainDataHandler.updateData() cached $parameters")
sendEvent(cachedResult)
sendEvent(cachedResult.copy(sequenceNumber = sequenceNumber))
} else {
Log.d(LOG_TAG, "MainDataHandler.updateData() fetch $parameters")
FetchDataTask(dataMode, dataProvider!!, {
Expand All @@ -174,7 +177,7 @@ class MainDataHandler @Inject constructor(
if (!it.containsRealtimeData()) {
cache.put(event.parameters, event)
}
sendEvent(event)
sendEvent(event.copy(sequenceNumber = sequenceNumber))
}, ::toast).execute(parameters = parameters, history = history)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DataCache @Inject constructor() {
}

fun put(parameters: Parameters, dataEvent: ResultEvent) {
cache[parameters] = Timestamped(dataEvent)
cache[parameters] = Timestamped(dataEvent.copy(sequenceNumber = null))
}

fun calculateTotalSize(): CacheSize = cache.entries.fold(CacheSize(0, 0)) { acc, entry ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ data class ResultEvent(
val referenceTime: Long = 0,
val parameters: Parameters,
val history: History? = null,
val flags: Flags
val flags: Flags,
val sequenceNumber: Long? = null,
) : DataEvent, Serializable {

fun containsRealtimeData(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ internal class DataBuilder {
lateralError = jsonArray.getDouble(3),
altitude = 0,
amplitude = jsonArray.getDouble(4).toFloat(),
stationCount = jsonArray.getInt(5).toShort()
)
} catch (e: JSONException) {
throw IllegalStateException("error with JSON format while parsing strike data", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.blitzortung.android.data.Parameters
import org.blitzortung.android.data.provider.GLOBAL_REGION
import org.blitzortung.android.data.provider.LOCAL_REGION
import org.blitzortung.android.jsonrpc.JsonRpcClient
import org.blitzortung.android.jsonrpc.JsonRpcResponse
import org.json.JSONObject
import java.net.URL

Expand All @@ -12,7 +13,7 @@ class JsonRpcData(
private val serviceUrl: URL,
) {

fun requestData(parameters: Parameters) : JSONObject {
fun requestData(parameters: Parameters) : JsonRpcResponse {
val intervalDuration = parameters.intervalDuration
val intervalOffset = parameters.intervalOffset
val rasterBaselength = parameters.rasterBaselength
Expand All @@ -22,19 +23,21 @@ class JsonRpcData(

return when (region) {
GLOBAL_REGION -> {
val jsonRpcResponse: JsonRpcResponse = client.call(
serviceUrl,
"get_global_strikes_grid",
intervalDuration,
rasterBaselength,
intervalOffset,
countThreshold
)
with(
client.call(
serviceUrl,
"get_global_strikes_grid",
intervalDuration,
rasterBaselength,
intervalOffset,
countThreshold
)
jsonRpcResponse
) {
put("y1", 0.0)
put("x0", 0.0)
data.put("y1", 0.0)
data.put("x0", 0.0)
}
jsonRpcResponse
}
LOCAL_REGION -> {
client.call(
Expand Down
Loading

0 comments on commit cc5d610

Please sign in to comment.