addons/isl-server/src/analytics/environment.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {ApplicationInfo} from './types';
b69ab319
b69ab3110import os from 'node:os';
b69ab3111import {nullthrows, randomId} from 'shared/utils';
b69ab3112
b69ab3113export function getUsername(): string {
b69ab3114 try {
b69ab3115 return os.userInfo().username;
b69ab3116 } catch (osInfoError) {
b69ab3117 try {
b69ab3118 const {env} = process;
b69ab3119 return nullthrows(env.LOGNAME || env.USER || env.LNAME || env.USERNAME);
b69ab3120 } catch (processEnvError) {
b69ab3121 throw new Error(String(processEnvError) + String(osInfoError));
b69ab3122 }
b69ab3123 }
b69ab3124}
b69ab3125
b69ab3126export function generateAnalyticsInfo(
b69ab3127 platformName: string,
b69ab3128 version: string,
b69ab3129 sessionId?: string,
b69ab3130): ApplicationInfo {
b69ab3131 return {
b69ab3132 platform: platformName,
b69ab3133 version,
b69ab3134 repo: undefined,
b69ab3135 /**
b69ab3136 * Random id for this ISL session, created at startup.
b69ab3137 * Note: this is only generated on the server, so client-logged events share the ID with the server.
b69ab3138 * May be manually specified instead of randomly created, e.g. if your platform has a well-defined session ID already.
b69ab3139 */
b69ab3140 sessionId: sessionId ?? randomId(),
b69ab3141 unixname: getUsername(),
b69ab3142 osArch: os.arch(),
b69ab3143 osType: os.platform(),
b69ab3144 osRelease: os.release(),
b69ab3145 hostname: os.hostname(),
b69ab3146 };
b69ab3147}