Skip to content

Commit 82ff90d

Browse files
Updated summary creation
1 parent 38aa7d4 commit 82ff90d

3 files changed

Lines changed: 95 additions & 77 deletions

File tree

src/index.ts

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { getDistance } from "geolib";
2+
import { getActivityById } from "./controllers/activities/getActivityById";
13
import { triggerAlarm } from "./controllers/alarms/triggerAlarm";
4+
import { getReverseGeocoding } from "./controllers/maps/getReverseGeocoding";
25
import createRouter from "./domains/router";
6+
import { createActivitySummary } from "./controllers/activities/summary/createActivitySummary";
7+
import { updatePersonalBestActivitySummary } from "./controllers/activities/summary/updatePersonalBestActivitySummary";
38

49
const router = createRouter();
510

@@ -61,24 +66,92 @@ export default {
6166

6267
export class ActivityDurableObject {
6368
state: DurableObjectState;
69+
env: Env;
6470

6571
constructor(state: DurableObjectState, env: Env) {
6672
this.state = state;
73+
this.env = env;
6774
};
6875

6976
async fetch(request: Request) {
70-
const body = await request.json() as {
71-
id?: string;
77+
const { activityId } = await request.json() as {
78+
activityId?: string;
7279
};
7380

74-
if(!body.id)
81+
if(!activityId)
7582
return Response.json({ success: false });
7683

77-
console.log("recieved id: " + body.id);
84+
const activity = await getActivityById(this.env.DATABASE, activityId);
85+
86+
if(!activity)
87+
return Response.json({ success: false });
88+
89+
const bucket = await this.env.BUCKET.get(`activities/${activity.id}.json`);
90+
91+
if(!bucket)
92+
return Response.json({ success: false });
93+
94+
const sessions = await bucket.json<Array<any>>();
95+
96+
let startArea = null;
97+
let finishArea = null;
98+
let distance = 0;
99+
let elevation = 0;
100+
let maxSpeed = 0;
101+
102+
if(sessions.length && sessions[0].locations.length) {
103+
const getAreaName = async (coords: any) => {
104+
const geocoding = await getReverseGeocoding(this.env.GOOGLE_MAPS_API_TOKEN, coords.latitude, coords.longitude);
105+
106+
if(geocoding.results.length) {
107+
const geocodingResult = geocoding.results[0];
108+
109+
const geocodingComponent = geocodingResult.address_components.find((component: any) => component.types.includes("postal_town")) ?? geocodingResult.address_components.find((component: any) => component.types.includes("political")) ?? geocodingResult.address_components.find((component: any) => component.types.includes("country"));
110+
111+
return geocodingComponent?.long_name ?? null;
112+
}
113+
114+
return null;
115+
}
116+
117+
await Promise.all([
118+
getAreaName(sessions[0].locations[0].coords).then((name) => startArea = name),
119+
getAreaName(sessions[sessions.length - 1].locations[sessions[sessions.length - 1].locations.length - 1].coords).then((name) => finishArea = name)
120+
]);
121+
}
122+
123+
const speeds = [];
124+
125+
for(let session of sessions) {
126+
for(let index = 1; index < session.locations.length; index++) {
127+
distance += getDistance(session.locations[index - 1].coords, session.locations[index].coords, 1);
128+
129+
speeds.push(session.locations[index].coords.speed);
130+
131+
elevation += Math.max(0, session.locations[index].coords.altitude -session.locations[index - 1].coords.altitude);
132+
133+
if(session.locations[index].coords.speed > maxSpeed)
134+
maxSpeed = session.locations[index].coords.speed;
135+
}
136+
}
137+
138+
const speedSum = speeds.reduce((a, b) => a + b, 0);
139+
const averageSpeed = (speedSum / speeds.length) || 0;
140+
141+
let distancePersonalBest = null;
142+
let averageSpeedPersonalBest = null;
143+
let elevationPersonalBest = null;
144+
let maxSpeedPersonalBest = null;
145+
146+
const activitySummary = await createActivitySummary(this.env.DATABASE, activity.id, startArea, finishArea, distance, distancePersonalBest, averageSpeed, averageSpeedPersonalBest, elevation, elevationPersonalBest, maxSpeed, maxSpeedPersonalBest);
147+
148+
if(!activitySummary)
149+
return Response.json({ success: false });
150+
151+
await updatePersonalBestActivitySummary(this.env.DATABASE, activity.user);
78152

79153
return Response.json({
80-
hello: "world",
81-
activity: body.id
154+
success: true
82155
});
83156
};
84157
};

src/routes/activities/[activityId]/summary.ts

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import { getDistance } from "geolib";
2-
import { createActivity } from "../../../controllers/activities/createActivity";
31
import { getActivityById } from "../../../controllers/activities/getActivityById";
4-
import { createActivitySummary } from "../../../controllers/activities/summary/createActivitySummary";
52
import { getActivitySummaryById } from "../../../controllers/activities/summary/getActivitySummaryById";
6-
import { getBikeById } from "../../../controllers/bikes/getBikeById";
7-
import { Bike } from "../../../models/bike";
8-
import { getReverseGeocoding } from "../../../controllers/maps/getReverseGeocoding";
9-
import { updatePersonalBestActivitySummary } from "../../../controllers/activities/summary/updatePersonalBestActivitySummary";
103

114
export const activitySummaryRequestSchema = {
125
params: {
@@ -17,7 +10,7 @@ export const activitySummaryRequestSchema = {
1710
}
1811
};
1912

20-
export async function handleActivitySummaryRequest(request: RequestWithKey, env: Env) {
13+
export async function handleActivitySummaryRequest(request: RequestWithKey, env: Env, context: EventContext<Env, string, null>) {
2114
const { activityId } = request.params;
2215

2316
const activity = await getActivityById(env.DATABASE, activityId);
@@ -28,69 +21,21 @@ export async function handleActivitySummaryRequest(request: RequestWithKey, env:
2821
let activitySummary = await getActivitySummaryById(env.DATABASE, activity.id);
2922

3023
if(!activitySummary) {
31-
const bucket = await env.BUCKET.get(`activities/${activity.id}.json`);
32-
33-
if(!bucket)
34-
return Response.json({ success: false });
35-
36-
const sessions = await bucket.json<Array<any>>();
37-
38-
let startArea = null;
39-
let finishArea = null;
40-
let distance = 0;
41-
let elevation = 0;
42-
let maxSpeed = 0;
43-
44-
if(sessions.length && sessions[0].locations.length) {
45-
async function getAreaName(coords: any) {
46-
const geocoding = await getReverseGeocoding(env.GOOGLE_MAPS_API_TOKEN, coords.latitude, coords.longitude);
47-
48-
if(geocoding.results.length) {
49-
const geocodingResult = geocoding.results[0];
24+
const durableObjectId = env.ACTIVITY_DURABLE_OBJECT.idFromName("default");
25+
const durableObject = env.ACTIVITY_DURABLE_OBJECT.get(durableObjectId);
5026

51-
const geocodingComponent = geocodingResult.address_components.find((component: any) => component.types.includes("postal_town")) ?? geocodingResult.address_components.find((component: any) => component.types.includes("political")) ?? geocodingResult.address_components.find((component: any) => component.types.includes("country"));
52-
53-
return geocodingComponent?.long_name ?? null;
54-
}
55-
56-
return null;
57-
}
58-
59-
await Promise.all([
60-
getAreaName(sessions[0].locations[0].coords).then((name) => startArea = name),
61-
getAreaName(sessions[sessions.length - 1].locations[sessions[sessions.length - 1].locations.length - 1].coords).then((name) => finishArea = name)
62-
]);
63-
}
64-
65-
const speeds = [];
66-
67-
for(let session of sessions) {
68-
for(let index = 1; index < session.locations.length; index++) {
69-
distance += getDistance(session.locations[index - 1].coords, session.locations[index].coords, 1);
70-
71-
speeds.push(session.locations[index].coords.speed);
72-
73-
elevation += Math.max(0, session.locations[index].coords.altitude -session.locations[index - 1].coords.altitude);
74-
75-
if(session.locations[index].coords.speed > maxSpeed)
76-
maxSpeed = session.locations[index].coords.speed;
77-
}
78-
}
79-
80-
const speedSum = speeds.reduce((a, b) => a + b, 0);
81-
const averageSpeed = (speedSum / speeds.length) || 0;
82-
83-
let distancePersonalBest = null;
84-
let averageSpeedPersonalBest = null;
85-
let elevationPersonalBest = null;
86-
let maxSpeedPersonalBest = null;
87-
88-
activitySummary = await createActivitySummary(env.DATABASE, activity.id, startArea, finishArea, distance, distancePersonalBest, averageSpeed, averageSpeedPersonalBest, elevation, elevationPersonalBest, maxSpeed, maxSpeedPersonalBest);
89-
90-
if(!activitySummary)
91-
return Response.json({ success: false });
92-
93-
await updatePersonalBestActivitySummary(env.DATABASE, activity.user);
27+
context.waitUntil(durableObject.fetch("", {
28+
body: JSON.stringify({
29+
activityId: activity.id
30+
})
31+
}));
32+
33+
return Response.json({
34+
success: true
35+
}, {
36+
status: 102,
37+
statusText: "Processing"
38+
});
9439
}
9540

9641
return Response.json({

src/routes/activities/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export async function handleCreateActivityRequest(request: RequestWithKey, env:
124124

125125
context.waitUntil(durableObject.fetch("", {
126126
body: JSON.stringify({
127-
id: activity.id
127+
activityId: activity.id
128128
})
129129
}));
130130

0 commit comments

Comments
 (0)