[PYTHON] I want to calculate the allowable downtime from the operating rate

Often, we talk about operating rates and permissible downtime in SLAs, but I want to be able to quickly ask for this.

Mathematically, it's as follows.

Occupancy rate= \frac{Total time-Failure time}{Total time}

So, for example, "If the operating rate is 99%, how much suspension is allowed per month or year?" Does not come out in numbers, so let's write a program so that it can be calculated. It has a lot of second decoction, but leave it alone.

I would like to write it in Python.

Premise

One month is 30 days (24 x 60 x 60 seconds).

One year is 365 days (365 x 24 x 60 x 60 seconds).

Confirmed Python version.

$ python3 -V
Python 3.6.9

theme

Find the following three.

Make the above available from the utilization rate, monthly permissible downtime, or annual permissible downtime.

In short, I want to make something that seeks the other element when one is given.

The permissible downtime is given in units of s (seconds), m (minutes), h (hours), and d (days), and is interpreted as seconds if omitted. Shall be.

The result is output in Markdown tabular format.

program

Here is the one I created.

calc.py

import datetime

year_time =  365 * 24 * 60 * 60
month_time = 30 * 24 * 60 * 60

units = {"s": "seconds", "m": "minutes", "h": "hours", "d": "days"}

def calc_from_percentage(percentage):
    stop_time_per_year_raw = year_time * ((100 - percentage) / 100)
    stop_time_per_month_raw = month_time * ((100 - percentage) / 100)

    def format_time(time):
        if time > 24 * 60 * 60:
            formatted_time = f"{time / (24 * 60 * 60):.2f}Day"
        elif time > 60 * 60:
            formatted_time = f"{time / (60 * 60):.2f}time"
        elif time > 60:
            formatted_time = f"{time / 60:.2f}Minutes"
        else:
            formatted_time = f"{time:.2f}Seconds"

        return formatted_time

    stop_time_per_year = format_time(stop_time_per_year_raw)
    stop_time_per_month = format_time(stop_time_per_month_raw)

    return (f"{percentage}%", stop_time_per_month, stop_time_per_year)

def calc_from_month_stop_time(stop_time):
    try:
        seconds = int(stop_time)
    except ValueError:
        t = int(stop_time[:-1])
        unit = units[stop_time[-1]]

        seconds = datetime.timedelta(**{unit: t}).total_seconds()
        
    percentage = 100 - ((seconds * 100) / month_time)
    return calc_from_percentage(percentage)

def calc_from_year_stop_time(stop_time):
    try:
        seconds = int(stop_time)
    except ValueError:
        t = int(stop_time[:-1])
        unit = units[stop_time[-1]]

        seconds = datetime.timedelta(**{unit: t}).total_seconds()
        
    percentage = 100 - ((seconds * 100) / year_time)
    return calc_from_percentage(percentage)

def print_table(percentages_or_stop_times, calc_func):
    print("|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|")
    print("|:------:|:----------------:|:----------------:|")


    if isinstance(percentages_or_stop_times, list):
        targets = percentages_or_stop_times
    else:
        targets = [percentages_or_stop_times]

    for t in targets:
        result = calc_func(t)
        print(f"| {result[0]} | {result[1]} | {result[2]} |")

How to use

Load and launch it in Python's interactive shell.

$ python3 -i calc.py 
>>> 

Obtain the allowable downtime from the operating rate. If you want to give more than one, use the list.

>>> print_table(99, calc_from_percentage)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 99% | 7.20 hours| 3.65 days|


>>> print_table([90, 95, 99, 99.5, 99.9, 99.95, 99.99, 99.999, 99.9999], calc_from_percentage)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 90% | 3.00 days| 36.50 days|
| 95% | 1.50 days| 18.25th|
| 99% | 7.20 hours| 3.65 days|
| 99.5% | 3.60 hours| 1.82nd|
| 99.9% | 43.20 min| 8.76 hours|
| 99.95% | 21.60 minutes| 4.38 hours|
| 99.99% | 4.32 minutes| 52.56 minutes|
| 99.999% | 25.92 seconds| 5.26 minutes|
| 99.9999% | 2.59 seconds| 31.54 seconds|

