Changes

Jump to: navigation, search

OPS435 Python/assignment 2 fall2022

3,137 bytes added, 21:38, 6 November 2022
replace with better instructions from 2021
=Assignment 2 - Usage Report=
'''Weight:''' 15% of the overall grade
 
'''What you need''' A github account with a private repository named ops435-a2
'''Due Date:''' Please follow the three stages of submission schedule:
* Complete the detail algorithm requirements for this assignment script the first milestone and push to GitHub by March 20April 4, 2020 on github2021 by 11:59 PM,* Complete the your Python script coding and upload push to your vm in myvmlab GitHub by March 26April 18, 2021 at 11:59 PM, 2020 on githuband* Complete the testing Copy your Python script into a Word document and debugging submit to Blackboard by April 3 on the vm in myvmlab18, and also submit the detail algorithm file (algorithm.txt), test results and the python script (a1_[seneca_id].py)to blackboard. '''Late Penalty2021 at 11:''' 20% per school day, and note that this assignment must be completed satisfactorily in order to pass the course even if you get zero mark for this assignment59 PM.
==Overview==
asmith pts/11 10.40.105.130 Tue Feb 13 14:07:43 2018 - Tue Feb 13 16:07:43 2018 (02:00)
</pre>
It is always desirable to have a daily, weekly, or monthly usage reports by user or by remote host based on the above information.
== Tasks for this assignment ==
In this assignment, your should preform the following activities:# Complete you will create a detail algorithm for producing daily, weekly, and monthly script that can generate usage reports by user or by remote host based on the information stored in any given files generated off of output from the 'last' command, either or from a text file or real-timein a similar format. # Once you have complete You will use usage_data_file for testing but the detail algorithm, you script should then <b>design the structure of your python script</b> by identifying the appropriate python objects, functions and modules to also be used for each task in your algorithm tested on some other Linux machines and the main control logicon Matrix. Make sure to identify the followings:## input data, ## computation tasksDepending on the options selected, and ## outputs.# implement your computational solution using a single python script. You can use any built-in functions and functions from the python modules should list in the "Allowed Python Modules" section below to implement your solutionusers or remote IP addresses, either overall or limited by a specific date. # Test and review your working python code to see whether you can improve the interface of each function to facilitate better code re-use (this process is called <b>refactoring<It should also generate daily usage reports for specific users/b>)remote hosts, or weekly usage reports as well.
== Allowed Python Modules ==
* the <b>os, sys</b> modules
* the <b>argparse</b> module
* The <b>timedatetime</b> module * The <b>ur_funcssubprocess</b> module (provide by your teacher on github)** === Argparse ===Argparse is a much, much better way of dealing with command line arguments. It not only handles positional arguments, but options as well and will generate usage messages.It's <i>very highly recommended</i> that you spend at least a few minutes reading through the [https://docs.python.org/3/howto/argparse.html Argparse Tutorial] . === Datetime ===Since Python is a <i>batteries</i> included language, it's important to get accustomed with using some of the modules in the standard library. Since we are dealing with dates and times, you are required to work with the datetime module. The full docs can be found [https://docs.python.org/3/library/datetime.html here]. Datetime objects can be initialized from strings that match a particular format. One example is provided for you in the codebase: [https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime the strptime] function. Once you have created datetime objects, you can do useful things with them: * <code> d1 > d2 </code> will return True is d1 is <b>later</b> than d2.* <code> d2 - should read d1 </code> will return a <i>timedelta</i> object, which is an amount of time between d2 and d1. More interesting methods are in the [https://docs.python.org/3/library/datetime.html#datetime.datetime.fold table] in the docs. === Timedelta ===When performing some operations with datetime objects, we may see timedelta objects being created. In math, <b>delta</b> refers to a difference. So a timedelta object is essentially an object that represents a duration. You can do useful things with timedelta objects:* <code> delta1 + delta2 </code> will add up two durations. For example, if delta1 is two hours and delta2 is three hours, then this firstoperation will return five hours.** <code> str(delta1) </code> will represent the timedelta in a friendly format: H:MM:SS if the duration is less than 24 hours.  More interesting methods are in the [https://docs.python.org/3/library/argparsedatetime.html Argparse API reference information page#datetime.datetime.fold table]in the docs.
== Instructions ==
 
Accept the Assignment #2 via the link on Blackboard, and clone the Github repository on a Linux machine of your choosing. Your code should only be located in the file "assignment2.py".
=== Program Name and valid command line arguments ===
Name your Python3 script as <code>ur_[student_id].py</code>. Create a symbolic link to your script as ur.py (e.g. use the command ln -s ur_rchan.py ur.py to create the link) so that you can refer to your script as ur.py. Your script must accept one or more "file name" as its command line parameters and other optional parameters as shown below. Your python script should produce the following usage text when run with the --help option:
<pre>
[rchaneric@centos7 a1]$ python3 ./urassignment2.py -husage: ur_rchanassignment2.py [-h] [-l {user,host}] [-r RHOST] [-t {daily,weekly,monthly}] [-d DATE] [-u USER] [-v] F [F ...files]
Usage Report based on the last command
positional arguments:
F list of files file to be processed, if blank, will call last
optional arguments:
-r RHOST, --rhost RHOST
usage report for the given remote host IP
-t {daily,weekly,monthly}, --type time {daily,weekly,monthly} type of report: dailyday or week -d DATE, weekly, and monthly--date DATE specify date for report
-u USER, --user USER usage report for the given user name
-v, --verbose tune turn on output verbosity Copyright 2021 - Eric Brauer 
Copyright 2020 - Raymond Chan
</pre>
Replace the last line with your own full name .
Compare the usage output you have now with the one above. There is one option missing, you will need to change the <code>argparse</code> function to implement it.
If You will that there is only one file name provided at an 'args' object in assignment2.py. Once the command line, read the login<code>parse_command_args()</logout records from the contents of the given file. If the file name code> function is "online"called, get the record on the system your script is being execute using the Linux command "last -iwF"it will return an args object. The format of each command line in the file should arguments will be the same stored as the output attributes of 'last -Fiw'that object. <b>Do not use sys.argv to parse arguments. Filter out incomplete login</logout record (hints: check for the number of fields in each record).b>
If there is more than one a file name providedat the command line, merge all read the login/logout records from the contents of the files together with given file. If there is not file name, get the first one at record on the top and system your script is being execute using the Linux command "last one at -iwF". The format of each line in the file should be the same as the bottomoutput of 'last -Fiw'. Read and process Filter out incomplete login/logout record (hints: check for the file contents number of fields in that order in your programeach record).
=== Header ===
All your Python codes for this assignment must be placed in a <font color='red'><b><u>single source file</u></b></font>. Please include the following declaration by <b><u>you</u></b> as the <font color='blue'><b>script level docstring</b></font> in your Python source code file (replace [Student_id] with your Seneca email user name, and "Student Name" with your own name):
<source>OPS435 Assignment 2 - Winter 20202021Program: <b>ur_[seneca_id]assignment2.py</b>Author: "<font color='red'>Student Name</font>"The python code in this file <b>ur_[seneca_id]assignment2.py</b> is original work written by"<font color='red'>Student Name</font>". No code in this file is copied from any other source
including any person, textbook, or on-line resource except those provided
by the course instructor. I have not shared this python file with anyone
</source>
=== Sample outputs Use of Github ===You will once again be graded partly on <b>correct use of version control</b>, that is use of numerous commits with sensible commit messages. In professional practice, this is critically important for the timely delivery of code. You will be expected to use: <ol><li><code>git add assignment2.py</code><li><code>git commit -m "a message that describes the change"</code><li><code>git push</code> after completing each step. There is no penalty for "too many commits", there is no such thing! === Suggested Process ===The following are <ol><li> Read the rest of this document, try and understand what is expected. <li> Use the invite link posted to Blackboard to accept the assignment, and clone the repo to a Linux machine.<li> Run the script itself. Investigate argparse. <b>In the main block, print(args).</b> Experiment with the reports generated by various options. <li> Read the usage report output in the docs, what option must you implement? Go ahead and implement it. <b>Commit the change.</b><li> Use the check script (urto check your work: <code>./checkA2.py-f -v TestHelp</code>. It should succeed.<li> Investigate the <code>parse_for_user() </code> function, with the "<code>usage_data_file" mentioned </code>.<li> <code>parse_for_user()</code> should take the list of lines from the file, and instead return a list of usernames. <b>In main, print the title header and the output. Commit the change.</b><li> <b>Once you have `output` --> `parse_for_user()` --> correct output being printed, use if conditions to print only when `-l user` is in the overview sectioncommand line arguments.</b><li> Test using <code>./checkA2.py -f -v TestList</code>. You can download should see some tests succeeding, but some failing. Use the check script to start implementing the file [https:functions needed for <b>-l host</b>.<li> <b>Continue committing these changes as your proceed.</scsb> Your script should now be passing the TestList tests.senecac<li> Now implement the -d <date> option.This will filter your user list based onthe date provided by the user. <li> Use <code>.ca/~raymondcheckA2.chanpy -f -v TestDate</ops435code> to check your work. <b>You have completed the first milestone!</b><li> The next stage will be to implement the daily/a2weekly reports. Use <code>TestDaily</code> and <code>TestWeekly</code> with the check script.<li> Perform last checks and document your code. Write **why** your code is doing what it does, rather than **what** it's doing. You should have 100% of tests succeeding.</usage_data_file here] ol> === Output Format ===The format of your log tables should be identical to the sample output below, in order to minimize test your check error. The horizontal banner between title and data should be composed of equal signs (=), and be the length of the title string.List tables should need no extra formatting.For daily/montly tables with two columns, The first column should be 10 characters long and be left-aligned.The second column should be 15 characters long and be right-aligned. <pre>Daily Usage Report for rchan < same number of characters============================ <Date Usage < right justified13/02/2018 0:26:00 <15/02/2018 0:33:00Total 0:59:20llllllllllrrrrrrrrrrrrrrr < left column is 10 chars, right column is 15^left just.</pre> === Sample Outputs ===The following are the reports generated by the usage report script (ur.py script) with the "usage_data_file" mentioned in the overview section.
==== User List ====
The following is the user list extracted from the usage_data_file created by the command:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -l user usage_data_file
</pre>
The following is the remote host list extracted from the usage_file_file created by the command:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -l host usage_data_file
</pre>
</pre>
==== Daily Usage Report by User The Verbose Option ====Either of the following two tests can be modified with the <code>--verbose</code> option. You shouldn't have to do anything to get this working:<pre>[eric@centos7 a2]$ ./assignment2.py -l host -v usage_data_file</pre> <pre>Files to be processed: usage_data_fileType of args for files <class 'str'>Host list for usage_data_file=============================10.40.105.13010.40.91.23610.40.91.24710.43.115.162</pre> ==== List For Specific Day ====The Specifying a <code>--date</code> in YYYY-MM-DD format should list all users or hosts that were logged in at some point during that date, even if their start time or end time is different. For example, user <code>cwsmith</code> logged in on Feb 14 and logged off on Feb 15, but they show up when the following command is a Daily Usage Report created run:<pre>[eric@centos7 a2]$ ./assignment2.py -l user -d 2018-02-14 usage_data_file</pre> <pre>User list for usage_data_file=============================cwsmith</pre> This should work for hosts as well:<pre>[eric@centos7 a2]$ ./assignment2.py -l host -d 2018-02-14 usage_data_file</pre> <pre>Host list for usage_data_file=============================10.40.105.130</pre> If the user rchan by types in an invalid date, the script should halt and print the following commanderror message:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -u rchan l host -t daily d 2018-02-xx usage_data_file
</pre>
<pre>
Daily Usage Report for rchan============================Date Usage in Seconds2018 02 15 19802018 02 13 1580Total 3560not recognized. Use YYYY-MM-DD format.
</pre>
 
 
==== Daily Usage Report by User ====
The following are Daily Usage Report is created for user rchan.
<pre>
[rchaneric@centos a2]$ ./urassignment2.py -u cwsmith rchan -t daily usage_data_file
</pre>
 
<pre>
Daily Usage Report for cwsmithrchan==============================Date Usage in Seconds13/02/2018 03 13 22800:26:002018 15/02 15 7883/2018 02 14 30470:33:00Total 13210 0:59:20
</pre>
 
Be also sure to test with the <code>--verbose</code>
==== Daily Usage Report by Remote Host====
The following is a Daily Usage Report created for the Remote Host 10.40.105.103 by the command:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -r 10.40.105.130 -t daily usage_data_file
</pre>
Daily Usage Report for 10.40.105.130
====================================
Date Usage in Seconds14/02/2018 3:02 15 7883:112018 13/02 14 3047/2018 02 13 79692:12:49Total 18899 5:15:00
</pre>
==== Weekly Usage Report by User ====
The following is a Weekly Usage Report created for user rchan cwsmith by the command:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -u rchan cwsmith -t weekly usage_data_file
</pre>
<pre>
Weekly Usage Report for rchancwsmith===============================Week # Date Usage in Seconds2018 07 06 3:02:112018 10 35600:38:00Total 35603:40:11
</pre>
==== Weekly Usage Report by Remote Host ====
The following is a Weekly Usage Report created for the remote host 10.40.105.130 by the command:
<pre>
[rchaneric@centos7 a2]$ ./urassignment2.py -u cwsmith r 10.40.105.130 -t weekly usage_data_file
</pre>
<pre>
Weekly Usage Report for cwsmith10.40.105.130=====================================Week # Date Usage in Seconds2018 11 06 22800:02:312018 07 10930Total 0:02:31Total 13210</pre>
==== Daily Report From Online ====
Running the script with <B>no filename</b> as a file argument should call a subprocess.Popen object and run the command <code>last -Fiw</code>.
<pre>
[eric@mtrx-node06pd ~]$ ./assignment2.py -l user
</pre>
==== Weekly Usage Report by Remote Host ====The following is a Weekly Usage Report created for the remote host 10.40.105.130 by the command(Example Output from Matrix):
<pre>
[rchan@centos7 a2]$ ./urUser list for online====================aabbas28aaddae1aali309aaljajahaalves-staffaaanees1aarhamaassankanovabalandinabhaseenabholayacamuzcuacchikotiadas20adeel.py -r 10javed.40.105.130 -t weekly usage_data_file
</pre>
<pre>
Weekly Usage Report for 10[eric@mtrx-node06pd ~]$ .40/assignment2.105.130=====================================Week # Usage in Seconds2018 07 18899Total 18899py -u adas20 -t daily
</pre>
<pre>Daily Usage Report for abholay===================== Suggested work-flow for this assignment ===== create a <font color="blue">private</font> repository on github ===* Use the same github account you used for your assignment 1 repository.* Create a private repository named "ops435-a2" for this assignment.Date Usage* Populate your private repository with appropriate files. Please check out the sample repository <b><font color='blue'>[https:16/07/github.com2020 00:13:0917/rayfreeping07/ops435-a2 here]</font></b>2020 00:08:59Total 00:22:08
=== Add collaborator to your ops435-a2 private repository ===* Add your professor's github account as one of the collaborators to your ops435-a2 private repository. This will allow your professor to pull the contents of your ops435-a2 repository and also to review and suggest changes and fixes to your algorithm and/or python script.* <b><font color='blue'>Make sure that your professor accepted your invitation from github.com.</font></bpre>
=== Detail Algorithm Document ===Follow the standard computation procedure: input - process - ouput when creating the algorithm document Please note that there will no unit test for this assignment.==== input ====* get data (command line arguments/options) from the user using the functions provided by the argparse module* according to the arguments/options given at the command line, take appropriate processing action. ==== processing ====* based on the file(s) specified, read the contents of each file and use appropriate objects to store but it* based on the command line arguments/options, process the data accordingly, which includes** data preprocessing (split is still a multi-day record into single day record)** record processing (preform required computation)==== output ====* output the required report based on the processed data==== identify and select appropriate python objects and functions ====The following python functions (to be created, you may have more) are useful in handling the following sub-tasks:* reads login records from files and filters out unwanted records* convert login records into proper python object type so that it can be processed using as much built-in functions as possible * create function which generates daily usage reports by user and/or by remote host* create function which generates weekly usage reports by user and/or by remote hostrequirement.
To help you with this assignment=== First Milestone ===By April 4, you can use the ur_template.py in the sample ops435-a2 repository as a starting point in designing should have your own Python Usage Report script.<font color='blue'code>TestHelp</code>, <code>TestList</code> and <code>TestDate<b/code>If you don't have enough time to create tests all passing. Make sure that the functions for the data processing steps, you should study the functions code is in the ur_funcs.py (provided by your teacher), pick and use the one that may helpGitHub repository. If you I will use any of the functions from ur_funcs.py, there will be a cost of 10% pull request comment to your overall grade. If you create all the functions yourselfgive feedback, suggest changes or get you will get a bonus of 10%unstuck.</b></font>
=== Python script coding and debugging ===
Once you have all the individual function tested and that each is working properly, perform the final test with test data provided by your professor and verify that your script produces the correct results before submitting your python program on Blackboard. Upload all the files for this assignment 2 to your vm in myvmlab and perform the final test.
== Sample login/logout records file and sample test run results==
* can be found from the sample repository github.com/rayfreeping/ops435-a2
== Rubric ==
! Task !! Maximum mark !! Actual mark
|-
| User Requirement DocumentFirst Milestone ||20 10 ||
|-
| Program usage and Options Check Script Results || 20 30 ||
|-
| Generate user name list Additional Check: 'online' || 10 5 ||
|-
| Generate remote host IP listGitHub Use || 10 15 ||
|-
| Daily Usage Report by User List Functions || 10 5 ||
|-
| Daily Usage Report by Remote Host /Weekly Functions || 10 ||
|-
| Weekly Usage Report by User Date Functions || 10 5 ||
|-
| Weekly Usage Report by Remote Host Output/other Functions || 5 |||-| Overall Design/Coherence || 10 |||-| Documentation || 5 ||
|-
 
| '''Total''' || 100 ||
 
|}
== Submission ==
* Stage 1: upload your algorithm document file to your ops435-a2 repository in github.com Complete the first milestone on GitHub by March 20April 4, 20202021.* Stage 2: upload Use commits to push your python script for this assignment to your ops435-a2 repository in githubGithub.com and to . The final state of your vm in myvmlab by March 26repository will be looked at on April 18, 20202021 at 11:59 PM.* Stage 3: After fully tested and debugged Copy your python script for this assignment, update your algorithm, your python script, into a Word document and your est results to your ops435-a2 repository in github.com. Also submit the algorithm document, the python script and final test result to blackboard Blackboard by April 318, 20202021 at 11:59 PM.

Navigation menu