@pinelab/vendure-plugin-metrics
Version:
Vendure plugin measuring and visualizing e-commerce metrics
70 lines (69 loc) • 2.5 kB
TypeScript
import { OnApplicationBootstrap, OnModuleInit } from '@nestjs/common';
import { ID, JobQueueService, RequestContext } from '@vendure/core';
import { DataSource } from 'typeorm';
import { MetricRequest } from '../entities/metric-request.entity';
import { MetricsPluginOptions } from '../metrics.plugin';
import { PageVisitInput } from '../ui/generated/graphql';
export interface Session {
identifier: string;
deviceType: string;
start: Date;
end: Date;
}
type RequestData = {
ipAddress: string;
userAgent: string;
channelId: string | number;
path?: string;
productId?: ID;
productVariantId?: ID;
};
export declare class RequestService implements OnModuleInit, OnApplicationBootstrap {
private jobQueueService;
private dataSource;
private options;
/**
* This queue is used to temporarily batch requests before they are persisted
*/
private requestBatch;
private requestQueue;
private dailySalt?;
constructor(jobQueueService: JobQueueService, dataSource: DataSource, options: MetricsPluginOptions);
onModuleInit(): Promise<void>;
onApplicationBootstrap(): void;
/**
* Adds a request to the batch, and pushes the batch to the queue once it reaches a certain size
*/
logRequest(ctx: RequestContext, input?: PageVisitInput): void;
/**
* Processes the current request log queue, creates a job and clears the queue
*/
private createLogRequestJobs;
/**
* Handles the job data and stores it in the database
*/
handleLogRequestJobs(requests: RequestData[]): Promise<void>;
/**
* Get the number of visits since a certain date.
* Multiple requests from the same user within the same session are counted as one visit.
*/
getSessions(ctx: RequestContext, since: Date, sessionLengthInMinutes: number): Promise<Session[]>;
getRequests(ctx: RequestContext, since: Date): Promise<MetricRequest[]>;
/**
* Removes old requests from the database.
* Removes requests older than 2 x displayPastMonths
*/
removeOldRequests(): void;
/**
* Get or create a salt for the current day
*
* Checks if current salt exists in memory or in DB, and if it is still valid (24 hours).
* If not, generates a new salt and persists it in DB.
*/
private getSalt;
/**
* Extracts basic device information from user agent string
*/
extractDeviceInfo(userAgent: string): 'mobile' | 'other';
}
export {};