1.5.0 • Published 2 years ago

@mangar2/messagetree v1.5.0

Weekly downloads
7
License
LGPL-3.0-or-later
Repository
github
Last release
2 years ago

Abstract

This function implements a cleanup of the message tree Elements with no actual message are deleted

This file includes helper functions to compress history entries of messages

History entry for data with same value and reason coming in regular intervals . These values are counted and not stored separately

This class manages a message store in tree format . yaha home automation uses it to store all communication between its components .

Contents

Meta

Filecleanup.js
AbstractThis function implements a cleanup of the message tree Elements with no actual message are deleted
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .
Filecompress.js
AbstractThis file includes helper functions to compress history entries of messages
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .
Fileintervalentry.js
AbstractHistory entry for data with same value and reason coming in regular intervals . These values are counted and not stored separately
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .
Filemessagetree.js
AbstractThis class manages a message store in tree format . yaha home automation uses it to store all communication between its components .
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose .

Type definitions

Entry {Object}

Entry to add to the history | Name | Type | Description | | ---- | ---- | ------- | | time | timestamp | timestamp of the entry | | | value | Any | message value | | | reason | Array.<Object> | message trace information | |

SingleEntry {Object}

History entry holding a single value | Name | Type | Description | | ---- | ---- | ------- | | type | 'single' | type of the entry | | | time | timestamp | timestamp of the entry | | | value | Any | value of the entry | | | reason | Array.<Object> | reason of the entry | |

TimeValueEntry {Object}

History entry holding several different values with the same reason | Name | Type | Description | | ---- | ---- | ------- | | type | 'timeValue' | type of the entry | | | values | Array.<Object.<time:number, value:Any>> | time/value pairs | | | reason | Array.<Object> | reason of the oldest entry | |

TimeEntry {Object}

History entry holding several identical values with the same reason | Name | Type | Description | | ---- | ---- | ------- | | type | 'time' | type of the entry | | | timestamps | Array.<number> | timestampls of the entries | | | reason | Array.<Object> | reason of the oldest entry | |

HistoryEntry {SingleEntry, TimeValueEntry, TimeEntry, IntervalEntry}

Options {Object}

Options to manage the message tree and its history | Name | Type | Description | | ---- | ---- | ------- | | upperBoundFactor | number | factor to multiply the joined interval for the upper bound | | | upperBoundAddInMilliseconds | integer | constant to add to the joined interval for the upper bound | | | lowerBoundFactor | number | factor to multiply the joined interval for the lower bound | | | lowerBoundSubInMilliseconds | integer | constant to sub from the joined interval for the lower bound | | | maxHistoryLength | integer | maximal amount of entries in the history | | | historyHysterese | integer | amount of entries to delete , if history exeeds max length | | | maxValuesPerHistoryEntry | integer | maximum amount of values per history entry | | | lengthForFurtherCompression | integer | history timeValue length to look for additional compression | |

TimeEntry {Object}

History entry holding several identical values with the same reason | Name | Type | Description | | ---- | ---- | ------- | | type | 'time' | type of the entry | | | timestamps | Array.<number> | timestampls of the entries | | | reason | Array.<Object> | reason of the oldest entry | |

Reason {Array.}

NameTypeDescription
messagestringdescription of the event reason
timestringtimestamp the event happened in ISO-Format

Decompressed {Object}

NameTypeDescription
valuenumbervalue of the entry
timestringtime the value was received ( ISO-Format )
reasonReasonoptionalarray of reason information

IntervalEntry {Object}

History entry holding several identical values with the same reason and regular updates | Name | Type | Description | | ---- | ---- | ------- | | type | 'interval' | type of the entry | | | amount | number | amount of entries | | | value | number | value of the entry | | | reason | Array.<Object> | reason of the oldest entry | | | firstTime | timestamp | timestamp of the oldes entry | | | lastTime | timestamp | timestamp of the newest entry | |

MessageNode {Object}

NameTypeDescription
timenumbertimestamp of last entry in the node
valuenumbernode value
reasonReasonnode reason
historyHistorynode history
childsMapmap of child names to child .

Global functions

addToHistory

addToHistory (history, entryToAdd, options) => {Array.<HistoryEntry>}

Adds an entry to the history and possible add it or join it with the latest history entry

addToHistory Parameters

NameTypeDescription
historyArray.<HistoryEntry>history list
entryToAddEntryentry to add to history
optionsOptionsoptions to calculate the bounds

addToHistory returns

TypeDescription
Array.<HistoryEntry>updated history

cleanNodeRec

cleanNodeRec (tree, three, daysWithoutUpdateToBeRemoved)

Removes all outdated nodes . An outdated node is a node with no update in the last days and without any child with an update in the last days

cleanNodeRec Parameters

NameTypeDescription
treeNodemessage treee
three
daysWithoutUpdateToBeRemovednumberamount of days the last change is entered before a node is old

tree properties

NameTypeDescription
timenumbertimestamp of the last change

three properties

NameTypeDescription
childsObjectobject with a list of all direct childs of the tree

createIntervalEntryFromTimeEntry

createIntervalEntryFromTimeEntry (timeEntry)

Creates an History entry holding several identical values with same reason and regular updates by a time entry .

createIntervalEntryFromTimeEntry Parameters

NameTypeDescription
timeEntryTimeEntryentry of type 'time'

decompress

decompress (history) => {Array.<Decompressed>}

Decomresses a history entry to an external format

decompress Parameters

