<commit_before>import { DataChangeType, Dictionary } from "farmbot/dist";
import { getDevice } from "./device";
import { box } from "boxed_value";
import * as _ from "lodash";
import { ResourceName } from "./resources/tagged_resources";

export let METHOD_MAP: Dictionary<DataChangeType> = {
  "post": "add",
  "put": "update",
  "patch": "update",
  "delete": "remove"
};
export let METHODS = ["post", "put", "patch", "delete"];

export let RESOURCES: ResourceName[] = [
  "Point",
  "Regimen",
  "Peripheral",
  "Log",
  "Sequence",
  "FarmEvent",
  "Point",
  "Device"
];

// PROBLEM:       The bot doesn't know if the user has changed any of the data.
// GOOD SOLUTION: Create a push notification system on the API.
// FAST SOLUTION: Ping the bot every time we push "save" or "update".
//                Our hope is to eventually move this logic into the API.
export function notifyBotOfChanges(url: string | undefined, action: DataChangeType) {
  if (url) {
    url.split("/").filter((chunk: ResourceName) => {
      return RESOURCES.includes(chunk);
    }).map(async function (resource: ResourceName) {
      getDevice().dataUpdate(action, { [resource]: inferUpdateId(url) });
    });
  }
}

/** More nasty hacks until we have time to implement proper API push state
 * notifications. */
function inferUpdateId(url: string) {
  try {
    let ids = url
      .split("/")
      .filter(x => !x.includes(",")) // Don't allow batch endpoints to participate.
      .map(x => parseInt(x, 10))
      .filter(x => !_.isNaN(x));
    let id: number | undefined = ids[0];
    let isNum = _.isNumber(id);
    let onlyOne = ids.length === 1;
    return (isNum && onlyOne) ? ("" + id) : "*";
  } catch (error) { // Don't crash - just keep moving along. This is a temp patch.
    return "*";
  }<commit_msg>Add TEMP_LEGACY_RESOURCE_NAMES for now<commit_after>import { DataChangeType, Dictionary } from "farmbot/dist";
import { getDevice } from "./device";
import { box } from "boxed_value";
import * as _ from "lodash";
import { ResourceName } from "./resources/tagged_resources";

export let METHOD_MAP: Dictionary<DataChangeType> = {
  "post": "add",
  "put": "update",
  "patch": "update",
  "delete": "remove"
};
export let METHODS = ["post", "put", "patch", "delete"];

export let RESOURCES: ResourceName[] = [
  "Point",
  "Regimen",
  "Peripheral",
  "Log",
  "Sequence",
  "FarmEvent",
  "Point",
  "Device"
];

/** Temporary stub until auto_sync rollout */
export const LEGACY_RESOURCE_NAMES = [
  "device",
  "farm_events",
  "logs",
  "peripherals",
  "plants",
  "points",
  "regimens",
  "sequences",
  "tool_slots",
  "tools"
];

// PROBLEM:       The bot doesn't know if the user has changed any of the data.
// GOOD SOLUTION: Create a push notification system on the API.
// FAST SOLUTION: Ping the bot every time we push "save" or "update".
//                Our hope is to eventually move this logic into the API.
export function notifyBotOfChanges(url: string | undefined, action: DataChangeType) {
  if (url) {
    url.split("/").filter((chunk: ResourceName) => {
      return LEGACY_RESOURCE_NAMES.includes(chunk);
    }).map(async function (resource: ResourceName) {
      getDevice().dataUpdate(action, { [resource]: inferUpdateId(url) });
    });
  }
}

/** More nasty hacks until we have time to implement proper API push state
 * notifications. */
function inferUpdateId(url: string) {
  try {
    let ids = url
      .split("/")
      .filter(x => !x.includes(",")) // Don't allow batch endpoints to participate.
      .map(x => parseInt(x, 10))
      .filter(x => !_.isNaN(x));
    let id: number | undefined = ids[0];
    let isNum = _.isNumber(id);
    let onlyOne = ids.length === 1;
    return (isNum && onlyOne) ? ("" + id) : "*";
  } catch (error) { // Don't crash - just keep moving along. This is a temp patch.
    return "*";
  }
{'fraction_non_alphanumeric': 0.15711693025667617, 'fraction_numerical': 0.002074150894477573, 'mean_word_length': 3.3544018058690743, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '7564927', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1221, 'n_words': 389}