-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from the-hideout/archived-prices-groundwork
lay groundwork for archived prices
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const WorkerKVSplit = require('../utils/worker-kv-split'); | ||
|
||
class ArchivedPricesAPI extends WorkerKVSplit { | ||
constructor(dataSource) { | ||
super('archived_price_data', dataSource); | ||
} | ||
|
||
async getByItemId(context, itemId) { | ||
await this.init(context, itemId); | ||
const data = await this.getKVData(context, itemId); | ||
if (!data) { | ||
return Promise.reject(new Error('Archived prices data is empty')); | ||
} | ||
|
||
let prices = data.ArchivedPrices[itemId]; | ||
if (!prices) { | ||
return []; | ||
} | ||
return prices; | ||
} | ||
} | ||
|
||
module.exports = ArchivedPricesAPI; |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const WorkerKV = require('./worker-kv'); | ||
|
||
class WorkerKVSplit { | ||
constructor(kvName, dataSource, idLength = 1) { | ||
this.dataExpires = false; | ||
this.kvs = {}; | ||
this.idLength = idLength; | ||
const hexKeys = []; | ||
const maxDecimalValue = parseInt('f'.padEnd(idLength, 'f'), 16); | ||
for (let i = 0; i <= maxDecimalValue; i++) { | ||
const hexValue = i.toString(16).padStart(idLength, '0'); | ||
hexKeys.push(hexValue); | ||
} | ||
for (const hexKey of hexKeys) { | ||
this.kvs[hexKey] = new WorkerKV(`${kvName}_${hexKey}`, dataSource); | ||
} | ||
} | ||
|
||
getIdSuffix(id) { | ||
return id.substring(id.length-this.idLength, id.length); | ||
} | ||
|
||
async init(context, id) { | ||
const kvId = this.getIdSuffix(id); | ||
return this.kvs[kvId].init(context).then(() => { | ||
if (this.kvs[kvId].cache?.expiration) { | ||
this.dataExpiress = new Date(this.kvs[kvId].cache.expiration).valueOf(); | ||
} | ||
}); | ||
} | ||
|
||
async getKVData(context, id) { | ||
await this.init(context, id); | ||
const kvId = this.getIdSuffix(id); | ||
return this.kvs[kvId].cache; | ||
} | ||
} | ||
|
||
module.exports = WorkerKVSplit; |