Created
July 21, 2021 15:22
-
-
Save jsjoeio/4c56673ec8dd9020ec495d17ae3b3a43 to your computer and use it in GitHub Desktop.
Counting streaks based on updates
This file contains hidden or 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
| async function getStreaks() { | |
| // Initialize google spreadsheet | |
| const doc = await initializeGoogleSpreadsheet("updates") | |
| const sheetTitle = "Test Group" | |
| const sheet = doc.sheetsByTitle[sheetTitle] | |
| // NOTE@jsjoeio - this is hardcoded to the test group | |
| const groupId = -514638766 | |
| const groupStartDate = getGroupStartDate(groupId) | |
| if (!groupStartDate) { | |
| logger.error(`Could not find groupStartDate for groupId ${groupId}.`) | |
| return | |
| } | |
| // NOTE@jsjoeio - we just need a date to calculate the offset | |
| // Thinking outloud here if today is 7/21 at 9am | |
| // and this thing runs. We want it to grab updates from | |
| // the beginning to yesterday | |
| // which means endingCellNumber should be 3, since we also grab | |
| // the header row | |
| const date = convertTZ(new Date(), "America/Phoenix") | |
| const offsetInDays = calculateRowOffset(date, groupStartDate) + 1 | |
| if (offsetInDays < 0) { | |
| logger.error( | |
| `offsetInDays is less than 0: ${offsetInDays}. Something is wrong with date ${date.toString()} and groupStartDate ${groupStartDate.toString()}` | |
| ) | |
| return | |
| } | |
| // 1 to make sure we get the header row | |
| const STARTING_CELL_NUMBER = 1 | |
| // We know the left most column is D | |
| const STARTING_CELL_COLUMN_LETTER = "D" | |
| const ENDING_CELL_COLUMN_LETTER = "F" | |
| // We +1 to account for the header row | |
| const endingCellNumber = offsetInDays + 1 | |
| // By getting say D1:F3, we get all the updates for the group | |
| // it's like selecting a grid of cells | |
| const cellRange = `${STARTING_CELL_COLUMN_LETTER}1:${ENDING_CELL_COLUMN_LETTER}${endingCellNumber}` | |
| await sheet.loadCells(cellRange) | |
| const users = ["jsjoeio", "TheRyanFurrer", "seanjun21"] | |
| const updates: { [telegramUsername: string]: string[] } = {} | |
| for (const user of users) { | |
| const columnLetter = getColumnLetterByTelegramUsername(user) | |
| const endingCellNumber = offsetInDays + 1 | |
| getUpdatesForUser({ | |
| sheet, | |
| updates, | |
| columnLetter, | |
| endingCellNumber, | |
| }) | |
| } | |
| console.log(updates) | |
| // and count streak | |
| } | |
| type GetUpdatesForUser = { | |
| sheet: gs.GoogleSpreadsheetWorksheet | |
| updates: { [telegramUsername: string]: string[] } | |
| columnLetter: string | |
| endingCellNumber: number | |
| } | |
| /** | |
| * This is a helper function to get the updates | |
| * | |
| * The last value in the updates array is the most recent date | |
| * e.g. ["x", "x", ""] means they were on a two-day streak | |
| * but it ended yesterday | |
| */ | |
| function getUpdatesForUser({ | |
| sheet, | |
| updates, | |
| columnLetter, | |
| endingCellNumber, | |
| }: GetUpdatesForUser) { | |
| for (let i = 1; i <= endingCellNumber; i++) { | |
| const telegramUsername = String(sheet.getCellByA1(`${columnLetter}1`).value) | |
| if (i === 1) { | |
| // We know it will be a string but TS doesn't | |
| // We initialize the user in the updates object | |
| updates[telegramUsername] = [] | |
| } else { | |
| const value = String(sheet.getCellByA1(`${columnLetter}${i}`).value) | |
| // If there's no value it will be null | |
| if (value === "null") { | |
| updates[telegramUsername].push("") | |
| } else { | |
| updates[telegramUsername].push(value) | |
| } | |
| } | |
| } | |
| return updates | |
| } | |
| function getColumnLetterByTelegramUsername(telegramUsername: string) { | |
| const columnLettersByTelegramUsername: { | |
| [telegramUsername: string]: string | |
| } = { | |
| jsjoeio: "D", | |
| TheRyanFurrer: "E", | |
| seanjun21: "F", | |
| } | |
| return columnLettersByTelegramUsername[telegramUsername] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment