Skip to content

Commit

Permalink
Adding params to heatmap API (#481)
Browse files Browse the repository at this point in the history
closes: #462
  • Loading branch information
minxylynx authored Jan 4, 2023
1 parent d4ccc93 commit a4103ec
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ktlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.47.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/
- name: run ktlint
run: |
ktlint --disabled_rules=filename,chain-wrapping,enum-entry-name-case --reporter=checkstyle,output=build/ktlint-report.xml **/*.kt
ktlint --disabled_rules=filename,chain-wrapping,enum-entry-name-case,multiline-if-else --reporter=checkstyle,output=build/ktlint-report.xml **/*.kt
continue-on-error: true
- uses: yutailang0119/action-ktlint@v3
with:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Improvements
* Add date parameters to `/api/v3/txs/heatmap` #462
* Added `fromDate`, `toDate`, `timeframe`
* Defaulting to `FOREVER` to return all data

### Bug Fixes
* Fixed `/api/v3/validators` url
* Now handling no fee amount in the tx
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ brew install ktlint
In order to automatically lint/check for things that can't be autocorrected run:
```
ktlint -F "**/*.kt" --disabled_rules=filename,chain-wrapping,enum-entry-name-case
ktlint -F "**/*.kt" --disabled_rules=filename,chain-wrapping,enum-entry-name-case,multiline-if-else
```
This will also correct linting issues, and you can add and commit the updates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ enum class PeriodInSeconds(val seconds: Int) {
YEAR(31536000)
}

enum class Timeframe { WEEK, DAY, HOUR, FOREVER }
enum class Timeframe { QUARTER, MONTH, WEEK, DAY, HOUR, FOREVER }
9 changes: 0 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,4 @@ subprojects {
apiVersion = "1.6"
}
}

configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.apache.logging.log4j" && (requested.version == "2.14.1") || (requested.version == "2.15.0")) {
useVersion("2.15.0")
because("CVE-2021-44228")
}
}
}
}
8 changes: 4 additions & 4 deletions docker/Dockerfile-service
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ COPY build.gradle.kts gradle.properties settings.gradle.kts buildSrc/build.gradl
RUN gradle clean build --no-daemon > /dev/null 2>&1 || true

# Copy all files
COPY service /app/service/
COPY database /app/database/
COPY api-model /app/api-model/
COPY buildSrc /app/buildSrc/
COPY .git /app/.git/
COPY build.gradle.kts gradle.properties settings.gradle.kts /app/
COPY api-model /app/api-model/
COPY database /app/database/
COPY service /app/service/
COPY CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md LICENSE README.md /app/
COPY .git /app/.git/

# Do the actual build
RUN gradle clean build --no-daemon
Expand Down
9 changes: 0 additions & 9 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ dependencyManagement {
}
}

configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.apache.logging.log4j" && (requested.version == "2.14.1") || (requested.version == "2.15.0")) {
useVersion("2.15.0")
because("CVE-2021-44228")
}
}
}

var profiles = System.getenv("SPRING_PROFILES_ACTIVE") ?: "development"

tasks.getByName<BootRun>("bootRun") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.IntegerColumnType
import org.jetbrains.exposed.sql.Max
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.greaterEq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.SqlExpressionBuilder.lessEq
import org.jetbrains.exposed.sql.Sum
import org.jetbrains.exposed.sql.VarCharColumnType
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.count
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insertIgnore
Expand Down Expand Up @@ -355,7 +359,7 @@ class BlockCacheHourlyTxCountsRecord(id: EntityID<DateTime>) : Entity<DateTime>(
}
}