This is the table I wanted.

Occupancy rate Monthly permissible downtime Annual permissible downtime
90% 3.00 days 36.50 days
95% 1.50 days 18.25th
99% 7.20 hours 3.65 days
99.5% 3.60 hours 1.82nd
99.9% 43.20 min 8.76 hours
99.95% 21.60 minutes 4.38 hours
99.99% 4.32 minutes 52.56 minutes
99.999% 25.92 seconds 5.26 minutes
99.9999% 2.59 seconds 31.54 seconds

Calculated from the monthly permissible downtime.

>>> print_table("10", calc_from_month_stop_time)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 99.99961419753086% | 10.00 seconds| 2.03 minutes|


>>> print_table(["3d", "10m", "10s", "10"], calc_from_month_stop_time)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 90.0% | 3.00 days| 36.50 days|
| 99.97685185185185% | 10.00 minutes| 2.03 hours|
| 99.99961419753086% | 10.00 seconds| 2.03 minutes|
| 99.99961419753086% | 10.00 seconds| 2.03 minutes|

I wondered if the number of digits in the percentage would be good this time ...

Calculated from the annual permissible downtime.

>>> print_table("30d", calc_from_year_stop_time)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 91.78082191780823% | 2.47th| 30.00 days|


>>> print_table(["30d", "45m", "30s", "30"], calc_from_year_stop_time)
|Occupancy rate|Monthly permissible downtime|Annual permissible downtime|
|:------:|:----------------:|:----------------:|
| 91.78082191780823% | 2.47th| 30.00 days|
| 99.99143835616438% | 3.70 minutes| 45.00 minutes|
| 99.99990487062405% | 2.47 seconds| 30.00 seconds|
| 99.99990487062405% | 2.47 seconds| 30.00 seconds|

Like this.

Recommended Posts

I want to calculate the allowable downtime from the operating rate
I want to see the file name from DataLoader
I want to automatically find high-quality parts from the videos I shot
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to use jar from python
I want to display the progress bar
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to send a signal only from the sub thread to the main thread
I want to connect to PostgreSQL from various languages
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to handle the rhyme part7 (BOW)
I want to make fits from my head
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to customize the appearance of zabbix
I want to use the activation function Mish
I want to display the progress in Python!
[LINE Messaging API] I want to send a message from the program to everyone's LINE
[Ansible] I want to call my own function from the template module (macro)
I want to detect images of cats from Instagram
I tried to detect the iris from the camera image
I want to grep the execution result of strace
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
I want to fully understand the basics of Bokeh
[Python3] I want to generate harassment names from Japanese!
I want to write in Python! (3) Utilize the mock
I want to handle the rhyme part6 (organize once)
I want to automate ssh using the expect command!
I want to publish the product at the lowest cost
I read the Chainer reference (updated from time to time)
I want to use the R dataset in python
I want to handle the rhyme part8 (finished once)
I want to increase the security of ssh connections
[Python] I made a system to introduce "recipes I really want" from the recipe site!
[TensorFlow] I want to master the indexing for Ragged Tensor
I want to initialize if the value is empty (python)
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
I want to save the photos sent by LINE to S3
I want to automate ssh using the expect command! part2
maya Python I want to fix the baked animation again.
I want to move selenium for the time being [for mac]
I want to start a lot of processes from python
I want to use only the normalization process of SudachiPy
I want to get the operation information of yahoo route
I tweeted from the terminal!
I want to change the Japanese flag to the Palau flag with Numpy
How to calculate the amount of calculation learned from ABC134-D
I want to solve Sudoku (Sudoku)
[Python] I want to use the -h option with argparse
I want to judge the authenticity of the elements of numpy array
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot