You are here: Foswiki>Applications Web>ApLsaRestDetectingTrims (08 Apr 2022, AnnekeWalter)Edit Attach

Detecting Trims via LSA REST API

LSA REST APIS (v2)

JSON Data

{"RESIDENT_PATTERNS":
    {"ESR_EXP22_02_238U92_Brandau_30March22":
        {"LAST_TRIMMED":"2022-04-07T14:52:47.626Z","CHAINS":{…
     "SIS18_FAST_E142_20220325_110213":
        {"LAST_TRIMMED":"2022-04-07T07:47:27.848Z","CHAINS":{…
 …

The JSON data contains information about the "last trimmed" timestamp. If this timestamp does not change, there have been no trims and therefore no setting changes for the specified pattern.

The LSA data is cached and updated regularly.

Access via Java library: Polling

<groupId>de.gsi.fcc.commons</groupId>
<artifactId>bss-client-lib</artifactId>

The library bss-client-lib provides the de.gsi.fcc.applications.running.utils.lsainfo.DefaultLsaDataProvider . The DefaultLsaDataProvider tries to use the LSA REST API for the appropriate environment (defined in the default properties). If the LSA REST API is not accessible (e.g. webserver is down), the LSA server is accessed directly.

To receive a Map that contains the Pattern names as keys, with the associated trim timestamps as values:
final LsaDataProvider trimPollDataProvider = DefaultLsaDataProvider.INSTANCE; 
final Map<String, String> newPatternNameToLastTrimTimestamp = trimPollDataProvider.getResidentPatternsWithLastTrimTimestamp();

By storing the timestamp map and comparing stored results with new results, it is possible to determine which Patterns have had their Settings changed.

Example:
private static Set<String> getNamesOfTrimmedPatterns( final Map<String, String> oldPatternNameToLastTrimTimestamp, final Map<String, String> newPatternNameToLastTrimTimestamp) { 
    final Map<String, String> diff = new HashMap<>(); 
    diff.putAll(oldPatternNameToLastTrimTimestamp); 
    diff.putAll(newPatternNameToLastTrimTimestamp); 
    diff.entrySet().removeAll(oldPatternNameToLastTrimTimestamp.entrySet()); 
    return diff.keySet(); 
}

Possible Future: Automatic Notifications via SSE

The endpoint HOSTNAME/lsa/client/v2/resident_patterns_change is a Server-Sent Events (SSE) endpoint that theoretically could be used to receive notifications when the data from the HOSTNAME/lsa/client/v2/status is changed.

Ideally, we would be able to avoid polling by using this endpoint.

Caution: This is largely untested, we make no guarantees that either the server side implementation or the example code are "correct" or the best way to use it!

There may still be bugs, and when we tested it in the past, we encountered some cases in which multiple SSE connections caused problems on the REST service because there were no more open sockets left. We would therefore strongly suggest that usage attempts should first be made against the INT REST API before using it in production.

Subscription examples

Subscriptions in various languages (e.g. Java, Python, ...) should be easy to implement, but we do not have an example for all of them yet. smile

JavaScript

An example subscription using JavaScript is available under HOSTNAME/lsa/client/v2/resident_patterns_change_example .

Via console

curl --no-buffer --header "Accept:text/event-stream" https://HOSTNAME/lsa/client/v2/resident_patterns_change
Example output
[17:29:18][awalter@asl751:~]$ curl --no-buffer --header "Accept:text/event-stream" https://restint00a.acc.gsi.de/lsa/client/v2/resident_patterns_change

data: {"RESIDENT_PATTERNS":{"SCRATCH_HL_CRYRING_FAST_20210824_NewFgPrep":{"LAST_TRIMMED":"2022-03-22T07:24:49.414Z"},"SIS100_PROTON_20210915_HL_NewFgPrep":{"LAST_TRIMMED":"2022-03-02T09:54:32.541Z"},"SCRATCH_HH_SIS18_FAST_HHD_20211021_090824":{"LAST_TRIMMED":"2022-04-07T12:27:33.660Z"},"SCRATCH_HL_SIS18_FAST_HHD_20220113_DAQ_TEST":{"LAST_TRIMMED":"2022-04-07T09:39:32.130Z"},"SCRATCH_HL_SIS18_FAST_TE_ESR_20210824_NewFgPrep":{"LAST_TRIMMED":"2022-04-07T09:51:57.520Z"},"ESR_FAST_TO_YRT1MH2_20211004_INT_TEST":{"LAST_TRIMMED":"2022-03-22T07:25:06.320Z"},"SCRATCH_AW_SIS18_FAST_HADES_20211220_112502":{"LAST_TRIMMED":"2022-04-07T12:27:14.705Z"}}}

: Heartbeat, no new information.

: Heartbeat, no new information.

: Heartbeat, no new information.

[Note by editor: Trim to SCRATCH_AW_SIS18_FAST_HADES_20211220_112502 was performed here]

data: {"RESIDENT_PATTERNS":{"SCRATCH_HL_CRYRING_FAST_20210824_NewFgPrep":{"LAST_TRIMMED":"2022-03-22T07:24:49.414Z"},"SIS100_PROTON_20210915_HL_NewFgPrep":{"LAST_TRIMMED":"2022-03-02T09:54:32.541Z"},"SCRATCH_HH_SIS18_FAST_HHD_20211021_090824":{"LAST_TRIMMED":"2022-04-07T12:27:33.660Z"},"SCRATCH_HL_SIS18_FAST_HHD_20220113_DAQ_TEST":{"LAST_TRIMMED":"2022-04-07T09:39:32.130Z"},"SCRATCH_HL_SIS18_FAST_TE_ESR_20210824_NewFgPrep":{"LAST_TRIMMED":"2022-04-07T09:51:57.520Z"},"ESR_FAST_TO_YRT1MH2_20211004_INT_TEST":{"LAST_TRIMMED":"2022-03-22T07:25:06.320Z"},"SCRATCH_AW_SIS18_FAST_HADES_20211220_112502":{"LAST_TRIMMED":"2022-04-07T15:31:48.380Z"}}}

: Heartbeat, no new information.

…

-- AnnekeWalter - 07 Apr 2022
Topic revision: r3 - 08 Apr 2022, AnnekeWalter
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback