<commit_before><template>
    <main>
        <h1>Marta</h1>
        <h2 v-show="position.lon !== 0">{{ position.lon }}</h2>
        <h2 v-show="position.lat !== 0">{{ position.lat }}</h2>
        <h2 v-show="position.lowerBoundLatitude !== 0">{{ position.lowerBoundLatitude }}</h2>
        <h2 v-show="position.lowerBoundLongitude !== 0">{{ position.lowerBoundLongitude }}</h2>
        <h2 v-show="position.upperBoundLatitude !== 0">{{ position.upperBoundLatitude }}</h2>
        <h2 v-show="position.upperBoundLongitude !== 0">{{ position.upperBoundLongitude }}</h2>
        <ul v-show="stationsNearby.length">
            <li
                v-for="station in stationsNearby"
                :key="station.stop_id"
            >
                {{ station.stop_name }}
            </li>
        </ul>
        <Bus/>
        <Train/>
    </main>
</template>

<script>
import axios from "axios";
import Bus from "@/components/Bus";
import Train from "@/components/Train";

export default {
    name: "Marta",
    components: {
        Bus,
        Train
    },
    data() {
        return {
            searchRadius: 0.21,
            stations: [],
            stationsNearby: [],
            position: {
                lat: 0,
                lon: 0,
                lowerBoundLatitude: 0,
                upperBoundLatitude: 0,
                lowerBoundLongitude: 0,
                upperBoundLongitude: 0
            }
        };
    },
    watch: {
        position: {
            handler: "getStations",
            deep: true
        },
        stations: {
            handler: "getNearbyStations",
            deep: true
        }
    },
    created() {
        this.getPosition()
            .then(position => {
                console.log(position);
                this.position.lat = position.coords.latitude;
                this.position.lon = position.coords.longitude;

                // http://www.arubin.org/files/geo_search.pdf
                // Create a search area with center as user's position coordinates
                // and radius equal to search radius within a bounding rectangle
                // defined by latitudes and longitudes around the user's position
                this.position.lowerBoundLatitude = position.coords.latitude - this.searchRadius / 69;
                this.position.upperBoundLatitude = position.coords.latitude + this.searchRadius / 69;
                this.position.lowerBoundLongitude = position.coords.longitude - this.searchRadius / Math.abs(Math.cos(Math.PI / 180 * position.coords.latitude) * 69);
                this.position.upperBoundLongitude = position.coords.longitude + this.searchRadius / Math.abs(Math.cos(Math.PI / 180 * position.coords.latitude) * 69);
            }).catch(error => {
                throw error.message;
            });
    },
    methods: {
        getPosition() {
            const options = {
                enableHighAccuracy: true,
                timeout: 9000,
                maximumAge: 0
            };

            return new Promise((resolve, reject) => {
                if (!navigator.geolocation) {
                    reject("Geolocation is not supported in your browser.");
                } else {
                    navigator.geolocation.getCurrentPosition(resolve, reject, options);
                }
            });
        },

        getStations(position) {
            const query = `select * from stops where stop_lon between ${position.lowerBoundLongitude} and ${position.upperBoundLongitude} and stop_lat between ${position.lowerBoundLatitude} and ${position.upperBoundLatitude} order by stop_lat`;

            axios.get("/api", {
                params: {
                    sql: query
                }
            }).then(response => {
                console.log(response);
                if (response.status === 200) {
                    this.stations = response.data.rows;
                }
            }).catch(error => {
                throw error;
            });
        },

        getNearbyStations(stations) {
            this.stationsNearby = stations.reduce((filteredStations, station) => {
                const isNear = this.withinReach(station[3], station[4]);
                if (isNear) {
                    filteredStations.push({
                        stop_id: station[0],
                        stop_name: station[2],
                        stop_lat: station[3],
                        stop_lon: station[4],
                        stop_dist: this.stopDistance(station[3], station[4])
                    });
                }
                return filteredStations;
            }, []);
        },

        withinReach(lat, lon) {
            return (this.stopDistance(lat, lon) <= this.searchRadius);
        },

        stopDistance(lat, lon) {
            return +(3956 * 2 * Math.asin(Math.sqrt(Math.sin((this.position.lat - Math.abs(lat)) * Math.PI / 180 / 2) ** 2 + Math.cos(this.position.lat * Math.PI / 180) * Math.cos(Math.abs(lat) * Math.PI / 180) * Math.sin((this.position.lon - lon) * Math.PI / 180 / 2) ** 2))).toFixed(2);
        }
    }
};
</script>
<commit_msg>Use Promises and remove Watchers<commit_after><template>
    <main>
        <h1>Marta</h1>
        <h2 v-show="position.longitude !== 0">{{ position.longitude }}</h2>
        <h2 v-show="position.latitude !== 0">{{ position.latitude }}</h2>
        <h2 v-show="position.lowerBoundLatitude !== 0">{{ position.lowerBoundLatitude }}</h2>
        <h2 v-show="position.lowerBoundLongitude !== 0">{{ position.lowerBoundLongitude }}</h2>
        <h2 v-show="position.upperBoundLatitude !== 0">{{ position.upperBoundLatitude }}</h2>
        <h2 v-show="position.upperBoundLongitude !== 0">{{ position.upperBoundLongitude }}</h2>
        <ul v-show="stopsNearby.length">
            <li
                v-for="stop in stopsNearby"
                :key="stop.stop_id"
            >
                {{ stop.stop_name }}
            </li>
        </ul>
        <Bus/>
        <Train/>
    </main>
