|
| 1 | +package com.whylogs.api.logger.rollingLogger; |
| 2 | + |
| 3 | +import com.whylogs.api.logger.Logger; |
| 4 | +import com.whylogs.api.writer.Writer; |
| 5 | +import com.whylogs.core.DatasetProfile; |
| 6 | +import com.whylogs.core.schemas.DatasetSchema; |
| 7 | +import java.time.Instant; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.Callable; |
| 11 | + |
| 12 | +public class TimedRollingLogger extends Logger implements AutoCloseable { |
| 13 | + // A rolling logger that continuously rotates files based on time |
| 14 | + private DatasetSchema schema; |
| 15 | + private String baseName; |
| 16 | + private String fileExtension; |
| 17 | + private int interval; |
| 18 | + private Character when = 'H'; // TODO: Make the Literals of S M H D |
| 19 | + private boolean utc = false; |
| 20 | + private boolean align = true; |
| 21 | + private boolean skipEmpty = false; |
| 22 | + private String suffix; |
| 23 | + |
| 24 | + private DatasetProfile currentProfile; |
| 25 | + private Callable<Writer> callback; // TODO: this isn't the write signature |
| 26 | + private Scheduler scheduler; |
| 27 | + private int currentBatchTimestamp; |
| 28 | + |
| 29 | + // TODO: callback: Optional[Callable[[Writer, DatasetProfileView, str], None]] |
| 30 | + public TimedRollingLogger( |
| 31 | + DatasetSchema schema, String baseName, String fileExtension, int interval) { |
| 32 | + this(schema, baseName, fileExtension, interval, 'H', false, true, false); |
| 33 | + } |
| 34 | + |
| 35 | + public TimedRollingLogger( |
| 36 | + DatasetSchema schema, String baseName, String fileExtension, int interval, Character when) { |
| 37 | + this(schema, baseName, fileExtension, interval, when, false, true, false); |
| 38 | + } |
| 39 | + |
| 40 | + public TimedRollingLogger( |
| 41 | + DatasetSchema schema, |
| 42 | + String baseName, |
| 43 | + String fileExtension, |
| 44 | + int interval, |
| 45 | + Character when, |
| 46 | + boolean utc, |
| 47 | + boolean align, |
| 48 | + boolean skipEmpty) { |
| 49 | + super(schema); |
| 50 | + |
| 51 | + this.schema = schema; |
| 52 | + this.baseName = baseName; |
| 53 | + this.fileExtension = fileExtension; |
| 54 | + this.interval = interval; |
| 55 | + this.when = Character.toUpperCase(when); |
| 56 | + this.utc = utc; |
| 57 | + this.align = align; |
| 58 | + this.skipEmpty = skipEmpty; |
| 59 | + |
| 60 | + if (this.baseName == null || this.baseName.isEmpty()) { |
| 61 | + this.baseName = "profile"; |
| 62 | + } |
| 63 | + if (this.fileExtension == null || this.fileExtension.isEmpty()) { |
| 64 | + this.fileExtension = ".bin"; // TODO: should we make this .whylogs? |
| 65 | + } |
| 66 | + |
| 67 | + switch (this.when) { |
| 68 | + case 'S': |
| 69 | + this.interval = 1; // one second |
| 70 | + this.suffix = "%Y-%m-%d_%H-%M-%S"; |
| 71 | + break; |
| 72 | + case 'M': |
| 73 | + this.interval = 60; // one minute |
| 74 | + this.suffix = "%Y-%m-%d_%H-%M"; |
| 75 | + break; |
| 76 | + case 'H': |
| 77 | + this.interval = 60 * 60; // one hour |
| 78 | + this.suffix = "%Y-%m-%d_%H"; |
| 79 | + break; |
| 80 | + case 'D': |
| 81 | + this.interval = 60 * 60 * 24; // one day |
| 82 | + this.suffix = "%Y-%m-%d"; |
| 83 | + break; |
| 84 | + default: |
| 85 | + throw new IllegalArgumentException( |
| 86 | + "Invalid value for when: " + this.when + ". Must be S, M, H, or D"); |
| 87 | + } |
| 88 | + |
| 89 | + this.interval = this.interval * interval; // / multiply by units requested |
| 90 | + this.utc = utc; |
| 91 | + |
| 92 | + Instant currentTime = Instant.now(); |
| 93 | + this.currentBatchTimestamp = this.computeCurrentBatchTimestamp(currentTime.getEpochSecond()); |
| 94 | + this.currentProfile = new DatasetProfile(schema, currentTime, currentTime); |
| 95 | + int initialRunAfter = |
| 96 | + (this.currentBatchTimestamp + this.interval) - (int) currentTime.getEpochSecond(); |
| 97 | + if (initialRunAfter < 0) { |
| 98 | + // TODO: Add logging error as this shouldn't happen |
| 99 | + initialRunAfter = this.interval; |
| 100 | + } |
| 101 | + |
| 102 | + this.scheduler = new Scheduler(initialRunAfter, this.interval, this::doRollover, null); |
| 103 | + this.scheduler.start(); |
| 104 | + |
| 105 | + // autocloseable closes at end |
| 106 | + } |
| 107 | + |
| 108 | + private int computeCurrentBatchTimestamp(long nowEpoch) { |
| 109 | + int roundedNow = (int) nowEpoch; // rounds by going from an long to a int (truncates) |
| 110 | + if (this.align) { |
| 111 | + return (Math.floorDiv((roundedNow - 1), this.interval)) * this.interval + this.interval; |
| 112 | + } |
| 113 | + return roundedNow; |
| 114 | + } |
| 115 | + |
| 116 | + public void checkWriter(Writer writer) { |
| 117 | + writer.check_interval(this.interval); |
| 118 | + } |
| 119 | + |
| 120 | + private ArrayList<DatasetProfile> getMatchingProfiles() { |
| 121 | + ArrayList<DatasetProfile> matchingProfiles = new ArrayList<>(); |
| 122 | + matchingProfiles.add(this.currentProfile); |
| 123 | + return matchingProfiles; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected ArrayList<DatasetProfile> getMatchingProfiles(Object data) { |
| 128 | + return this.getMatchingProfiles(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected <O> ArrayList<DatasetProfile> getMatchingProfiles(Map<String, O> data) { |
| 133 | + return this.getMatchingProfiles(); |
| 134 | + } |
| 135 | + |
| 136 | + private void doRollover() { |
| 137 | + if (this.isClosed()) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + DatasetProfile oldProfile = this.currentProfile; |
| 142 | + Instant currentTime = Instant.now(); |
| 143 | + this.currentBatchTimestamp = this.computeCurrentBatchTimestamp(currentTime.getEpochSecond()); |
| 144 | + this.currentProfile = new DatasetProfile(schema, currentTime, currentTime); |
| 145 | + |
| 146 | + this.flush(oldProfile); |
| 147 | + } |
| 148 | + |
| 149 | + private void flush(DatasetProfile profile) { |
| 150 | + if (profile == null) { |
| 151 | + return; |
| 152 | + } else if (this.skipEmpty && profile.isEmpty()) { |
| 153 | + // set logger logger.debug("skip_empty is set. Skipping empty profiles") |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + // get time to get name |
| 158 | + String timedFileName = this.baseName + "_" + this.currentBatchTimestamp + this.fileExtension; |
| 159 | + |
| 160 | + // Sleep while the profile is active? |
| 161 | + // TODO: this is where we call the store list.write |
| 162 | + // TODO: go through through the writers |
| 163 | + } |
| 164 | + |
| 165 | + public void close() { |
| 166 | + // TODO log that we are closing the writer |
| 167 | + if (!this.isClosed()) { |
| 168 | + // Autoclose handles the isCLosed() |
| 169 | + this.scheduler.stop(); |
| 170 | + this.flush(this.currentProfile); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments