-
Notifications
You must be signed in to change notification settings - Fork 31
/
Util.java
88 lines (77 loc) · 3.72 KB
/
Util.java
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
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.solutions.df.video.analytics.common;
import static org.apache.beam.sdk.schemas.Schema.toSchema;
import com.google.protobuf.Duration;
import java.net.URI;
import java.util.stream.Stream;
import org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Util {
private static final Logger LOG = LoggerFactory.getLogger(Util.class);
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
private static final Long SPLIT_SECONDS_PARAM = 5L;
static final Schema detectionInstanceSchema =
Stream.of(
Schema.Field.of("processing_timestamp", FieldType.STRING).withNullable(true),
Schema.Field.of("timeOffset", FieldType.STRING).withNullable(true),
Schema.Field.of("confidence", FieldType.DOUBLE).withNullable(true),
Schema.Field.of("left", FieldType.FLOAT).withNullable(true),
Schema.Field.of("top", FieldType.FLOAT).withNullable(true),
Schema.Field.of("right", FieldType.FLOAT).withNullable(true),
Schema.Field.of("bottom", FieldType.FLOAT).withNullable(true))
.collect(toSchema());
public static final Schema videoMlCustomOutputListSchema =
Stream.of(
Schema.Field.of("file_name", FieldType.STRING).withNullable(true),
Schema.Field.of("entity", FieldType.STRING).withNullable(true),
Schema.Field.of(
"frame_data", FieldType.array(FieldType.row(detectionInstanceSchema))))
.collect(toSchema());
public static final Schema videoMlCustomOutputSchema =
Stream.of(
Schema.Field.of("file_name", FieldType.STRING).withNullable(true),
Schema.Field.of("entity", FieldType.STRING).withNullable(true),
Schema.Field.of("detection", FieldType.row(detectionInstanceSchema))
.withNullable(true))
.collect(toSchema());
public static final Schema errorSchema =
Stream.of(
Schema.Field.of("file_name", FieldType.STRING).withNullable(true),
Schema.Field.of("transaction_timestamp", FieldType.STRING).withNullable(true),
Schema.Field.of("error_code", FieldType.INT32).withNullable(true),
Schema.Field.of("error_message", FieldType.STRING).withNullable(true))
.collect(toSchema());
static String convertDurationToSeconds(Duration offset, Long splitSeconds) {
long offsetValue = SPLIT_SECONDS_PARAM * splitSeconds;
return String.valueOf((offset.getSeconds() + offsetValue) + offset.getNanos() / 1e9);
}
static String getCurrentTimeStamp() {
return TIMESTAMP_FORMATTER.print(Instant.now().toDateTime(DateTimeZone.UTC));
}
static String getFileName(String gcsPath) {
GcsPath path = GcsPath.fromUri(URI.create(gcsPath));
return path.getObject();
}
}