Skip to content

Commit

Permalink
Merge pull request #255 from the-hideout/historical-prices-days
Browse files Browse the repository at this point in the history
Add ability to specify number of days for historical prices
  • Loading branch information
Razzmatazzz authored Dec 25, 2023
2 parents 8d826a4 + c0643a4 commit 35789af
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
interval: "monthly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
interval: "monthly"
6 changes: 3 additions & 3 deletions .github/workflows/branch-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: setup node
if: ${{ steps.branch-deploy.outputs.continue == 'true' }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm
Expand All @@ -55,7 +55,7 @@ jobs:
environment: "development"

# Post comment on PR with development deploy info
- uses: GrantBirki/[email protected].7
- uses: GrantBirki/[email protected].8
if: ${{ steps.branch-deploy.outputs.continue == 'true' &&
steps.branch-deploy.outputs.noop != 'true' &&
steps.branch-deploy.outputs.environment == 'development' }}
Expand All @@ -81,7 +81,7 @@ jobs:
apiToken: ${{ secrets.CF_API_TOKEN }}

# Post comment on PR with production deploy info
- uses: GrantBirki/[email protected].7
- uses: GrantBirki/[email protected].8
if: ${{ steps.branch-deploy.outputs.continue == 'true' &&
steps.branch-deploy.outputs.noop != 'true' &&
steps.branch-deploy.outputs.environment == 'production' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: actions/checkout@v4

- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/new-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v4

- name: comment
uses: GrantBirki/[email protected].7
uses: GrantBirki/[email protected].8
continue-on-error: true
with:
file: .github/new-pr-comment.md
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v4

- name: setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm
Expand Down
34 changes: 28 additions & 6 deletions datasources/historical-prices.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,40 @@ const WorkerKV = require('../utils/worker-kv');
class historicalPricesAPI extends WorkerKV {
constructor(dataSource) {
super('historical_price_data', dataSource);
this.defaultDays = 7;
this.maxDays = 7;
this.itemLimitDays = 2;
}

async getByItemId(requestId, itemId, limit = 0) {
await this.init(requestId);
async getByItemId(context, itemId, days = this.defaultDays, halfResults = false) {
await this.init(context);
if (!this.cache) {
return Promise.reject(new Error('Historical prices cache is empty'));
}
if (!this.cache.historicalPricePoint[itemId]) return [];
if (limit) {
return this.cache.historicalPricePoint[itemId].slice(Math.max(this.cache.historicalPricePoint[itemId].length - limit, 0));

if (days > this.maxDays || days < 1) {
const warningMessage = `Historical prices days argument of ${days} must be 1-${this.maxDays}; defaulting to ${this.defaultDays}.`;
days = this.defaultDays;
if (!context.warnings.some(warning => warning.message === warningMessage)) {
context.warnings.push({message: warningMessage});
}
}

let prices = this.cache.historicalPricePoint[itemId];
if (!prices) {
return [];
}
else if (days === this.maxDays) {
return prices;
}
else {
const cutoffTimestamp = new Date().setDate(new Date().getDate() - days);
let dayFiltered = prices.filter(hp => hp.timestamp >= cutoffTimestamp);
if (halfResults) {
dayFiltered = dayFiltered.filter((hp, index) => index % 2 === 0);
}
return dayFiltered;
}
return this.cache.historicalPricePoint[itemId];
}
}

Expand Down
114 changes: 75 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"license": "ISC",
"devDependencies": {
"newman": "^6.0.0",
"prettier": "^3.0.3",
"prettier": "^3.1.0",
"wrangler": "^3.10.1"
},
"dependencies": {
"@graphql-tools/merge": "9.0.0",
"@graphql-tools/schema": "10.0.0",
"@graphql-tools/merge": "9.0.1",
"@graphql-tools/schema": "10.0.2",
"graphql": "^15.8.0",
"uuid": "9.0.1"
}
Expand Down
6 changes: 3 additions & 3 deletions resolvers/itemResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = {
return context.data.item.getItemsByBsgCategoryId(context, args.bsgCategoryId, undefined);
},
historicalItemPrices(obj, args, context, info) {
return context.util.paginate(context.data.historicalPrice.getByItemId(context, args.id), args);
return context.util.paginate(context.data.historicalPrice.getByItemId(context, args.id, args.days), args);
},
armorMaterials(obj, args, context) {
return context.data.item.getArmorMaterials(context);
Expand Down Expand Up @@ -191,11 +191,11 @@ module.exports = {
},
async historicalPrices(data, args, context, info) {
context.util.testDepthLimit(info, 1);
const warningMessage = 'Querying historicalPrices on the Item object will return only the 10 most recent prices. For a full list of historical prices, use the historicalItemPrices query.';
const warningMessage = `Querying historicalPrices on the Item object will only provide half the prices from the last ${context.data.historicalPrice.itemLimitDays} days. For up to ${context.data.historicalPrice.maxDays} days of historical prices, use the historicalItemPrices query.`;
if (!context.warnings.some(warning => warning.message === warningMessage)) {
context.warnings.push({message: warningMessage});
}
return context.data.historicalPrice.getByItemId(context, data.id, 10);
return context.data.historicalPrice.getByItemId(context, data.id, context.data.historicalPrice.itemLimitDays, true);
},
imageLink(data) {
return data.inspectImageLink;
Expand Down
Loading

0 comments on commit 35789af

Please sign in to comment.