-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hourly, monthly and quarterly snapshots
- Loading branch information
Showing
5 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,14 +1,51 @@ | ||
import { BigInt } from "@graphprotocol/graph-ts" | ||
import { log } from "matchstick-as" | ||
|
||
export const MINUTES_15 = BigInt.fromI32(60 * 15) | ||
export const HOUR = BigInt.fromI32(60 * 60) | ||
export const DAY = BigInt.fromI32(60 * 60 * 24) | ||
export const WEEK = BigInt.fromI32(60 * 60 * 24 * 7) | ||
export const MONTH = BigInt.fromI32(60 * 60 * 24 * 30) | ||
export const QUARTER = BigInt.fromI32(60 * 60 * 24 * 30 * 3) | ||
export const YEAR = BigInt.fromI32(60 * 60 * 24 * 365) | ||
export const SNAPSHOT_PERIODS = [DAY, WEEK, YEAR] | ||
export const SNAPSHOT_PERIODS = [HOUR, DAY, WEEK, MONTH, QUARTER, YEAR] | ||
|
||
|
||
@inline | ||
export function getIntervalFromTimestamp(timestamp: BigInt, period: BigInt): BigInt { | ||
// if the period is not stable, use date math to calculate the interval | ||
if (period.ge(WEEK)) { | ||
const date = new Date(timestamp.toI64() * 1000) | ||
date.setUTCMilliseconds(0) | ||
date.setUTCSeconds(0) | ||
date.setUTCMinutes(0) | ||
date.setUTCHours(0) | ||
date.setUTCDate(date.getUTCDate() - date.getUTCDay()) | ||
if (period.equals(WEEK)) { | ||
return BigInt.fromI64(date.getTime() / 1000) | ||
} | ||
date.setUTCDate(1) | ||
if (period.equals(MONTH)) { | ||
return BigInt.fromI64(date.getTime() / 1000) | ||
} | ||
date.setUTCMonth(date.getUTCMonth() - (date.getUTCMonth() % 3)) | ||
if (period.equals(QUARTER)) { | ||
return BigInt.fromI64(date.getTime() / 1000) | ||
} | ||
date.setUTCMonth(0) | ||
if (period.equals(YEAR)) { | ||
return BigInt.fromI64(date.getTime() / 1000) | ||
} | ||
|
||
log.error("Unsupported period: {}", [period.toString()]) | ||
throw Error("Unsupported period: " + period.toString()) | ||
} | ||
return timestamp.div(period).times(period) | ||
} | ||
|
||
|
||
@inline | ||
export function getPreviousIntervalFromTimestamp(timestamp: BigInt, period: BigInt): BigInt { | ||
const truncated = getIntervalFromTimestamp(timestamp, period) | ||
return getIntervalFromTimestamp(truncated.minus(BigInt.fromI32(10)), period) | ||
} |
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,60 @@ | ||
import { assert, test, describe } from "matchstick-as/assembly/index" | ||
import { BigInt } from "@graphprotocol/graph-ts" | ||
import { | ||
DAY, | ||
HOUR, | ||
MONTH, | ||
QUARTER, | ||
WEEK, | ||
YEAR, | ||
getIntervalFromTimestamp, | ||
getPreviousIntervalFromTimestamp, | ||
} from "../../src/utils/time" | ||
|
||
describe("time.getIntervalFromTimestamp", () => { | ||
test("Support all the different periods", () => { | ||
const timestamp = BigInt.fromString("1712744972") | ||
|
||
// simple periods | ||
let res = getIntervalFromTimestamp(timestamp, HOUR) | ||
assert.assertTrue(res.equals(BigInt.fromString("1712743200"))) | ||
|
||
res = getIntervalFromTimestamp(timestamp, DAY) | ||
assert.assertTrue(res.equals(BigInt.fromString("1712707200"))) | ||
|
||
res = getIntervalFromTimestamp(timestamp, WEEK) | ||
assert.assertTrue(res.equals(BigInt.fromString("1712448000"))) | ||
|
||
res = getIntervalFromTimestamp(timestamp, MONTH) | ||
assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) | ||
|
||
res = getIntervalFromTimestamp(timestamp, QUARTER) | ||
assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) | ||
|
||
res = getIntervalFromTimestamp(timestamp, YEAR) | ||
assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) | ||
}) | ||
|
||
test("can query the previous interval as well", () => { | ||
const timestamp = BigInt.fromString("1712744972") | ||
|
||
// simple periods | ||
let res = getPreviousIntervalFromTimestamp(timestamp, HOUR) | ||
assert.assertTrue(res.equals(BigInt.fromString("1712739600"))) | ||
|
||
res = getPreviousIntervalFromTimestamp(timestamp, DAY) | ||
assert.assertTrue(res.equals(BigInt.fromString("1712620800"))) | ||
|
||
res = getPreviousIntervalFromTimestamp(timestamp, WEEK) | ||
assert.assertTrue(res.equals(BigInt.fromString("1711843200"))) | ||
|
||
res = getPreviousIntervalFromTimestamp(timestamp, MONTH) | ||
assert.assertTrue(res.equals(BigInt.fromString("1709251200"))) | ||
|
||
res = getPreviousIntervalFromTimestamp(timestamp, QUARTER) | ||
assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) | ||
|
||
res = getPreviousIntervalFromTimestamp(timestamp, YEAR) | ||
assert.assertTrue(res.equals(BigInt.fromString("1672531200"))) | ||
}) | ||
}) |