Skip to content
This repository was archived by the owner on Nov 10, 2021. It is now read-only.

Commit 73c8786

Browse files
Merge pull request #12 from veqtor/edge_cache_feed
Implement edge cache for feed
2 parents 9f3302f + e29b094 commit 73c8786

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

conseilUtil.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ const getArtisticOutputForAddress = async (address) => {
136136
return objectInfo.flat(1).sort((a, b) => parseInt(b.objectId) - parseInt(a.objectId))
137137
}
138138

139-
const getArtisticUniverse = async (offset) => {
139+
const getArtisticUniverse = async (max_time) => {
140140
let mintOperationQuery = conseiljs.ConseilQueryBuilder.blankQuery();
141141
mintOperationQuery = conseiljs.ConseilQueryBuilder.addFields(mintOperationQuery, 'operation_group_hash');
142142
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'kind', conseiljs.ConseilOperator.EQ, ['transaction'])
143143
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'timestamp', conseiljs.ConseilOperator.AFTER, [1612240919000]) // 2021 Feb 1
144+
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'timestamp', conseiljs.ConseilOperator.BEFORE, [max_time]) // quantized time (1min)
144145
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'status', conseiljs.ConseilOperator.EQ, ['applied'])
145146
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'destination', conseiljs.ConseilOperator.EQ, [mainnet.protocol])
146147
mintOperationQuery = conseiljs.ConseilQueryBuilder.addPredicate(mintOperationQuery, 'parameters_entrypoints', conseiljs.ConseilOperator.EQ, ['mint_OBJKT'])

index.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,20 @@ const filterTz = (data, tz) => _.filter(data, { tz: tz })
128128

129129
const test = async () => console.log(desc(await getObjkts()))
130130

131-
const getFeed = async (counter, res) => {
131+
const customFloor = function(value, roundTo) {
132+
return Math.floor(value / roundTo) * roundTo;
133+
}
134+
135+
const ONE_MINUTE_MILLIS = 60 * 1000
132136

133137

134-
var arr = await conseilUtil.getArtisticUniverse(0)
138+
const getFeed = async (counter, max_time, res) => {
139+
140+
const now_time = Date.now()
141+
const immutable = (typeof max_time !== 'undefined') && (max_time < now_time)
142+
max_time = (typeof max_time !== 'undefined') ? max_time : customFloor(now_time, ONE_MINUTE_MILLIS)
143+
144+
var arr = await conseilUtil.getArtisticUniverse(max_time)
135145

136146
var feed = offset(desc(arr), counter)
137147
console.log(feed)
@@ -142,11 +152,19 @@ const getFeed = async (counter, res) => {
142152
return e
143153
})
144154
//console.log(feed)
145-
155+
var cache_time
156+
if (immutable) {
157+
cache_time = 60*10
158+
}
159+
else {
160+
cache_time = (int)(((max_time + ONE_MINUTE_MILLIS) - now_time) / 1000)
161+
}
146162
var promise = Promise.all(feed.map(e => e))
147163
promise.then(async (results) => {
148164
var aux_arr = results.map(e => e)
149-
165+
166+
res.set('Cache-Control', `public, max-age=${cache_time}`)
167+
150168
console.log(aux_arr)
151169
res.json({ result: aux_arr })
152170
})
@@ -254,8 +272,14 @@ const app = express()
254272
app.use(express.json())
255273
app.use(cors({ origin: '*' }))
256274

257-
app.post('/feed', async (req, res) => {
258-
await getFeed(req.body.counter, res)
275+
app.get('/feed', async (req, res) => {
276+
var counter = req.query.counter
277+
var max_time = req.query.hasOwnProperty('time') ? customFloor(req.query.time, ONE_MINUTE_MILLIS) : null
278+
const now_time_qt = customFloor(Date.now(), ONE_MINUTE_MILLIS)
279+
if (max_time != null & max_time > now_time_qt) {
280+
max_time = null
281+
}
282+
await getFeed(counter, max_time, res)
259283
})
260284

261285
app.post('/tz', async (req, res) => {

0 commit comments

Comments
 (0)