MNTOR-1859 - expose stats API, starting with scans (#3280)

* MNTOR-1859 - expose stats API, starting with scans

- monthly quota
- current scan count
This commit is contained in:
Robert Helmer 2023-08-01 21:03:14 -07:00 коммит произвёл GitHub
Родитель 35745c090c
Коммит d0b29c2046
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 42 добавлений и 0 удалений

Просмотреть файл

@ -138,3 +138,6 @@ PREMIUM_ENABLED=
# Link to start user on the subscription process. PREMIUM_ENABLED must be set to `true`.
PREMIUM_PRODUCT_ID=prod_NErZh679W62lai
PREMIUM_PLAN_ID_US=price_1MUNq0Kb9q6OnNsL4BoJgepf
MONTHLY_SCANS_QUOTA=
STATS_TOKEN=

Просмотреть файл

@ -0,0 +1,32 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { NextRequest, NextResponse } from "next/server";
import { getScansCount } from "../../../../db/tables/onerep_scans";
import { bearerToken } from "../../utils/auth";
export async function GET(req: NextRequest) {
const headerToken = bearerToken(req);
if (headerToken !== process.env.STATS_TOKEN) {
return NextResponse.json({ success: "false" }, { status: 401 });
}
const monthlyQuota = process.env.MONTHLY_SCANS_QUOTA;
const now = new Date();
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const result = await getScansCount(
firstDayOfMonth.toDateString(),
now.toDateString()
);
const scansCount = result[0]["count"];
const message = {
monthlyQuota,
scansCount,
};
return NextResponse.json({ success: true, message }, { status: 200 });
}

Просмотреть файл

@ -83,10 +83,17 @@ async function setOnerepScanResults(
});
}
async function getScansCount(startDate: string, endDate: string) {
return await knex("onerep_scans")
.count("id")
.whereBetween("created_at", [startDate, endDate]);
}
export {
getLatestOnerepScan,
getOnerepScanResults,
setOnerepProfileId,
setOnerepScan,
setOnerepScanResults,
getScansCount,
};