-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (107 loc) · 4.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const _ = require("lodash");
const express = require("express");
const app = express();
const PORT = 3000;
const FETCH_OPTIONS = {
method: "GET",
headers: {
"x-hasura-admin-secret": "32qR4KmXOIpsGPQKMqEJHGJS27G5s7HdSKO3gdtQd2kv5e852SiYwWNfxkZOBuQ6"
}
};
const URL = 'https://intent-kit-16.hasura.app/api/rest/blogs';
let searchBlogsCache = null;
let analyzeBlogsCache = null;
const ANALYZED_BLOGS_CACHE_KEY = "analyzedBlogs";
const FILTERED_BLOGS_CACHE_KEY = "filteredBlogs";
function cacheData(func, cacheKey, timeout) {
const cache = _.memoize(func, () => cacheKey);
// Timeout to expire cache after the specified time in ms
setTimeout(() => {
if (cacheKey === FILTERED_BLOGS_CACHE_KEY && searchBlogsCache) {
try {
console.log(`Cache expired for key "${cacheKey}"`);
searchBlogsCache.cache && searchBlogsCache.cache.clear();
searchBlogsCache = null;
} catch (error) {
console.error(`Error clearing cache for key ${cacheKey}: ${error}`);
}
}
if (cacheKey === ANALYZED_BLOGS_CACHE_KEY && analyzeBlogsCache) {
try {
console.log(`Cache expired for key "${cacheKey}"`);
analyzeBlogsCache.cache && analyzeBlogsCache.cache.clear();
analyzeBlogsCache = null;
} catch (error) {
console.error(`Error clearing cache for key ${cacheKey}: ${error}`);
}
}
}, timeout);
return cache;
}
async function fetchBlogs() {
try {
const response = await fetch(URL, FETCH_OPTIONS);
if (!response.ok) {
throw new Error(`Error! Failed to fetch blogs: ${response.status} ${response.statusText}`);
}
const data = await response.json();
const blogs = data.blogs;
if (!Array.isArray(blogs)) {
throw new Error("Unable to parse blogs");
}
return blogs;
} catch (error) {
console.error(error);
throw error;
}
}
app.get("/api/blog-stats", async (_req, res) => {
try {
if (!analyzeBlogsCache) {
analyzeBlogsCache = cacheData(async () => {
const blogs = await fetchBlogs();
const totalNumOfBlogs = _.size(blogs);
const blogWithLongestTitle = _.maxBy(blogs, blog => blog.title.length).title;
const numOfBlogsContainingTheWordPrivacy = _.filter(blogs, blog => _.includes(blog.title.toLowerCase(), 'privacy')).length;
const uniqueBlogTitleArray = _.uniqBy(blogs, blog => blog.title).map(blog => blog.title);
return {
totalNumOfBlogs,
blogWithLongestTitle,
numOfBlogsContainingTheWordPrivacy,
uniqueBlogTitleArray
};
}, ANALYZED_BLOGS_CACHE_KEY, 10000);
}
const responseObject = await analyzeBlogsCache();
res.json(responseObject);
} catch (error) {
console.error(error);
res.status(500).json({ error: error.message });
}
});
app.get("/api/blog-search", async (req, res) => {
try {
const query = req.query.query;
if (!query) {
return res.status(400).json({ error: "Query parameter is required." });
}
if (!searchBlogsCache) {
searchBlogsCache = cacheData(async () => {
const blogs = await fetchBlogs();
return _.filter(blogs, blog => _.includes(blog.title.toLowerCase(), query.toLowerCase()));
}, FILTERED_BLOGS_CACHE_KEY, 10000);
}
const filteredBlogs = await searchBlogsCache();
if (filteredBlogs.length === 0) {
res.status(404).json({ error: `No matching blogs found for ${query.toLowerCase()}` });
} else {
res.json(filteredBlogs);
}
} catch (error) {
console.error(error);
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});