---
# System prepended metadata

title: GraphQL APIs example (temp)

---

# GraphQL APIs example (temp)
---
## 前言

整合iFactory,dbmaster提供原iFactory 擴充Parameter API,以下API都是透過federation方式連接至api-portal-wise-mplus的Parameter Type之下

## API 說明

#### Query (擴充 Parameter)

- lastValue: 取得最新一筆值
  - arguments (optional)
    - before: 在哪個時間點之前
    - after: 在哪個時間點之後
    - includeBefore: 是否包含 before 本身的時間點
    - includeAfter: 是否包含 after 本身的時間點
  - return value
    - num: Float
    - str: String
    - time: DateTime
    - savedAt: DateTime
  
  - example
    ```
    query ($parameterId: ID!, $before: DateTime, $after: DateTime, $includeBefore: Boolean,  $includeAfter: Boolean) {
      parameter(id: $parameterId) {    
        lastValue (before: $from, after: $after,includeBefore: $includeBefore, includeAfter: $includeAfter){
          num
          str
          time
          savedAt
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "before": "2023-12-12 07:08:00",
      "after": "2023-12-12 07:05:00",
      "includeBefore": false,
      "includeAfter": false,
    } 

    response:
    {
      "data": {
        "parameter": {        
          "lastValue": {
            "num": 54476,
            "str": null,
            "time": "2023-12-12T07:07:56.582Z",
            "savedAt": "2023-12-12T07:07:56.631Z"
          }
        }
      }
    }
    ```

- lastBadValue: 取得最新一筆斷線值(\*)
  - no arguments
  - return value
    - value: String!
    - time: DateTime!
    - savedAt: DateTime

  - example
    ```
    query ($parameterId: ID!) {
      parameter(id: $parameterId) {    
        lastBadValue {
          value          
          time
          savedAt
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",      
    } 

    response:
    {
      "data": {
        "parameter": {        
          "lastBadValue": {
            "value": "*",
            "time": "2023-12-07T23:59:56.000Z",
            "savedAt": "2023-12-07T23:59:56.000Z"
          }
        }
      }
    }
    ```

- valuesInRange: 取得一段時間內的所有值
  - arguments
    - from: 開始時間
    - to: 結束時間
  - return value    
    - num: Float
    - str: String
    - time: DateTime
    - savedAt: DateTime
  
  - example
    ```
    query ($parameterId: ID!, $from: DateTime!, $to: DateTime!) {
      parameter(id: $parameterId) {    
        valuesInRange(from: $from, to: $to){
          num
          str
          time
          savedAt
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "from": "2023-12-12 07:08:00",
      "to": "2023-12-12 07:08:30",      
    } 

    response:
    {
      "data": {
        "parameter": {        
          "valuesInRange": [
            {
              "num": 54486,
              "str": null,
              "time": "2023-12-12T07:08:06.573Z",              
              "savedAt": "2023-12-12T07:08:06.627Z"
            },
            {
              "num": 54496,
              "str": null,
              "time": "2023-12-12T07:08:16.511Z",              
              "savedAt": "2023-12-12T07:08:16.560Z"
            },
            {
              "num": 54506,
              "str": null,
              "time": "2023-12-12T07:08:26.691Z",              
              "savedAt": "2023-12-12T07:08:26.739Z"
            }
          ]
        }
      }
    }
    ```

- badValuesInRange: 取得一段時間內的所有斷線值(\*)
  - arguments
    - from: 開始時間
    - to: 結束時間
  - return value
    - value: String!
    - time: DateTime!    
    - savedAt: DateTime
  
  - example
    ```
    query ($parameterId: ID!, $from: DateTime!, $to: DateTime!) {
      parameter(id: $parameterId) {    
        badValuesInRange(from: $from, to: $to){
          value          
          time
          savedAt
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "from": "2023-12-07 23:59:30",
      "to": "2023-12-08 00:00:00",      
    } 

    response:
    {
      "data": {
        "parameter": {        
          "badValuesInRange": [
            {
              "value": "*",
              "time": "2023-12-07T23:59:36.000Z",
              "savedAt": "2023-12-07T23:59:36.000Z"
            },
            {
              "value": "*",
              "time": "2023-12-07T23:59:46.000Z",
              "savedAt": "2023-12-07T23:59:46.000Z"
            },
            {
              "value": "*",
              "time": "2023-12-07T23:59:56.000Z",
              "savedAt": "2023-12-07T23:59:56.000Z"
            }
          ]
        }
      }
    }
    ``` 

