use db wifi_location as metric source
This commit is contained in:
		| @ -1,12 +1,10 @@ | ||||
| import express, { Request, Response } from "express"; | ||||
| import { container } from "tsyringe"; | ||||
| import { Counter, Gauge, collectDefaultMetrics, register } from "prom-client"; | ||||
| import { MetricsService } from "../services/metricsService"; | ||||
| import { LocationService } from "../services/locationService"; | ||||
| import { WifiLocationService } from "../services/wifiLocationService"; | ||||
|  | ||||
| const router = express.Router(); | ||||
| const metricsService = container.resolve(MetricsService); | ||||
| const locationService = container.resolve(LocationService); | ||||
| const wifiLocationService = container.resolve(WifiLocationService); | ||||
|  | ||||
| @ -24,21 +22,28 @@ const requestCounter = new Counter({ | ||||
| const locationsTotal = new Gauge({ | ||||
|   name: `${prefix}locations_total`, | ||||
|   help: "Total number of location entries in database", | ||||
|   labelNames: ["database"],  | ||||
|   labelNames: ["database"], | ||||
| }); | ||||
|  | ||||
| const wifiLocationTotal = new Gauge({ | ||||
|   name: `${prefix}wifi_locations_total`, | ||||
|   help: "Total number of wifi location entries in database", | ||||
|   labelNames: ["database"],  | ||||
|   labelNames: ["database"], | ||||
| }); | ||||
|  | ||||
| const wigleApiRequest = new Gauge({ | ||||
|   name: `${prefix}wigle_api_requests_total`, | ||||
|   help: "Total number of wifi location entries in database", | ||||
|   labelNames: ["wigle"],  | ||||
| const wifiLocationNotResolvable = new Gauge({ | ||||
|   name: `${prefix}wifi_locations_not_resolvable`, | ||||
|   help: "Unresolved number of wifi location entries in database", | ||||
|   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 | ||||
| router.get("/", async (req: Request, res: Response) => { | ||||
|   try { | ||||
| @ -46,7 +51,8 @@ router.get("/", async (req: Request, res: Response) => { | ||||
|  | ||||
|     locationsTotal.set((await locationService.getAllLocations()).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 | ||||
|     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 { | ||||
|   response?: WifiLocationResponse, | ||||
|   status_code: number, | ||||
| @ -52,7 +47,6 @@ interface Result { | ||||
| export const getLocationForWifi = async ( | ||||
|   mac: string | ||||
| ): Promise<WigleApiResonse | undefined> => { | ||||
|   metricsService.incWigleApiNumberOfRequests(); | ||||
|   try { | ||||
|     const url = `${process.env.WIGLE_BASE_URL!}${process.env | ||||
|       .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) { | ||||
|     let wifiLocation = await this.repository.findById(mac); | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user