2019-11-02 19:26:26 +03:30
|
|
|
import Queue from "bull";
|
|
|
|
import useragent from "useragent";
|
|
|
|
import geoip from "geoip-lite";
|
|
|
|
import URL from "url";
|
|
|
|
|
|
|
|
import { createVisit, addLinkCount } from "./db/link";
|
2019-11-02 20:14:00 +03:30
|
|
|
import { getStatsLimit } from "./utils";
|
2019-11-02 19:26:26 +03:30
|
|
|
|
|
|
|
const redis = {
|
|
|
|
port: Number(process.env.REDIS_PORT) || 6379,
|
|
|
|
host: process.env.REDIS_HOST || "127.0.0.1",
|
|
|
|
...(process.env.REDIS_PASSWORD && { password: process.env.REDIS_PASSWORD })
|
|
|
|
};
|
|
|
|
|
|
|
|
export const visitQueue = new Queue("visit", { redis });
|
|
|
|
|
|
|
|
const browsersList = ["IE", "Firefox", "Chrome", "Opera", "Safari", "Edge"];
|
|
|
|
const osList = ["Windows", "Mac OS", "Linux", "Android", "iOS"];
|
|
|
|
const filterInBrowser = agent => item =>
|
|
|
|
agent.family.toLowerCase().includes(item.toLocaleLowerCase());
|
|
|
|
const filterInOs = agent => item =>
|
|
|
|
agent.os.family.toLowerCase().includes(item.toLocaleLowerCase());
|
|
|
|
|
|
|
|
visitQueue.process(({ data }) => {
|
2019-11-02 20:16:00 +03:30
|
|
|
const tasks = [];
|
|
|
|
|
|
|
|
tasks.push(addLinkCount(data.link.id));
|
|
|
|
|
|
|
|
if (data.link.visit_count < getStatsLimit()) {
|
|
|
|
const agent = useragent.parse(data.headers["user-agent"]);
|
|
|
|
const [browser = "Other"] = browsersList.filter(filterInBrowser(agent));
|
|
|
|
const [os = "Other"] = osList.filter(filterInOs(agent));
|
|
|
|
const referrer = data.referrer && URL.parse(data.referrer).hostname;
|
|
|
|
const location = geoip.lookup(data.realIP);
|
|
|
|
const country = location && location.country;
|
|
|
|
tasks.push(
|
2019-11-02 20:14:00 +03:30
|
|
|
createVisit({
|
|
|
|
browser: browser.toLowerCase(),
|
|
|
|
country: country || "Unknown",
|
|
|
|
domain: data.customDomain,
|
|
|
|
id: data.link.id,
|
|
|
|
os: os.toLowerCase().replace(/\s/gi, ""),
|
|
|
|
referrer: (referrer && referrer.replace(/\./gi, "[dot]")) || "Direct"
|
|
|
|
})
|
2019-11-02 20:16:00 +03:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(tasks);
|
2019-11-02 19:26:26 +03:30
|
|
|
});
|