diff --git a/.rr.yaml b/.rr.yaml index e5d924f..ca4a8c6 100644 --- a/.rr.yaml +++ b/.rr.yaml @@ -25,10 +25,5 @@ service: remain_after_exit: true restart_sec: 1 -grpc: - listen: tcp://0.0.0.0:9001 - proto: - - "proto/service.proto" - metrics: address: 0.0.0.0:2112 diff --git a/GRPC/Twitter/AddRequest.php b/GRPC/Twitter/AddRequest.php deleted file mode 100644 index d6a3e84..0000000 --- a/GRPC/Twitter/AddRequest.php +++ /dev/null @@ -1,58 +0,0 @@ -twitter.dto.AddRequest - */ -class AddRequest extends \Google\Protobuf\Internal\Message -{ - /** - * Generated from protobuf field string id = 1; - */ - protected $id = ''; - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * @type string $id - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - - /** - * Generated from protobuf field string id = 1; - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Generated from protobuf field string id = 1; - * @param string $var - * @return $this - */ - public function setId($var) - { - GPBUtil::checkString($var, True); - $this->id = $var; - - return $this; - } - -} - diff --git a/GRPC/Twitter/AddResponse.php b/GRPC/Twitter/AddResponse.php deleted file mode 100644 index 54d52b3..0000000 --- a/GRPC/Twitter/AddResponse.php +++ /dev/null @@ -1,58 +0,0 @@ -twitter.dto.AddResponse - */ -class AddResponse extends \Google\Protobuf\Internal\Message -{ - /** - * Generated from protobuf field bool status = 1; - */ - protected $status = false; - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * @type bool $status - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - - /** - * Generated from protobuf field bool status = 1; - * @return bool - */ - public function getStatus() - { - return $this->status; - } - - /** - * Generated from protobuf field bool status = 1; - * @param bool $var - * @return $this - */ - public function setStatus($var) - { - GPBUtil::checkBool($var); - $this->status = $var; - - return $this; - } - -} - diff --git a/GRPC/Twitter/AllRequest.php b/GRPC/Twitter/AllRequest.php deleted file mode 100644 index 1ed7df6..0000000 --- a/GRPC/Twitter/AllRequest.php +++ /dev/null @@ -1,31 +0,0 @@ -twitter.dto.AllRequest - */ -class AllRequest extends \Google\Protobuf\Internal\Message -{ - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - -} - diff --git a/GRPC/Twitter/AllResponse.php b/GRPC/Twitter/AllResponse.php deleted file mode 100644 index 3b85f64..0000000 --- a/GRPC/Twitter/AllResponse.php +++ /dev/null @@ -1,58 +0,0 @@ -twitter.dto.AllResponse - */ -class AllResponse extends \Google\Protobuf\Internal\Message -{ - /** - * Generated from protobuf field repeated string ids = 1; - */ - private $ids; - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * @type string[]|\Google\Protobuf\Internal\RepeatedField $ids - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - - /** - * Generated from protobuf field repeated string ids = 1; - * @return \Google\Protobuf\Internal\RepeatedField - */ - public function getIds() - { - return $this->ids; - } - - /** - * Generated from protobuf field repeated string ids = 1; - * @param string[]|\Google\Protobuf\Internal\RepeatedField $var - * @return $this - */ - public function setIds($var) - { - $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING); - $this->ids = $arr; - - return $this; - } - -} - diff --git a/GRPC/Twitter/GPBMetadata/Message.php b/GRPC/Twitter/GPBMetadata/Message.php deleted file mode 100644 index 2806c12..0000000 --- a/GRPC/Twitter/GPBMetadata/Message.php +++ /dev/null @@ -1,24 +0,0 @@ -internalAddGeneratedFile(hex2bin( - "0af2010a0d6d6573736167652e70726f746f120b747769747465722e64746f22180a0a41646452657175657374120a0a026964180120012809221d0a0b416464526573706f6e7365120e0a06737461747573180120012808221b0a0d52656d6f766552657175657374120a0a02696418012001280922200a0e52656d6f7665526573706f6e7365120e0a06737461747573180120012808220c0a0a416c6c52657175657374221a0a0b416c6c526573706f6e7365120b0a03696473180120032809422aca020c475250435c54776974746572e20218475250435c547769747465725c4750424d65746164617461620670726f746f33" - ), true); - - static::$is_initialized = true; - } -} - diff --git a/GRPC/Twitter/GPBMetadata/Service.php b/GRPC/Twitter/GPBMetadata/Service.php deleted file mode 100644 index 23fb51d..0000000 --- a/GRPC/Twitter/GPBMetadata/Service.php +++ /dev/null @@ -1,25 +0,0 @@ -internalAddGeneratedFile(hex2bin( - "0a9c020a0d736572766963652e70726f746f12077477697474657232cd010a0e5477697474657253657276696365123a0a0341646412172e747769747465722e64746f2e416464526571756573741a182e747769747465722e64746f2e416464526573706f6e7365220012430a0652656d6f7665121a2e747769747465722e64746f2e52656d6f7665526571756573741a1b2e747769747465722e64746f2e52656d6f7665526573706f6e73652200123a0a03416c6c12172e747769747465722e64746f2e416c6c526571756573741a182e747769747465722e64746f2e416c6c526573706f6e73652200422aca020c475250435c54776974746572e20218475250435c547769747465725c4750424d65746164617461620670726f746f33" - ), true); - - static::$is_initialized = true; - } -} - diff --git a/GRPC/Twitter/RemoveRequest.php b/GRPC/Twitter/RemoveRequest.php deleted file mode 100644 index fe4f07e..0000000 --- a/GRPC/Twitter/RemoveRequest.php +++ /dev/null @@ -1,58 +0,0 @@ -twitter.dto.RemoveRequest - */ -class RemoveRequest extends \Google\Protobuf\Internal\Message -{ - /** - * Generated from protobuf field string id = 1; - */ - protected $id = ''; - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * @type string $id - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - - /** - * Generated from protobuf field string id = 1; - * @return string - */ - public function getId() - { - return $this->id; - } - - /** - * Generated from protobuf field string id = 1; - * @param string $var - * @return $this - */ - public function setId($var) - { - GPBUtil::checkString($var, True); - $this->id = $var; - - return $this; - } - -} - diff --git a/GRPC/Twitter/RemoveResponse.php b/GRPC/Twitter/RemoveResponse.php deleted file mode 100644 index 8329fc0..0000000 --- a/GRPC/Twitter/RemoveResponse.php +++ /dev/null @@ -1,58 +0,0 @@ -twitter.dto.RemoveResponse - */ -class RemoveResponse extends \Google\Protobuf\Internal\Message -{ - /** - * Generated from protobuf field bool status = 1; - */ - protected $status = false; - - /** - * Constructor. - * - * @param array $data { - * Optional. Data for populating the Message object. - * - * @type bool $status - * } - */ - public function __construct($data = NULL) { - \GRPC\Twitter\GPBMetadata\Message::initOnce(); - parent::__construct($data); - } - - /** - * Generated from protobuf field bool status = 1; - * @return bool - */ - public function getStatus() - { - return $this->status; - } - - /** - * Generated from protobuf field bool status = 1; - * @param bool $var - * @return $this - */ - public function setStatus($var) - { - GPBUtil::checkBool($var); - $this->status = $var; - - return $this; - } - -} - diff --git a/GRPC/Twitter/TwitterServiceInterface.php b/GRPC/Twitter/TwitterServiceInterface.php deleted file mode 100644 index ef90ade..0000000 --- a/GRPC/Twitter/TwitterServiceInterface.php +++ /dev/null @@ -1,40 +0,0 @@ -cache = $storageProvider->storage(AccountCacheRepository::STORAGE_NAME); - } - - public function Add(GRPC\ContextInterface $ctx, AddRequest $in): AddResponse - { - $this->registry->add($in->getId()); - $this->persist(); - - return new AddResponse([ - 'status' => true, - ]); - } - - public function Remove(GRPC\ContextInterface $ctx, RemoveRequest $in): RemoveResponse - { - $this->registry->add($in->getId()); - $this->persist(); - - return new RemoveResponse([ - 'status' => true, - ]); - } - - public function All(GRPC\ContextInterface $ctx, AllRequest $in): AllResponse - { - return new AllResponse([ - 'ids' => $this->registry->getAccounts(), - ]); - } - - private function persist(): void - { - $this->cache->set( - AccountCacheRepository::CACHE_KEY, - $this->registry->getAccounts() - ); - } -} diff --git a/app/src/Application/Bootloader/ExceptionHandlerBootloader.php b/app/src/Application/Bootloader/ExceptionHandlerBootloader.php index 54800c2..5ab1483 100644 --- a/app/src/Application/Bootloader/ExceptionHandlerBootloader.php +++ b/app/src/Application/Bootloader/ExceptionHandlerBootloader.php @@ -6,7 +6,6 @@ use Spiral\Boot\Bootloader\Bootloader; use Spiral\Exceptions\ExceptionHandlerInterface; -use Spiral\Exceptions\Renderer\ConsoleRenderer; use Spiral\Exceptions\Reporter\FileReporter; use Spiral\Exceptions\Reporter\LoggerReporter; use Spiral\Http\ErrorHandler\PlainRenderer; diff --git a/app/src/Application/Bootloader/TwitterBootloader.php b/app/src/Application/Bootloader/TwitterBootloader.php index 4ca7f67..9fd8969 100644 --- a/app/src/Application/Bootloader/TwitterBootloader.php +++ b/app/src/Application/Bootloader/TwitterBootloader.php @@ -5,7 +5,6 @@ namespace App\Application\Bootloader; use Abraham\TwitterOAuth\TwitterOAuth; -use App\Application\Repository\AccountCacheRepository; use App\Application\Repository\AccountConfigRepository; use App\Application\Repository\AccountEvnRepository; use App\Application\TwitterAccountRegistry; @@ -14,7 +13,6 @@ use App\Infrastructure\Twitter\TwitterConfig; use Spiral\Boot\Bootloader\Bootloader; use Spiral\Boot\EnvironmentInterface; -use Spiral\Cache\CacheStorageProviderInterface; use Spiral\Config\ConfiguratorInterface; final class TwitterBootloader extends Bootloader @@ -32,12 +30,10 @@ public function __construct( private function initTwitterAccountRegistry( AccountConfigRepository $configRepository, AccountEvnRepository $envRepository, - AccountCacheRepository $cacheRepository ): TwitterAccountRegistry { $accounts = \array_merge( $configRepository->all(), $envRepository->all(), - $cacheRepository->all(), ); return new TwitterAccountRegistry( diff --git a/app/src/Application/GRPC/Interceptor/ExceptionHandlerInterceptor.php b/app/src/Application/GRPC/Interceptor/ExceptionHandlerInterceptor.php deleted file mode 100644 index 9115cc0..0000000 --- a/app/src/Application/GRPC/Interceptor/ExceptionHandlerInterceptor.php +++ /dev/null @@ -1,40 +0,0 @@ -callAction($controller, $action, $parameters); - } catch (\Throwable $e) { - $this->errorHandler->report($e); - - if ($e instanceof GRPCExceptionInterface) { - throw $e; - } - - throw new GRPCException( - message: $e->getMessage(), - previous: $e - ); - } - } -} diff --git a/app/src/Application/Job/TwitterDataCollector.php b/app/src/Application/Job/TwitterDataCollector.php index 3482a34..b1d84c4 100644 --- a/app/src/Application/Job/TwitterDataCollector.php +++ b/app/src/Application/Job/TwitterDataCollector.php @@ -4,6 +4,7 @@ namespace App\Application\Job; +use App\Application\Metrics\TwitterMetrics; use App\Infrastructure\Twitter\ClientInterface; use Carbon\Carbon; use Psr\Log\LoggerInterface; @@ -11,7 +12,6 @@ use Spiral\Queue\Exception\RetryException; use Spiral\Queue\JobHandler; use Spiral\Queue\Options; -use Spiral\RoadRunner\Metrics\MetricsInterface; final class TwitterDataCollector extends JobHandler { @@ -19,7 +19,7 @@ final class TwitterDataCollector extends JobHandler * @throws RetryException */ public function invoke( - MetricsInterface $metrics, + TwitterMetrics $metrics, LoggerInterface $logger, ClientInterface $client, ExceptionReporterInterface $reporter, @@ -38,8 +38,13 @@ public function invoke( try { $user = $client->user($userId); - $metrics->set('twitter_followers', (float)$user->metrics->followers, [$user->username]); - $metrics->set('twitter_tweets', (float)$user->metrics->tweets, [$user->username]); + $metrics->setFollowers( + (float)$user->metrics->followers, $user->username + ); + + $metrics->setTweets( + (float)$user->metrics->tweets, $user->username + ); // Get tweets from the last 2 days $tweets = $client->timeline( @@ -49,12 +54,11 @@ public function invoke( // Collect metrics for each found tweet foreach ($tweets->tweets as $tweet) { - $metrics->set('twitter_tweet_retweet_count', $tweet->retweetCount, [$user->username, $tweet->id]); - $metrics->set('twitter_tweet_reply_count', $tweet->replyCount, [$user->username, $tweet->id]); - $metrics->set('twitter_tweet_like_count', $tweet->likeCount, [$user->username, $tweet->id]); - $metrics->set('twitter_tweet_quote_count', $tweet->quoteCount, [$user->username, $tweet->id]); + $metrics->setRetweetsCount($tweet->retweetCount, $user->username, $tweet->id,); + $metrics->setRepliesCount($tweet->replyCount, $user->username, $tweet->id,); + $metrics->setLikesCount($tweet->likeCount, $user->username, $tweet->id,); + $metrics->setQuotesCount($tweet->quoteCount, $user->username, $tweet->id); } - } catch (\Throwable $e) { $reporter->report($e); diff --git a/app/src/Application/Kernel.php b/app/src/Application/Kernel.php index 113a699..576cbf3 100644 --- a/app/src/Application/Kernel.php +++ b/app/src/Application/Kernel.php @@ -39,11 +39,9 @@ protected function defineBootloaders(): array Bootloader\ExceptionHandlerBootloader::class, // RoadRunner - RoadRunnerBridge\GRPCBootloader::class, RoadRunnerBridge\MetricsBootloader::class, RoadRunnerBridge\QueueBootloader::class, RoadRunnerBridge\LoggerBootloader::class, - RoadRunnerBridge\CacheBootloader::class, // Core Services Framework\SnapshotsBootloader::class, diff --git a/app/src/Application/Metrics/TwitterMetrics.php b/app/src/Application/Metrics/TwitterMetrics.php new file mode 100644 index 0000000..61d1eac --- /dev/null +++ b/app/src/Application/Metrics/TwitterMetrics.php @@ -0,0 +1,70 @@ +metrics->set( + TwitterCollectors::FOLLOWERS, + $count, + [$username] + ); + } + + public function setTweets(float $count, string $username): void + { + $this->metrics->set( + TwitterCollectors::TWEETS, + $count, + [$username] + ); + } + + public function setRetweetsCount(float $count, string $username, string $tweetId): void + { + $this->metrics->set( + TwitterCollectors::RETWEETS_COUNT, + $count, + [$username, $tweetId] + ); + } + + public function setRepliesCount(float $count, string $username, string $tweetId): void + { + $this->metrics->set( + TwitterCollectors::REPLIES_COUNT, + $count, + [$username, $tweetId] + ); + } + + public function setLikesCount(float $count, string $username, string $tweetId): void + { + $this->metrics->set( + TwitterCollectors::LIKES_COUNT, + $count, + [$username, $tweetId] + ); + } + + public function setQuotesCount(float $count, string $username, string $tweetId): void + { + $this->metrics->set( + TwitterCollectors::QUOTES_COUNT, + $count, + [$username, $tweetId] + ); + } +} diff --git a/app/src/Application/Repository/AccountCacheRepository.php b/app/src/Application/Repository/AccountCacheRepository.php deleted file mode 100644 index f5f6503..0000000 --- a/app/src/Application/Repository/AccountCacheRepository.php +++ /dev/null @@ -1,27 +0,0 @@ -cache = $provider->storage(self::STORAGE_NAME); - } - - public function all(): array - { - return $this->cache->get(self::CACHE_KEY) ?? []; - } -} diff --git a/app/src/Application/Repository/AccountConfigRepository.php b/app/src/Application/Repository/AccountConfigRepository.php index d4adbf7..83c9c7c 100644 --- a/app/src/Application/Repository/AccountConfigRepository.php +++ b/app/src/Application/Repository/AccountConfigRepository.php @@ -9,7 +9,7 @@ final class AccountConfigRepository implements AccountRepositoryInterface { public function __construct( - private TwitterConfig $config, + private readonly TwitterConfig $config, ) { } diff --git a/app/src/Application/TwitterCollectors.php b/app/src/Application/TwitterCollectors.php index 95bd97a..7164556 100644 --- a/app/src/Application/TwitterCollectors.php +++ b/app/src/Application/TwitterCollectors.php @@ -9,29 +9,36 @@ final class TwitterCollectors implements CollectorsInterface { + public const FOLLOWERS = 'twitter_followers'; + public const TWEETS = 'twitter_tweets'; + public const RETWEETS_COUNT = 'twitter_tweet_retweet_count'; + public const REPLIES_COUNT = 'twitter_tweet_reply_count'; + public const LIKES_COUNT = 'twitter_tweet_like_count'; + public const QUOTES_COUNT = 'twitter_tweet_quote_count'; + public function getIterator(): \Traversable { - yield 'twitter_followers' => Collector::gauge() + yield self::FOLLOWERS => Collector::gauge() ->withHelp('Twitter followers.') ->withLabels('username'); - yield 'twitter_tweets' => Collector::gauge() + yield self::TWEETS => Collector::gauge() ->withHelp('Twitter tweets.') ->withLabels('username'); - yield 'twitter_tweet_retweet_count' => Collector::gauge() + yield self::RETWEETS_COUNT => Collector::gauge() ->withHelp('Twitter tweet retweet statistics.') ->withLabels('username', 'id'); - yield 'twitter_tweet_reply_count' => Collector::gauge() + yield self::REPLIES_COUNT => Collector::gauge() ->withHelp('Twitter tweet reply statistics.') ->withLabels('username', 'id'); - yield 'twitter_tweet_like_count' => Collector::gauge() + yield self::LIKES_COUNT => Collector::gauge() ->withHelp('Twitter likes reply statistics.') ->withLabels('username', 'id'); - yield 'twitter_tweet_quote_count' => Collector::gauge() + yield self::QUOTES_COUNT => Collector::gauge() ->withHelp('Twitter likes quote statistics.') ->withLabels('username', 'id'); } diff --git a/docker/Dockerfile b/docker/Dockerfile index 3b45e3c..4ba8756 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -27,7 +27,7 @@ EXPOSE 9001/tcp EXPOSE 2112/tcp LABEL org.opencontainers.image.source=$REPOSITORY -LABEL org.opencontainers.image.description="MetrixIO Twitter" +LABEL org.opencontainers.image.description="Twitter metrics collector - metrix.io" LABEL org.opencontainers.image.licenses=MIT CMD ./rr serve diff --git a/proto/message.proto b/proto/message.proto deleted file mode 100644 index 3889200..0000000 --- a/proto/message.proto +++ /dev/null @@ -1,30 +0,0 @@ -syntax = "proto3"; - -package twitter.dto; - -option php_namespace = "GRPC\\Twitter"; -option php_metadata_namespace = "GRPC\\Twitter\\GPBMetadata"; - -message AddRequest { - string id = 1; -} - -message AddResponse { - bool status = 1; -} - -message RemoveRequest { - string id = 1; -} - -message RemoveResponse { - bool status = 1; -} - -message AllRequest { - -} - -message AllResponse { - repeated string ids = 1; -} diff --git a/proto/service.proto b/proto/service.proto deleted file mode 100644 index d0725dc..0000000 --- a/proto/service.proto +++ /dev/null @@ -1,22 +0,0 @@ -syntax = "proto3"; - -package twitter; - -option php_namespace = "GRPC\\Twitter"; -option php_metadata_namespace = "GRPC\\Twitter\\GPBMetadata"; - -import "message.proto"; - -service TwitterService { - // Add an account ID to collect metrics - rpc Add (twitter.dto.AddRequest) returns (twitter.dto.AddResponse) { - } - - // Remove an existing account ID - rpc Remove (twitter.dto.RemoveRequest) returns (twitter.dto.RemoveResponse) { - } - - // Show all available accounts - rpc All (twitter.dto.AllRequest) returns (twitter.dto.AllResponse) { - } -} diff --git a/protoc-gen-php-grpc b/protoc-gen-php-grpc deleted file mode 100755 index d14d63d..0000000 Binary files a/protoc-gen-php-grpc and /dev/null differ