mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Rehash paths and add tests
This commit is contained in:
parent
1580fb8ade
commit
b2e6a141fc
7 changed files with 89 additions and 13 deletions
15
app/controllers/api/v1/maps/tile_usage_controller.rb
Normal file
15
app/controllers/api/v1/maps/tile_usage_controller.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::Maps::TileUsageController < ApiController
|
||||
def create
|
||||
Maps::TileUsage::Track.new(tile_usage_params[:count].to_i).call
|
||||
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def tile_usage_params
|
||||
params.require(:tile_usage).permit(:count)
|
||||
end
|
||||
end
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::TileUsagesController < ApiController
|
||||
def create
|
||||
TileUsage::Track.new(params[:tile_count].to_i).call
|
||||
|
||||
head :ok
|
||||
end
|
||||
end
|
||||
|
|
@ -40,14 +40,16 @@ export class TileMonitor {
|
|||
const currentCount = this.tileQueue;
|
||||
console.log('Sending tile usage batch:', currentCount);
|
||||
|
||||
fetch('/api/v1/tile_usages', {
|
||||
fetch('/api/v1/maps/tile_usage', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.apiKey}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
tile_count: currentCount
|
||||
tile_usage: {
|
||||
count: currentCount
|
||||
}
|
||||
})
|
||||
})
|
||||
.then(response => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TileUsage::Track
|
||||
class Maps::TileUsage::Track
|
||||
def initialize(count = 1)
|
||||
@count = count
|
||||
end
|
||||
|
|
@ -97,7 +97,9 @@ Rails.application.routes.draw do
|
|||
end
|
||||
end
|
||||
|
||||
resources :tile_usages, only: [:create]
|
||||
namespace :maps do
|
||||
resources :tile_usage, only: [:create]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
37
spec/requests/api/v1/maps/tile_usage_spec.rb
Normal file
37
spec/requests/api/v1/maps/tile_usage_spec.rb
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Maps::TileUsage', type: :request do
|
||||
describe 'POST /api/v1/maps/tile_usage' do
|
||||
let(:tile_count) { 5 }
|
||||
let(:track_service) { instance_double(Maps::TileUsage::Track) }
|
||||
|
||||
before do
|
||||
allow(Maps::TileUsage::Track).to receive(:new).with(tile_count).and_return(track_service)
|
||||
allow(track_service).to receive(:call)
|
||||
end
|
||||
|
||||
context 'when user is authenticated' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
it 'tracks tile usage' do
|
||||
post '/api/v1/maps/tile_usage',
|
||||
params: { tile_usage: { count: tile_count } },
|
||||
headers: { 'Authorization' => "Bearer #{user.api_key}" }
|
||||
|
||||
expect(Maps::TileUsage::Track).to have_received(:new).with(tile_count)
|
||||
expect(track_service).to have_received(:call)
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is not authenticated' do
|
||||
it 'returns unauthorized' do
|
||||
post '/api/v1/maps/tile_usage', params: { tile_usage: { count: tile_count } }
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
29
spec/services/maps/tile_usage/track_spec.rb
Normal file
29
spec/services/maps/tile_usage/track_spec.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
require 'prometheus_exporter/client'
|
||||
|
||||
RSpec.describe Maps::TileUsage::Track do
|
||||
describe '#call' do
|
||||
subject(:track) { described_class.new(tile_count).call }
|
||||
|
||||
let(:tile_count) { 5 }
|
||||
let(:prometheus_client) { instance_double(PrometheusExporter::Client) }
|
||||
|
||||
before do
|
||||
allow(PrometheusExporter::Client).to receive(:default).and_return(prometheus_client)
|
||||
end
|
||||
|
||||
it 'tracks tile usage' do
|
||||
expect(prometheus_client).to receive(:send_json).with(
|
||||
{
|
||||
type: 'counter',
|
||||
name: 'dawarich_map_tiles',
|
||||
value: tile_count
|
||||
}
|
||||
)
|
||||
|
||||
track
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue