I'm trying to find the aggregate time each distinct process_name is unhealthy with sql as it is the only language I know. FALSE indicates a process is unhealthy until it switches to TRUE. If the process ends on False, I assume it is unhealthy until the end time in query.
The desired output would be something like: (process_name=A 4seconds |process_name=B 'x seconds')
ts
process_name
value
2022-06-30 09:30:00.856839-04
A
false
2022-06-30 09:30:00.856839-04
A
false
2022-06-30 09:30:05.857945-04
A
true
2022-06-30 09:30:05.857945-04
A
true
2022-06-30 09:30:14.58143-04
B
false
2022-06-30 09:30:19.581629-04
B
true
2022-06-30 09:32:07.383898-04
C
false
2022-06-30 09:32:07.383898-04
C
false
2022-06-30 09:32:12.385355-04
C
true
2022-06-30 09:32:12.385355-04
C
true
2022-06-30 09:32:16.562974-04
D
false
2022-06-30 09:32:51.606147-04
E
false
2022-06-30 09:32:51.606147-04
E
false
2022-06-30 09:32:53.481923-04
Z
false
2022-06-30 09:32:53.481923-04
Z
false
2022-06-30 09:32:53.737522-04
X
false
2022-06-30 09:32:53.737522-04
X
false
2022-06-30 09:32:56.6067-04
E
true
2022-06-30 09:32:56.6067-04
E
true
2022-06-30 09:32:58.482162-04
Z
true
2022-06-30 09:32:58.482162-04
Z
true
2022-06-30 09:32:58.73768-04
X
true
2022-06-30 09:32:58.73768-04
X
true
2022-06-30 09:33:41.574388-04
D
true
2022-06-30 09:33:52.562954-04
F
false
2022-06-30 09:33:52.562954-04
F
false
2022-06-30 09:33:57.988884-04
G
false
2022-06-30 09:33:57.988884-04
G
false
Related
I have this
device_id new_trip corridor_trip_id
a_10003750032338423796 true 1
a_10003750032338423796 false 1
a_10003750032338423796 false 1
a_1001349994099853586 true 3
a_1001349994099853586 false 3
a_1001349994099853586 false 3
a_1001349994099853586 false 3
a_1001349994099853586 true 3
a_1001349994099853586 false 3
a_1001349994099853586 true 3
a_1001349994099853586 false 3
I want to find the cumulative amount of corridor_trip_id as here
device_id new_trip corridor_trip_id
a_10003750032338423796 True 1
a_10003750032338423796 False 1
a_10003750032338423796 False 1
a_1001349994099853586 True 1
a_1001349994099853586 False 1
a_1001349994099853586 False 1
a_1001349994099853586 False 1
a_1001349994099853586 True 2
a_1001349994099853586 False 2
a_1001349994099853586 True 3
a_1001349994099853586 False 3
-- a)
leapYear :: Int -> Bool
leapYear n = (n `mod` 4 == 0) && ( (n `mod` 100 /= 0) || (n `mod` 400 == 0) )
-- b)
data Month = Jan | Feb | Mar | Apr | May | Aug | Jun | Jul | Sep | Oct | Nov | Dec
deriving (Enum, Eq, Show)
-- c)
type Day = Int
type Year = Int
data Date = Date Day Month Year
deriving Show
-- d)
dateIsValid :: Date -> Bool
dateIsValid (Date d m y) | y < 0 || y > 9999 = False
| d < 0 = False
| m `elem` [Jan, Mar, May, Jul, Aug, Oct, Dec] = d < 32
| m `elem` [Apr, Jun, Sep, Nov, Feb] = d < 31
| m == Feb = if leapYear y then d < 30 else d < 29
| otherwise = False
-- e)
daysBetween :: Date -> Date -> Int
daysBetween (Date a b c) (Date x y z) = if dateDay (Date a b c) > dateDay (Date x y z)
then dateDay (Date a b c) - dateDay (Date x y z)
else dateDay (Date x y z) - dateDay (Date a b c)
where monthDay m = case m of Jan -> 0
Feb -> 31
Mar -> 59
Apr -> 90
May -> 120
Jun -> 151
Jul -> 181
Aug -> 211
Sep -> 243
Oct -> 273
Nov -> 304
Dec -> 334
countLeapYears (Date d m y) | m `elem` [Jan, Feb, May] = (y-1) `div` 4 - (y-1) `div` 100 + (y-1) `div` 400
| otherwise = y `div` 4 - y `div` 100 + y `div` 400
dateDay (Date d m y) = d + monthDay m + (y-1)*365 + countLeapYears (Date d m y)
getMonthNr :: Month -> Int
getMonthNr m = case m of Jan -> 1
Feb -> 2
Mar -> 3
Apr -> 4
May -> 5
Jun -> 6
Jul -> 7
Aug -> 8
Sep -> 9
Oct -> 10
Nov -> 11
Dec -> 12
-- f)
data Dow = Mon | Tue | Wen | Thu | Fri | Sat | Sun
deriving Show
-- g)
weekday :: Date -> Dow
weekday (Date d m y) = case daysBetween (Date d m y) (Date 3 Jan 2000) `mod` 7 of 0 -> Sun
1 -> Mon
2 -> Tue
3 -> Wen
4 -> Thu
5 -> Fri
_ -> Sat
I have got this code where I need to calculate the number of days between two days and then find the weekday of that date. While this code is correct in finding the answer for the given examples in the exercises daysBetween (Date 4 Nov 2021) (Date 1 Jan 2000) → 7978 and January 1st, 2021 was a Friday, it shows problems with most other dates. One of my tests involved the date Date 13 Aug 2009 and Date 3 Jan 2000 where the error is one day. I am not allowed to use predefined functions to find the difference between two dates or the specific weekday. Is there something wrong with the logic I have used? Or is there another way to implement an algorithm that searches for the difference between two days?
getMonthNr can be ignored or not, since I wrote it down while experimenting and left it there in case I would need it.
Edit: After correcting the code a 1 day shift occurs when the days are a few years apart (almost a decade) or a 2 day shift when the days are centuries to a few millennia apart.
Do you just have Aug in the wrong place? It should be between Jul and Sep but it is between May and Jun in your code.
This question already has an answer here:
Linear indexing, logical indexing, and all that
(1 answer)
Closed 6 years ago.
I'm trying to select some elements by using boolean operations in MATLAB.
I have A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
When using A([true true false; true true false]) I get:
1
4
7
2
Isn't it supposed to be?:
1
4
2
5
Does anyone know what's going on?
See this example for the documentation on logical indexing. It may not be explained as clearly as it should, but if you specify a logical index with fewer elements that then the indexed matrix (A) then the indexing matrix is linearized such that:
A = [1 2 3; 4 5 6; 7 8 9];
idx1 = [true true false; true true false];
A(idx1)
is equivalent to:
idx1 = [true true false; true true false];
A(idx1(:))
In other words, the index matrix (idx1) elements specify the output in column-wise order.
If you want what you though you should get, you can use:
idx2 = [true false true; true true false];
A(idx2)
or you can transform your original index array:
idx1 = [true true false; true true false];
idx2 = reshape(idx1.',2,3);
A(idx2)
or just use:
idx3 = [true true false true true].';
A(idx3)
hi I have two tables test1 and test2 like below
test1: test2:
folder analytic analytic status
---------- ---------- ---------- ----------
1 a1 a1 C
1 a2 a2 C
1 a3 a3 N
2 b1 b1 N
2 b2 b2 N
2 b3 b3 N
3 c1 c1 N
3 c2 c2 C
3 c3 c3 N
In crystal reports I need to display the analytics which has at least one record with status 'C' in that folder.
That means expected output is
analytic
----------
a1
a2
a3
c1
c2
c3
how to achieve this... please help me.
thanks in advance.
Under properties of you field where you are going to show your analytic, you need to create a formula condition like this:
If {#status} = "C" Then
True
Else
False
Of course, if it is string.
I have a 10x10 binary bit map as follows. I am looking for an efficient way of finding its contour in MATLAB. (I have tried letting every value "look around" its neighbors' values and decide, but it is too inefficient. I expect the algorithm to scale up.)
false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false
Let's assume each boolean value resembles a square, and the left-bottom one sits over x: 0-1; y: 0-1. The output should be the points that form the boundary. You may assume the inner true block is alway convex.
This is dead simple. Use the bwperim command in MATLAB, assuming you have the image processing toolbox.
You can call the function like so:
out = bwperim(A); %//or
out = bwperim(A,conn);
The first way assumes that the pixel connectivity is a 4-pixel neighbourhood. This will only look at the north, south, east and west directions.
If you specify an additional parameter called conn which is a single number, you can override this behaviour and specify the kind of behaviour you want when looking at neighbouring pixels. For example, if conn=8, you would look at 8-pixel neighbourhoods for 2D (so N, NE, E, SE, S, SW, W, NW), or you can go into 3D if you have a 3D binary image... but for now, I'm assuming it's just 2D. For the best accuracy, use 8.
As such, we have:
A = [false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false];
out = bwperim(A,8);
And it looks like:
out =
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 1 0 0 0 0 1 1 0
0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
MATLAB outputs 1 for true and 0 for false.
As a bonus, this is what the shapes look like side by side:
Edit from the comments
Going with your comments, you wish to find the set of points that make the perimeter. As such, you can simply use the find command to do that for you.
[X,Y] = find(out == 1);
coords = [X Y];
What the find command does is that it searches your array and finds locations in the array that match the Boolean expression given in the parameter of find. In this case, we wish to find all co-ordinates that have a pixel in out equal to 1, and out is our perimeter image. As such, this effectively finds all pixels that are perimeter pixels.
We thus get:
coords =
3 2
4 2
5 2
6 2
7 2
8 2
2 3
3 3
8 3
9 3
2 4
9 4
2 5
9 5
2 6
9 6
2 7
9 7
2 8
3 8
8 8
9 8
3 9
4 9
5 9
6 9
7 9
8 9
X are the row co-ordinates, while Y are the column co-ordinates. I've placed X and Y into a single 2D array for better presentation, but you can take the X and Y variables by themselves for further processing.
Hope this helps!
Here's another option:
B = bwboundaries(A)
this will get the x-y coordinates of any boundary. see more info here...
Another option with image processing toolbox:
B = A - imerode(A,SE);
where SE is one of the kernels:
0 1 0 1 1 1
1 1 1 1 1 1
0 1 0 1 1 1
depending on which connectivity you want to use: first one for 8-connectivity, second one for 4-connectivity. For the difference between the two, recall that 8-connectivity allows diagonal neighbours.
With the image B, you can find all the points of the perimeters with the same technique displayed in another answer:
[Xp,Yp] = find(B);