- limitToNthValues: 取得最新或最舊的 n 筆值
  - arguments
    - n: 資料筆數，若為正數，則為最舊的 n 筆值，若為負數，則為最新的 n 筆值
    - from: 開始時間
    - to: 結束時間
  - return value
    - num
    - str
    - time
    - savedAt
  
  - example
    ```
    query ($parameterId: ID!, $from: DateTime!, $to: DateTime!,$n: Int!) {
      parameter(id: $parameterId) {    
        limitToNthValues(n: $n,from: $from, to: $to){
          num
          str
          time
          savedAt
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "from": "2023-12-12 07:08:00",
      "to": "2023-12-12 07:09:00",
      "n":5,    
    } 

    response:
    {
      "data": {
        "parameter": {        
          "limitToNthValues": [
            {
              "num": 54486,
              "str": null,
              "time": "2023-12-12T07:08:06.573Z",
              "savedAt": "2023-12-12T07:08:06.627Z"
            },
            {
              "num": 54496,
              "str": null,
              "time": "2023-12-12T07:08:16.511Z",
              "savedAt": "2023-12-12T07:08:16.560Z"
            },
            {
              "num": 54506,
              "str": null,
              "time": "2023-12-12T07:08:26.691Z",
              "savedAt": "2023-12-12T07:08:26.739Z"
            },
            {
              "num": 54516,
              "str": null,
              "time": "2023-12-12T07:08:36.650Z",
              "savedAt": "2023-12-12T07:08:36.972Z"
            },
            {
              "num": 54526,
              "str": null,
              "time": "2023-12-12T07:08:46.610Z",
              "savedAt": "2023-12-12T07:08:46.660Z"
            }
          ]
        }
      }
    }
    ```
- calculatedValue: 將一段時間內的所有值，統計成一筆值
  - arguments
    - from: 開始時間
    - to: 結束時間
    - fn: 統計方式
      - Max: 最大值
      - Min: 最小值
      - Sum: 加總值(目前不支援)
      - Mean: 平均值(數據加權平均)
  - return value
    - num
    - str
    - time
  
  - example
    ```
    query ($parameterId: ID!, $from: DateTime!, $to: DateTime!,$fn: CalculationFn!) {
      parameter(id: $parameterId) {    
        calculatedValue(fn: $fn, from: $from, to: $to){
          num
          str
          time          
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "from": "2023-12-12 07:08:00",
      "to": "2023-12-12 07:09:00",
      "fn": "Mean",
    } 

    response:
    {
      "data": {
        "parameter": {        
          "calculatedValue": {
            "num": 5451.1,
            "str": null,
            "time": "2023-12-13T05:21:11.506Z"
          }
        }
      }
    }
    ```

- downSampledInRange: 將一段時間內的資料，降採樣到最大資料數以下
  - arguments
    - from: 開始時間
    - to: 結束時間
    - maxDataPoints: 最大資料數
    - fillOption: 補值方式 (optional default: "None")
      - None: 不補值
      - Linear: 線性
      - Previous: 補前一筆數據
  - return value
    - num
    - str
    - time
  
  - example
    ```
    query ($parameterId: ID!, $from: DateTime!, $to: DateTime!, $maxDataPoints: Int!, $fillOption: FillOption) {
      parameter(id: $parameterId) {  
        downSampledInRange(from: $from, to: $to, maxDataPoints: $maxDataPoints, fillOption: $fillOption){          
          num
          str
          time          
        }
      }
    }

    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",
      "from": "2023-12-12 07:08:00",
      "to": "2023-12-12 07:09:00",
      "maxDataPoints": 2,
      "fillOption": "None",
    } 

    response:
    {
      "data": {
        "parameter": {        
          "downSampledInRange": [
            {
              "num": 54506,
              "str": null,
              "time": "2023-12-12T07:08:00.000Z"
            },
            {
              "num": 54536,
              "str": null,
              "time": "2023-12-12T07:08:30.000Z"
            }
          ]
        }
      }
    }
    ```

- tagValueType
  - no arguments
  - return value:
    - UnKnow
    - Number
    - String
    - Discrete
  
  - example
    ```
    query ($parameterId: ID!) {
      parameter(id: $parameterId) {
        tagValueType          
      }
    }
    variables
    {
      "parameterId": "MTM1NjgmUHJpXXXX",      
    } 

    response:
    {
      "data": {
        "parameter": {
          "tagValueType": "Number"         
        }
      }
    }
    ```
---
###### tags: `Documentation`