use db wifi_location as metric source
This commit is contained in:
parent
e8154d1e13
commit
8a4eadefcb
@ -1,12 +1,10 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
import { container } from "tsyringe";
|
import { container } from "tsyringe";
|
||||||
import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client";
|
import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client";
|
||||||
import { MetricsService } from "../services/metricsService";
|
|
||||||
import { LocationService } from "../services/locationService";
|
import { LocationService } from "../services/locationService";
|
||||||
import { WifiLocationService } from "../services/wifiLocationService";
|
import { WifiLocationService } from "../services/wifiLocationService";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const metricsService = container.resolve(MetricsService);
|
|
||||||
const locationService = container.resolve(LocationService);
|
const locationService = container.resolve(LocationService);
|
||||||
const wifiLocationService = container.resolve(WifiLocationService);
|
const wifiLocationService = container.resolve(WifiLocationService);
|
||||||
|
|
||||||
@ -24,21 +22,28 @@ const requestCounter = new Counter({
|
|||||||
const locationsTotal = new Gauge({
|
const locationsTotal = new Gauge({
|
||||||
name: `${prefix}locations_total`,
|
name: `${prefix}locations_total`,
|
||||||
help: "Total number of location entries in database",
|
help: "Total number of location entries in database",
|
||||||
labelNames: ["database"],
|
labelNames: ["database"],
|
||||||
});
|
});
|
||||||
|
|
||||||
const wifiLocationTotal = new Gauge({
|
const wifiLocationTotal = new Gauge({
|
||||||
name: `${prefix}wifi_locations_total`,
|
name: `${prefix}wifi_locations_total`,
|
||||||
help: "Total number of wifi location entries in database",
|
help: "Total number of wifi location entries in database",
|
||||||
labelNames: ["database"],
|
labelNames: ["database"],
|
||||||
});
|
});
|
||||||
|
|
||||||
const wigleApiRequest = new Gauge({
|
const wifiLocationNotResolvable = new Gauge({
|
||||||
name: `${prefix}wigle_api_requests_total`,
|
name: `${prefix}wifi_locations_not_resolvable`,
|
||||||
help: "Total number of wifi location entries in database",
|
help: "Unresolved number of wifi location entries in database",
|
||||||
labelNames: ["wigle"],
|
labelNames: ["database"],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const wifiLocationRequestLimitExceeded = new Gauge({
|
||||||
|
name: `${prefix}wifi_locations_request_limit_exceeded`,
|
||||||
|
help: "Unresolved number of wifi location because request limit exceeded entries in database",
|
||||||
|
labelNames: ["database"],
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Define the metrics endpoint
|
// Define the metrics endpoint
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
@ -46,7 +51,8 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
locationsTotal.set((await locationService.getAllLocations()).length);
|
locationsTotal.set((await locationService.getAllLocations()).length);
|
||||||
wifiLocationTotal.set((await wifiLocationService.getAllWifiLocations()).length);
|
wifiLocationTotal.set((await wifiLocationService.getAllWifiLocations()).length);
|
||||||
wigleApiRequest.set(metricsService.getWigleApiNumberOfRequests());
|
wifiLocationNotResolvable.set((await wifiLocationService.getAllWifiLocationsByNotResolvable).length);
|
||||||
|
wifiLocationRequestLimitExceeded.set((await wifiLocationService.getAllWifiLocationsByRequestLimitExceeded).length);
|
||||||
|
|
||||||
// Increment the counter with labels
|
// Increment the counter with labels
|
||||||
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
|
requestCounter.inc({ method: req.method, route: req.route.path, status: 200 });
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
import { container } from "tsyringe";
|
|
||||||
import { MetricsService } from "../services/metricsService";
|
|
||||||
|
|
||||||
const metricsService = container.resolve(MetricsService);
|
|
||||||
|
|
||||||
interface WigleApiResonse {
|
interface WigleApiResonse {
|
||||||
response?: WifiLocationResponse,
|
response?: WifiLocationResponse,
|
||||||
status_code: number,
|
status_code: number,
|
||||||
@ -52,7 +47,6 @@ interface Result {
|
|||||||
export const getLocationForWifi = async (
|
export const getLocationForWifi = async (
|
||||||
mac: string
|
mac: string
|
||||||
): Promise<WigleApiResonse | undefined> => {
|
): Promise<WigleApiResonse | undefined> => {
|
||||||
metricsService.incWigleApiNumberOfRequests();
|
|
||||||
try {
|
try {
|
||||||
const url = `${process.env.WIGLE_BASE_URL!}${process.env
|
const url = `${process.env.WIGLE_BASE_URL!}${process.env
|
||||||
.WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`;
|
.WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`;
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
import { inject, injectable } from "tsyringe";
|
|
||||||
|
|
||||||
@injectable()
|
|
||||||
export class MetricsService {
|
|
||||||
private wigleApiNumberOfRequests: number;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.wigleApiNumberOfRequests = 0; // Initialize the variable
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method to increment the request count
|
|
||||||
public incWigleApiNumberOfRequests() {
|
|
||||||
console.log("inc")
|
|
||||||
this.wigleApiNumberOfRequests += 1; // Increment the number of requests
|
|
||||||
console.log(this.wigleApiNumberOfRequests)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method to get the current request count
|
|
||||||
public getWigleApiNumberOfRequests() {
|
|
||||||
console.log("get: " + this.wigleApiNumberOfRequests)
|
|
||||||
return this.wigleApiNumberOfRequests;
|
|
||||||
}
|
|
||||||
}
|
|
@ -29,6 +29,18 @@ export class WifiLocationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getAllWifiLocationsByNotResolvable() {
|
||||||
|
return this.repository.findAll({
|
||||||
|
where: { location_not_resolvable: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getAllWifiLocationsByRequestLimitExceeded() {
|
||||||
|
return this.repository.findAll({
|
||||||
|
where: { request_limit_exceeded: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public async getWifiLocationByMac(mac: string) {
|
public async getWifiLocationByMac(mac: string) {
|
||||||
let wifiLocation = await this.repository.findById(mac);
|
let wifiLocation = await this.repository.findById(mac);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user