> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Mẫu Thời Gian

> Theo dõi mức tiêu thụ tài nguyên dựa trên thời gian đã trôi qua cho tính toán, chức năng không máy chủ, container và thanh toán theo thời gian chạy.

## Các Trường Hợp Sử Dụng

Khám phá các kịch bản phổ biến được hỗ trợ bởi Mẫu Thời Gian:

<CardGroup cols={2}>
  <Card title="Serverless Functions" icon="function">
    Thanh toán dựa trên thời gian thực thi hàm và lượng bộ nhớ sử dụng.
  </Card>

  <Card title="Container Runtime" icon="container-storage">
    Theo dõi thời gian chạy của container để thanh toán theo mức sử dụng.
  </Card>

  <Card title="Compute Instances" icon="server">
    Giám sát thời gian chạy VM và tính phí theo phút hoặc giờ.
  </Card>

  <Card title="Background Jobs" icon="briefcase">
    Theo dõi thời gian xử lý cho việc xuất dữ liệu, báo cáo và các công việc theo lô.
  </Card>
</CardGroup>

<Info>
  Hoàn hảo cho việc thanh toán dựa trên thời gian tính toán, thời lượng thực thi hàm, thời gian chạy container hoặc bất kỳ mức sử dụng nào dựa trên thời gian.
</Info>

## Bắt Đầu Nhanh

Theo dõi mức sử dụng tài nguyên theo thời gian:

<Steps>
  <Step title="Install the SDK">
    ```bash theme={null}
    npm install @dodopayments/ingestion-blueprints
    ```
  </Step>

  <Step title="Get Your API Keys">
    * **Dodo Payments API Key**: Lấy từ [Dodo Payments Dashboard](https://app.dodopayments.com/developer/api-keys)
  </Step>

  <Step title="Create a Meter">
    Tạo một phép đo trong [Dodo Payments Dashboard](https://app.dodopayments.com/):

    * **Event Name**: `time_range_usage` (hoặc tên bạn lựa chọn)
    * **Aggregation Type**: `sum` để theo dõi tổng thời lượng
    * **Over Property**: `durationSeconds`, `durationMinutes`, hoặc `durationMs`
  </Step>

  <Step title="Track Time Usage">
    <CodeGroup>
      ```javascript Serverless Functions theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'test_mode',
        eventName: 'function_execution'
      });

      // Track function execution time
      const startTime = Date.now();

      // Execute your function (example: image processing)
      const result = await yourImageProcessingLogic();

      const durationMs = Date.now() - startTime;

      await trackTimeRange(ingestion, {
        customerId: 'customer_123',
        durationMs: durationMs
      });
      ```

      ```javascript Container Runtime theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'test_mode',
        eventName: 'container_runtime'
      });

      // Track container runtime in seconds
      await trackTimeRange(ingestion, {
        customerId: 'customer_456',
        durationSeconds: 120
      });
      ```

      ```javascript VM Instance Runtime theme={null}
      import { Ingestion, trackTimeRange } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'test_mode',
        eventName: 'vm_runtime'
      });

      // Track VM runtime in minutes
      await trackTimeRange(ingestion, {
        customerId: 'customer_789',
        durationMinutes: 60
      });
      ```
    </CodeGroup>
  </Step>
</Steps>

## Cấu Hình

### Cấu Hình Nhập Dữ Liệu

<ParamField path="apiKey" type="string" required>
  Khóa API Dodo Payments của bạn từ bảng điều khiển.
</ParamField>

<ParamField path="environment" type="string" required>
  Chế độ môi trường: `test_mode` hoặc `live_mode`.
</ParamField>

<ParamField path="eventName" type="string" required>
  Tên sự kiện khớp với cấu hình phép đo của bạn.
</ParamField>

### Tùy Chọn Theo Dõi Thời Gian

<ParamField path="customerId" type="string" required>
  ID khách hàng để phân bổ thanh toán.
</ParamField>

<ParamField path="durationMs" type="number">
  Thời lượng tính bằng mili giây. Dùng cho độ chính xác dưới một giây.
</ParamField>

<ParamField path="durationSeconds" type="number">
  Thời lượng tính bằng giây. Thường dùng cho việc thực thi hàm và tác vụ ngắn.
</ParamField>

<ParamField path="durationMinutes" type="number">
  Thời lượng tính bằng phút. Hữu ích cho các tài nguyên chạy lâu như VM.
</ParamField>

<ParamField path="metadata" type="object">
  Metadata tùy chọn về tài nguyên như CPU, bộ nhớ, khu vực, v.v.
</ParamField>

## Thực Hành Tốt Nhất

<Tip>
  **Chọn Đơn Vị Phù Hợp**: Dùng mili giây cho các thao tác ngắn, giây cho hàm, và phút cho tài nguyên chạy lâu.
</Tip>

<Warning>
  **Đo thời gian chính xác**: Dùng `Date.now()` hoặc `performance.now()` để theo dõi thời gian chính xác, đặc biệt cho các hàm serverless.
</Warning>
