Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/commands/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {command as pushCommand} from './push.js';
import {command as runCommand} from './run-function.js';
import {command as setupLogsCommand} from './setup-logs.js';
import {command as authStatusCommand} from './show-authorized-user.js';
import {command as metricsCommand} from './show-metrics.js';
import {command as filesStatusCommand} from './show-file-status.js';
import {command as mcpCommand} from './start-mcp.js';
import {command as tailLogsCommand} from './tail-logs.js';
Expand Down Expand Up @@ -169,6 +170,7 @@ export function makeProgram(exitOveride?: (err: CommanderError) => void) {
openWebappCommand,
runCommand,
listCommand,
metricsCommand,
createVersionCommand,
listVersionsCommand,
mcpCommand,
Expand Down
57 changes: 57 additions & 0 deletions src/commands/show-metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {Command} from 'commander';
import {Clasp} from '../core/clasp.js';
import {intl} from '../intl.js';
import {withSpinner} from './utils.js';

export const command = new Command('show-metrics')
.alias('metrics')
.description('Show project metrics')
.option('--json', 'Show output in JSON format')
.action(async function (this: Command): Promise<void> {
const options = this.optsWithGlobals();
const clasp: Clasp = options.clasp;

const spinnerMsg = intl.formatMessage({
defaultMessage: 'Fetching project metrics...',
});
const metrics = await withSpinner(spinnerMsg, async () => {
return clasp.project.getMetrics();
});

if (options.json) {
console.log(JSON.stringify(metrics, null, 2));
return;
}

if (!metrics) {
const msg = intl.formatMessage({
defaultMessage: 'No metrics found.',
});
console.log(msg);
return;
}

const activeUsers = metrics.activeUsers ?? [];
const failedExecutions = metrics.failedExecutions ?? [];
const totalExecutions = metrics.totalExecutions ?? [];

if (activeUsers.length === 0 && failedExecutions.length === 0 && totalExecutions.length === 0) {
console.log('No metrics available for this period.');
return;
}

const displayMetrics = (title: string, data: any[]) => {
console.log(title);
if (data.length === 0) {
console.log(' No data');
return;
}
data.forEach((item) => {
console.log(` ${item.startTime} - ${item.endTime}: ${item.value}`);
});
};

displayMetrics('Active Users:', activeUsers);
displayMetrics('Failed Executions:', failedExecutions);
displayMetrics('Total Executions:', totalExecutions);
});
21 changes: 21 additions & 0 deletions src/core/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,25 @@ export class Project {
const manifest: Manifest = JSON.parse(content.toString());
return manifest;
}

/**
* Fetch metrics for the Apps Script project.
* @returns {Promise<script_v1.Schema$Metrics | undefined>} A promise that resolves to the metrics object.
*/
async getMetrics(): Promise<script_v1.Schema$Metrics | undefined> {
debug('Fetching metrics');
assertAuthenticated(this.options);
assertScriptConfigured(this.options);

const scriptId = this.options.project.scriptId;
const credentials = this.options.credentials;

const script = google.script({version: 'v1', auth: credentials});
try {
const res = await script.projects.getMetrics({scriptId});
return res.data;
} catch (error) {
handleApiError(error);
}
}
}