From 701549c96baedc1300585207f5b9e8261620353e Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 9 May 2019 10:24:45 -0400 Subject: [PATCH] Add handleBackgroundEvent (#22) --- .../main/java/mozilla/telemetry/glean/Glean.kt | 9 +++++++++ .../mozilla/telemetry/glean/rust/LibGleanFFI.kt | 2 ++ .../java/mozilla/telemetry/glean/GleanTest.kt | 16 +++++++++++++++- glean-core/ffi/examples/glean.h | 2 ++ glean-core/ffi/src/lib.rs | 9 +++++++++ glean-core/src/lib.rs | 2 ++ 6 files changed, 39 insertions(+), 1 deletion(-) mode change 100644 => 100755 glean-core/ffi/src/lib.rs mode change 100644 => 100755 glean-core/src/lib.rs diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt index 7c4883dc3..260e80370 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt @@ -63,6 +63,15 @@ open class GleanInternalAPI internal constructor () { val s = LibGleanFFI.INSTANCE.glean_ping_collect(handle, ping_name, e)!! LibGleanFFI.INSTANCE.glean_str_free(s) } + + fun handleBackgroundEvent() { + sendPing("baseline") + sendPing("events") + } + + private fun sendPing(pingName: String) { + LibGleanFFI.INSTANCE.glean_send_ping(handle, pingName) + } } object Glean : GleanInternalAPI() diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/rust/LibGleanFFI.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/rust/LibGleanFFI.kt index f980d6688..80d45cf6e 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/rust/LibGleanFFI.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/rust/LibGleanFFI.kt @@ -42,6 +42,8 @@ internal interface LibGleanFFI : Library { fun glean_is_upload_enabled(glean_handle: Long): Byte + fun glean_send_ping(glean_handle: Long, ping_name: String) + fun glean_new_boolean_metric(category: String, name: String, send_in_pings: StringArray, send_in_pings_len: Int, lifetime: Int, err: RustError.ByReference): Long fun glean_new_counter_metric(category: String, name: String, send_in_pings: StringArray, send_in_pings_len: Int, lifetime: Int, err: RustError.ByReference): Long diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt index 45208c2cc..1d7d28fd3 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanTest.kt @@ -4,15 +4,29 @@ package mozilla.telemetry.glean +import android.content.Context import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import java.io.File @RunWith(RobolectricTestRunner::class) class GleanTest { @Test fun `simple smoke test`() { - Glean.initialize(ApplicationProvider.getApplicationContext()) + GleanInternalAPI().initialize(ApplicationProvider.getApplicationContext()) + } + + @Test + fun `send a ping`() { + val context: Context = ApplicationProvider.getApplicationContext() + val glean = GleanInternalAPI() + glean.initialize(context) + glean.handleBackgroundEvent() + val pingPath = File(context.applicationInfo.dataDir, "glean_data/pings") + val path = pingPath.toString() + assertEquals(2, pingPath.listFiles()?.size) } } diff --git a/glean-core/ffi/examples/glean.h b/glean-core/ffi/examples/glean.h index 5f6736052..7537b55e4 100644 --- a/glean-core/ffi/examples/glean.h +++ b/glean-core/ffi/examples/glean.h @@ -174,6 +174,8 @@ uint8_t glean_is_initialized(uint64_t glean_handle); uint8_t glean_is_upload_enabled(uint64_t glean_handle); +void glean_send_ping(uint64_t glean_handle, FfiStr ping_name); + uint64_t glean_new_boolean_metric(FfiStr category, FfiStr name, RawStringArray send_in_pings, diff --git a/glean-core/ffi/src/lib.rs b/glean-core/ffi/src/lib.rs old mode 100644 new mode 100755 index 05a529cc9..a3d4980bd --- a/glean-core/ffi/src/lib.rs +++ b/glean-core/ffi/src/lib.rs @@ -80,6 +80,15 @@ pub extern "C" fn glean_set_upload_enabled(glean_handle: u64, flag: u8) { }) } +#[no_mangle] +pub extern "C" fn glean_send_ping(glean_handle: u64, ping_name: FfiStr) { + let mut err = ExternError::success(); + GLEAN.call_with_output(&mut err, glean_handle, |glean| { + // FIXME: Error handling + let _ = glean.send_ping(ping_name.as_str()); + }) +} + #[no_mangle] pub extern "C" fn glean_new_boolean_metric( category: FfiStr, diff --git a/glean-core/src/lib.rs b/glean-core/src/lib.rs old mode 100644 new mode 100755 index edc5e0383..6e21a63d2 --- a/glean-core/src/lib.rs +++ b/glean-core/src/lib.rs @@ -129,6 +129,8 @@ impl Glean { let url_path = self.make_path(ping_name, &doc_id); let ping_content = ::serde_json::to_string_pretty(&ping_maker.collect(self.storage(), ping_name)).unwrap(); + // FIXME: Logging ping content for now. Eventually this should be controlled by a flag + log::info!("{}", ping_content); ping_maker.store_ping(&doc_id, &self.get_data_path(), &url_path, &ping_content) } }