Last active
September 10, 2020 15:41
-
-
Save fpcorso/00c504dbfe187105d7208fe4987704df to your computer and use it in GitHub Desktop.
EDD Per Download License Calculations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script: EDD Per Download License Calculations | |
# Author: Frank Corso | |
# Date Created: 04/05/2020 | |
# Last Modified: 07/16/2020 | |
# Python Version: 3.6.5 | |
# Takes exports of licenses from EDD and turns it into a CSV of totals and averages. | |
# To use, have CSVs of licenses for each download in EDD in a 'licenses' directory with each | |
# saved as edd-export-licenses-(download-slug).csv | |
# Put this script above that directory. In terminal, run python license_calculations.py | |
from os import listdir | |
from os import path | |
import pandas as pd | |
import re | |
def calculate_license_stats(): | |
"""Loads all license exports and generates simplified aggregated license CSV.""" | |
# Edit this if you name your CSV files something different. | |
p = re.compile(r'edd-export-licenses-(.+).csv') | |
# Edit this to change input and output directories | |
in_dir = 'licenses' | |
out_dir = 'data-output' | |
# Begins creating our new dataframe for our new CSV. | |
totals = pd.DataFrame(columns=['Extension', 'Total Licences', 'Active/(Active+Inactive)', 'Active Licenses', | |
'Inactive Licenses', 'Expired Licenses', 'Total Activated Sites', | |
'AVG Site Per Licence', 'Total Active Activated Sites', | |
'AVG Active Site Per Active License']) | |
# Loop over all files in directory. | |
for file in listdir(in_dir): | |
print('Loading...', file) | |
# Checks filename for REGEX defined above. If matches, uses that as download name. | |
# If not matching, skips this file. | |
extension_search = p.findall(file) | |
if len(extension_search) > 0: | |
extension_name = extension_search[0] | |
else: | |
print('Not using this file...') | |
continue | |
df = pd.read_csv(path.join(in_dir, file)) | |
# Make our calculations. | |
total_activated = df['Activation Count'].sum() | |
total_avg_activated = round(df['Activation Count'].mean(), 2) | |
active = df[df['License Status'] == 'active'] | |
inactive = df[df['License Status'] == 'inactive'] | |
expired = df[df['License Status'] == 'expired'] | |
active_activated = active['Activation Count'].sum() | |
active_avg_activated = round(active['Activation Count'].mean(), 2) | |
active_percent = round(len(active) / (len(active) + len(inactive)), 3) | |
# Adds new row to dataframe. | |
totals.loc[len(totals)] = [extension_name, len(df), active_percent, len(active), len(inactive), len(expired), | |
total_activated, total_avg_activated, active_activated, active_avg_activated] | |
# Puts dataframe into our new CSV. | |
print('Writing results to file...') | |
totals.to_csv(path.join(out_dir, 'license-data.csv')) | |
if __name__ == '__main__': | |
print('Calculating license stats...') | |
calculate_license_stats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment