|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rubix\Server\Providers; |
| 4 | + |
| 5 | +use Rubix\ML\Estimator; |
| 6 | +use Rubix\ML\Learner; |
| 7 | +use Rubix\ML\Probabilistic; |
| 8 | +use Rubix\ML\Ranking; |
| 9 | +use Rubix\Server\CommandBus; |
| 10 | +use Rubix\Server\Http\Controllers\PredictionsController; |
| 11 | +use Rubix\Server\Http\Controllers\SamplePredictionController; |
| 12 | +use Rubix\Server\Http\Controllers\ProbabilitiesController; |
| 13 | +use Rubix\Server\Http\Controllers\SampleProbabilitiesController; |
| 14 | +use Rubix\Server\Http\Controllers\QueryModelController; |
| 15 | +use Rubix\Server\Http\Controllers\ScoresController; |
| 16 | +use Rubix\Server\Http\Controllers\SampleScoreController; |
| 17 | +use Rubix\Server\Http\Controllers\ServerStatusController; |
| 18 | +use FastRoute\RouteCollector; |
| 19 | +use FastRoute\RouteParser\Std; |
| 20 | +use FastRoute\DataGenerator\GroupCountBased as GroupCountBasedDataGenerator; |
| 21 | +use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher; |
| 22 | +use FastRoute\Dispatcher; |
| 23 | + |
| 24 | +class RouterProvider |
| 25 | +{ |
| 26 | + public const MODEL_PREFIX = '/model'; |
| 27 | + |
| 28 | + public const SERVER_PREFIX = '/server'; |
| 29 | + |
| 30 | + public const PREDICT_ENDPOINT = '/predictions'; |
| 31 | + |
| 32 | + public const PREDICT_SAMPLE_ENDPOINT = '/sample_prediction'; |
| 33 | + |
| 34 | + public const PROBA_ENDPOINT = '/probabilities'; |
| 35 | + |
| 36 | + public const PROBA_SAMPLE_ENDPOINT = '/sample_probabilities'; |
| 37 | + |
| 38 | + public const SCORE_ENDPOINT = '/scores'; |
| 39 | + |
| 40 | + public const SCORE_SAMPLE_ENDPOINT = '/sample_score'; |
| 41 | + |
| 42 | + public const SERVER_STATUS_ENDPOINT = '/status'; |
| 43 | + |
| 44 | + /** |
| 45 | + * The command bus. |
| 46 | + * |
| 47 | + * @var \Rubix\ML\Estimator |
| 48 | + */ |
| 49 | + protected $estimator; |
| 50 | + |
| 51 | + /** |
| 52 | + * The command bus. |
| 53 | + * |
| 54 | + * @var \Rubix\Server\CommandBus |
| 55 | + */ |
| 56 | + protected $bus; |
| 57 | + |
| 58 | + /** |
| 59 | + * Static factory for method chaining. |
| 60 | + * |
| 61 | + * @param \Rubix\ML\Estimator $estimator |
| 62 | + * @param \Rubix\Server\CommandBus $bus |
| 63 | + * @return self |
| 64 | + */ |
| 65 | + public static function with(Estimator $estimator, CommandBus $bus) : self |
| 66 | + { |
| 67 | + return new self($estimator, $bus); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param \Rubix\ML\Estimator $estimator |
| 72 | + * @param \Rubix\Server\CommandBus $bus |
| 73 | + */ |
| 74 | + public function __construct(Estimator $estimator, CommandBus $bus) |
| 75 | + { |
| 76 | + $this->estimator = $estimator; |
| 77 | + $this->bus = $bus; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return \FastRoute\Dispatcher |
| 82 | + */ |
| 83 | + public function boot() : Dispatcher |
| 84 | + { |
| 85 | + $collector = new RouteCollector(new Std(), new GroupCountBasedDataGenerator()); |
| 86 | + |
| 87 | + $collector->get(self::MODEL_PREFIX, new QueryModelController($this->bus)); |
| 88 | + |
| 89 | + $collector->addGroup( |
| 90 | + self::MODEL_PREFIX, |
| 91 | + function ($group) { |
| 92 | + $group->post( |
| 93 | + self::PREDICT_ENDPOINT, |
| 94 | + new PredictionsController($this->bus) |
| 95 | + ); |
| 96 | + |
| 97 | + if ($this->estimator instanceof Learner) { |
| 98 | + $group->post( |
| 99 | + self::PREDICT_SAMPLE_ENDPOINT, |
| 100 | + new SamplePredictionController($this->bus) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + if ($this->estimator instanceof Probabilistic) { |
| 105 | + $group->post( |
| 106 | + self::PROBA_ENDPOINT, |
| 107 | + new ProbabilitiesController($this->bus) |
| 108 | + ); |
| 109 | + |
| 110 | + $group->post( |
| 111 | + self::PROBA_SAMPLE_ENDPOINT, |
| 112 | + new SampleProbabilitiesController($this->bus) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + if ($this->estimator instanceof Ranking) { |
| 117 | + $group->post( |
| 118 | + self::SCORE_ENDPOINT, |
| 119 | + new ScoresController($this->bus) |
| 120 | + ); |
| 121 | + |
| 122 | + $group->post( |
| 123 | + self::SCORE_SAMPLE_ENDPOINT, |
| 124 | + new SampleScoreController($this->bus) |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + ); |
| 129 | + |
| 130 | + $collector->addGroup( |
| 131 | + self::SERVER_PREFIX, |
| 132 | + function ($group) { |
| 133 | + $group->get( |
| 134 | + self::SERVER_STATUS_ENDPOINT, |
| 135 | + new ServerStatusController($this->bus) |
| 136 | + ); |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + return new GroupCountBasedDispatcher($collector->getData()); |
| 141 | + } |
| 142 | +} |
0 commit comments