Skip to content

Commit bcfcedf

Browse files
committed
fixed metrics calcs
1 parent f8ebdf9 commit bcfcedf

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import {
2+
useAPIResult,
3+
} from "@macrostrat/ui-components";
4+
import h from "@macrostrat/hyper";
5+
import { BlankImage, Image } from "../index";
6+
7+
export function getCheckins(lat1, lat2, lng1, lng2) {
8+
// abitrary bounds around click point
9+
let minLat = Math.floor(lat1 * 100) / 100;
10+
let maxLat = Math.floor(lat2 * 100) / 100;
11+
let minLng = Math.floor(lng1 * 100) / 100;
12+
let maxLng = Math.floor(lng2 * 100) / 100;
13+
14+
// change use map coords
15+
return useAPIResult("https://rockd.org/api/v2/protected/checkins?minlat=" + minLat +
16+
"&maxlat=" + maxLat +
17+
"&minlng=" + minLng +
18+
"&maxlng=" + maxLng);
19+
}
20+
21+
export function imageExists(image_url){
22+
var http = new XMLHttpRequest();
23+
24+
http.open('HEAD', image_url, false);
25+
http.send();
26+
27+
return http.status != 404;
28+
}
29+
30+
export function createSelectedCheckins(result) {
31+
let checkins = [];
32+
33+
result.forEach((checkin) => {
34+
// format rating
35+
let ratingArr = [];
36+
for(var i = 0; i < checkin.rating; i++) {
37+
ratingArr.push(h(Image, {className: "star", src: "blackstar.png"}));
38+
}
39+
40+
for(var i = 0; i < 5 - checkin.rating; i++) {
41+
ratingArr.push(h(Image, {className: "star", src: "emptystar.png"}));
42+
}
43+
let image;
44+
45+
if (imageExists("https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo)) {
46+
image = h(BlankImage, {className: 'observation-img', src: "https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo});
47+
} else {
48+
image = h(Image, { className: 'observation-img', src: "rockd.jpg"});
49+
}
50+
51+
52+
let temp = h('div', { className: 'checkin' }, [
53+
h('div', {className: 'checkin-header'}, [
54+
h('h3', {className: 'profile-pic'}, h(BlankImage, {src: "https://rockd.org/api/v2/protected/gravatar/" + checkin.person_id, className: "profile-pic"})),
55+
h('div', {className: 'checkin-info'}, [
56+
h('h3', {className: 'name'}, checkin.first_name + " " + checkin.last_name),
57+
h('h4', {className: 'edited'}, checkin.created),
58+
h('p', "Near " + checkin.near),
59+
h('h3', {className: 'rating'}, ratingArr),
60+
]),
61+
]),
62+
h('p', {className: 'description'}, checkin.notes),
63+
h('a', {className: 'checkin-link', href: "/dev/test-site/checkin?checkin=" + checkin.checkin_id, target: "_blank"}, [
64+
image,
65+
h('div', {className: "image-details"}, [
66+
h('h1', "Details"),
67+
h(Image, {className: 'details-image', src: "explore/white-arrow.png"})
68+
])
69+
]),
70+
h('div', {className: 'checkin-footer'}, [
71+
h('div', {className: 'likes-container'}, [
72+
h(Image, {className: 'likes-image', src: "explore/thumbs-up.png"}),
73+
h('h3', {className: 'likes'}, checkin.likes),
74+
]),
75+
h('div', {className: 'observations-container'}, [
76+
h(Image, {className: 'observations-image', src: "explore/observations.png"}),
77+
h('h3', {className: 'comments'}, checkin.observations.length),
78+
]),
79+
h('div', {className: 'comments-container'}, [
80+
h(Image, {className: 'comments-image', src: "explore/comment.png"}),
81+
h('h3', {className: 'comments'}, checkin.comments),
82+
])
83+
]),
84+
]);
85+
86+
checkins.push(temp);
87+
});
88+
89+
return checkins;
90+
}
91+
92+
export function formatCoordinates(latitude, longitude) {
93+
// Round latitude and longitude to 4 decimal places
94+
const roundedLatitude = latitude.toFixed(4);
95+
const roundedLongitude = longitude.toFixed(4);
96+
97+
const latitudeDirection = latitude >= 0 ? 'N' : 'S';
98+
const longitudeDirection = longitude >= 0 ? 'E' : 'W';
99+
100+
// Return the formatted string with rounded values
101+
return `${Math.abs(roundedLatitude)}° ${latitudeDirection}, ${Math.abs(roundedLongitude)}° ${longitudeDirection}`;
102+
}
103+
104+
export function getSelectedCheckins(result) {
105+
let checkins = [];
106+
107+
// Selected checkin
108+
if (result == null) {
109+
return null;
110+
} else {
111+
result = result.success.data;
112+
checkins = createSelectedCheckins(result);
113+
114+
if (checkins.length > 0) {
115+
return h("div", {className: 'checkin-container'}, checkins);
116+
} else {
117+
return null;
118+
}
119+
}
120+
}
121+
122+
export function createFeaturedCheckins(result, mapRef) {
123+
let checkins = [];
124+
let map = mapRef?.current;
125+
let stop = 0;
126+
127+
result.forEach((checkin) => {
128+
stop++;
129+
let pin = h('div',
130+
{ src: "marker_container.png",
131+
className: "marker_container",
132+
onClick: () => {
133+
map.flyTo({center: [checkin.lng, checkin.lat], zoom: 12});
134+
}
135+
}, [
136+
h(Image, { src: "marker_red.png", className: "marker" }),
137+
h('span', { className: "marker-number" }, stop)
138+
])
139+
140+
141+
// format rating
142+
let ratingArr = [];
143+
for(var i = 0; i < checkin.rating; i++) {
144+
ratingArr.push(h(Image, {className: "star", src: "blackstar.png"}));
145+
}
146+
147+
for(var i = 0; i < 5 - checkin.rating; i++) {
148+
ratingArr.push(h(Image, {className: "star", src: "emptystar.png"}));
149+
}
150+
let image;
151+
152+
if (imageExists("https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo)) {
153+
image = h(BlankImage, {className: 'observation-img', src: "https://rockd.org/api/v1/protected/image/" + checkin.person_id + "/thumb_large/" + checkin.photo});
154+
} else {
155+
image = h(Image, { className: 'observation-img', src: "rockd.jpg"});
156+
}
157+
158+
159+
let temp = h('div', { className: 'checkin' }, [
160+
h('div', {className: 'checkin-header'}, [
161+
h('h3', {className: 'profile-pic'}, h(BlankImage, {src: "https://rockd.org/api/v2/protected/gravatar/" + checkin.person_id, className: "profile-pic"})),
162+
h('div', {className: 'checkin-info'}, [
163+
h('h3', {className: 'name'}, checkin.first_name + " " + checkin.last_name),
164+
h('h4', {className: 'edited'}, checkin.created),
165+
h('p', "Near " + checkin.near),
166+
h('h3', {className: 'rating'}, ratingArr),
167+
]),
168+
pin,
169+
]),
170+
h('p', {className: 'description'}, checkin.notes),
171+
h('a', {className: 'checkin-link', href: "/dev/test-site/checkin?checkin=" + checkin.checkin_id, target: "_blank"}, [
172+
image,
173+
h('div', {className: "image-details"}, [
174+
h('h1', "Details"),
175+
h(Image, {className: 'details-image', src: "explore/white-arrow.png"})
176+
])
177+
]),
178+
h('div', {className: 'checkin-footer'}, [
179+
h('div', {className: 'likes-container'}, [
180+
h(Image, {className: 'likes-image', src: "explore/thumbs-up.png"}),
181+
h('h3', {className: 'likes'}, checkin.likes),
182+
]),
183+
h('div', {className: 'observations-container'}, [
184+
h(Image, {className: 'observations-image', src: "explore/observations.png"}),
185+
h('h3', {className: 'comments'}, checkin.observations.length),
186+
]),
187+
h('div', {className: 'comments-container'}, [
188+
h(Image, {className: 'comments-image', src: "explore/comment.png"}),
189+
h('h3', {className: 'comments'}, checkin.comments),
190+
])
191+
]),
192+
]);
193+
194+
checkins.push(temp);
195+
});
196+
197+
return checkins;
198+
}

pages/dev/test-site/metrics/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export function Metrics() {
126126
Total: parseInt(item.count)
127127
});
128128
}
129+
checkins_by_month.pop();
129130
currentTotal = checkins_by_month[checkins_by_month.length - 1].Total;
130131
currentName = checkins_by_month[checkins_by_month.length - 1].name;
131132
checkins_by_month[checkins_by_month.length - 1].Total = Math.round(currentTotal * scale);
@@ -150,6 +151,7 @@ export function Metrics() {
150151
Total: parseInt(item.count)
151152
});
152153
}
154+
signups_by_month.pop();
153155
currentTotal = signups_by_month[signups_by_month.length - 1].Total;
154156
currentName = signups_by_month[signups_by_month.length - 1].name;
155157
signups_by_month[signups_by_month.length - 1].Total = Math.round(currentTotal * scale);
@@ -172,11 +174,14 @@ export function Metrics() {
172174
Total: parseInt(item.count)
173175
});
174176
}
177+
active_users_by_month.pop();
175178
currentTotal = active_users_by_month[active_users_by_month.length - 1].Total;
176179
currentName = active_users_by_month[active_users_by_month.length - 1].name;
177180
active_users_by_month[active_users_by_month.length - 1].Total = Math.round(currentTotal * scale);
178181
active_users_by_month[active_users_by_month.length - 1].name = currentName + ` (est)`;
179182

183+
console.log("Scaled the month: ", currentName)
184+
180185
// chart array
181186
let areaArr = [
182187
h(CartesianGrid, { strokeDasharray: "3 3" }),

0 commit comments

Comments
 (0)