Files
librenms-librenms/app/Models/Location.php

151 lines
4.1 KiB
PHP
Raw Normal View History

Refactored and update Location Geocoding (#9359) - Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls) - Parse coordinates from the location more consistently - Add settings to webui - ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~ - Google Maps, Bing, Mapquest, and OpenStreetMap supported initially. - Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though. - All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate) - Update all (I think) sql queries to handle the new structure - Remove final vestiges of override_sysLocation as a device attribute - Update existing device groups and rules in DB - Tested all APIs with good/bad location, no/bad/good key, and no connection. - Cannot fix advanced queries that use location This blocks #8868 DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926` After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
<?php
/**
* Location.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Models;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
public $fillable = ['location', 'lat', 'lng'];
const CREATED_AT = null;
const UPDATED_AT = 'timestamp';
private $location_regex = '/\[\s*(?<lat>[-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?))\s*,\s*(?<lng>[-+]?(?:180(?:\.0+)?|(?:(?:1[0-7]\d)|(?:[1-9]?\d))(?:\.\d+)?))\s*\]/';
/**
* Set up listeners for this Model
*/
public static function boot()
{
parent::boot();
static::creating(function (Location $location) {
// parse coordinates for new locations
$location->lookupCoordinates();
});
}
// ---- Helper Functions ----
/**
* Checks if this location has resolved latitude and longitude.
*
* @return bool
*/
public function hasCoordinates()
{
return !(is_null($this->lat) || is_null($this->lng));
}
/**
* Check if the coordinates are valid
* Even though 0,0 is a valid coordinate, we consider it invalid for ease
*/
public function coordinatesValid()
{
return $this->lat && $this->lng &&
abs($this->lat) <= 90 && abs($this->lng) <= 180;
}
/**
* Try to parse coordinates then
* call geocoding API to resolve latitude and longitude.
*/
public function lookupCoordinates()
{
if (!$this->hasCoordinates() && $this->location) {
$this->parseCoordinates();
if (!$this->hasCoordinates() &&
\LibreNMS\Config::get('geoloc.latlng', true) &&
(!$this->id || $this->timestamp && $this->timestamp->diffInDays() > 2)
Refactored and update Location Geocoding (#9359) - Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls) - Parse coordinates from the location more consistently - Add settings to webui - ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~ - Google Maps, Bing, Mapquest, and OpenStreetMap supported initially. - Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though. - All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate) - Update all (I think) sql queries to handle the new structure - Remove final vestiges of override_sysLocation as a device attribute - Update existing device groups and rules in DB - Tested all APIs with good/bad location, no/bad/good key, and no connection. - Cannot fix advanced queries that use location This blocks #8868 DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926` After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
) {
$this->fetchCoordinates();
$this->updateTimestamps();
}
}
}
/**
* Remove encoded GPS for nicer display
*
* @return string
*/
public function display()
{
return trim(preg_replace($this->location_regex, '', $this->location)) ?: $this->location;
}
protected function parseCoordinates()
{
if (preg_match($this->location_regex, $this->location, $parsed)) {
$this->fill($parsed);
}
}
protected function fetchCoordinates()
{
try {
/** @var \LibreNMS\Interfaces\Geocoder $api */
$api = app(\LibreNMS\Interfaces\Geocoder::class);
$this->fill($api->getCoordinates($this->location));
} catch (BindingResolutionException $e) {
// could not resolve geocoder, Laravel isn't booted. Fail silently.
}
}
// ---- Query scopes ----
/**
* @param Builder $query
* @param User $user
* @return Builder
*/
public function scopeHasAccess($query, $user)
{
if ($user->hasGlobalRead()) {
return $query;
}
$ids = Device::hasAccess($user)
->distinct()
->whereNotNull('location_id')
->pluck('location_id');
return $query->whereIn('id', $ids);
}
// ---- Define Relationships ----
public function devices()
{
return $this->hasMany('App\Models\Device', 'location_id');
}
}