mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-01-10 20:22:58 -05:00
move /api/popular to /api/stats, calculate totals
This commit is contained in:
@@ -44,7 +44,6 @@ header {
|
|||||||
letter-spacing: -0.01em;
|
letter-spacing: -0.01em;
|
||||||
}
|
}
|
||||||
|
|
||||||
// mix up logo colors on hover
|
|
||||||
&:hover {
|
&:hover {
|
||||||
@include colors() {
|
@include colors() {
|
||||||
color: c(links);
|
color: c(links);
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ const pluralize = require("pluralize");
|
|||||||
const rssParser = require("rss-parser");
|
const rssParser = require("rss-parser");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
|
const baseUrl = "https://jarv.is/";
|
||||||
|
const feedUrl = "https://jarv.is/feed.xml";
|
||||||
|
|
||||||
exports.handler = async () => {
|
exports.handler = async () => {
|
||||||
try {
|
try {
|
||||||
const parser = new rssParser({
|
const parser = new rssParser({
|
||||||
@@ -16,7 +19,7 @@ exports.handler = async () => {
|
|||||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||||
});
|
});
|
||||||
|
|
||||||
const feed = await parser.parseURL("https://jarv.is/feed.xml");
|
const feed = await parser.parseURL(feedUrl);
|
||||||
const result = await client.query(
|
const result = await client.query(
|
||||||
q.Map(
|
q.Map(
|
||||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||||
@@ -24,29 +27,42 @@ exports.handler = async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
let posts = result.data;
|
let stats = {
|
||||||
|
total: {
|
||||||
|
hits: 0,
|
||||||
|
},
|
||||||
|
pages: result.data,
|
||||||
|
};
|
||||||
|
|
||||||
posts.map((p) => {
|
stats.pages.map((p) => {
|
||||||
// match URLs from RSS feed with db to populate some metadata
|
// match URLs from RSS feed with db to populate some metadata
|
||||||
let match = feed.items.find((x) => x.link === "https://jarv.is/" + p.slug + "/");
|
let match = feed.items.find((x) => x.link === baseUrl + p.slug + "/");
|
||||||
if (match) {
|
if (match) {
|
||||||
p.title = match.title;
|
p.title = match.title;
|
||||||
p.url = match.link;
|
p.url = match.link;
|
||||||
p.date = match.isoDate;
|
p.date = match.isoDate;
|
||||||
|
delete p.slug;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add comma-separated numbers and proper pluralization on the backend
|
// it's easier to add comma-separated numbers and proper pluralization here on the backend
|
||||||
p.pretty_hits = numeral(p.hits).format("0,0");
|
p.pretty_hits = numeral(p.hits).format("0,0");
|
||||||
p.pretty_unit = pluralize("hit", p.hits);
|
p.pretty_unit = pluralize("hit", p.hits);
|
||||||
|
|
||||||
|
// add these hits to running tally
|
||||||
|
stats.total.hits += p.hits;
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
});
|
});
|
||||||
|
|
||||||
// sort by hits (descending)
|
// sort by hits (descending)
|
||||||
posts.sort((a, b) => {
|
stats.pages.sort((a, b) => {
|
||||||
return a.hits > b.hits ? -1 : 1;
|
return a.hits > b.hits ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// do same prettification as above to totals
|
||||||
|
stats.total.pretty_hits = numeral(stats.total.hits).format("0,0");
|
||||||
|
stats.total.pretty_unit = pluralize("hit", stats.total.hits);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -54,7 +70,7 @@ exports.handler = async () => {
|
|||||||
"Access-Control-Allow-Methods": "GET",
|
"Access-Control-Allow-Methods": "GET",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"Access-Control-Allow-Origin": "*",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(posts),
|
body: JSON.stringify(stats),
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
Reference in New Issue
Block a user