-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from CodeForAfrica/outline-vpn-user-stats
@Outlinevpn Process Daily usage statistics
- Loading branch information
Showing
13 changed files
with
253 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import betterSqlite3 from "better-sqlite3"; | ||
import path from "path"; | ||
|
||
const dbPath = path.resolve(process.cwd(), "data", "database.sqlite"); | ||
const db = betterSqlite3(dbPath); | ||
|
||
export interface Record { | ||
ID?: number; | ||
userId: string; | ||
usage: number; | ||
date: string; | ||
cumulativeData: number; | ||
email: string; | ||
createdAt: string; | ||
} | ||
|
||
export interface Filters { | ||
email?: string; | ||
date?: string | { start: string; end: string }; | ||
userId?: string; | ||
ID?: number; | ||
groupBy?: "email" | "date"; | ||
orderBy?: string; | ||
} | ||
|
||
class Model { | ||
static initialize() { | ||
const createTable = ` | ||
CREATE TABLE IF NOT EXISTS records ( | ||
ID INTEGER PRIMARY KEY AUTOINCREMENT, | ||
userId TEXT NOT NULL, | ||
usage INTEGER NOT NULL, | ||
date TEXT NOT NULL, | ||
cumulativeData INTEGER NOT NULL, | ||
email TEXT NOT NULL, | ||
createdAt TEXT NOT NULL, | ||
UNIQUE (date, userId) | ||
) | ||
`; | ||
db.exec(createTable); | ||
} | ||
|
||
static createOrUpdate(record: Record) { | ||
const insertData = db.prepare(` | ||
INSERT INTO records (userId, usage, date, cumulativeData, email, createdAt) | ||
VALUES (?, ?, ?, ?, ?, ?) | ||
ON CONFLICT(date, userId) | ||
DO UPDATE SET | ||
usage = excluded.usage, | ||
cumulativeData = excluded.cumulativeData, | ||
email = excluded.email, | ||
createdAt = excluded.createdAt; | ||
`); | ||
const info = insertData.run( | ||
record.userId, | ||
record.usage, | ||
record.date, | ||
record.cumulativeData, | ||
record.email, | ||
record.createdAt, | ||
); | ||
return { ...record, ID: info.lastInsertRowid }; | ||
} | ||
|
||
static update(ID: number, updates: Partial<Record>) { | ||
const setClause = Object.keys(updates) | ||
.map((key) => `${key} = ?`) | ||
.join(", "); | ||
const query = `UPDATE records SET ${setClause} WHERE ID = ?`; | ||
const stmt = db.prepare(query); | ||
return stmt.run([...Object.values(updates), ID]); | ||
} | ||
|
||
static delete(ID: number) { | ||
const stmt = db.prepare("DELETE FROM records WHERE ID = ?"); | ||
return stmt.run(ID); | ||
} | ||
|
||
static getAll(filters: Filters = {}) { | ||
let query = "SELECT"; | ||
const params: any[] = []; | ||
if (filters.groupBy === "email" || filters.groupBy === "date") { | ||
query += | ||
filters.groupBy === "email" | ||
? " email, userId, SUM(usage) as totalUsage FROM records" | ||
: " date, SUM(usage) as totalUsage FROM records"; | ||
} else { | ||
query += " * FROM records"; | ||
} | ||
query += " WHERE 1=1"; | ||
if (filters.email) { | ||
query += " AND email = ?"; | ||
params.push(filters.email); | ||
} | ||
if (filters.date) { | ||
if (typeof filters.date === "string") { | ||
query += " AND date = ?"; | ||
params.push(filters.date); | ||
} else { | ||
query += " AND date BETWEEN ? AND ?"; | ||
params.push(filters.date.start, filters.date.end); | ||
} | ||
} | ||
if (filters.userId) { | ||
query += " AND userId = ?"; | ||
params.push(filters.userId); | ||
} | ||
if (filters.ID) { | ||
query += " AND ID = ?"; | ||
params.push(filters.ID); | ||
} | ||
|
||
if (filters.groupBy) { | ||
if (filters.groupBy === "email") { | ||
query += " GROUP BY email"; | ||
} else if (filters.groupBy === "date") { | ||
query += " GROUP BY date"; | ||
} | ||
} | ||
if (filters.orderBy) { | ||
query += ` ORDER BY ${filters.orderBy}`; | ||
} | ||
const stmt = db.prepare(query); | ||
return stmt.all(params); | ||
} | ||
} | ||
|
||
// Initialize the database | ||
Model.initialize(); | ||
|
||
export { Model }; |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { NextApiRequest } from "next/types"; | ||
import { OutlineVPN } from "./outline"; | ||
import { Filters, Model, Record } from "@/vpnmanager/lib/data/database"; | ||
|
||
const vpnManager = new OutlineVPN({ | ||
apiUrl: process.env.NEXT_APP_VPN_API_URL as string, | ||
}); | ||
|
||
export async function processUserStats() { | ||
const date = `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}`; | ||
const { bytesTransferredByUserId = {} } = await vpnManager.getDataUsage(); | ||
const allUsers = await vpnManager.getUsers(); | ||
const unprocessedUsers: Omit<Record, "ID" | "createdAt">[] = Object.keys( | ||
bytesTransferredByUserId, | ||
).map((key: string) => { | ||
const userDetails = allUsers.find(({ id }) => id === key); | ||
const newData = { | ||
userId: key, | ||
usage: Math.ceil(bytesTransferredByUserId[key] / 30), | ||
date, | ||
cumulativeData: bytesTransferredByUserId[key], | ||
email: userDetails?.name || "", | ||
}; | ||
Model.createOrUpdate({ ...newData, createdAt: new Date().toISOString() }); | ||
return newData; | ||
}); | ||
return unprocessedUsers; | ||
} | ||
|
||
export async function getStats(req: NextApiRequest) { | ||
const filters: Partial<Filters> & { | ||
"date.start"?: string; | ||
"date.end"?: string; | ||
} = req.query; | ||
const validFilters = { | ||
email: filters.email, | ||
ID: filters.ID, | ||
userId: filters.userId, | ||
groupBy: filters.groupBy as "email" | "date", | ||
orderBy: filters.orderBy, | ||
date: | ||
filters["date.start"] && filters["date.end"] | ||
? { | ||
start: filters["date.start"], | ||
end: filters["date.end"], | ||
} | ||
: filters.date, | ||
}; | ||
|
||
return Model.getAll(validFilters); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { NextRequest } from "next/server"; | ||
|
||
// Limit the middleware to paths starting with `/api/` | ||
export const config = { | ||
matcher: "/api/:function*", | ||
}; | ||
|
||
export function middleware(req: NextRequest) { | ||
const key: string = req.headers.get("x-api-key") as string; | ||
const API_SECRET_KEY = process.env.API_SECRET_KEY; | ||
if (!(key && key === API_SECRET_KEY)) { | ||
return Response.json( | ||
{ success: false, message: "INVALID_API_KEY" }, | ||
{ status: 403 }, | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextApiResponse, NextApiRequest } from "next"; | ||
import { processUserStats, getStats } from "@/vpnmanager/lib/statistics"; | ||
import { RestMethodFunctions, RestMethods } from "@/vpnmanager/types"; | ||
|
||
const methodToFunction: RestMethodFunctions = { | ||
POST: processUserStats, | ||
GET: getStats, | ||
}; | ||
|
||
export async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
const statFunc = methodToFunction[req.method as RestMethods]; | ||
if (!statFunc) { | ||
return res.status(404).json({ message: "Requested path not found" }); | ||
} | ||
const data = await statFunc(req); | ||
return res.status(200).json(data); | ||
} catch (error) { | ||
return res.status(500).json(error); | ||
} | ||
} | ||
export default handler; |
This file contains 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
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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