Code examplesCommon operationsFunctions on Objects

Functions on Objects

Typescript

Assign alerts to analysts in a round-robin fashion

How do I assign a list of objects to another group of objects in a round-robin fashion using Typescript?

This code defines an Ontology edit function that assigns alerts to analysts in a round-robin fashion. It iterates through the alerts and assigns them to analysts by their employee ID, looping back to the first analyst when all analysts have been assigned an alert.

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @OntologyEditFunction() @Edits(Alert) public async alertAssignment(alerts: Alert[]>): Promise<void>{ const analysts = Objects.search.analyst.all(); const numAnalysts = analysts.length; if(numAnalysts === 0){ return } let analystIndex = 0; for(const alert of alerts) { const employeeId = analysts.at(analystIndex)!.employeeId; alert.assignedTo = employeeId; analystIndex += 1; if (analystIndex === numAnalysts){ analystIndex = 0; } } }
  • Date submitted: 2024-04-29
  • Tags: functions on objects, typescript, alert, analyst, round-robin

Get days between timestamps

How do I calculate the number of days between two timestamps in Typescript?

This code defines a function that takes two Timestamp objects as input and returns the number of days between them as an Integer.

Copied!
1 2 3 4 5 6 7 import { Function, Integer, Timestamp} from "@foundry/functions-api"; @Function() public getDaysBetweenTimestamp(date1: Timestamp, date2: Timestamp): Integer{ return Math.floor((Math.abs((date1.getTime()-date2.getTime())) / (1000 * 3600 * 24))); }
  • Date submitted: 2024-03-26
  • Tags: typescript, functions on objects