-
-
Notifications
You must be signed in to change notification settings - Fork 390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3516 provide occurrence log #3517
Draft
Lakoja
wants to merge
12
commits into
tuskyapp:develop
Choose a base branch
from
Lakoja:3516-provied-occurrence-log
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
117c9fa
3516: (working) Prototype of a DB api call logger
Lakoja a5610e2
3516: Add an activity to display occurrences (called from context menu)
Lakoja 02aa360
3516: Improve item display (and colors)
Lakoja fbddaa3
3516: Support crashes (use the proper "root cause" there)
Lakoja 720eb83
3516: Move occurrence stuff to a new component
Lakoja 820c2d1
3516: Move menu entry to main drawer
Lakoja 68f3322
3516: Use constraint layout
Lakoja 7892469
3516: Refresh async
Lakoja 67e385a
3516: Use themable colors
Lakoja 2b2c705
3516: (collateral) Use consistent colors
Lakoja 711d85a
3516: Add comments about log interception
Lakoja e5e529b
3516: Save full stack, only reduce on display
Lakoja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,057 changes: 1,057 additions & 0 deletions
1,057
app/schemas/com.keylesspalace.tusky.db.AppDatabase/49.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/keylesspalace/tusky/components/occurrence/LogToDbInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright Tusky Contributors | ||
* | ||
* This file is a part of Tusky. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Tusky; if not, | ||
* see <http://www.gnu.org/licenses>. */ | ||
|
||
package com.keylesspalace.tusky.components.occurrence | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import java.io.IOException | ||
|
||
class LogToDbInterceptor(private val occurrenceRespository: OccurrenceRepository) : Interceptor { | ||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request: Request = chain.request() | ||
val what = request.method + " " + request.url.toString() | ||
|
||
val entityId = occurrenceRespository.handleApiCallStart(what) | ||
|
||
val response: Response | ||
try { | ||
response = chain.proceed(request) | ||
occurrenceRespository.handleApiCallFinish(entityId, response.code) | ||
} catch (e: Exception) { | ||
// TODO this case is used? If so add its message to the occurrence entity? | ||
occurrenceRespository.handleApiCallFinish(entityId, 499) | ||
|
||
throw e | ||
} | ||
|
||
return response | ||
} | ||
} |
230 changes: 230 additions & 0 deletions
230
app/src/main/java/com/keylesspalace/tusky/components/occurrence/OccurrenceActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/* Copyright Tusky Contributors | ||
* | ||
* This file is a part of Tusky. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Tusky; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package com.keylesspalace.tusky.components.occurrence | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.graphics.Color | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.DividerItemDecoration | ||
import androidx.recyclerview.widget.ListAdapter | ||
import com.google.android.material.color.MaterialColors | ||
import com.keylesspalace.tusky.BaseActivity | ||
import com.keylesspalace.tusky.R | ||
import com.keylesspalace.tusky.databinding.ActivityOccurrencesBinding | ||
import com.keylesspalace.tusky.databinding.ItemOccurrenceBinding | ||
import com.keylesspalace.tusky.db.AccountEntity | ||
import com.keylesspalace.tusky.db.AppDatabase | ||
import com.keylesspalace.tusky.di.Injectable | ||
import com.keylesspalace.tusky.di.ViewModelFactory | ||
import com.keylesspalace.tusky.util.BindingHolder | ||
import com.keylesspalace.tusky.util.getDurationStringAllowMillis | ||
import com.keylesspalace.tusky.util.getRelativeTimeSpanString | ||
import com.keylesspalace.tusky.util.viewBinding | ||
import com.keylesspalace.tusky.util.visible | ||
import dagger.android.DispatchingAndroidInjector | ||
import dagger.android.HasAndroidInjector | ||
import kotlinx.coroutines.launch | ||
import java.text.DateFormat | ||
import javax.inject.Inject | ||
|
||
class OccurrenceActivity : BaseActivity(), Injectable, HasAndroidInjector { | ||
|
||
@Inject | ||
lateinit var viewModelFactory: ViewModelFactory | ||
|
||
@Inject | ||
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any> | ||
Lakoja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Inject | ||
lateinit var occurrenceRepository: OccurrenceRepository | ||
|
||
@Inject | ||
lateinit var db: AppDatabase | ||
|
||
// private val viewModel: ListsViewModel by viewModels { viewModelFactory } | ||
|
||
private val binding by viewBinding(ActivityOccurrencesBinding::inflate) | ||
|
||
private val adapter = OccurrenceAdapter() | ||
|
||
private var loading = false | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(binding.root) | ||
|
||
setSupportActionBar(binding.includedToolbar.toolbar) | ||
supportActionBar?.apply { | ||
title = getString(R.string.title_occurrences) | ||
setDisplayHomeAsUpEnabled(true) | ||
setDisplayShowHomeEnabled(true) | ||
} | ||
|
||
binding.occurrenceList.adapter = adapter | ||
binding.occurrenceList.addItemDecoration( | ||
DividerItemDecoration(this, DividerItemDecoration.VERTICAL) | ||
) | ||
|
||
binding.swipeRefreshLayout.setOnRefreshListener { load() } | ||
binding.swipeRefreshLayout.setColorSchemeResources(R.color.tusky_blue) | ||
|
||
// It's only function here so far: show "there is nothing" | ||
binding.messageView.setup( | ||
R.drawable.elephant_friend_empty, | ||
R.string.message_empty, | ||
null | ||
) | ||
|
||
load() | ||
} | ||
|
||
private fun load() { | ||
if (loading) { | ||
return | ||
} | ||
|
||
lifecycleScope.launch { | ||
binding.swipeRefreshLayout.isRefreshing = true | ||
loading = true | ||
|
||
val occurrences = occurrenceRepository.loadAll() | ||
|
||
adapter.submitList(occurrences) | ||
|
||
binding.messageView.visible(occurrences.isEmpty()) | ||
binding.occurrenceList.visible(occurrences.isNotEmpty()) | ||
|
||
binding.swipeRefreshLayout.isRefreshing = false | ||
loading = false | ||
} | ||
} | ||
|
||
override fun androidInjector() = dispatchingAndroidInjector | ||
|
||
companion object { | ||
fun newIntent(context: Context) = Intent(context, OccurrenceActivity::class.java) | ||
} | ||
|
||
private object OccurrenceDiffer : DiffUtil.ItemCallback<OccurrenceEntity>() { | ||
override fun areItemsTheSame(oldItem: OccurrenceEntity, newItem: OccurrenceEntity): Boolean { | ||
return oldItem.id == newItem.id | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: OccurrenceEntity, newItem: OccurrenceEntity): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
|
||
private inner class OccurrenceAdapter : | ||
ListAdapter<OccurrenceEntity, BindingHolder<ItemOccurrenceBinding>>(OccurrenceDiffer) { | ||
|
||
private val dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT) | ||
private var lastAccount: AccountEntity? = null | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ItemOccurrenceBinding> { | ||
return BindingHolder(ItemOccurrenceBinding.inflate(LayoutInflater.from(parent.context), parent, false)) | ||
} | ||
|
||
override fun onBindViewHolder(holder: BindingHolder<ItemOccurrenceBinding>, position: Int) { | ||
val occurrence = getItem(position) | ||
|
||
val defaultTextColor = MaterialColors.getColor(binding.root, android.R.attr.textColorPrimary) | ||
|
||
holder.binding.what.text = occurrence.what | ||
holder.binding.what.setTextColor( | ||
if (occurrence.type == OccurrenceEntity.Type.CRASH) { | ||
Color.RED | ||
} else { | ||
defaultTextColor | ||
} | ||
) | ||
|
||
holder.binding.code.text = occurrence.code?.toString() ?: "" | ||
holder.binding.code.setTextColor( | ||
if (occurrence.code != null && occurrence.code > 0) { | ||
if (occurrence.code >= 400) { | ||
baseContext.getColor(R.color.colorError) | ||
} else if (occurrence.code >= 300) { | ||
baseContext.getColor(R.color.colorWarning) | ||
} else { | ||
baseContext.getColor(R.color.colorSuccess) | ||
} | ||
} else { | ||
defaultTextColor | ||
} | ||
) | ||
|
||
holder.binding.whenDate.text = | ||
getRelativeTimeSpanString([email protected], occurrence.startedAt.time, System.currentTimeMillis()) | ||
//dateFormat.format(occurrence.startedAt) | ||
// TODO or AbsoluteTimeFormatter? | ||
|
||
// TODO how does one get the current locale /and/or format numbers here? | ||
val currentLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
resources.configuration.locales[0] | ||
} else { | ||
resources.configuration.locale | ||
} | ||
|
||
var duration = "" | ||
var durationMs = 0L | ||
if (occurrence.finishedAt != null) { | ||
durationMs = occurrence.finishedAt.time - occurrence.startedAt.time | ||
duration = getDurationStringAllowMillis(currentLocale, durationMs) | ||
} | ||
holder.binding.duration.text = duration | ||
holder.binding.duration.setTextColor( | ||
if (durationMs >= 1000) { | ||
baseContext.getColor(R.color.colorBad) | ||
} else if (durationMs >= 400) { | ||
baseContext.getColor(R.color.colorWarning) | ||
} else { | ||
baseContext.getColor(R.color.colorSuccess) | ||
} | ||
) | ||
|
||
holder.binding.who.text = if (occurrence.accountId != null) { | ||
val account = getAccount(occurrence.accountId) | ||
account?.displayName ?: "" | ||
} else { | ||
"" | ||
} | ||
|
||
holder.binding.trace.visible(occurrence.callTrace.isNotEmpty()) | ||
holder.binding.trace.text = occurrence.callTrace // TODO this could/should be normal multi-line | ||
|
||
// TODO cache some objects here? For example different helper objects (locale, number format, ...) | ||
} | ||
|
||
private fun getAccount(accountId: Long): AccountEntity? { | ||
if (lastAccount?.id == accountId) { | ||
return lastAccount | ||
} | ||
|
||
lastAccount = db.accountDao().get(accountId) | ||
|
||
return lastAccount | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call it DebugActivity/DebugLogActivity or something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, it's not used for debugging.
It should/could have the same name as in the ui (and there "debug" would be somewhat misleading).
It shows (hopefully) significant events in the app life. Therefore I was looking for a synonym of "event".