<commit_before>package ams

import (
	"context"
	"encoding/xml"
	"fmt"
	"path"

	"github.com/pkg/errors"
)

const (
	jobsEndpoint   = "Jobs"
	jobInputAsset  = "JobInputAsset(0)"
	jobOutputAsset = "JobOutputAsset(0)"
)

const (
	JobQueued = iota
	JobScheduled
	JobProcessing
	JobFinished
	JobError
	JobCanceled
	JobCanceling
)

type MetaData struct {
	URI string `json:"uri"`
}

type MediaAsset struct {
	MetaData MetaData `json:"__metadata"`
}

func NewMediaAsset(uri string) MediaAsset {
	return MediaAsset{
		MetaData: MetaData{
			URI: uri,
		},
	}
}

type Job struct {
	ID              string  `json:"Id"`
	Name            string  `json:"Name"`
	StartTime       string  `json:"StartTime"`
	EndTime         string  `json:"EndTime"`
	LastModified    string  `json:"LastModified"`
	Priority        int     `json:"Priority"`
	RunningDuration float64 `json:"RunningDuration"`
	State           int     `json:"State"`
}

func (c *Client) addJob(ctx context.Context, assetID, mediaProcessorID, configuration string, taskBody TaskBody) (*Job, error) {
	jobName := fmt.Sprintf("Job - Asset %s", assetID)
	assetURI := c.buildAssetURI(assetID)
	taskName := fmt.Sprintf("Task - Asset %s", assetID)
	taskBodyXML, err := xml.Marshal(taskBody)
	if err != nil {
		return nil, errors.Wrap(err, "taskBody xml marshal failed")
	}

	params := map[string]interface{}{
		"Name": jobName,
		"InputMediaAssets": []MediaAsset{
			NewMediaAsset(assetURI),
		},
		"Tasks": []Task{
			{
				Name:             taskName,
				Configuration:    configuration,
				MediaProcessorID: mediaProcessorID,
				TaskBody:         string(taskBodyXML),
			},
		},
	}
	var out struct {
		Data Job `json:"d"`
	}
	if err := c.post(ctx, jobsEndpoint, params, &out, withOData(true)); err != nil {
		return nil, err
	}
	return &out.Data, nil
}

func (c *Client) AddEncodeJob(ctx context.Context, assetID, mediaProcessorID, outputAssetName string) (*Job, error) {
	configuration := "Adaptive Streaming"

	c.logger.Printf("[INFO] post encode asset[#%s] job ...", assetID)

	taskBody := TaskBody{
		InputAsset: AssetTag{Asset: jobInputAsset},
		OutputAsset: AssetTag{
			Asset: jobOutputAsset,
			Name:  outputAssetName,
		},
	}
	job, err := c.addJob(ctx, assetID, mediaProcessorID, configuration, taskBody)
	if err != nil {
		return nil, err
	}
	c.logger.Printf("[INFO] completed")
	return job, nil
}

func (c *Client) AddThumbnailJob(ctx context.Context, assetID, mediaProcessorID string) (*Job, error) {
	configuration := `{
  "Version": 1.0,
  "Codecs": [
    {
      "PngLayers": [
        {
          "Type": "PngLayer",
          "Width": "100%",
          "Height": "100%"
        }
      ],
      "Start": "{Best}",
      "Type": "PngImage"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "PngFormat"
      }
    }
  ]
}`
	c.logger.Printf("[INFO] post thumbnail create [#%s] job ...", assetID)

	taskBody := newTaskBody()
	job, err := c.addJob(ctx, assetID, mediaProcessorID, configuration, *taskBody)
	if err != nil {
		return nil, err
	}
	c.logger.Printf("[INFO] completed")
	return job, nil<commit_msg>refactor: change addJob signature<commit_after>package ams

import (
	"context"
	"encoding/xml"
	"fmt"
	"path"

	"github.com/pkg/errors"
)

const (
	jobsEndpoint   = "Jobs"
	jobInputAsset  = "JobInputAsset(0)"
	jobOutputAsset = "JobOutputAsset(0)"
)

const (
	JobQueued = iota
	JobScheduled
	JobProcessing
	JobFinished
	JobError
	JobCanceled
	JobCanceling
)

type MetaData struct {
	URI string `json:"uri"`
}

type MediaAsset struct {
	MetaData MetaData `json:"__metadata"`
}

func NewMediaAsset(uri string) MediaAsset {
	return MediaAsset{
		MetaData: MetaData{
			URI: uri,
		},
	}
}

type Job struct {
	ID              string  `json:"Id"`
	Name            string  `json:"Name"`
	StartTime       string  `json:"StartTime"`
	EndTime         string  `json:"EndTime"`
	LastModified    string  `json:"LastModified"`
	Priority        int     `json:"Priority"`
	RunningDuration float64 `json:"RunningDuration"`
	State           int     `json:"State"`
}

func (c *Client) addJob(ctx context.Context, assetID, mediaProcessorID, configuration string, taskBody *TaskBody) (*Job, error) {
	jobName := fmt.Sprintf("Job - Asset %s", assetID)
	assetURI := c.buildAssetURI(assetID)
	taskName := fmt.Sprintf("Task - Asset %s", assetID)
	taskBodyXML, err := xml.Marshal(taskBody)
	if err != nil {
		return nil, errors.Wrap(err, "taskBody xml marshal failed")
	}

	params := map[string]interface{}{
		"Name": jobName,
		"InputMediaAssets": []MediaAsset{
			NewMediaAsset(assetURI),
		},
		"Tasks": []Task{
			{
				Name:             taskName,
				Configuration:    configuration,
				MediaProcessorID: mediaProcessorID,
				TaskBody:         string(taskBodyXML),
			},
		},
	}
	var out struct {
		Data Job `json:"d"`
	}
	if err := c.post(ctx, jobsEndpoint, params, &out, withOData(true)); err != nil {
		return nil, err
	}
	return &out.Data, nil
}

func (c *Client) AddEncodeJob(ctx context.Context, assetID, mediaProcessorID, outputAssetName string) (*Job, error) {
	configuration := "Adaptive Streaming"

	c.logger.Printf("[INFO] post encode asset[#%s] job ...", assetID)

	taskBody := newTaskBody()
	taskBody.OutputAsset.Name = outputAssetName
	job, err := c.addJob(ctx, assetID, mediaProcessorID, configuration, taskBody)
	if err != nil {
		return nil, err
	}
	c.logger.Printf("[INFO] completed")
	return job, nil
}

func (c *Client) AddThumbnailJob(ctx context.Context, assetID, mediaProcessorID string) (*Job, error) {
	configuration := `{
  "Version": 1.0,
  "Codecs": [
    {
      "PngLayers": [
        {
          "Type": "PngLayer",
          "Width": "100%",
          "Height": "100%"
        }
      ],
      "Start": "{Best}",
      "Type": "PngImage"
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "PngFormat"
      }
    }
  ]
}`
	c.logger.Printf("[INFO] post thumbnail create [#%s] job ...", assetID)

	taskBody := newTaskBody()
	job, err := c.addJob(ctx, assetID, mediaProcessorID, configuration, taskBody)
	if err != nil {
		return nil, err
	}
	c.logger.Printf("[INFO] completed")
	return job, nil
{'fraction_non_alphanumeric': 0.14580312550346383, 'fraction_numerical': 0.0038666022232962784, 'mean_word_length': 3.344296710986704, 'pattern_counts': {'":': 30, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1278895', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2133, 'n_words': 507}