assistance-engine/ingestion/docs/22_System_utilities_transfo...

157 lines
3.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

SECTION VI: System Utilities and Transformation
This section documents the native commands for advanced string manipulation, precise time handling, and dynamic data generation.
6.1 Time and Date Management (getDateTime / stampToDatetime)
AVAP handles time in two formats:
Epoch/Timestamp (numeric): Ideal for calculations.
Formatted Datetime (string): Ideal for human readability and database storage.
6.1.1 getDateTime
Generates the current time with high precision.
Interface:
getDateTime(format, timeDelta, timeZone, targetVar)
Parameters
format:
Example: "%Y-%m-%d %H:%M:%S".
If left empty, returns the current Epoch timestamp.
timeDelta:
Seconds to add (positive) or subtract (negative).
Particularly useful for calculating token expiration times.
timeZone:
Time zone region (e.g., "Europe/Madrid").
6.1.2 stampToDatetime
Converts a numeric value (Unix Timestamp) into a human-readable string.
Interface:
stampToDatetime(timestamp, format, offset, targetVar)
Common Use Case:
Formatting dates retrieved from the database (Section V) before sending them to the client (Section II).
6.2 Advanced String Manipulation (replace / randomString)
6.2.1 replace
Allows text cleaning and transformation. Essential when receiving client data that requires sanitization.
Interface:
replace(sourceText, oldText, newText, targetVar)
Example Use Case:
Removing spaces or unwanted characters from a username before executing a SQL query.
6.2.2 randomString
Generates secure random alphanumeric strings.
Interface:
randomString(length, targetVar)
Applications:
Temporary password generation
Session ID creation
Unique file name generation
6.3 Security and Hash Operations (encodeSHA256)
Although previously mentioned in the persistence section, this is fundamentally a data transformation utility.
Mechanics
Deterministic one-way function.
AVAP uses an optimized implementation ensuring that the same input always produces the same hash.
This enables secure login comparisons without storing or exposing the actual password.
6.4 The Return Command (return)
Within functions and execution flows, return not only stops execution but can also inject the result of a subroutine back into the main flow.
Complete Utility Flow Example
// 1. Generate a temporary token
randomString(16, token_raw)
// 2. Calculate expiration (within 1 hour = 3600 seconds)
getDateTime("%Y-%m-%d %H:%M:%S", 3600, "UTC", expiration_date)
// 3. Format a system message using Section I
message = "Your token %s expires on %s" % (token_raw, expiration_date)
// 4. Send to client (Section II)
addResult(message)
6.5 Common Format Tokens (Cheat Sheet)
Token Description Example
%Y Full year 2026
%m Month (0112) 02
%d Day (0131) 23
%H Hour (0023) 21
%M Minute (0059) 45
Examples
1. Unix Timestamp Retrieval
Code snippet
getDateTime("", 0, "UTC", now)
addResult(now)
2. Database-Formatted Date
Code snippet
getDateTime("%Y-%m-%d %H:%M:%S", 0, "Europe/Madrid", sql_date)
addResult(sql_date)
3. Expiration Calculation (1 Day)
Code snippet
getDateTime("", 86400, "UTC", expires_at)
addResult(expires_at)
4. Timestamp to Readable Conversion
Code snippet
stampToDatetime(1708726162, "%d/%m/%Y", 0, human_date)
addResult(human_date)
5. String Cleaning (Replace)
Code snippet
replace("REF_1234_OLD", "OLD", "NEW", updated_ref)
addResult(updated_ref)
6. Random Token Generator
Code snippet
randomString(32, security_token)
addResult(security_token)
7. SHA256 Hash for Integrity
Code snippet
encodeSHA256("payload_data", checksum)
addResult(checksum)