</template>

<script>
import axios from "axios";
import Bus from "@/components/Bus";
import Train from "@/components/Train";

export default {
    name: "Marta",
    components: {
        Bus,
        Train
    },
    data() {
        return {
            searchRadius: 0.21,
            stops: [],
            stopsNearby: [],
            position: {
                latitude: 0,
                longitude: 0,
                lowerBoundLatitude: 0,
                upperBoundLatitude: 0,
                lowerBoundLongitude: 0,
                upperBoundLongitude: 0
            }
        };
    },
    created() {
        this.getPosition()
            .then(this.getStops)
            .then(this.getStopsInsideSearchRadius);
    },
    methods: {
        getPosition() {
            const options = {
                enableHighAccuracy: true,
                timeout: 9000,
                maximumAge: 0
            };

            return new Promise((resolve, reject) => {
                if (!navigator.geolocation) {
                    reject("Geolocation is not supported in your browser.");
                } else {
                    navigator.geolocation.getCurrentPosition(
                        position => {
                            resolve([position.coords.latitude, position.coords.longitude]);
                        },
                        error => {
                            reject(`${error.message}: Unable to retrieve your location`);
                        },
                        options
                    );
                }
            });
        },

        getStops(coords) {
            // http://www.arubin.org/files/geo_search.pdf
            // Create a search area with center as user's position coordinates
            // and radius equal to search radius within a bounding rectangle
            // defined by latitudes and longitudes around the user's position
            this.position.latitude = coords[0];
            this.position.longitude = coords[1];
            this.position.lowerBoundLatitude = coords[0] - this.searchRadius / 69;
            this.position.upperBoundLatitude = coords[0] + this.searchRadius / 69;
            this.position.lowerBoundLongitude = coords[1] - this.searchRadius / Math.abs(Math.cos(Math.PI / 180 * coords[0]) * 69);
            this.position.upperBoundLongitude = coords[1] + this.searchRadius / Math.abs(Math.cos(Math.PI / 180 * coords[0]) * 69);
            console.log(coords, this.position);

            const query = `select * from stops where stop_lon between ${this.position.lowerBoundLongitude} and ${this.position.upperBoundLongitude} and stop_lat between ${this.position.lowerBoundLatitude} and ${this.position.upperBoundLatitude} order by stop_lat`;

            return new Promise((resolve, reject) => {
                axios.get("/api", {
                    params: {
                        sql: query
                    }
                }).then(response => {
                    console.log(response);
                    if (response.status === 200) {
                        resolve(response.data.rows);
                    }
                }).catch(error => {
                    reject(error);
                });
            });
        },

        getStopsInsideSearchRadius(stops) {
            this.stops = stops;
            this.stopsNearby = stops.reduce((filteredStops, stop) => {
                const isNear = this.isInsideSearchRadius(stop[3], stop[4]);
                if (isNear) {
                    filteredStops.push({
                        stop_id: stop[0],
                        stop_name: stop[2],
                        stop_lat: stop[3],
                        stop_lon: stop[4],
                        stop_dist: this.stopDistance(stop[3], stop[4])
                    });
                }
                return filteredStops;
            }, []);
        },

        isInsideSearchRadius(lat, lon) {
            return (this.stopDistance(lat, lon) <= this.searchRadius);
        },

        stopDistance(lat, lon) {
            return +(3956 * 2 * Math.asin(Math.sqrt(Math.sin((this.position.latitude - Math.abs(lat)) * Math.PI / 180 / 2) ** 2 + Math.cos(this.position.latitude * Math.PI / 180) * Math.cos(Math.abs(lat) * Math.PI / 180) * Math.sin((this.position.longitude - lon) * Math.PI / 180 / 2) ** 2))).toFixed(2);
        }
    }
};
</script>

{'fraction_non_alphanumeric': 0.12470023980815348, 'fraction_numerical': 0.01630695443645084, 'mean_word_length': 1.497843794920939, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '739144', 'n_tokens_mistral': 3011, 'n_tokens_neox': 2762, 'n_words': 557}