Created
June 20, 2019 20:46
-
-
Save shrugs/cab6eda7e72dd171a782e428943ccc3b to your computer and use it in GitHub Desktop.
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
import { Context } from "aws-lambda"; | |
import AWS = require("aws-sdk"); | |
import { orderBy, uniq } from "lodash"; | |
import dynamodb from "./dynamodb"; | |
import { kLiveChannelsTable } from "./environment"; | |
import { LiveChannelInfo, LiveChannelRecord } from "./types"; | |
import { InternalError } from "./errors"; | |
import { getKnownChannelInfos } from "./channelInfo"; | |
const kLiveChannelConstantPartitionKey = "constant"; | |
export const handler = main( | |
async (event, context): Promise<LiveChannelInfo[]> => { | |
const results = await dynamodb | |
.query({ | |
TableName: kLiveChannelsTable, | |
IndexName: "by_view_count", | |
KeyConditionExpression: "#key = :constant", | |
ExpressionAttributeNames: { | |
"#key": "key", | |
}, | |
ExpressionAttributeValues: { | |
":constant": { S: kLiveChannelConstantPartitionKey }, | |
}, | |
ScanIndexForward: true, | |
Limit: 20, | |
}) | |
.promise(); | |
if (!results.Items) { | |
throw new InternalError(`No Items Returned: ${JSON.stringify(results)}`); | |
} | |
const items = (results.Items || []).map( | |
item => AWS.DynamoDB.Converter.unmarshall(item) as LiveChannelRecord, | |
); | |
const ids = uniq(items.map(le => le.id)); | |
const channelInfos = await getKnownChannelInfos(ids, [ | |
"id", | |
"login", | |
"display_name", | |
"profile_image_url", | |
"view_count", | |
]); | |
const liveChannelInfos = channelInfos.map<LiveChannelInfo>(ei => ({ | |
...(items.find(le => le.id === ei.id) as LiveChannelRecord), | |
...ei, | |
})); | |
return orderBy(liveChannelInfos, ["view_count"], ["desc"]); | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment