Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: update measurement types #86

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/bot-utils/src/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-await-in-loop */
import got, { Headers, Response } from 'got';

import type { MeasurementResponse } from './types.js';
import type { Measurement } from './types.js';
import { userAgent } from './user-agent.js';

class MeasurementsFetcher {
Expand All @@ -12,15 +12,15 @@ class MeasurementsFetcher {
etags: Record<string, string>;

// caches Measurements by ETag
measurements: Record<string, MeasurementResponse>;
measurements: Record<string, Measurement>;

constructor (apiUrl: string) {
this.apiUrl = apiUrl;
this.etags = {};
this.measurements = {};
}

async fetchMeasurement (id: string): Promise<MeasurementResponse> {
async fetchMeasurement (id: string): Promise<Measurement> {
const headers: Headers = {
'User-Agent': userAgent(),
'Accept-Encoding': 'br',
Expand Down Expand Up @@ -59,7 +59,7 @@ class MeasurementsFetcher {
}
}

const measurementResponse: MeasurementResponse = JSON.parse(res.body);
const measurementResponse: Measurement = JSON.parse(res.body);

// save etag and response to cache
etag = res.headers.etag;
Expand All @@ -78,7 +78,7 @@ export const ApiUrl = 'https://api.globalping.io/v1/measurements';
// api poll interval in milliseconds
const apiPollInterval = 1000;

export const getMeasurement = async (id: string): Promise<MeasurementResponse> => {
export const getMeasurement = async (id: string): Promise<Measurement> => {
const measurementsFetcher = new MeasurementsFetcher(ApiUrl);

let data = await measurementsFetcher.fetchMeasurement(id);
Expand All @@ -93,11 +93,15 @@ export const getMeasurement = async (id: string): Promise<MeasurementResponse> =
};

export const getTag = (tags: string[]): string | undefined => {
if (tags.length === 0) { return undefined; }
if (tags.length === 0) {
return undefined;
}

// Iterarate through tags and return the first one that has its last character be a number
for (const tag of tags) {
if (Number.isInteger(Number(tag.slice(-1)))) { return `${tag}`; }
if (Number.isInteger(Number(tag.slice(-1)))) {
return `${tag}`;
}
}

return undefined;
Expand Down
10 changes: 5 additions & 5 deletions packages/bot-utils/src/measurements-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import {
isHttpProtocol,
isMtrProtocol,
isTraceProtocol,
Locations,
PostMeasurement,
Location,
MeasurementCreate,
} from './types.js';
import { throwArgError } from './utils.js';

function buildLocations (from: string): Locations[] {
function buildLocations (from: string): Location[] {
return from
.split(',')
.map(f => f.trim())
.map((l): Locations => ({ magic: l }));
.map((l): Location => ({ magic: l }));
}

export const buildPostMeasurements = (args: Flags): PostMeasurement => {
export const buildPostMeasurements = (args: Flags): MeasurementCreate => {
const {
cmd,
target,
Expand Down
14 changes: 9 additions & 5 deletions packages/bot-utils/src/post.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import got, { HTTPError } from 'got';

import { PostError } from './errors.js';
import type { PostMeasurement, PostMeasurementResponse } from './types.js';
import type {
Location,
MeasurementCreate,
MeasurementCreateResponse,
} from './types.js';
import { userAgent } from './user-agent.js';

export const postMeasurement = async (
opts: PostMeasurement,
opts: MeasurementCreate,
token?: string,
): Promise<PostMeasurementResponse> => {
): Promise<MeasurementCreateResponse> => {
try {
const headers: { [key: string]: string } = {
'Content-Type': 'application/json',
Expand All @@ -27,15 +31,15 @@ export const postMeasurement = async (

if (res.statusCode !== 202) {
const body = JSON.parse(res.body);
body.location = opts.locations[0].magic;
body.location = (opts.locations as Location[])[0].magic;
throw new Error(body);
}

return JSON.parse(res.body);
} catch (error) {
if (error instanceof HTTPError) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const location = opts.locations[0].magic!;
const location = (opts.locations as Location[])[0].magic!;
const newError = new PostError(error.response, location, error.message);

throw newError;
Expand Down
Loading
Loading