fun getTxHeatmap() = transaction {
fun getTxHeatmap(fromDate: DateTime? = null, toDate: DateTime? = null) = transaction {
val blockTimestamp = BlockCacheHourlyTxCountsTable.blockTimestamp
val dow = ExtractDOW(blockTimestamp)
val day = ExtractDay(blockTimestamp)
Expand All @@ -364,30 +368,23 @@ class BlockCacheHourlyTxCountsRecord(id: EntityID<DateTime>) : Entity<DateTime>(
val result = BlockCacheHourlyTxCountsTable
.slice(dow, day, hour, txSum)
.selectAll()
.andWhere {
if (fromDate != null && toDate != null)
BlockCacheHourlyTxCountsTable.blockTimestamp.between(fromDate.startOfDay(), toDate.startOfDay())
else Op.TRUE
}
.groupBy(dow)
.groupBy(day)
.groupBy(hour)
.orderBy(dow, SortOrder.ASC)
.orderBy(hour, SortOrder.ASC)
.map {
TxHeatmapRaw(
it[dow],
it[day].trim(),
it[hour],
it[txSum]!!
)
}
.map { TxHeatmapRaw(it[dow], it[day].trim(), it[hour], it[txSum]!!) }
.groupBy { it.dow }
.map {
TxHeatmap(
dow = it.key,
day = it.value[0].day,
data = it.value.map { row ->
TxHeatmapHour(
row.hour,
row.numberTxs
)
}
data = it.value.map { row -> TxHeatmapHour(row.hour, row.numberTxs) }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import io.provenance.explorer.domain.entities.TxMsgTypeSubtypeRecord
import io.provenance.explorer.domain.entities.ValidatorMarketRateRecord
import io.provenance.explorer.domain.entities.ValidatorStateRecord
import io.provenance.explorer.domain.entities.getFeepayer
import io.provenance.explorer.domain.exceptions.InvalidArgumentException
import io.provenance.explorer.domain.exceptions.validate
import io.provenance.explorer.domain.extensions.formattedString
import io.provenance.explorer.domain.extensions.msgEventsToObjectNodePrint
import io.provenance.explorer.domain.extensions.pageCountOfResults
import io.provenance.explorer.domain.extensions.startOfDay
import io.provenance.explorer.domain.extensions.toCoinStr
import io.provenance.explorer.domain.extensions.toFeePaid
import io.provenance.explorer.domain.extensions.toFees
Expand All @@ -42,13 +44,15 @@ import io.provenance.explorer.model.MsgInfo
import io.provenance.explorer.model.MsgTypeSet
import io.provenance.explorer.model.TxDetails
import io.provenance.explorer.model.TxGov
import io.provenance.explorer.model.TxHeatmapRes
import io.provenance.explorer.model.TxHistoryChartData
import io.provenance.explorer.model.TxMessage
import io.provenance.explorer.model.TxSmartContract
import io.provenance.explorer.model.TxStatus
import io.provenance.explorer.model.TxSummary
import io.provenance.explorer.model.TxType
import io.provenance.explorer.model.base.PagedResults
import io.provenance.explorer.model.base.Timeframe
import io.provenance.explorer.model.base.getParentForType
import io.provenance.explorer.model.base.isMAddress
import io.provenance.explorer.model.base.toMAddress
Expand Down Expand Up @@ -227,7 +231,30 @@ class TransactionService(
}
}

fun getTxHeatmap() = BlockCacheHourlyTxCountsRecord.getTxHeatmap()
fun getTxHeatmap(fromDate: DateTime? = null, toDate: DateTime? = null, timeframe: Timeframe = Timeframe.FOREVER): TxHeatmapRes {
if (fromDate != null && toDate != null)
return BlockCacheHourlyTxCountsRecord.getTxHeatmap(fromDate, toDate)

val (from, to) = when (timeframe) {
Timeframe.FOREVER -> null to null
Timeframe.QUARTER ->
if (fromDate != null) fromDate to fromDate.plusMonths(3)
else if (toDate != null) toDate.minusMonths(3) to toDate
else DateTime.now().startOfDay().let { it.minusMonths(3) to it }
Timeframe.MONTH ->
if (fromDate != null) fromDate to fromDate.plusMonths(1)
else if (toDate != null) toDate.minusMonths(1) to toDate
else DateTime.now().startOfDay().let { it.minusMonths(1) to it }
Timeframe.WEEK ->
if (fromDate != null) fromDate to fromDate.plusWeeks(1)
else if (toDate != null) toDate.minusWeeks(1) to toDate
else DateTime.now().startOfDay().let { it.minusWeeks(1) to it }
Timeframe.DAY, Timeframe.HOUR ->
throw InvalidArgumentException("Timeframe ${timeframe.name} is not supported for heatmap data")
}

return BlockCacheHourlyTxCountsRecord.getTxHeatmap(from, to)
}

private fun getMonikers(txId: EntityID<Int>): Map<String, String> {
val monikers = TxAddressJoinRecord.findValidatorsByTxHash(valService.getActiveSet(), txId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ class ValidatorService(
Timeframe.DAY -> hourlyBlockCount * 24
Timeframe.HOUR -> hourlyBlockCount
Timeframe.FOREVER -> currentHeight - 1
Timeframe.QUARTER -> hourlyBlockCount * 24 * (365 / 4)
Timeframe.MONTH -> hourlyBlockCount * 24 * (365 / 12)
}

val validators = MissedBlocksRecord
Expand Down Expand Up @@ -580,6 +582,8 @@ class ValidatorService(
Timeframe.DAY -> hourlyBlockCount * 24
Timeframe.HOUR -> hourlyBlockCount
Timeframe.FOREVER -> currentHeight - 1
Timeframe.QUARTER -> hourlyBlockCount * 24 * (365 / 4)
Timeframe.MONTH -> hourlyBlockCount * 24 * (365 / 12)
}

val valConsAddr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class TransactionControllerV2(private val transactionService: TransactionService

@ApiOperation("Returns a heatmap of transaction activity on chain")
@GetMapping("/heatmap")
@Deprecated("Use /api/v3/txs/heatmap")
@java.lang.Deprecated
fun txHeatmap() = transactionService.getTxHeatmap()

@ApiOperation("Return list of transaction types")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.provenance.explorer.web.v3

import io.provenance.explorer.domain.models.explorer.TxHistoryDataRequest
import io.provenance.explorer.model.base.DateTruncGranularity
import io.provenance.explorer.model.base.Timeframe
import io.provenance.explorer.service.TransactionService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
Expand Down Expand Up @@ -107,4 +108,33 @@ class TransactionControllerV3(private val transactionService: TransactionService
@RequestParam(required = false)
blockHeight: Int? = null
) = transactionService.getTxTypesByTxHash(hash)

@ApiOperation("Returns a heatmap of transaction activity on chain")
@GetMapping("/heatmap")
fun txHeatmap(
@ApiParam(
type = "DateTime",
value = "DateTime format as `yyyy-MM-dd` — for example, \"2000-10-31\"",
required = false
)
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
fromDate: DateTime?,
@ApiParam(
type = "DateTime",
value = "DateTime format as `yyyy-MM-dd` — for example, \"2000-10-31\"",
required = false
)
@RequestParam(required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
toDate: DateTime?,
@ApiParam(
value = "The timeframe of data, either QUARTER, MONTH, WEEK, or FOREVER",
defaultValue = "FOREVER",
required = false,
allowableValues = "FOREVER,QUARTER,MONTH,WEEK"
)
@RequestParam(defaultValue = "FOREVER", required = false)
timeframe: Timeframe
) = transactionService.getTxHeatmap(fromDate, toDate, timeframe)
}

0 comments on commit a4103ec

Please sign in to comment.