<commit_before>import { VendorDrop, VendorDropType, VendorDropXyz, toVendorDrop } from './vendorDrops';
import { t } from 'app/i18next-t';

export class VendorEngramsXyzService {
  refreshInterval: number = 1000 * 60 * 15;
  cachedResponse: VendorDrop[];
  lastUpdated = 0;
  refreshPromise?: Promise<VendorDropXyz[]>;

  handleVendorEngramsErrors(response: Response): Promise<VendorDropXyz[]> {
    if (response.status !== 200) {
      throw new Error(t('VendorEngramsXyz.ServiceCallFailed'));
    }

    return response.json() || [];
  }

  async dropsNeedRefresh(): Promise<boolean> {
    const vendorDrops = this.cachedResponse;

    if (!vendorDrops) {
      return true;
    }

    return Boolean(vendorDrops.find((vd) => vd.nextRefresh <= new Date()));
  }

  cacheExpired(): boolean {
    if (!this.lastUpdated) {
      return true;
    }

    return Date.now() - this.lastUpdated >= this.refreshInterval;
  }

  vendorEngramsFetch(url: string) {
    const request = new Request(url, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      }
    });

    return Promise.resolve(fetch(request));
  }

  async getAllVendorDrops(): Promise<VendorDrop[]> {
    const dropsNeedRefresh = await this.dropsNeedRefresh();
    if ((this.cachedResponse && !this.cacheExpired()) || !dropsNeedRefresh) {
      return this.cachedResponse;
    }

    this.refreshPromise =
      this.refreshPromise ||
      this.vendorEngramsFetch('https://api.vendorengrams.xyz/getVendorDrops?source=DIM').then(
        this.handleVendorEngramsErrors,
        this.handleVendorEngramsErrors
      );

    const xyzResponse = await this.refreshPromise;

    if (xyzResponse) {
      this.cachedResponse = xyzResponse.map(toVendorDrop);
    }

    this.lastUpdated = Date.now();
    this.refreshPromise = undefined;

    return this.cachedResponse;
  }
}

export function getVendorDropsForVendor(
  vendorHash: number,
  vendorDrops?: VendorDrop[]
): VendorDrop[] {
  return (
    (vendorDrops && vendorDrops.filter((vd) => vd.display && vd.vendorId === vendorHash)) || []
  );
}

export function powerLevelMatters(powerLevel?: number): boolean {
  return (powerLevel && powerLevel >= 380) || false;
}

export function isDroppingHigh(vendorDrop: VendorDrop): boolean {
  return vendorDrop.drop === VendorDropType.DroppingHigh && vendorDrop.display;
}

export const dimVendorEngramsService = new VendorEngramsXyzService();<commit_msg>Power level only mattered in v1 of ve.xyz.<commit_after>import { VendorDrop, VendorDropType, VendorDropXyz, toVendorDrop } from './vendorDrops';
import { t } from 'app/i18next-t';

export class VendorEngramsXyzService {
  refreshInterval: number = 1000 * 60 * 15;
  cachedResponse: VendorDrop[];
  lastUpdated = 0;
  refreshPromise?: Promise<VendorDropXyz[]>;

  handleVendorEngramsErrors(response: Response): Promise<VendorDropXyz[]> {
    if (response.status !== 200) {
      throw new Error(t('VendorEngramsXyz.ServiceCallFailed'));
    }

    return response.json() || [];
  }

  async dropsNeedRefresh(): Promise<boolean> {
    const vendorDrops = this.cachedResponse;

    if (!vendorDrops) {
      return true;
    }

    return Boolean(vendorDrops.find((vd) => vd.nextRefresh <= new Date()));
  }

  cacheExpired(): boolean {
    if (!this.lastUpdated) {
      return true;
    }

    return Date.now() - this.lastUpdated >= this.refreshInterval;
  }

  vendorEngramsFetch(url: string) {
    const request = new Request(url, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      }
    });

    return Promise.resolve(fetch(request));
  }

  async getAllVendorDrops(): Promise<VendorDrop[]> {
    const dropsNeedRefresh = await this.dropsNeedRefresh();
    if ((this.cachedResponse && !this.cacheExpired()) || !dropsNeedRefresh) {
      return this.cachedResponse;
    }

    this.refreshPromise =
      this.refreshPromise ||
      this.vendorEngramsFetch('https://api.vendorengrams.xyz/getVendorDrops?source=DIM').then(
        this.handleVendorEngramsErrors,
        this.handleVendorEngramsErrors
      );

    const xyzResponse = await this.refreshPromise;

    if (xyzResponse) {
      this.cachedResponse = xyzResponse.map(toVendorDrop);
    }

    this.lastUpdated = Date.now();
    this.refreshPromise = undefined;

    return this.cachedResponse;
  }
}

export function getVendorDropsForVendor(
  vendorHash: number,
  vendorDrops?: VendorDrop[]
): VendorDrop[] {
  return (
    (vendorDrops && vendorDrops.filter((vd) => vd.display && vd.vendorId === vendorHash)) || []
  );
}
export function isDroppingHigh(vendorDrop: VendorDrop): boolean {
  return vendorDrop.drop === VendorDropType.DroppingHigh && vendorDrop.display;
}

export const dimVendorEngramsService = new VendorEngramsXyzService();
{'fraction_non_alphanumeric': 0.12646566164154105, 'fraction_numerical': 0.006700167504187605, 'mean_word_length': 3.743793445878848, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7585728', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1460, 'n_words': 284}