def _update_state_aggregations()

in lca-ai-stack/source/lambda_functions/call_event_stream_processor/tumbling_window_state/call_state_manager.py [0:0]


    def _update_state_aggregations(self) -> None:
        """Updates the state statistics/aggregations

        Maintains a subset of the Transcribe Call Analytics output:
        https://docs.aws.amazon.com/transcribe/latest/dg/call-analytics-output.html
        """
        for call_id in self._changed_call_ids:
            # call_state is used to update the state in place
            call_state = self._state["StatePerCallId"][call_id]
            call_state_per_channel = call_state["StatePerChannel"]
            for channel in call_state_per_channel.keys():
                sentiment_list = call_state_per_channel[channel].get("SentimentList", [])

                sentiment_scores = [i["Score"] for i in sentiment_list]
                sentiment_average = fmean(sentiment_scores) if sentiment_scores else 0

                sentiment_per_quarter = (
                    self._get_sentiment_per_quarter(sentiment_list) if sentiment_list else []
                )
                previous_sentiment = call_state.get("Sentiment", {})
                previous_overall_sentiment = previous_sentiment.get("OverallSentiment", {})
                previous_sentiment_by_quarter = previous_sentiment.get("SentimentByPeriod", {}).get(
                    "QUARTER", {}
                )

                # update sentiment state
                call_state["Sentiment"] = {
                    **previous_sentiment,  # type: ignore
                    "OverallSentiment": {
                        **previous_overall_sentiment,
                        channel: sentiment_average,
                    },
                    "SentimentByPeriod": {
                        "QUARTER": {
                            **previous_sentiment_by_quarter,
                            channel: sentiment_per_quarter,
                        },
                    },
                }