-
Notifications
You must be signed in to change notification settings - Fork 674
/
artifact.go
121 lines (105 loc) · 3.61 KB
/
artifact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package transformers
import (
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"github.com/flyteorg/flyte/datacatalog/pkg/errors"
"github.com/flyteorg/flyte/datacatalog/pkg/repositories/models"
"github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/datacatalog"
)
func SerializedMetadata(metadata *datacatalog.Metadata) ([]byte, error) {
serializedMetadata, err := marshalMetadata(metadata)
if err != nil {
return []byte{}, err
}
return serializedMetadata, nil
}
func CreateArtifactModel(request *datacatalog.CreateArtifactRequest, artifactData []models.ArtifactData, dataset models.Dataset) (models.Artifact, error) {
datasetID := request.GetArtifact().GetDataset()
serializedMetadata, err := marshalMetadata(request.GetArtifact().GetMetadata())
if err != nil {
return models.Artifact{}, err
}
partitions := make([]models.Partition, len(request.GetArtifact().GetPartitions()))
for i, partition := range request.GetArtifact().GetPartitions() {
partitions[i] = models.Partition{
DatasetUUID: dataset.UUID,
Key: partition.GetKey(),
Value: partition.GetValue(),
}
}
return models.Artifact{
ArtifactKey: models.ArtifactKey{
DatasetProject: datasetID.GetProject(),
DatasetDomain: datasetID.GetDomain(),
DatasetName: datasetID.GetName(),
DatasetVersion: datasetID.GetVersion(),
ArtifactID: request.GetArtifact().GetId(),
},
DatasetUUID: dataset.UUID,
ArtifactData: artifactData,
SerializedMetadata: serializedMetadata,
Partitions: partitions,
}, nil
}
func FromArtifactModel(artifact models.Artifact) (*datacatalog.Artifact, error) {
datasetID := &datacatalog.DatasetID{
Project: artifact.DatasetProject,
Domain: artifact.DatasetDomain,
Name: artifact.DatasetName,
Version: artifact.DatasetVersion,
UUID: artifact.DatasetUUID,
}
metadata, err := unmarshalMetadata(artifact.SerializedMetadata)
if err != nil {
return &datacatalog.Artifact{}, err
}
partitions := make([]*datacatalog.Partition, len(artifact.Partitions))
for i, partition := range artifact.Partitions {
partitions[i] = &datacatalog.Partition{
Key: partition.Key,
Value: partition.Value,
}
}
tags := make([]*datacatalog.Tag, len(artifact.Tags))
for i, tag := range artifact.Tags {
tags[i] = FromTagModel(datasetID, tag)
}
createdAt, err := ptypes.TimestampProto(artifact.CreatedAt)
if err != nil {
return &datacatalog.Artifact{}, errors.NewDataCatalogErrorf(codes.Internal,
"artifact [%+v] invalid createdAt time conversion", artifact)
}
return &datacatalog.Artifact{
Id: artifact.ArtifactID,
Dataset: datasetID,
Metadata: metadata,
Partitions: partitions,
Tags: tags,
CreatedAt: createdAt,
}, nil
}
func FromArtifactModels(artifacts []models.Artifact) ([]*datacatalog.Artifact, error) {
retArtifacts := make([]*datacatalog.Artifact, 0, len(artifacts))
for _, artifact := range artifacts {
retArtifact, err := FromArtifactModel(artifact)
if err != nil {
return nil, err
}
retArtifacts = append(retArtifacts, retArtifact)
}
return retArtifacts, nil
}
// Transforms datasetID and artifact combination into an ArtifactKey
// The DatasetID is optional since artifactIDs are unique per Artifact
func ToArtifactKey(datasetID *datacatalog.DatasetID, artifactID string) models.ArtifactKey {
artifactKey := models.ArtifactKey{
ArtifactID: artifactID,
}
if datasetID != nil {
artifactKey.DatasetProject = datasetID.GetProject()
artifactKey.DatasetDomain = datasetID.GetDomain()
artifactKey.DatasetName = datasetID.GetName()
artifactKey.DatasetVersion = datasetID.GetVersion()
}
return artifactKey
}