@adventurelabs/scout-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -0
- package/dist/api_keys/actions.d.ts +2 -0
- package/dist/api_keys/actions.js +20 -0
- package/dist/api_keys/index.d.ts +1 -0
- package/dist/api_keys/index.js +1 -0
- package/dist/constants/annotator.d.ts +1 -0
- package/dist/constants/annotator.js +11 -0
- package/dist/constants/app.d.ts +2 -0
- package/dist/constants/app.js +2 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.js +2 -0
- package/dist/helpers/auth.d.ts +1 -0
- package/dist/helpers/auth.js +8 -0
- package/dist/helpers/bounding_boxes.d.ts +5 -0
- package/dist/helpers/bounding_boxes.js +43 -0
- package/dist/helpers/db.d.ts +3 -0
- package/dist/helpers/db.js +22 -0
- package/dist/helpers/devices.d.ts +8 -0
- package/dist/helpers/devices.js +92 -0
- package/dist/helpers/email.d.ts +7 -0
- package/dist/helpers/email.js +22 -0
- package/dist/helpers/events.d.ts +6 -0
- package/dist/helpers/events.js +71 -0
- package/dist/helpers/finance.d.ts +9 -0
- package/dist/helpers/finance.js +16 -0
- package/dist/helpers/gps.d.ts +5 -0
- package/dist/helpers/gps.js +11 -0
- package/dist/helpers/herds.d.ts +9 -0
- package/dist/helpers/herds.js +80 -0
- package/dist/helpers/index.d.ts +17 -0
- package/dist/helpers/index.js +17 -0
- package/dist/helpers/location.d.ts +1 -0
- package/dist/helpers/location.js +3 -0
- package/dist/helpers/plans.d.ts +5 -0
- package/dist/helpers/plans.js +61 -0
- package/dist/helpers/tags.d.ts +7 -0
- package/dist/helpers/tags.js +158 -0
- package/dist/helpers/time.d.ts +3 -0
- package/dist/helpers/time.js +11 -0
- package/dist/helpers/ui.d.ts +4 -0
- package/dist/helpers/ui.js +12 -0
- package/dist/helpers/users.d.ts +6 -0
- package/dist/helpers/users.js +66 -0
- package/dist/helpers/web.d.ts +1 -0
- package/dist/helpers/web.js +7 -0
- package/dist/helpers/zones.d.ts +18 -0
- package/dist/helpers/zones.js +108 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/useScoutDbListener.d.ts +1 -0
- package/dist/hooks/useScoutDbListener.js +94 -0
- package/dist/hooks/useScoutRefresh.d.ts +7 -0
- package/dist/hooks/useScoutRefresh.js +45 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +43 -0
- package/dist/providers/ScoutRefreshProvider.d.ts +1 -0
- package/dist/providers/ScoutRefreshProvider.js +8 -0
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.js +1 -0
- package/dist/store/hooks.d.ts +1 -0
- package/dist/store/hooks.js +3 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +1 -0
- package/dist/store/scout.d.ts +103 -0
- package/dist/store/scout.js +196 -0
- package/dist/supabase/client.d.ts +1 -0
- package/dist/supabase/client.js +5 -0
- package/dist/supabase/index.d.ts +3 -0
- package/dist/supabase/index.js +3 -0
- package/dist/supabase/middleware.d.ts +2 -0
- package/dist/supabase/middleware.js +46 -0
- package/dist/supabase/server.d.ts +636 -0
- package/dist/supabase/server.js +22 -0
- package/dist/types/bounding_boxes.d.ts +27 -0
- package/dist/types/bounding_boxes.js +11 -0
- package/dist/types/db.d.ts +42 -0
- package/dist/types/db.js +15 -0
- package/dist/types/gps.d.ts +21 -0
- package/dist/types/gps.js +13 -0
- package/dist/types/herd_module.d.ts +31 -0
- package/dist/types/herd_module.js +82 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +6 -0
- package/dist/types/requests.d.ts +18 -0
- package/dist/types/requests.js +25 -0
- package/dist/types/supabase.d.ts +754 -0
- package/dist/types/supabase.js +25 -0
- package/dist/types/ui.d.ts +7 -0
- package/dist/types/ui.js +8 -0
- package/package.json +52 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { User } from "@supabase/supabase-js";
|
|
2
|
+
import { Database } from "./supabase";
|
|
3
|
+
export type Role = Database["public"]["Enums"]["role"];
|
|
4
|
+
export type DeviceType = Database["public"]["Enums"]["device_type"];
|
|
5
|
+
export type MediaType = Database["public"]["Enums"]["media_type"];
|
|
6
|
+
export type TagObservationType = Database["public"]["Enums"]["tag_observation_type"];
|
|
7
|
+
export type IUser = User;
|
|
8
|
+
export type IDevice = Database["public"]["CompositeTypes"]["device_pretty_location"] & {
|
|
9
|
+
api_keys_scout?: IApiKeyScout[];
|
|
10
|
+
recent_events?: IEventWithTags[];
|
|
11
|
+
};
|
|
12
|
+
export type IEvent = Database["public"]["Tables"]["events"]["Row"];
|
|
13
|
+
export type ITag = Database["public"]["Tables"]["tags"]["Row"];
|
|
14
|
+
export type IPlan = Database["public"]["Tables"]["plans"]["Row"];
|
|
15
|
+
export type IAction = Database["public"]["Tables"]["actions"]["Row"];
|
|
16
|
+
export type IZone = Database["public"]["Tables"]["zones"]["Row"];
|
|
17
|
+
export type IUserRolePerHerd = Database["public"]["Tables"]["users_roles_per_herd"]["Row"];
|
|
18
|
+
export type IHerd = Database["public"]["Tables"]["herds"]["Row"];
|
|
19
|
+
export type IEventWithTags = Database["public"]["CompositeTypes"]["event_with_tags"] & {
|
|
20
|
+
earthranger_url: string | null;
|
|
21
|
+
};
|
|
22
|
+
export type IDevicePrettyLocation = Database["public"]["CompositeTypes"]["device_pretty_location"];
|
|
23
|
+
export type IEventAndTagsPrettyLocation = Database["public"]["CompositeTypes"]["event_and_tags_pretty_location"];
|
|
24
|
+
export type IZonesAndActionsPrettyLocation = Database["public"]["CompositeTypes"]["zones_and_actions_pretty_location"];
|
|
25
|
+
export interface IZoneWithActions extends IZone {
|
|
26
|
+
actions: IAction[];
|
|
27
|
+
}
|
|
28
|
+
export type IUserAndRole = {
|
|
29
|
+
user: {
|
|
30
|
+
id: string;
|
|
31
|
+
username: string | null;
|
|
32
|
+
} | null;
|
|
33
|
+
role: Role;
|
|
34
|
+
};
|
|
35
|
+
export interface IApiKeyScout {
|
|
36
|
+
id: string;
|
|
37
|
+
description: string;
|
|
38
|
+
key: string;
|
|
39
|
+
}
|
|
40
|
+
export type Tag = ITag;
|
|
41
|
+
export type TagClassName = string;
|
|
42
|
+
export declare const DUMMY_EVENT: IEvent;
|
package/dist/types/db.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Dummy event for testing/development
|
|
2
|
+
export const DUMMY_EVENT = {
|
|
3
|
+
id: 0,
|
|
4
|
+
inserted_at: new Date().toISOString(),
|
|
5
|
+
device_id: 0,
|
|
6
|
+
message: "Dummy event",
|
|
7
|
+
media_url: "",
|
|
8
|
+
media_type: "image",
|
|
9
|
+
location: null,
|
|
10
|
+
altitude: 0,
|
|
11
|
+
heading: 0,
|
|
12
|
+
is_public: true,
|
|
13
|
+
timestamp_observation: new Date().toISOString(),
|
|
14
|
+
earthranger_url: null,
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class GpsData {
|
|
2
|
+
id: number;
|
|
3
|
+
timestamp_utc: number;
|
|
4
|
+
latitude: number;
|
|
5
|
+
longitude: number;
|
|
6
|
+
altitude: number;
|
|
7
|
+
constructor(id: number, timestamp_utc: number, latitude: number, longitude: number, altitude: number);
|
|
8
|
+
static fromLocationObject(location: ILocationObject): GpsData;
|
|
9
|
+
}
|
|
10
|
+
export interface IGpsMetadata {
|
|
11
|
+
log_id: number;
|
|
12
|
+
total_distance_km: number;
|
|
13
|
+
max_speed_kph: number;
|
|
14
|
+
average_speed_kph: number;
|
|
15
|
+
max_altitude_m: number;
|
|
16
|
+
}
|
|
17
|
+
export interface ILocationObject {
|
|
18
|
+
latitude: number;
|
|
19
|
+
longitude: number;
|
|
20
|
+
id: number;
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class GpsData {
|
|
2
|
+
constructor(id, timestamp_utc, latitude, longitude, altitude) {
|
|
3
|
+
this.id = id;
|
|
4
|
+
this.timestamp_utc = timestamp_utc;
|
|
5
|
+
this.latitude = latitude;
|
|
6
|
+
this.longitude = longitude;
|
|
7
|
+
this.altitude = altitude;
|
|
8
|
+
}
|
|
9
|
+
// add a method to compose fromm a location object
|
|
10
|
+
static fromLocationObject(location) {
|
|
11
|
+
return new GpsData(location.id, 0, location.latitude, location.longitude, 0);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
2
|
+
import { IDevice, IEventWithTags, IHerd, IPlan, IUserAndRole, IZoneWithActions } from "../types/db";
|
|
3
|
+
export declare class HerdModule {
|
|
4
|
+
herd: IHerd;
|
|
5
|
+
devices: IDevice[];
|
|
6
|
+
events: IEventWithTags[];
|
|
7
|
+
zones: IZoneWithActions[];
|
|
8
|
+
timestamp_last_refreshed: number;
|
|
9
|
+
user_roles: IUserAndRole[] | null;
|
|
10
|
+
events_page_index: number;
|
|
11
|
+
total_events: number;
|
|
12
|
+
total_events_with_filters: number;
|
|
13
|
+
labels: string[];
|
|
14
|
+
plans: IPlan[];
|
|
15
|
+
constructor(herd: IHerd, devices: IDevice[], events: IEventWithTags[], timestamp_last_refreshed: number, user_roles?: IUserAndRole[] | null, events_page_index?: number, total_events?: number, total_events_with_filters?: number, labels?: string[], plans?: IPlan[], zones?: IZoneWithActions[]);
|
|
16
|
+
to_serializable(): IHerdModule;
|
|
17
|
+
static from_herd(herd: IHerd, client: SupabaseClient): Promise<HerdModule>;
|
|
18
|
+
}
|
|
19
|
+
export interface IHerdModule {
|
|
20
|
+
herd: IHerd;
|
|
21
|
+
devices: IDevice[];
|
|
22
|
+
events: IEventWithTags[];
|
|
23
|
+
timestamp_last_refreshed: number;
|
|
24
|
+
user_roles: IUserAndRole[] | null;
|
|
25
|
+
events_page_index: number;
|
|
26
|
+
total_events: number;
|
|
27
|
+
total_events_with_filters: number;
|
|
28
|
+
labels: string[];
|
|
29
|
+
plans: IPlan[];
|
|
30
|
+
zones: IZoneWithActions[];
|
|
31
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LABELS } from "../constants/annotator";
|
|
2
|
+
import { get_devices_by_herd } from "../helpers/devices";
|
|
3
|
+
import { server_get_total_events_by_herd } from "../helpers/events";
|
|
4
|
+
import { server_get_plans_by_herd } from "../helpers/plans";
|
|
5
|
+
import { server_get_events_and_tags_for_device } from "../helpers/tags";
|
|
6
|
+
import { server_get_users_with_herd_access } from "../helpers/users";
|
|
7
|
+
import { EnumWebResponse } from "./requests";
|
|
8
|
+
import { server_get_more_zones_and_actions_for_herd } from "../helpers/zones";
|
|
9
|
+
import { server_list_api_keys } from "../api_keys/actions";
|
|
10
|
+
export class HerdModule {
|
|
11
|
+
constructor(herd, devices, events, timestamp_last_refreshed, user_roles = null, events_page_index = 0, total_events = 0, total_events_with_filters = 0, labels = [], plans = [], zones = []) {
|
|
12
|
+
this.user_roles = null;
|
|
13
|
+
this.events_page_index = 0;
|
|
14
|
+
this.total_events = 0;
|
|
15
|
+
this.total_events_with_filters = 0;
|
|
16
|
+
this.labels = [];
|
|
17
|
+
this.plans = [];
|
|
18
|
+
this.herd = herd;
|
|
19
|
+
this.devices = devices;
|
|
20
|
+
this.events = events;
|
|
21
|
+
this.timestamp_last_refreshed = timestamp_last_refreshed;
|
|
22
|
+
this.user_roles = user_roles;
|
|
23
|
+
this.events_page_index = events_page_index;
|
|
24
|
+
this.total_events = total_events;
|
|
25
|
+
this.total_events_with_filters = total_events_with_filters;
|
|
26
|
+
this.labels = labels;
|
|
27
|
+
this.plans = plans;
|
|
28
|
+
this.zones = zones;
|
|
29
|
+
}
|
|
30
|
+
to_serializable() {
|
|
31
|
+
return {
|
|
32
|
+
herd: this.herd,
|
|
33
|
+
devices: this.devices,
|
|
34
|
+
events: this.events,
|
|
35
|
+
timestamp_last_refreshed: this.timestamp_last_refreshed,
|
|
36
|
+
user_roles: this.user_roles,
|
|
37
|
+
events_page_index: this.events_page_index,
|
|
38
|
+
total_events: this.total_events,
|
|
39
|
+
total_events_with_filters: this.total_events_with_filters,
|
|
40
|
+
labels: this.labels,
|
|
41
|
+
plans: this.plans,
|
|
42
|
+
zones: this.zones,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static async from_herd(herd, client) {
|
|
46
|
+
// load devices
|
|
47
|
+
let response_new_devices = await get_devices_by_herd(herd.id, client);
|
|
48
|
+
if (response_new_devices.status == EnumWebResponse.ERROR ||
|
|
49
|
+
!response_new_devices.data) {
|
|
50
|
+
console.warn("No devices found for herd");
|
|
51
|
+
return new HerdModule(herd, [], [], Date.now());
|
|
52
|
+
}
|
|
53
|
+
const new_devices = response_new_devices.data;
|
|
54
|
+
// get api keys for each device... run requests in parallel
|
|
55
|
+
if (new_devices.length > 0) {
|
|
56
|
+
let api_keys_promises = new_devices.map((device) => server_list_api_keys(device.id?.toString() ?? ""));
|
|
57
|
+
let api_keys = await Promise.all(api_keys_promises);
|
|
58
|
+
for (let i = 0; i < new_devices.length; i++) {
|
|
59
|
+
new_devices[i].api_keys_scout = api_keys[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// get recent events for each device... run requests in parallel
|
|
63
|
+
let recent_events_promises = new_devices.map((device) => server_get_events_and_tags_for_device(device.id ?? 0));
|
|
64
|
+
// Run all requests in parallel
|
|
65
|
+
const [recent_events, res_zones, res_user_roles, total_event_count, res_plans,] = await Promise.all([
|
|
66
|
+
Promise.all(recent_events_promises),
|
|
67
|
+
server_get_more_zones_and_actions_for_herd(herd.id, 0, 10),
|
|
68
|
+
server_get_users_with_herd_access(herd.id),
|
|
69
|
+
server_get_total_events_by_herd(herd.id),
|
|
70
|
+
server_get_plans_by_herd(herd.id),
|
|
71
|
+
]);
|
|
72
|
+
for (let i = 0; i < new_devices.length; i++) {
|
|
73
|
+
let x = recent_events[i].data;
|
|
74
|
+
if (recent_events[i].status == EnumWebResponse.SUCCESS && x) {
|
|
75
|
+
new_devices[i].recent_events = x;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// TODO: store in DB and retrieve on load?
|
|
79
|
+
const newLabels = LABELS;
|
|
80
|
+
return new HerdModule(herd, new_devices, [], Date.now(), res_user_roles.data, 0, total_event_count.data ? total_event_count.data : 0, total_event_count.data ? total_event_count.data : 0, newLabels, res_plans.data ? res_plans.data : [], res_zones.data ? res_zones.data : []);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare enum EnumWebResponse {
|
|
2
|
+
SUCCESS = "success",
|
|
3
|
+
ERROR = "error"
|
|
4
|
+
}
|
|
5
|
+
export declare class IWebResponse<T> {
|
|
6
|
+
status: EnumWebResponse;
|
|
7
|
+
data: T | null;
|
|
8
|
+
msg: string | null;
|
|
9
|
+
constructor(status: EnumWebResponse, data: T | null, msg: string | null);
|
|
10
|
+
static success<T>(data: T): IWebResponse<T>;
|
|
11
|
+
static error<T>(msg: string): IWebResponse<T>;
|
|
12
|
+
to_compatible(): IWebResponseCompatible<T>;
|
|
13
|
+
}
|
|
14
|
+
export interface IWebResponseCompatible<T> {
|
|
15
|
+
status: EnumWebResponse;
|
|
16
|
+
data: T | null;
|
|
17
|
+
msg: string | null;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export var EnumWebResponse;
|
|
2
|
+
(function (EnumWebResponse) {
|
|
3
|
+
EnumWebResponse["SUCCESS"] = "success";
|
|
4
|
+
EnumWebResponse["ERROR"] = "error";
|
|
5
|
+
})(EnumWebResponse || (EnumWebResponse = {}));
|
|
6
|
+
export class IWebResponse {
|
|
7
|
+
constructor(status, data, msg) {
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.data = data;
|
|
10
|
+
this.msg = msg;
|
|
11
|
+
}
|
|
12
|
+
static success(data) {
|
|
13
|
+
return new IWebResponse(EnumWebResponse.SUCCESS, data, null);
|
|
14
|
+
}
|
|
15
|
+
static error(msg) {
|
|
16
|
+
return new IWebResponse(EnumWebResponse.ERROR, null, msg);
|
|
17
|
+
}
|
|
18
|
+
to_compatible() {
|
|
19
|
+
return {
|
|
20
|
+
status: this.status,
|
|
21
|
+
data: this.data,
|
|
22
|
+
msg: this.msg,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|