Skip to main content
Glama

PTP MCP Server

by aneeshkp
ptp_query_engine.cpython-311.pyc19 kB
� �|hKE���dZddlZddlZddlZddlmZmZmZmZm Z ddl m Z m Z ej e ��ZGd�d��ZdS)z@ PTP Query Engine - Natural language processing for PTP queries �N)�Dict�List�Optional�Any�Tuple)�datetime� timedeltac ���eZdZdZd�Zddededeeeffd�Zdedefd�Z dede efd �Z dedeeeffd �Z d eeefd eeefdefd �Z d eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zd eeefdefd�Zddedeefd�ZdS)�PTPQueryEnginez1Natural language query engine for PTP informationc �z�gd�gd�gd�gd�gd�gd�gd�gd�gd �gd �d � |_gd �gd �gd�gd�d�|_dS)N)zwhat is the current grandmasterzwho is the grandmasterzshow grandmasterzgrandmaster statusz current gm)zshow ptpconfig parameterszptp configuration� ptpconfigzconfiguration parametersz show config)zcheck for sync lossz sync statuszsynchronization statusz is it syncedz sync health)zclock hierarchyzshow clock hierarchyzclock topologyzclock structure)z offset trendzget offset trendzoffset analysiszfrequency offset)z bmca statezbest master clockzmaster clock algorithmz bmca status)zclockclass changez clock class� clockclassz class change)z search logsz log searchz find in logsz log analysis)z health checkz ptp healthz system health� diagnostics)zitu compliancez g\.8275\.1zitu-tzcompliance check) � grandmaster� configuration� sync_status�clock_hierarchy� offset_trend� bmca_state� clock_class� logs_search� health_check�itu_compliance)z last hourz past hourzin the last hour)zlast dayzpast dayzin the last day� yesterday)z last weekz past weekzin the last week)�"last (\d+) (minutes?|hours?|days?)z"past (\d+) (minutes?|hours?|days?)z!(\d+) (minutes?|hours?|days?) ago)� last_hour�last_day� last_week� custom_time)�query_patterns� time_patterns)�selfs �</home/aputtur/Ai Projects/ptp-mcp-server/ptp_query_engine.py�__init__zPTPQueryEngine.__init__s������������ � � � ��� ��� ��� ��� ��� ���u@ �@ ���H��� ��� ��� ���# � �����N�question�context�returnc�$�|������}|�|��}|�|��}|�|��}|||||t j�����d�S)z3Parse natural language query into structured format)�original_questionr'� query_type� time_range� parameters� parsed_at)�lower�strip�_determine_query_type�_extract_time_range�_extract_parametersr�now� isoformat)r"r&r'�question_lowerr+r,r-s r#� parse_queryzPTPQueryEngine.parse_queryns���!���)�)�/�/�1�1���/�/��?�?� ��-�-�n�=�=� ��-�-�n�=�=� �"*��$�$�$�!����1�1�3�3�  � � r%c��|j���D]0\}}|D](}tj||tj��r|ccS�)�1dS)z'Determine the type of query being asked�general)r �items�re�search� IGNORECASE)r"r&r+�patterns�patterns r#r1z$PTPQueryEngine._determine_query_type�so��$(�$7�$=�$=�$?�$?� &� &� �J��#� &� &���9�W�h�� �>�>�&�%�%�%�%�%�%�&� &� �yr%c���|j���D]0\}}|D](}tj||tj��r|ccS�)�1tjd|tj��}|r{|�d��}|�d��}|�d��rd|�d�S|�d��rd|�d�S|�d ��rd|�d �Sd S) z Extract time range from questionr���minute�last_�m�hour�h�day�dN)r!r:r;r<r=�group� startswith)r"r&r,r>r?� custom_match�amount�units r#r2z"PTPQueryEngine._extract_time_range�s&��%)�$6�$<�$<�$>�$>� &� &� �J��#� &� &���9�W�h�� �>�>�&�%�%�%�%�%�%�&� &� �y�!F��RT�R_�`�`� � � )�!�'�'��*�*�F��%�%�a�(�(�D����x�(�(� )�(�v�(�(�(�(�����(�(� )�(�v�(�(�(�(�����'�'� )�(�v�(�(�(�(��tr%c�F�i}gd�}|D] }||vr||d<n�gd�}|D] }||vr||d<n�tjd|tj��}|r|�d��|d<tjd|��}|r%t |�d����|d <|S) z-Extract specific parameters from the question)�error�warning�info�debug� log_level)�ptp4l�phc2sys�ts2phc�dpll�gnss�gm� componentzinterface (\w+)rA� interfacez(\d+)�value)r;r<r=rJ�int) r"r&r-� log_levels�level� componentsr[�interface_match� value_matchs r#r3z"PTPQueryEngine._extract_parameters�s���� �;�:�:� �� � �E��� � �*/� �;�'���!� J�I�I� �#� � �I��H�$�$�*3� �;�'���%� �)�$6��"�-�P�P�� � ?�&5�&;�&;�A�&>�&>�J�{� #��i��(�3�3� � � <�"%�k�&7�&7��&:�&:�";�";�J�w� ��r%� query_info�datac���|d}|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|dkr|�|��S|d kr|�|��S|�|��S) z:Generate natural language response based on query and datar+rrrrrrrr) �_generate_grandmaster_response� _generate_configuration_response�_generate_sync_status_response�"_generate_clock_hierarchy_response�_generate_offset_trend_response�_generate_bmca_state_response�_generate_health_check_response�!_generate_itu_compliance_response�_generate_general_response)r"rdrer+s r#�generate_responsez PTPQueryEngine.generate_response�s��� �-� � �� &� &��6�6�t�<�<� <� �?� *� *��8�8��>�>� >� �=� (� (��6�6�t�<�<� <� �,� ,� ,��:�:�4�@�@� @� �>� )� )��7�7��=�=� =� �<� '� '��5�5�d�;�;� ;� �>� )� )��7�7��=�=� =� �+� +� +��9�9�$�?�?� ?��2�2�4�8�8� 8r%c��|�di��}|r|�d��dkrdS|�dd��}|�dd��}|�d��}|�d��}|�d��}d |��}|dkr|d |��z }|� |d |�d �z }|� |d|�d�z }|r|d|��z }|S)z)Generate response for grandmaster queriesr�status�unknownz9No grandmaster information available in the current logs.r\�offset� frequency� last_seenzCurrent grandmaster status: z on interface Nz Current offset: z nsz Frequency adjustment: z ppbz Last seen: ��get) r"re�gm_inforrr\rtrurv�responses r#rgz-PTPQueryEngine._generate_grandmaster_response�s���(�(�=�"�-�-��� O�'�+�+�h�/�/�9�<�<�N�N����X�y�1�1���K�K� �Y�7�7� ����X�&�&���K�K� �,�,� ��K�K� �,�,� �:�&�:�:�� � � !� !� �4��4�4� 4�H� � � �8�V�8�8�8� 8�H� � � �B�9�B�B�B� B�H� � 4� �3� �3�3� 3�H��r%c�D�|�di��}|sdSd}|�dd��}|�dd��}|�dd��}|d|�d �z }|d |�d �z }|d |�d �z }|�d i��}|r+|d z }|���D]\}} |d|�d| �d �z }�|�d��} | � |d| �d �z }|�di��} | r+|dz }| ���D]\} } |d| �d| �d �z }�|S)z+Generate response for configuration queriesrzNo PTP configuration found.zPTP Configuration: �namers� clock_type�domainz- Name: � z- Clock Type: � - Domain: � prioritiesz- Priorities: � - z: rNz- Clock Class: �sync_intervalsz- Sync Intervals: )rxr:)r"re�configrzr|r}r~r�� priority_name�priority_valuerr�� interval_name�interval_values r#rhz/PTPQueryEngine._generate_configuration_response�s������/�2�.�.��� 1�0�0�)���z�z�&�)�,�,���Z�Z� �i�8�8� ����H�i�0�0���'�t�'�'�'�'���3�Z�3�3�3�3���+��+�+�+�+���Z�Z� �b�1�1� � � G� �)� )�H�1;�1A�1A�1C�1C� G� G�-� �~��F�=�F�F�N�F�F�F�F����j�j��/�/� � � "� �9�+�9�9�9� 9�H� ���$4�b�9�9�� � G� �-� -�H�1?�1E�1E�1G�1G� G� G�-� �~��F�=�F�F�N�F�F�F�F����r%c��|�di��}|sdSd}|�dd��}|�dd��}|�dd��}|�d��}|d |rd nd �d �z }|d |rd nd �d �z }|d|rd nd �d �z }|� |d|�d�z }|r|r|dz }n|r|s|dz }n|dz }|S)z)Generate response for sync status queriesrz0No synchronization status information available.zSynchronization Status: � dpll_lockedF�gnss_available�offset_in_range� last_offsetz- DPLL Locked: �Yes�Norz- GNSS Available: z- Offset in Range: Nz- Last Offset: � ns z( Status: HEALTHY - Clock is synchronizedz= Status: WARNING - Clock is locked but offset is out of rangez. Status: UNHEALTHY - Clock is not synchronizedrw)r"rerrzr�r�r�r�s r#riz-PTPQueryEngine._generate_sync_status_responses<���h�h�}�b�1�1� �� F�E�E�.��!�o�o�m�U�;�;� �$���)9�5�A�A��%�/�/�*;�U�C�C��!�o�o�m�4�4� ��H�{�&D�e�e��H�H�H�H���N�.�)J���d�N�N�N�N���P�?�*L�%�%��P�P�P�P�� � "� �<�+�<�<�<� <�H� � J�?� J� �C� C�H�H� � J�� J� �X� X�H�H� �I� I�H��r%c���|�di��}|sdSd}|�di��}|rQ|�dd��}|�dd��}|�dd��}|d |�d |�d |�d �z }|�d ��}|r|d|�d���d �z }n|dz }|�dg��} | r|dt| ���d�z }n|dz }|S)z-Generate response for clock hierarchy queriesrz)No clock hierarchy information available.zClock Hierarchy: � current_clock�typersr~rz- Current Clock: z (Domain z, Class z) rz"- Grandmaster: Active (last seen: rvz- Grandmaster: Not detected �boundary_clocksz- Boundary Clocks: z detected z!- Boundary Clocks: None detected )rx�len) r"re� hierarchyrzr�r}r~rrr�s r#rjz1PTPQueryEngine._generate_clock_hierarchy_response;s?���H�H�.��3�3� �� ?�>�>�'��!� � �o�r�:�:� � � b�&�*�*�6�9�=�=�J�"�&�&�x��;�;�F�'�+�+�M�9�E�E�K� �a�J�a�a��a�a�Q\�a�a�a� a�H��m�m�M�2�2� � � 8� �^�[�_�_�[�=Y�=Y�^�^�^� ^�H�H� �7� 7�H�#�-�-�(9�2�>�>�� � =� �O�c�/�.B�.B�O�O�O� O�H�H� �<� <�H��r%c��|�di��}|sdSd}|�d��}|�dd��}|�d��}|�d��}|�d ��}|�d d ��} |� |d |�d�z }|d|�d�z }|�|� |d|�d|�d�z }|� |d|�d�z }|d| �d�z }|S)z*Generate response for offset trend queriesrz&No offset trend information available.zOffset Trend Analysis: �current_offset�trendrs� min_offset� max_offset�average_offset�samplesrNz- Current Offset: r�z - Trend: rz - Range: z to z - Average: z - Samples: rw) r"rer�rzr��trend_directionr�r�r�r�s r#rkz.PTPQueryEngine._generate_offset_trend_responseZs&�������,�,��� <�;�;�-�����#3�4�4���)�)�G�Y�7�7���Y�Y�|�,�,� ��Y�Y�|�,�,� ����#3�4�4���)�)�I�q�)�)�� � %� �B�^�B�B�B� B�H��3��3�3�3�3�� � !�j�&<� �E�J�E�E�J�E�E�E� E�H� � %� �;�n�;�;�;� ;�H��-�'�-�-�-�-���r%c�:�|�di��}|sdSd}|�dd��}|�dd��}|�d��}|�d��}|d |�d �z }|d |�d �z }|� |d |�d �z }|� |d|�d �z }|S)z(Generate response for BMCA state queriesrz$No BMCA state information available.z*BMCA (Best Master Clock Algorithm) State: �rolersr~� priority1� priority2z- Role: rr�Nz- Priority 1: z- Priority 2: rw)r"rerrzr�r~r�r�s r#rlz,PTPQueryEngine._generate_bmca_state_responseys����X�X�l�B�/�/� �� :�9�9�@���~�~�f�i�0�0������)�4�4���N�N�;�/�/� ��N�N�;�/�/� ��'�t�'�'�'�'���+��+�+�+�+�� � � �6��6�6�6� 6�H� � � �6��6�6�6� 6�H��r%c�R�|�di��}|sdSd}|�dd��}|�dd��}|�dd��}|d|rd nd �d �z }|d |rd nd�d �z }|d|rd nd�d �z }|r |r|r|dz }n|s|dz }n |s|dz }n|dz }|S)z*Generate response for health check queries�healthz&No health check information available.zPTP Health Check: � config_validF� sync_healthy� logs_healthyz- Configuration: �Valid�Invalidrz- Synchronization: �Healthy� Unhealthyz- Logs: zIssues detectedz Overall Status: HEALTHYz0 Overall Status: CRITICAL - Configuration issuesz1 Overall Status: WARNING - Synchronization issuesz. Overall Status: WARNING - Log issues detectedrw)r"rer�rzr�r�r�s r#rmz.PTPQueryEngine._generate_health_check_response�s#�����(�B�'�'��� <�;�;�(���z�z�.�%�8�8� ��z�z�.�%�8�8� ��z�z�.�%�8�8� ��R�<�(N���Y�R�R�R�R���X�|�*T�)�)��X�X�X�X���S�L�O�y�y�>O�S�S�S�S�� � J�L� J�\� J� �3� 3�H�H�� J� �K� K�H�H�� J� �L� L�H�H� �I� I�H��r%c�B�|�di��}|sdSd}|�dd��}|�dg��}|�dg��}|d|rd nd �d �z }|r|d z }|D] }|d |�d �z }� |r|dz }|D] }|d |�d �z }� |s|s|dz }|S)z,Generate response for ITU compliance queriesrz3No ITU-T G.8275.1 compliance information available.z!ITU-T G.8275.1 Compliance Check: � compliantF�warnings�errorsz - Compliant: r�r�rz - Warnings: r�z - Errors: z- No issues detected rw) r"re� compliancerzr�r�r�rQrPs r#rnz0PTPQueryEngine._generate_itu_compliance_response�s���X�X�.��3�3� �� I�H�H�7���N�N�;��6�6� ��>�>�*�b�1�1������"�-�-���D�Y�$@�E�E�D�D�D�D�D�� � /� �� '�H�#� /� /���.�7�.�.�.�.��� � -� � � %�H�� -� -���,�5�,�,�,�,���� 1�� 1� �0� 0�H��r%c�4�dtj|d�����S)z2Generate general response for unrecognized queriesz!Query processed. Available data: rB)�indent)�json�dumps)r"res r#roz)PTPQueryEngine._generate_general_response�s��O�4�:�d�1�3M�3M�3M�O�O�Or%c��gd�}|S)z(Suggest related queries based on context) z What is the current grandmaster?zShow ptpconfig parameterszCheck for sync lossz!Search logs for clockClass changezGet offset trend in last hourzWhat is the BMCA state?zShow current clock hierarchyzCheck PTP healthz"Validate ITU-T G.8275.1 compliance�)r"r'� suggestionss r#�suggest_querieszPTPQueryEngine.suggest_queries�s��  �  �  � ��r%)N)�__name__� __module__� __qualname__�__doc__r$�strrrr7r1rr2r3rprgrhrirjrkrlrmrnrorr�r�r%r#r r s�������;�;�[ �[ �[ �z � �C� �#� ��c�3�h�� � � � �,�c��c������C��H�S�M�����,�C��D��c��N�����<9�D��c��N�9�$�s�C�x�.�9�UX�9�9�9�9�.�4��S��>��c�����8%�T�#�s�(�^�%��%�%�%�%�N�4��S��>��c�����>�t�C��H�~��#�����>�D��c��N��s�����>�$�s�C�x�.��S�����2�D��c��N��s�����:�d�3��8�n�������<P�t�C��H�~�P�#�P�P�P�P���s��d�3�i������r%r )r�r��loggingr;�typingrrrrrrr � getLoggerr��loggerr r�r%r#�<module>r�s����� � � � ����� � � � �3�3�3�3�3�3�3�3�3�3�3�3�3�3�(�(�(�(�(�(�(�(� �� �8� $� $��Q�Q�Q�Q�Q�Q�Q�Q�Q�Qr%

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aneeshkp/ptp-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server