Skip to content

Commit

Permalink
Filter unavailable gpus and sort by highest price drop
Browse files Browse the repository at this point in the history
  • Loading branch information
j-a-h-i-r committed Feb 13, 2024
1 parent 8e78add commit 6392129
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 27 deletions.
36 changes: 36 additions & 0 deletions src/startech/__tests__/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GpuPriceChange } from "../../types";
import {
prepareFormattedMessageForPostingToFacebook
} from "../events";

describe("Test facebook message posting", () => {
test("Correct formatting for no GPU change", () => {
const post = prepareFormattedMessageForPostingToFacebook([]);
expect(post).toEqual(
`GPU Price Update\n\n`
)
})

test("Correct format for a list of GPU change", () => {
const priceChanges: GpuPriceChange[] = [
{
gpuid: 1,
name: '',
url: 'https://sample/product',
isAvailable: true,
lastPrice: 10,
hasPriceChanged: true,
hasAvailabilityChanged: true,
previousPrice: 5,
priceDiff: 5,
changes: []
}
]
const post = prepareFormattedMessageForPostingToFacebook(priceChanges);
expect(post).toEqual(
`GPU Price Update\n\n\n`
+ `New Price: 10 (5 📈)\n`
+ `Link: https://sample/product\n`
)
})
})
34 changes: 24 additions & 10 deletions src/startech/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export async function sendEmailOnGpuPriceAvailablityChange() {
const gpuChanges = await getLatestGpuChanges();
logger.debug({ gpuChanges }, "GPU changes");

const gpuIds = gpuChanges.map((gpu) => gpu.gpuid);
// Don't want to email on GPUs that are not available
const availableGpus = gpuChanges.filter((gpu) => gpu.lastPrice > 0);

const gpuIds = availableGpus.map((gpu) => gpu.gpuid);
logger.info({ gpuIds }, "GpuIds with changes");

const recipients = await getGpuEmailSubscribers(gpuIds);
Expand Down Expand Up @@ -131,16 +134,9 @@ export async function postToFacebook() {
return;
}

const lines = gpuChanges.map((gpu) => {
const priceDiff = gpu.lastPrice - gpu.previousPrice;
const priceDiffSign = priceDiff >= 0? "+" : "";
return `${gpu.name} - ${gpu.lastPrice}(${priceDiffSign}${priceDiff})`
})

const gpuPriceText = lines.join("\n");
const availableGpus = gpuChanges.filter((gpu) => gpu.lastPrice > 0);

const post = `GPU Price Update\n\n`
+ `${gpuPriceText}`;
const post = prepareFormattedMessageForPostingToFacebook(availableGpus);

logger.debug(post, "Message to be posted to FB page");

Expand All @@ -154,6 +150,24 @@ export async function postToFacebook() {
})
}

export function prepareFormattedMessageForPostingToFacebook(gpuList: GpuPriceChange[]) {
const lines = gpuList.map((gpu) => {
const priceDiff = gpu.lastPrice - gpu.previousPrice;
const priceDiffSign = priceDiff >= 0? "📈" : "📉";
const absolutePriceDiff = Math.abs(gpu.priceDiff);
return `${gpu.name}\n`
+ `New Price: ${gpu.lastPrice} (${absolutePriceDiff} ${priceDiffSign})\n`
+ `Link: ${gpu.url}\n`
})

const gpuPriceText = lines.join("\n");

const postBody = `GPU Price Update\n\n`
+ `${gpuPriceText}`;

return postBody;
}

export function setupEventHandlers() {
parseEvent.subscribe(sendEmailOnGpuPriceAvailablityChange);
parseEvent.subscribe(postToFacebook)
Expand Down
48 changes: 31 additions & 17 deletions src/startech/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ export async function getGpuPrices(gpuId: number, filter?: { startDate: Date | u
}
}

function checkIfAvailabilityChanged(changes: dbTypes.PriceChange[]) {
if (changes.length !== 2) return false;
const [{ is_available: isAvailable }, { is_available: wasAvailable }] = changes;
const hasAvailabilityChanged = isAvailable !== wasAvailable;
return hasAvailabilityChanged;
function checkIfAvailabilityChanged(
currentGpu: dbTypes.PriceChange,
previousGpu: dbTypes.PriceChange,
) {
return currentGpu.is_available !== previousGpu.is_available;
}

function checkIfPriceChanged(changes: dbTypes.PriceChange[]) {
if (changes.length !== 2) return false;
const [{ price: currentPrice }, { price: previousPrice }] = changes;
const hasPriceChanged = currentPrice !== previousPrice;
return hasPriceChanged;
function checkIfPriceChanged(
currentGpu: dbTypes.PriceChange,
previousGpu: dbTypes.PriceChange,
) {
return currentGpu.price !== previousGpu.price;
}

export async function getLatestGpuChanges(): Promise<GpuPriceChange[]> {
Expand All @@ -72,27 +72,41 @@ export async function getLatestGpuChanges(): Promise<GpuPriceChange[]> {
const { changes } = gpu;
if (changes.length !== 2) return false;

const availabilityChanged = checkIfAvailabilityChanged(changes)
const priceChanged = checkIfPriceChanged(changes)
const [currentPrice, previousPrice] = changes;

const availabilityChanged = checkIfAvailabilityChanged(currentPrice, previousPrice);
const priceChanged = checkIfPriceChanged(currentPrice, previousPrice);

if (!(availabilityChanged || priceChanged)) return false;
return true;
})

const updatesFormatted = updatesWithChanges.map((gpu) => {
const isAvailable = gpu.changes[0].is_available;
const lastPrice = gpu.changes[0].price;
const previousPrice = gpu.changes[1].price;
const [currentGpu, previousGpu] = gpu.changes;
const isAvailable = currentGpu.is_available;
const lastPrice = currentGpu.price;
const previousPrice = previousGpu.price;
const priceDiff = lastPrice - previousPrice;

return {
isAvailable: isAvailable,
lastPrice: lastPrice,
previousPrice: previousPrice,
hasPriceChanged: checkIfPriceChanged(gpu.changes),
hasAvailabilityChanged: checkIfAvailabilityChanged(gpu.changes),
priceDiff: priceDiff,
hasPriceChanged: checkIfPriceChanged(currentGpu, previousGpu),
hasAvailabilityChanged: checkIfAvailabilityChanged(currentGpu, previousGpu),
...gpu,
}
})

updatesFormatted.sort((gpu1, gpu2) => {
if (gpu1.priceDiff < gpu2.priceDiff) return -1;
if (gpu1.priceDiff > gpu2.priceDiff) return 1;
return 0;
})

console.log(updatesFormatted);

return updatesFormatted;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export interface GpuPriceChange {
previousPrice: number
hasPriceChanged: boolean
hasAvailabilityChanged: boolean
priceDiff: number
changes: dbTypes.PriceChange[]
}

0 comments on commit 6392129

Please sign in to comment.