NameTypeAttributeDescription
historynumberoptional . lastTime ] timestamp the value was encoutered last ( only type 'interval' )

decompress returns

TypeDescription
Array.<Decompressed>array of decompressed history entries

getLastIntervalEntries

getLastIntervalEntries (timeEntry, options) => {IntervalEntry}

Checks , if last entries in a timeEntry can be converted to an interval entry

getLastIntervalEntries Parameters

NameTypeDescription
timeEntryTimeEntry
optionsOptionsoptions to calculate the bounds

getLastIntervalEntries returns

TypeDescription
IntervalEntryamount of last entries that can be converted

isIntervalEntry

isIntervalEntry (timeEntry, options) => {boolean}

Checks , if a time entry can be converted to a interval entry

isIntervalEntry Parameters

NameTypeDescription
timeEntryTimeEntry
optionsObjectoptions to calculate the bounds

options properties

NameTypeDescription
upperBoundFactornumberfactor to multiply the joined interval for the upper bound
upperBoundAddInMillisecondsnumberconstant to add to the joined interval for the upper bound
lowerBoundFactornumberfactor to multiply the joined interval for the lower bound
lowerBoundSubInMillisecondsnumberconstant to sub from the joined interval for the lower bound

isIntervalEntry returns

TypeDescription
booleantrue , if converting is possible

splitTimeEntryIfAppropriate

splitTimeEntryIfAppropriate (history, options) => {Array.<HistoryEntry>}

Checks to split the last history time entry

splitTimeEntryIfAppropriate Parameters

NameTypeDescription
historyArray.<HistoryEntry>history list
optionsOptionsoptions to calculate the bounds

splitTimeEntryIfAppropriate returns

TypeDescription
Array.<HistoryEntry>updated history

splitTimeValueEntryIfAppropriate

splitTimeValueEntryIfAppropriate (history, options) => {Array.<HistoryEntry>}

Checks to split the last history timeValue entry

splitTimeValueEntryIfAppropriate Parameters

NameTypeDescription
historyArray.<HistoryEntry>history list
optionsOptionsoptions to calculate the bounds

splitTimeValueEntryIfAppropriate returns

TypeDescription
Array.<HistoryEntry>updated history

Class MessageTree

new MessageTree(options)

Data structure holding a large amount of messages with topic , value and reason in a tree organized according the topic . Each tree node containing data provides a "history" including recent messages . It includes several methods to "compress" the history of messages of each topic

MessageTree Parameters

NameTypeAttributeDefaultDescription
optionsOptionsoptional{ }options for the tree

MessageTree Methods

_isRequiredReason

_isRequiredReason (reason1, reason2) => {boolan}

Compares two reason objects

_isRequiredReason Parameters
NameTypeDescription
reason1Object, undefinedfirst reason to compare
reason2Object, undefinedsecond reason to compare
_isRequiredReason returns
TypeDescription
boolantrue , if reasons are identical or ANY reason is undefined , else false

addData

addData (message, cur)

Adds data to the three node matching to the topic path . It will store the current time , data and descrition to the node and the former time and data entry is copied to the history array property .

addData Parameters
NameTypeAttributeDefaultDescription
messageMessage{ topic , value , reason }
curDateoptionalnew Date ( )current time

message properties

NameTypeDescription
topicstringtopic string
valueobjecObjecttvalue of the topic
reasonArray.<Object>list of reasons

cleanup

cleanup (daysWithoutUpdateToBeRemoved)

Removes all outdated nodes . An outdated node is a node with no update in the last days and without any child with an update in the last days

cleanup Parameters
NameTypeDescription
daysWithoutUpdateToBeRemovednumberamount of days the last change is entered before a node is old

getNode

getNode (topic) => {Object}

Searches a node in the message tree corresponding to the topic .

getNode Parameters
NameTypeDescription
topicstringtopic to store the data
getNode returns
TypeDescription
Objectdata to store

getNodes

getNodes (requiredNodes, addHistory, addReason)

Gets a list of nodes - if different ( in value or reason ) than the provided nodes

getNodes Parameters
NameTypeAttributeDefaultDescription
requiredNodesArray.<Object>list of nodes required . If empty , all nodes are required
addHistorybooleanoptionaltruetrue , if the history property will be added
addReasonbooleanoptionaltruetrue , if the reason property of the messages will be added

getSection

getSection (topic, levelAmount, nodes, addHistory, addReason) => {Object.<payload:Object[]>}

Gets several levels of child nodes in an array of nodes

getSection Parameters
NameTypeAttributeDefaultDescription
topicstringstart topic of the section
levelAmountnumberamount of levels to get
nodesArray.<Object>list of nodes required . If empty , all nodes are required
addHistorybooleanoptionaltruetrue , if the history property will be added
addReasonbooleanoptionaltruetrue , if the reason property of the messages will be added
getSection returns
TypeDescription
Object.<payload:Object[]>Object with topics and payloads of all nodes in output format for these ( sub ) topics

persist

async persist (directory, filenameBasis)

Persists all connections

persist Parameters
NameTypeDescription
directorystringdirectory of the files
filenameBasisstringbasis filename , a timestamp is added

readTreeFromPersistedFile

readTreeFromPersistedFile (directory, filenameBasis)

Reads the tree from a persisted file

readTreeFromPersistedFile Parameters
NameTypeDescription
directorystringdirectory of the files
filenameBasisstringbasis filename , a timestamp is added
1.5.0

2 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago