openapi: 3.0.3
info:
  title: Reader
  description: Спецификация REST-интерфейса для загрузки данных в Витрину
  version: "1.0"
paths:
  /v3/datamarts/{datamart_name}/tables/{table_name}/upload:
    post:
      summary: Add a new data to the datamart
      operationId: v2-datamarts-datamart_name-tables-table_name-upload
      description: Загрузка данных из внешних источников. К телу прикладывается файл csv, json или avro
      tags:
        - data
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/tableName'
      requestBody:
        $ref: '#/components/requestBodies/uploadData'
      responses:
        '200':
          $ref: '#/components/responses/requestIdResponse'
        '400':
          $ref: '#/components/responses/badRequest'
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/tables/{table_name}/modify:
    post:
      summary: Modify data in the datamart, add, update, delete from one file
      operationId: v2-datamarts-datamart_name-tables-table_name-modify
      description: Модификация данных из внешних источников. К телу прикладывается файл csv или avro, включая sys_op признак 0- для добавления, 1- для удаления записи
      tags:
        - data
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/tableName'
      requestBody:
        $ref: '#/components/requestBodies/modifyData'
      responses:
        '200':
          $ref: '#/components/responses/requestIdResponse'
        '400':
          $ref: '#/components/responses/badRequest'
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      security:
        - bearerAuth: []

  /v3/requests/{request_id}/status:
    get:
      summary: Return request status
      description: Возвращение статуса запроса
      operationId: get-v2-request-request_id-status
      tags:
        - data
      parameters:
        - $ref: '#/components/parameters/requestId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - description
                  - errorMessage
                properties:
                  code:
                    type: integer
                    description: Код статуса
                    example: 3
                  description:
                    type: string
                    description: Описание статуса
                    example: "Успешно обработан"
                  errorMessage:
                    type: string
                    description: Описание ошибки
            text/csv:
              schema:
                type: array
                items:
                  type: array
                  items:
                    type: string
              example: |
                code;description;errorMessage
                3;Успешно обработан;null
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      security:
        - bearerAuth: []

  /v3/datamarts/{datamart_name}/beginDelta:
    post:
      summary: begin delta
      operationId: post-v2-datamart_name-begin_delta
      tags:
        - delta
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/datamartUserId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/deltaBeginResponseJson'
            text/plain:
              schema:
                $ref: '#/components/schemas/deltaBeginResponsePlain'
            text/csv:
              schema:
                $ref: '#/components/schemas/deltaBeginResponseCsv'
        '400':
          description: Bad Request
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: открытие дельты
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/commitDelta:
    post:
      summary: commit delta
      operationId: post-v2-datamart_name-commit_delta
      tags:
        - delta
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/datamartUserId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/deltaCommitResponseJson'
            text/plain:
              schema:
                $ref: '#/components/schemas/deltaCommitResponsePlain'
            text/csv:
              schema:
                $ref: '#/components/schemas/deltaCommitResponseCsv'
        '400':
          description: Bad Request
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: применение дельты
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/rollbackDelta:
    post:
      summary: rollback delta
      operationId: post-v2-datamart_name-rollback_delta
      tags:
        - delta
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/datamartUserId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/deltaRollbackResponseJson'
            text/plain:
              schema:
                $ref: '#/components/schemas/deltaRollbackResponsePlain'
            text/csv:
              schema:
                $ref: '#/components/schemas/deltaRollbackResponseCsv'
        '400':
          description: Bad Request
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: отмена дельты
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/getDeltaOk:
    get:
      summary: get delta ok
      operationId: get-v2-datamart_name-get_delta_ok
      tags:
        - delta
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/datamartUserId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/deltaGetOkResponseJson'
            text/plain:
              schema:
                $ref: '#/components/schemas/deltaGetOkResponsePlain'
            text/csv:
              schema:
                $ref: '#/components/schemas/deltaGetOkResponseCsv'
        '400':
          description: Bad Request
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: получение примененной дельты
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/getDeltaHot:
    get:
      summary: get delta hot
      operationId: get-v2-datamart_name-get_delta_hot
      tags:
        - delta
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/datamartUserId'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/deltaGetHotResponseJson'
            text/plain:
              schema:
                $ref: '#/components/schemas/deltaGetHotResponsePlain'
            text/csv:
              schema:
                $ref: '#/components/schemas/deltaGetHotResponseCsv'
        '400':
          description: Bad Request
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: получение открытой дельты
      security:
        - bearerAuth: []
  /v3/datamarts/{datamart_name}/tables/{table_name}/delete:
    post:
      summary: Delete data by primary key array
      operationId: post-v2-datamart_name-tables-table_name-delete
      tags:
        - data
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/tableName'
      responses:
        '200':
          $ref: '#/components/responses/requestIdResponse'
        '400':
          $ref: '#/components/responses/badRequest'
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: Удаление данных по массиву первичных ключей
      security:
        - bearerAuth: []
      requestBody:
        $ref: '#/components/requestBodies/deleteData'
  /v3/datamarts/{datamart_name}/tables/{table_name}/truncate:
    post:
      summary: Delete historical data by primary key array
      operationId: post-v2-datamart_name-tables-table_name-truncate
      tags:
        - data
      parameters:
        - $ref: '#/components/parameters/datamartName'
        - $ref: '#/components/parameters/tableName'
        - $ref: '#/components/parameters/forSystemTime'
      responses:
        '200':
          $ref: '#/components/responses/requestIdResponse'
        '400':
          $ref: '#/components/responses/badRequest'
        '401':
          $ref: '#/components/responses/unauthorized'
        '500':
          $ref: '#/components/responses/internalServerError'
      description: Удаление исторических данных по массиву первичных ключей
      security:
        - bearerAuth: [ ]
      requestBody:
        $ref: '#/components/requestBodies/deleteData'
