Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
22 / 26 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| EventLogger | |
84.62% |
22 / 26 |
|
75.00% |
6 / 8 |
15.82 | |
0.00% |
0 / 1 |
| __construct | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| setPropertySuggesterName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setExistingProperties | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setExistingTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRequestDuration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setAddSuggestions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEvent | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| logEvent | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace PropertySuggester; |
| 4 | |
| 5 | use MediaWiki\Extension\EventLogging\EventLogging; |
| 6 | use MediaWiki\Registration\ExtensionRegistry; |
| 7 | |
| 8 | class EventLogger { |
| 9 | |
| 10 | /** |
| 11 | * Constants mapping to the keys in the AB-testing schema. |
| 12 | */ |
| 13 | public const EVENT_SUGGESTER_NAME = 'propertysuggester_name'; |
| 14 | public const EVENT_EXISTING_PROPERTIES = 'existing_properties'; |
| 15 | public const EVENT_EXISTING_TYPES = 'existing_types'; |
| 16 | public const EVENT_REQ_DURATION_MS = 'request_duration_ms'; |
| 17 | public const EVENT_ADD_SUGGESTIONS = 'add_suggestions_made'; |
| 18 | public const EVENT_LANGUAGE_CODE = 'language_code'; |
| 19 | public const EVENT_ID = 'event_id'; |
| 20 | |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | private $propertySuggesterName; |
| 25 | |
| 26 | /** |
| 27 | * @var string[] |
| 28 | */ |
| 29 | private $existingProperties; |
| 30 | |
| 31 | /** |
| 32 | * @var string[] |
| 33 | */ |
| 34 | private $existingTypes; |
| 35 | |
| 36 | /** |
| 37 | * @var int |
| 38 | */ |
| 39 | private $requestDuration; |
| 40 | |
| 41 | /** |
| 42 | * @var string[] |
| 43 | */ |
| 44 | private $addSuggestions; |
| 45 | |
| 46 | /** |
| 47 | * @var string |
| 48 | */ |
| 49 | private $languageCode; |
| 50 | |
| 51 | /** |
| 52 | * @var string |
| 53 | */ |
| 54 | private $eventID; |
| 55 | |
| 56 | /** |
| 57 | * @var string |
| 58 | */ |
| 59 | private $eventSchema; |
| 60 | |
| 61 | public function __construct( |
| 62 | string $eventID, |
| 63 | string $languageCode |
| 64 | ) { |
| 65 | $this->eventID = $eventID; |
| 66 | $this->languageCode = $languageCode; |
| 67 | |
| 68 | if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) { |
| 69 | $schemas = ExtensionRegistry::getInstance()->getAttribute( 'EventLoggingSchemas' ); |
| 70 | $this->eventSchema = $schemas['PropertySuggesterServerSidePropertyRequest']; |
| 71 | } else { |
| 72 | $this->eventSchema = ''; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $suggesterName |
| 78 | */ |
| 79 | public function setPropertySuggesterName( string $suggesterName ) { |
| 80 | $this->propertySuggesterName = $suggesterName; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string[] $existingProperties |
| 85 | */ |
| 86 | public function setExistingProperties( array $existingProperties ) { |
| 87 | $this->existingProperties = $existingProperties; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param string[] $existingTypes |
| 92 | */ |
| 93 | public function setExistingTypes( array $existingTypes ) { |
| 94 | $this->existingTypes = $existingTypes; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param int $requestDuration |
| 99 | * The duration of the request in milliseconds |
| 100 | * If the SchemaTreeSuggester request fails, the |
| 101 | * value logged will be -1 |
| 102 | */ |
| 103 | public function setRequestDuration( int $requestDuration ) { |
| 104 | $this->requestDuration = $requestDuration; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param string[] $additionalSuggestions |
| 109 | */ |
| 110 | public function setAddSuggestions( array $additionalSuggestions ) { |
| 111 | $this->addSuggestions = $additionalSuggestions; |
| 112 | } |
| 113 | |
| 114 | public function getEvent(): array { |
| 115 | return [ |
| 116 | '$schema' => $this->eventSchema, |
| 117 | self::EVENT_SUGGESTER_NAME => $this->propertySuggesterName ?: 'Failed setting name', |
| 118 | self::EVENT_EXISTING_PROPERTIES => $this->existingProperties ?: [], |
| 119 | self::EVENT_EXISTING_TYPES => $this->existingTypes ?: [], |
| 120 | self::EVENT_REQ_DURATION_MS => $this->requestDuration ?? -1, |
| 121 | self::EVENT_ADD_SUGGESTIONS => $this->addSuggestions ?: [], |
| 122 | self::EVENT_LANGUAGE_CODE => $this->languageCode, |
| 123 | self::EVENT_ID => $this->eventID |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | public function logEvent(): bool { |
| 128 | if ( !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) || $this->eventID === '' ) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | $event = $this->getEvent(); |
| 133 | |
| 134 | EventLogging::submit( 'wd_propertysuggester.server_side_property_request', $event ); |
| 135 | return true; |
| 136 | } |
| 137 | } |