Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking logs #1168

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/app/domain/geolocation/tracking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import {
WALKING_ACTIVITY,
LOW_CONFIDENCE_THRESHOLD
} from '/app/domain/geolocation/tracking/consts'
import { getOrCreateId } from '/app/domain/geolocation/tracking/user'
export { Log, getAllLogs, sendLogFile } from '/app/domain/geolocation/helpers'
export { getOrCreateId, updateId } from '/app/domain/geolocation/tracking/user'
export { uploadData } from '/app/domain/geolocation/tracking/upload'
export { GeolocationTrackingHeadlessTask } from '/app/domain/geolocation/tracking/headless'
export { storeFetchServiceWebHook } from '/app/domain/geolocation/tracking/storage'
import { getOrCreateId } from '/app/domain/geolocation/tracking/user'

export {
clearAllCozyGPSMemoryData,
Expand Down Expand Up @@ -228,6 +228,8 @@ export const startOpenPathUploadAndPipeline = async ({
}) => {
try {
const user = await getOrCreateId()
Log(`User: ${JSON.stringify(user)}`)

// Upload data to openpath server
const uploadedCount = await uploadData(user, { untilTs, force })
if (uploadedCount >= 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/app/domain/geolocation/tracking/tracking.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uploadUserCache } from '/app/domain/geolocation/tracking/upload'
import { getOrCreateUser } from '/app/domain/geolocation/tracking/user'
import { createOpenPathUserIfMissing } from '/app/domain/geolocation/tracking/user'
import { getTs, Log, parseISOString } from '/app/domain/geolocation/helpers'
import {
getActivities,
Expand Down Expand Up @@ -57,7 +57,7 @@ export const uploadTrackingData = async (
user,
{ force = false } = {}
) => {
await getOrCreateUser(user)
await createOpenPathUserIfMissing(user)

if (!locations || locations.length === 0) {
Log('No new locations')
Expand Down Expand Up @@ -483,7 +483,9 @@ export const getFilteredActivities = async ({ beforeTs, locations }) => {
if (isMovingStillActivity(loc) || isUnknownActivity(loc)) {
// Avoid unknown or "moving" still activities
location.activity.type = inferMotionActivity(loc)
Log('Changed ' + loc.activity.type + ' to: ' + location.activity.type)
if (loc.activity.type !== location.activity.type) {
Log('Changed ' + loc.activity.type + ' to: ' + location.activity.type)
}
}
return translateEventToEMissionMotionActivity(location)
})
Expand Down
16 changes: 14 additions & 2 deletions src/app/domain/geolocation/tracking/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const getOrCreateId = async () => {
}

export const createUser = async user => {
Log(
`Request openpath server to create new user with id ${JSON.stringify(user)}`
)
let response = await fetch(SERVER_URL + '/profile/create', {
method: 'POST',
headers: {
Expand All @@ -38,11 +41,16 @@ export const createUser = async user => {
throw new Error('FAILED_EMISSION_USER_CREATION') // Could be no Internet, offline server or unknown issue. Won't trigger if user already exists.
} else {
const jsonTokenResponse = await response.json()
Log('Success creating user ' + user + ', UUID: ' + jsonTokenResponse.uuid)
Log(
'Success creating user ' +
JSON.stringify(user) +
', UUID: ' +
jsonTokenResponse.uuid
)
}
}

export const getOrCreateUser = async user => {
export const createOpenPathUserIfMissing = async user => {
let response
try {
const respFetch = await fetch(SERVER_URL + '/profile/get', {
Expand All @@ -53,15 +61,19 @@ export const getOrCreateUser = async user => {
body: JSON.stringify({ user: user })
})
if (respFetch.status === 403) {
Log("No openpath user found, let's create one.")
return createUser(user)
}
response = await respFetch.json()
const uuid = response?.user_id?.['$uuid']
if (!uuid) {
Log("No openpath uuid found, let's create the user.")
await createUser(user)
}
return uuid
} catch (err) {
// If the user actually exist, the creation will return the existing user
Ldoppea marked this conversation as resolved.
Show resolved Hide resolved
Log(`Error when trying to get openpath user, let's create one`)
return createUser(user)
}
}
Expand Down
Loading