Skip to content

Instantly share code, notes, and snippets.

@shrugs
Created June 20, 2019 20:46
Show Gist options
  • Save shrugs/cab6eda7e72dd171a782e428943ccc3b to your computer and use it in GitHub Desktop.
Save shrugs/cab6eda7e72dd171a782e428943ccc3b to your computer and use it in GitHub Desktop.
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