components:
  requestBodies:
    uploadData:
      description: "Загружаемые данные, без указания типа операции (sys_op)"
      required: true
      content:
        text/csv:
          schema:
            type: array
            items:
              type: array
              items:
                type: string
        application/json:
          schema:
            type: array
            items:
              type: array
              items:
                type: string
        avro/binary:
          schema:
            type: array
            items:
              type: array
              items:
                oneOf:
                  - type: boolean
                  - type: string
                  - type: integer #asyncapi
                  - type: number  #asyncapi
                  #- type: int  #Prostor
                  #- type: long #Prostor
                  #- type: float #Prostor
                  #- type: double #Prostor
        multipart/form-data:
          schema:
            required:
              - uploadData
            properties:
              uploadData:
                type: string
                description: Data for uploading
                format: binary
    modifyData:
      description: "Загружаемые данные, с указанием типа операции (sys_op)"
      required: true
      content:
        text/csv:
          schema:
            type: array
            items:
              type: array
              items:
                type: string
        avro/binary:
          schema:
            type: array
            items:
              type: array
              items:
                oneOf:
                  - type: boolean
                  - type: string
                  - type: integer #asyncapi
                  - type: number  #asyncapi
                  #- type: int  #Prostor
                  #- type: long #Prostor
                  #- type: float #Prostor
                  #- type: double #Prostor
        multipart/form-data:
          schema:
            required:
              - uploadData
            properties:
              uploadData:
                type: string
                description: Data for uploading
                format: binary
    deleteData:
      description: "Удаляемые данные, важны лишь ключевые поля, остальные могут отсутствовать или будут проигнорированы"
      required: true
      content:
        text/csv:
          schema:
            type: array
            items:
              type: array
              items:
                type: string
        application/json:
          schema:
            oneOf:
              - type: array
                items:
                  type: array
                  items:
                    type: string
              - type: object
                properties:
                  primaryKeys:
                    type: array
                    description: Массив первичных ключей
                    items:
                      type: array
                      items:
                        type: string
        avro/binary:
          schema:
            type: array
            items:
              type: array
              items:
                oneOf:
                  - type: boolean
                  - type: string
                  - type: integer #asyncapi
                  - type: number  #asyncapi
                  #- type: int  #Prostor
                  #- type: long #Prostor
                  #- type: float #Prostor
                  #- type: double #Prostor
        multipart/form-data:
          schema:
            required:
              - uploadData
            properties:
              uploadData:
                type: string
                description: Data for uploading
                format: binary
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

  schemas:
    requestIdString:
      type: string
      description: Идентификатор запроса
      example: 125

    requestIdJson:
      type: object
      description: Идентификатор запроса в формате JSON
      properties:
        requestId:
          $ref: '#/components/schemas/requestIdString'

    requestIdCsv:
      type: array
      description: Идентификатор запроса в формате CSV
      items:
        type: array
        items:
          type: string
      example: |
        requestId
        e5197e05-67d3-4434-b27f-510a80b6239a

    deltaBeginResponseJson:
      type: object
      properties:
        deltaNum:
          type: integer

    deltaBeginResponsePlain:
      type: string
      example: "deltaNum: 12345"

    deltaBeginResponseCsv:
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        deltaNum
        12345

    deltaCommitResponseJson:
      type: object
      properties:
        deltaNum:
          type: integer
        deltaDate:
          type: string
        cnFrom:
          type: integer

    deltaCommitResponsePlain:
      type: string
      example: "deltaNum: 12345, deltaDate: 2023-10-05T12:00:00Z, cnFrom: 1000"

    deltaCommitResponseCsv:
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        deltaNum,deltaDate,cnFrom
        12345,2023-10-05T12:00:00Z,1000

    deltaRollbackResponseJson:
      type: object
      properties:
        deltaNum:
          type: integer

    deltaRollbackResponsePlain:
      type: string
      example: "deltaNum: 12345"

    deltaRollbackResponseCsv:
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        deltaNum
        12345

    deltaGetOkResponseJson:
      type: object
      properties:
        deltaNum:
          type: integer
        deltaDate:
          type: string
        cnFrom:
          type: integer

    deltaGetOkResponsePlain:
      type: string
      example: "deltaNum: 12345, deltaDate: 2023-10-05T12:00:00Z, cnFrom: 1000"

    deltaGetOkResponseCsv:
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        deltaNum,deltaDate,cnFrom
        12345,2023-10-05T12:00:00Z,1000

    deltaGetHotResponseJson:
      type: object
      properties:
        deltaNum:
          type: integer
        cnFrom:
          type: integer
        cnMax:
          type: integer
        isRollingBack:
          type: boolean
        writeOpFinished:
          type: string

    deltaGetHotResponsePlain:
      type: string
      example: "deltaNum: 12345, cnFrom: 1000, cnMax: 1500, isRollingBack: false, writeOpFinished: 2023-10-05T12:00:00Z"

    deltaGetHotResponseCsv:
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        deltaNum,cnFrom,cnMax,isRollingBack,writeOpFinished
        12345,1000,1500,false,2023-10-05T12:00:00Z

    badRequestErrorJson:
      description: "Ошибка валидации запроса в формате JSON"
      type: object
      properties:
        rows:
          type: array
          items:
            type: object
            properties:
              line:
                type: string
              field:
                type: string
              err_code:
                type: string
              value:
                type: string
              text:
                type: string
      example:
        rows:
          - line: "4"
            field: ""
            err_code: "wrongFieldsCount"
            value: ""
            text: "некорректное число столбцов в строке"
          - line: "6"
            field: ""
            err_code: "wrongFieldsCount"
            value: ""
            text: "некорректное число столбцов в строке"

    badRequestErrorCsv:
      description: "Ошибка валидации запроса в формате CSV"
      type: array
      items:
        type: array
        items:
          type: string
      example: |
        "line";"field";"err_code";"value";"text"
        "4";"";"wrongFieldsCount";"";"некорректное число столбцов в строке"
        "6";"";"wrongFieldsCount";"";"некорректное число столбцов в строке"

  parameters:
    datamartName:
      name: datamart_name
      in: path
      description: 'Мнемоника витрины данных'
      required: true
      schema:
        type: string
    tableName:
      name: table_name
      in: path
      description: 'Наименование таблицы'
      required: true
      schema:
        type: string
    requestId:
      name: request_id
      in: path
      description: Идентификатор запроса
      required: true
      schema:
        $ref: '#/components/schemas/requestIdString'
    forSystemTime:
      name: for_system_time
      in: header
      description: Метка времени для удаления истории операций
      required: false
      schema:
        type: string
    datamartUserId:
      name: X-DATAMART-USER-ID
      description: Автор запроса
      in: header
      required: true
      schema:
        type: string
      example: 'ivanovii'

  responses:
    badRequest:
      description: Bad request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/badRequestErrorJson'
        text/csv:
          schema:
            $ref: '#/components/schemas/badRequestErrorCsv'
    internalServerError:
      description: Internal server error
      content:
        text/plain:
          schema:
            type: string
            description: Сообщение об ошибке
            example: Internal server error

    unauthorized:
      description: Unauthorized
      content:
        text/plain:
          schema:
            type: string
            description: Сообщение об ошибке
            example: Unauthorized

    requestIdResponse:
      description: OK
      headers:
        requestId:
          schema:
            $ref: '#/components/schemas/requestIdString'
      content:
        text/plain:
          schema:
            $ref: '#/components/schemas/requestIdString'
        text/csv:
          schema:
            $ref: '#/components/schemas/requestIdCsv'
        application/json:
          schema:
            $ref: '#/components/schemas/requestIdJson'

security:
  - bearerAuth: []
tags:
  - name: data
    description: Загрузка и удаление данных из Витрины, получение статуса запроса
  - name: delta
    description: Управление дельтами