Skip to content

Instantly share code, notes, and snippets.

@LosantGists
LosantGists / icon-window-html.js
Created July 26, 2023 18:39
map icon window HTML
const iconWindowHTML = (data, iconsData) => {
const iconWindowElement = document.createElement('div');
iconWindowElement.classList.add('card', 'marker-window');
const iconsHTML = Object.values(iconsData).map(iconData => {
if(!iconData?.icon){
return '';
}
return `
<div class="d-flex flex-column align-items-center mr-1">
@LosantGists
LosantGists / icon-html.js
Created July 26, 2023 18:38
map icon HTML
// html for the marker
const iconHTML = (attr,percent,value,colors,unit) => {
if(attr === "battery"){
return `
<div title="${value}${unit}" class="marker">
<svg xmlns="http://www.w3.org/2000/svg" width="15" height="30" viewBox="0 0 55 120">
<rect x="10" y="7" width="45" height="102" stroke="black" fill="white" stroke-width="5"/>
<rect x="12" y="${107-percent}" width="40" height="${percent + 1}" stroke="black" fill="${colors.background}" stroke-width="0"/>
<rect x="12" y="30" width="20" height="4" fill="black" />
@LosantGists
LosantGists / build-map-icon.js
Created July 26, 2023 18:37
build map icon
const buildIcon = (data,attr) => {
const value = data.attributeValues?.[attr];
const meta = data.attributes?.find(a => a.name === attr);
if(!value || !meta){
throw new TypeError('Missing values'); // value at this point is string, so "0" won't fail
}
const min = meta.attributeTags?.min ? Number(meta.attributeTags.min) : 0;
@LosantGists
LosantGists / marker-setup.js
Created July 26, 2023 18:37
map marker setup
// set up individual marker
const setUpMarker = (id, data) => {
if(!data.attributeValues?.location){
return false;
}
// Pull marker from dictionary, or create new
let marker = markers[id];
@LosantGists
LosantGists / map-globals.js
Created July 26, 2023 18:36
Sets up variables and listeners
// Excerpt from https://github.com/Losant/google-map-advanced-markers/blob/main/custom-head-content.html
// Initialize globals
const markers = {};
let devices = [];
let map;
let numSelected = 0;
let AdvancedMarkerElement;
let bounds;
let selectedAttribute = $('#attribute-selector').val() || 'battery';
@LosantGists
LosantGists / exportsummary.py
Created July 23, 2021 19:31
Automating Unit Tests Blog (13) 7.23.21
def export_summary(summary_series, file_name=SUMMARY_OUTPUT_FILE):
directory = get_output_directory()
output = pd.DataFrame(summary_series).transpose()
output.rename(columns={"25%": "25th", "50%": "50th", "75%": "75th"},inplace=True)
output.to_csv(os.path.join(directory, file_name),index=False)
@LosantGists
LosantGists / test_remove_consecutive_same_values.py
Created July 23, 2021 19:30
Automating Unit Tests Blog (12) 7.23.21
import pandas as pd
import importlib
nb = importlib.import_module("ipynb.fs.defs.battery-stats")
remove_consecutive_same_values = nb.remove_consecutive_same_values
def build_battery_history(values, device_id="60df87b4be3fc900069dac1f", start_time=1600000000000):
id_array = [device_id] * len(values)
timestamp_array = map(lambda t: start_time + t * 10000, range(len(values)))
@LosantGists
LosantGists / test_is_row_before_charging.py
Created July 23, 2021 19:28
Automating Unit Tests Blog (11) 7.23.21
import pandas as pd
import importlib
nb = importlib.import_module("ipynb.fs.defs.battery-stats")
is_row_before_charging = nb.is_row_before_charging
# it should return True if the change from the previous row is positive (isn't charging)
# and the change to the next row is negative (will be charging)
about_to_charge = pd.DataFrame({
@LosantGists
LosantGists / test_get_rows_before_charging.py
Created July 23, 2021 19:27
Automating Unit Tests Blog (10) 7.23.21
import pandas as pd
import importlib
nb = importlib.import_module("ipynb.fs.defs.battery-stats")
get_rows_before_charging = nb.get_rows_before_charging
def build_battery_history(values, device_id="60df87b4be3fc900069dac1f", start_time=1600000000000):
id_array = [device_id] * len(values)
timestamp_array = map(lambda t: start_time + t * 10000, range(len(values)))
return pd.DataFrame({
@LosantGists
LosantGists / removeconsecutiverows.py
Created July 23, 2021 19:26
Automating Unit Tests Blog (9) 7.23.21
def is_row_before_charging(row):
return (row.battery_loss_next_row < 0) and (row.battery_loss_this_row > 0)
def remove_consecutive_same_values(df):
df['is_new_value'] = df.groupby(['ID'])['battery'].shift(1) != df['battery']
return df.loc[df['is_new_value']]
def get_rows_before_charging(df):
# sorting not necessary, but is much easier to follow during dev
# df.sort_values(by=['ID','Timestamp'],inplace=True)