Skip to content

Commit

Permalink
opti: refactor ApiDashboards (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Feb 5, 2021
1 parent 1a5c252 commit 7c471ed
Show file tree
Hide file tree
Showing 14 changed files with 633 additions and 887 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.itangcent.idea.plugin.StatusRecorder
import com.itangcent.idea.plugin.Worker
import com.itangcent.idea.plugin.WorkerStatus
import com.itangcent.idea.plugin.api.export.ClassExporter
import com.itangcent.idea.plugin.api.export.CompletedHandle
import com.itangcent.idea.plugin.api.export.DocHandle
import com.itangcent.idea.plugin.api.export.requestOnly
import com.itangcent.idea.psi.PsiMethodResource
Expand Down Expand Up @@ -74,10 +75,10 @@ class CachedRequestClassExporter : ClassExporter, Worker {
//no use cache,no read,no write
private var disabled: Boolean = false

override fun export(cls: Any, docHandle: DocHandle): Boolean {
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {

if (disabled || cls !is PsiClass) {
return delegateClassExporter!!.export(cls, docHandle)
return delegateClassExporter!!.export(cls, docHandle, completedHandle)
}

val psiFile = cls.containingFile
Expand All @@ -99,12 +100,13 @@ class CachedRequestClassExporter : ClassExporter, Worker {
statusRecorder.newWork()
actionContext.runInReadUI {
try {
readApiFromCache(cls, fileApiCache!!, docHandle)
readApiFromCache(cls, fileApiCache!!, docHandle, completedHandle)
} finally {
statusRecorder.endWork()
}
}
}
completedHandle(cls)
return@runAsync
}
}
Expand Down Expand Up @@ -136,7 +138,7 @@ class CachedRequestClassExporter : ClassExporter, Worker {
PsiClassUtils.fullNameOfMember(cls, request.resourceMethod()!!)
, tinyRequest
))
})
}, completedHandle)
actionContext.runAsync {
fileApiCache.md5 = md5
fileApiCache.lastModified = System.currentTimeMillis()
Expand All @@ -148,6 +150,7 @@ class CachedRequestClassExporter : ClassExporter, Worker {
}
} catch (e: ProcessCanceledException) {
//ignore cancel
completedHandle(cls)
} catch (e: Exception) {
logger!!.traceError("error to cache api info", e)

Expand All @@ -156,7 +159,7 @@ class CachedRequestClassExporter : ClassExporter, Worker {
statusRecorder.newWork()
actionContext.runInReadUI {
try {
delegateClassExporter!!.export(cls, docHandle)
delegateClassExporter!!.export(cls, docHandle, completedHandle)
} finally {
statusRecorder.endWork()
}
Expand All @@ -168,7 +171,8 @@ class CachedRequestClassExporter : ClassExporter, Worker {
return true
}

private fun readApiFromCache(cls: PsiClass, fileApiCache: FileApiCache, requestHandle: DocHandle) {
private fun readApiFromCache(cls: PsiClass, fileApiCache: FileApiCache, requestHandle: DocHandle,
completedHandle: CompletedHandle) {
fileApiCache.requests?.forEach { request ->
val method = request.key?.let { PsiClassUtils.findMethodFromFullName(it, cls as PsiElement) }
if (method == null) {
Expand All @@ -178,6 +182,7 @@ class CachedRequestClassExporter : ClassExporter, Worker {
request.request!!.resource = PsiMethodResource(method, cls)
requestHandle(request.request!!)
}
completedHandle(cls)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,23 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
@Inject
private val contextSwitchListener: ContextSwitchListener? = null

override fun export(cls: Any, docHandle: DocHandle): Boolean {
if (cls !is PsiClass) return false
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {
if (cls !is PsiClass) {
completedHandle(cls)
return false
}
contextSwitchListener?.switchTo(cls)
actionContext!!.checkStatus()
statusRecorder.newWork()
try {
when {
!hasApi(cls) -> return false
!hasApi(cls) -> {
completedHandle(cls)
return false
}
shouldIgnore(cls) -> {
logger!!.info("ignore class:" + cls.qualifiedName)
completedHandle(cls)
return true
}
else -> {
Expand All @@ -142,6 +149,7 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
} finally {
statusRecorder.endWork()
}
completedHandle(cls)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ import com.itangcent.common.model.Request
import kotlin.reflect.KClass

interface ClassExporter {
fun export(cls: Any, docHandle: DocHandle): Boolean

/**
* @return return true if this ClassExporter can parse the cls
*/
fun export(cls: Any, docHandle: DocHandle): Boolean {
return export(cls, docHandle, EMPTY_COMPLETED_HANDLE)
}

/**
* @return return true if any api be found
*/
fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean

/**
* the document type which be generate
*/
fun support(docType: KClass<*>): Boolean
}

typealias CompletedHandle = (Any) -> Unit

private val EMPTY_COMPLETED_HANDLE: CompletedHandle = {}

typealias DocHandle = (Doc) -> Unit

inline fun requestOnly(crossinline requestHandle: ((Request) -> Unit)): DocHandle {
Expand All @@ -24,10 +39,16 @@ inline fun requestOnly(crossinline requestHandle: ((Request) -> Unit)): DocHandl
}
}

inline fun methodDocOnly(crossinline requestHandle: ((MethodDoc) -> Unit)): DocHandle {
inline fun methodDocOnly(crossinline methodDocHandle: ((MethodDoc) -> Unit)): DocHandle {
return {
if (it is MethodDoc) {
requestHandle(it)
methodDocHandle(it)
}
}
}

inline fun docs(crossinline docHandle: DocHandle): DocHandle {
return {
docHandle(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ class ComboClassExporter : ClassExporter, Worker {
return this.subClassExporters?.stream()?.map { it.support(docType) }?.anyMatch { it } ?: false
}

override fun export(cls: Any, docHandle: DocHandle): Boolean {
return this.subClassExporters?.stream()?.map { it.export(cls, docHandle) }?.anyMatch { it } ?: false
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {
val ret = this.subClassExporters?.stream()?.map {
it.export(cls, docHandle)
}?.anyMatch { it } ?: false
completedHandle(cls)
return ret;
}

override fun status(): WorkerStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,26 @@ open class DefaultMethodDocClassExporter : ClassExporter, Worker {
@Inject
protected var apiHelper: ApiHelper? = null

override fun export(cls: Any, docHandle: DocHandle): Boolean {
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {
if (!methodDocEnable()) {
completedHandle(cls)
return false
}
if (cls !is PsiClass) {
completedHandle(cls)
return false
}
if (cls !is PsiClass) return false
actionContext!!.checkStatus()
statusRecorder.newWork()
try {
when {
!hasApi(cls) -> return false
!hasApi(cls) -> {
completedHandle(cls)
return false
}
shouldIgnore(cls) -> {
logger!!.info("ignore class:" + cls.qualifiedName)
completedHandle(cls)
return true
}
else -> {
Expand All @@ -124,6 +132,7 @@ open class DefaultMethodDocClassExporter : ClassExporter, Worker {
} finally {
statusRecorder.endWork()
}
completedHandle(cls)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,26 @@ open class SimpleMethodDocClassExporter : ClassExporter, Worker {
@Inject
protected var apiHelper: ApiHelper? = null

override fun export(cls: Any, docHandle: DocHandle): Boolean {
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {
if (!methodDocEnable()) {
completedHandle(cls)
return false
}
if (cls !is PsiClass) {
completedHandle(cls)
return false
}
if (cls !is PsiClass) return false
actionContext!!.checkStatus()
statusRecorder.newWork()
try {
when {
!hasApi(cls) -> return false
!hasApi(cls) -> {
completedHandle(cls)
return false
}
shouldIgnore(cls) -> {
logger!!.info("ignore class:" + cls.qualifiedName)
completedHandle(cls)
return true
}
else -> {
Expand All @@ -98,6 +106,7 @@ open class SimpleMethodDocClassExporter : ClassExporter, Worker {
} finally {
statusRecorder.endWork()
}
completedHandle(cls)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,27 @@ open class SimpleRequestClassExporter : ClassExporter, Worker {
@Inject
protected var apiHelper: ApiHelper? = null

override fun export(cls: Any, docHandle: DocHandle): Boolean {
if (cls !is PsiClass) return false
override fun export(cls: Any, docHandle: DocHandle, completedHandle: CompletedHandle): Boolean {
if (cls !is PsiClass) {
completedHandle(cls)
return false
}
actionContext!!.checkStatus()
statusRecorder.newWork()
try {
when {
!isCtrl(cls) -> return false
!isCtrl(cls) -> {
completedHandle(cls)
return false
}
shouldIgnore(cls) -> {
logger!!.info("ignore class:" + cls.qualifiedName)
completedHandle(cls)
return true
}
else -> {
logger!!.info("search api from:${cls.qualifiedName}")
completedHandle(cls)

foreachMethod(cls) { method ->
exportMethodApi(cls, method, docHandle)
Expand All @@ -91,6 +99,7 @@ open class SimpleRequestClassExporter : ClassExporter, Worker {
} finally {
statusRecorder.endWork()
}
completedHandle(cls)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ class SuvApiExporter {
private val folderNameCartMap: HashMap<String, CartInfo> = HashMap()

@Synchronized
override fun getCartForDoc(folder: Folder, privateToken: String): CartInfo? {
override fun getCartForFolder(folder: Folder, privateToken: String): CartInfo? {
var cartInfo = folderNameCartMap["$privateToken${folder.name}"]
if (cartInfo != null) return cartInfo

cartInfo = super.getCartForDoc(folder, privateToken)
cartInfo = super.getCartForFolder(folder, privateToken)
if (cartInfo != null) {
folderNameCartMap["$privateToken${folder.name}"] = cartInfo
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ open class AbstractYapiApiExporter {
return yapiApiHelper!!.getPrivateToken(module)
}

protected open fun getCartForDoc(resource: Any): CartInfo? {
protected open fun getCartForResource(resource: Any): CartInfo? {

//get token
val module = actionContext!!.callInReadUI { moduleHelper!!.findModule(resource) } ?: return null
Expand All @@ -63,11 +63,15 @@ open class AbstractYapiApiExporter {
}

//get cart
return getCartForResource(resource, privateToken)
}

protected fun getCartForResource(resource: Any, privateToken: String): CartInfo? {
val folder = formatFolderHelper!!.resolveFolder(resource)
return getCartForDoc(folder, privateToken)
return getCartForFolder(folder, privateToken)
}

protected open fun getCartForDoc(folder: Folder, privateToken: String): CartInfo? {
protected open fun getCartForFolder(folder: Folder, privateToken: String): CartInfo? {

val name: String = folder.name ?: "anonymous"

Expand Down Expand Up @@ -101,7 +105,7 @@ open class AbstractYapiApiExporter {

fun exportDoc(doc: Doc): Boolean {
if (doc.resource == null) return false
val cartInfo = getCartForDoc(doc.resource!!) ?: return false
val cartInfo = getCartForResource(doc.resource!!) ?: return false
return exportDoc(doc, cartInfo.privateToken!!, cartInfo.cartId!!)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.openapi.ui.Messages
import com.intellij.util.containers.ContainerUtil
import com.itangcent.common.model.Doc
import com.itangcent.idea.plugin.api.export.Folder
import java.util.HashMap
import java.util.*
import kotlin.collections.HashSet
import kotlin.collections.set

Expand Down Expand Up @@ -42,11 +42,11 @@ class YapiApiDashBoardExporter : AbstractYapiApiExporter() {
private val folderNameCartMap: HashMap<String, CartInfo> = HashMap()

@Synchronized
override fun getCartForDoc(folder: Folder, privateToken: String): CartInfo? {
override fun getCartForFolder(folder: Folder, privateToken: String): CartInfo? {
var cartInfo = folderNameCartMap["$privateToken${folder.name}"]
if (cartInfo != null) return cartInfo

cartInfo = super.getCartForDoc(folder, privateToken)
cartInfo = super.getCartForFolder(folder, privateToken)
if (cartInfo != null) {
folderNameCartMap["$privateToken${folder.name}"] = cartInfo
}
Expand All @@ -55,7 +55,7 @@ class YapiApiDashBoardExporter : AbstractYapiApiExporter() {

fun exportDoc(doc: Doc, privateToken: String): Boolean {
if (doc.resource == null) return false
val cartInfo = getCartForDoc(doc.resource!!) ?: return false
val cartInfo = getCartForResource(doc.resource!!, privateToken) ?: return false
return exportDoc(doc, privateToken, cartInfo.cartId!!)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ class YapiApiExporter : AbstractYapiApiExporter() {
private val folderNameCartMap: HashMap<String, CartInfo> = HashMap()

@Synchronized
override fun getCartForDoc(folder: Folder, privateToken: String): CartInfo? {
override fun getCartForFolder(folder: Folder, privateToken: String): CartInfo? {
var cartInfo = folderNameCartMap["$privateToken${folder.name}"]
if (cartInfo != null) return cartInfo

cartInfo = super.getCartForDoc(folder, privateToken)
cartInfo = super.getCartForFolder(folder, privateToken)
if (cartInfo != null) {
folderNameCartMap["$privateToken${folder.name}"] = cartInfo
}
Expand Down
Loading

0 comments on commit 7c471ed

Please sign in to comment.