This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
generated from slack-samples/deno-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Gather all logged runs in a standard date format #23
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
08b6546
verify all records are gathered with a standard date format
zimeg 8365131
improve typing for mockings of logged runs
zimeg dba9a61
update test dates to a friday
zimeg e7fa7d0
import std/testing with the import map
zimeg ce860f7
update test dates to check a complete week
zimeg f0baf26
merge w main
zimeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { DefineFunction, Schema, SlackFunction } from "deno-slack-sdk/mod.ts"; | ||
import RunningDatastore, { RUN_DATASTORE } from "../datastores/run_data.ts"; | ||
import { queryRunningDatastore } from "../datastores/run_data.ts"; | ||
import { RunnerStatsType } from "../types/runner_stats.ts"; | ||
|
||
export const CollectRunnerStatsFunction = DefineFunction({ | ||
|
@@ -16,6 +16,7 @@ export const CollectRunnerStatsFunction = DefineFunction({ | |
runner_stats: { | ||
type: Schema.types.array, | ||
items: { type: RunnerStatsType }, | ||
title: "Runner stats", | ||
description: "Weekly and all-time total distances for runners", | ||
}, | ||
}, | ||
|
@@ -24,28 +25,27 @@ export const CollectRunnerStatsFunction = DefineFunction({ | |
}); | ||
|
||
export default SlackFunction(CollectRunnerStatsFunction, async ({ client }) => { | ||
// Query the datastore for all the data we collected | ||
const runs = await client.apps.datastore.query< | ||
typeof RunningDatastore.definition | ||
>({ datastore: RUN_DATASTORE }); | ||
|
||
if (!runs.ok) { | ||
return { error: `Failed to retrieve past runs: ${runs.error}` }; | ||
} | ||
|
||
const runners = new Map<typeof Schema.slack.types.user_id, { | ||
runner: typeof Schema.slack.types.user_id; | ||
total_distance: number; | ||
weekly_distance: number; | ||
}>(); | ||
|
||
const startOfLastWeek = new Date(); | ||
startOfLastWeek.setDate(startOfLastWeek.getDate() - 6); | ||
const today = new Date(Date.now()); | ||
const startOfLastWeek = new Date( | ||
new Date(Date.now()).setDate(today.getDate() - 6), | ||
); | ||
|
||
// Query the datastore for all the data we collected | ||
const runs = await queryRunningDatastore(client); | ||
if (!runs.ok) { | ||
return { error: `Failed to retrieve past runs: ${runs.error}` }; | ||
} | ||
|
||
// Add run statistics to the associated runner | ||
runs.items.forEach((run) => { | ||
const isRecentRun = run.rundate >= | ||
startOfLastWeek.toLocaleDateString("en-CA", { timeZone: "UTC" }); | ||
const isRecentRun = | ||
run.rundate >= startOfLastWeek.toISOString().substring(0, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👨🍳 💋 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ISO-8601 just feels so much better 🙌 🙌 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ISO8601 is the best date standard IMO |
||
|
||
// Find existing runner record or create new one | ||
const runner = runners.get(run.runner) || | ||
|
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,47 @@ | ||
import * as mf from "mock-fetch/mod.ts"; | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { assertEquals } from "std/testing/asserts.ts"; | ||
import CollectRunnerStatsFunction from "./collect_runner_stats.ts"; | ||
import { DatastoreItem } from "deno-slack-api/types.ts"; | ||
import RunningDatastore from "../datastores/run_data.ts"; | ||
|
||
// Mocked date for stable testing | ||
Date.now = () => new Date("2023-01-06").getTime(); | ||
|
||
// Collection of runs stored in the mocked datastore | ||
const mockRuns: DatastoreItem<typeof RunningDatastore.definition>[] = [ | ||
{ id: "R006", runner: "U0123456", distance: 4, rundate: "2023-01-06" }, | ||
{ id: "R005", runner: "U0123456", distance: 2, rundate: "2023-01-06" }, | ||
{ id: "R004", runner: "U7777777", distance: 2, rundate: "2023-01-03" }, | ||
{ id: "R003", runner: "U0123456", distance: 4, rundate: "2022-12-31" }, | ||
{ id: "R002", runner: "U7777777", distance: 1, rundate: "2022-12-10" }, | ||
{ id: "R001", runner: "U0123456", distance: 2, rundate: "2022-11-11" }, | ||
]; | ||
|
||
// Replaces globalThis.fetch with the mocked copy | ||
mf.install(); | ||
|
||
mf.mock("POST@/api/apps.datastore.query", () => { | ||
return new Response(JSON.stringify({ ok: true, items: mockRuns })); | ||
}); | ||
|
||
const { createContext } = SlackFunctionTester("collect_runner_stats"); | ||
|
||
Deno.test("Collect runner stats", async () => { | ||
const { outputs, error } = await CollectRunnerStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
|
||
const expectedStats = [{ | ||
runner: "U0123456", | ||
weekly_distance: 10, | ||
total_distance: 12, | ||
}, { | ||
runner: "U7777777", | ||
weekly_distance: 2, | ||
total_distance: 3, | ||
}]; | ||
|
||
assertEquals(error, undefined); | ||
assertEquals(outputs?.runner_stats, expectedStats); | ||
}); |
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,65 @@ | ||
import * as mf from "mock-fetch/mod.ts"; | ||
import { assertEquals } from "std/testing/asserts.ts"; | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { DatastoreItem } from "deno-slack-api/types.ts"; | ||
import CollectTeamStatsFunction from "./collect_team_stats.ts"; | ||
import RunningDatastore from "../datastores/run_data.ts"; | ||
|
||
// Mocked date for stable testing | ||
Date.now = () => new Date("2023-01-06").getTime(); | ||
|
||
// Collection of runs stored in the mocked datastore | ||
let mockRuns: DatastoreItem<typeof RunningDatastore.definition>[]; | ||
|
||
// Replaces globalThis.fetch with the mocked copy | ||
mf.install(); | ||
|
||
mf.mock("POST@/api/apps.datastore.query", async (args) => { | ||
const body = await args.formData(); | ||
const dates = JSON.parse(body.get("expression_values") as string); | ||
const runs = mockRuns.filter((run) => ( | ||
run.rundate >= dates[":start_date"] && run.rundate <= dates[":end_date"] | ||
)); | ||
return new Response(JSON.stringify({ ok: true, items: runs })); | ||
}); | ||
|
||
const { createContext } = SlackFunctionTester("collect_team_stats"); | ||
|
||
Deno.test("Retrieve the empty set", async () => { | ||
mockRuns = []; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 0); | ||
assertEquals(outputs?.percent_change, 0); | ||
}); | ||
|
||
Deno.test("Count only runs from the past week", async () => { | ||
mockRuns = [ | ||
{ id: "R006", runner: "U0123456", distance: 8, rundate: "2023-01-07" }, | ||
{ id: "R005", runner: "U0123456", distance: 4, rundate: "2023-01-06" }, | ||
{ id: "R004", runner: "U7777777", distance: 2, rundate: "2023-01-02" }, | ||
{ id: "R003", runner: "U0123456", distance: 4, rundate: "2022-12-31" }, | ||
{ id: "R002", runner: "U7777777", distance: 6, rundate: "2022-12-31" }, | ||
{ id: "R001", runner: "U8888888", distance: 1, rundate: "2022-12-30" }, | ||
]; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 16); | ||
assertEquals(outputs?.percent_change, 1500); | ||
}); | ||
|
||
Deno.test("Handle the infinite change", async () => { | ||
mockRuns = [ | ||
{ id: "R001", runner: "U0123456", distance: 10, rundate: "2023-01-05" }, | ||
]; | ||
const { outputs, error } = await CollectTeamStatsFunction( | ||
createContext({ inputs: {} }), | ||
); | ||
assertEquals(error, undefined); | ||
assertEquals(outputs?.weekly_distance, 10); | ||
assertEquals(outputs?.percent_change, 0); | ||
}); |
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,38 @@ | ||
import { SlackFunctionTester } from "deno-slack-sdk/mod.ts"; | ||
import { assertEquals, assertStringIncludes } from "std/testing/asserts.ts"; | ||
import FormatLeaderboardFunction from "./format_leaderboard.ts"; | ||
|
||
const { createContext } = SlackFunctionTester("format_leaderboard"); | ||
|
||
Deno.test("Collect team stats", async () => { | ||
const inputs = { | ||
team_distance: 11, | ||
percent_change: 50, | ||
runner_stats: [{ | ||
runner: "U0123456", | ||
weekly_distance: 4, | ||
total_distance: 8, | ||
}, { | ||
runner: "U7777777", | ||
weekly_distance: 2, | ||
total_distance: 3, | ||
}], | ||
}; | ||
|
||
const { outputs, error } = await FormatLeaderboardFunction( | ||
createContext({ inputs }), | ||
); | ||
|
||
assertEquals(error, undefined); | ||
assertStringIncludes(outputs?.teamStatsFormatted || "", "11 miles"); | ||
assertStringIncludes(outputs?.teamStatsFormatted || "", "50%"); | ||
|
||
assertStringIncludes( | ||
outputs?.runnerStatsFormatted || "", | ||
"<@U0123456> ran 4 miles last week (8 total)", | ||
); | ||
assertStringIncludes( | ||
outputs?.runnerStatsFormatted || "", | ||
"<@U7777777> ran 2 miles last week (3 total)", | ||
); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do
/while
w/ cursors in Dynamo is exactly how I structure my DDB pagination calls 👍